star-3d

Surface structuring for efficient 3D geometric queries
git clone git://git.meso-star.fr/star-3d.git
Log | Files | Refs | README | LICENSE

s3d_c.h (5775B)


      1 /* Copyright (C) 2015-2023 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef S3D_C_H
     17 #define S3D_C_H
     18 
     19 #include "s3d.h"
     20 #include "s3d_backend.h"
     21 
     22 #include <rsys/rsys.h>
     23 
     24 /* Filter function and its associated user defined data */
     25 struct hit_filter {
     26   s3d_hit_filter_function_T func;
     27   void* data;
     28 };
     29 
     30 
     31 static FINLINE res_T
     32 rtc_error_to_res_T(const enum RTCError err)
     33 {
     34   switch(err) {
     35     case RTC_ERROR_NONE: return RES_OK;
     36     case RTC_ERROR_UNKNOWN: return RES_UNKNOWN_ERR;
     37     case RTC_ERROR_INVALID_ARGUMENT: return RES_BAD_ARG;
     38     case RTC_ERROR_INVALID_OPERATION: return RES_BAD_ARG;
     39     case RTC_ERROR_OUT_OF_MEMORY: return RES_MEM_ERR;
     40     case RTC_ERROR_UNSUPPORTED_CPU: return RES_BAD_ARG;
     41     case RTC_ERROR_CANCELLED: return RES_UNKNOWN_ERR;
     42     default: FATAL("Unreachable code\n"); break;
     43   }
     44 }
     45 
     46 static INLINE const char*
     47 rtc_error_string(const enum RTCError err)
     48 {
     49   const char* str = NULL;
     50   switch(err) {
     51     case RTC_ERROR_NONE: str = "No error"; break;
     52     case RTC_ERROR_UNKNOWN: str = "Unknown error"; break;
     53     case RTC_ERROR_INVALID_ARGUMENT: str = "Invalid argument"; break;
     54     case RTC_ERROR_INVALID_OPERATION: str = "Invalid operation"; break;
     55     case RTC_ERROR_OUT_OF_MEMORY: str = "Out of memory"; break;
     56     case RTC_ERROR_UNSUPPORTED_CPU: str = "Unsupported CPU"; break;
     57     case RTC_ERROR_CANCELLED: str = "Cancelled operation"; break;
     58     default: FATAL("Unreachable code\n"); break;
     59   }
     60   return str;
     61 }
     62 
     63 static INLINE unsigned
     64 s3d_type_get_dimension(const enum s3d_type type)
     65 {
     66   switch(type) {
     67     case S3D_FLOAT: return 1;
     68     case S3D_FLOAT2: return 2;
     69     case S3D_FLOAT3: return 3;
     70     case S3D_FLOAT4: return 4;
     71     default: FATAL("Unreachable code\n"); break;
     72   }
     73 }
     74 
     75 #define RAYN_GRAB(RayN, N, i, Type, Attr) \
     76   (((Type*)((char*)(RayN)+(offsetof(struct RTCRay, Attr)*N)))[i])
     77 #define HITN_GRAB(HitN, N, i, Type, Attr) \
     78   (((Type*)((char*)(HitN)+(offsetof(struct RTCHit, Attr)*N)))[i])
     79 #define RAYHITN_GET_RAYN(RayHitN, N) \
     80   ((struct RTCRayN*)((char*)RayHitN+offsetof(struct RTCRayHit, ray)*N))
     81 #define RAYHITN_GET_HITN(RayHitN, N) \
     82   ((struct RTCHitN*)((char*)RayHitN+offsetof(struct RTCRayHit, hit)*N))
     83 
     84 static FINLINE void
     85 rtc_rayN_get_ray
     86   (const struct RTCRayN* rayN, /* SoA layout */
     87    const size_t N, /* SoA width */
     88    const size_t i, /* Id of the ray */
     89    struct RTCRay* ray)
     90 {
     91   ASSERT(rayN && ray && i < N);
     92   ray->org_x = RAYN_GRAB(rayN, N, i, float, org_x);
     93   ray->org_y = RAYN_GRAB(rayN, N, i, float, org_y);
     94   ray->org_z = RAYN_GRAB(rayN, N, i, float, org_z);
     95   ray->tnear = RAYN_GRAB(rayN, N, i, float, tnear);
     96   ray->dir_x = RAYN_GRAB(rayN, N, i, float, dir_x);
     97   ray->dir_y = RAYN_GRAB(rayN, N, i, float, dir_y);
     98   ray->dir_z = RAYN_GRAB(rayN, N, i, float, dir_z);
     99   ray->time  = RAYN_GRAB(rayN, N, i, float, time);
    100   ray->tfar  = RAYN_GRAB(rayN, N, i, float, tfar);
    101   ray->mask  = RAYN_GRAB(rayN, N, i, unsigned, mask);
    102   ray->id    = RAYN_GRAB(rayN, N, i, unsigned, id);
    103   ray->flags = RAYN_GRAB(rayN, N, i, unsigned, flags);
    104 }
    105 
    106 static FINLINE void
    107 rtc_hitN_get_hit
    108   (const struct RTCHitN* hitN,
    109    const size_t N,
    110    const size_t i,
    111    struct RTCHit* hit)
    112 {
    113   size_t id;
    114   ASSERT(hitN && hit && i < N);
    115   hit->Ng_x   = HITN_GRAB(hitN, N, i, float, Ng_x);
    116   hit->Ng_y   = HITN_GRAB(hitN, N, i, float, Ng_y);
    117   hit->Ng_z   = HITN_GRAB(hitN, N, i, float, Ng_z);
    118   hit->u      = HITN_GRAB(hitN, N, i, float, u);
    119   hit->v      = HITN_GRAB(hitN, N, i, float, v);
    120   hit->primID = HITN_GRAB(hitN, N, i, unsigned, primID);
    121   hit->geomID = HITN_GRAB(hitN, N, i, unsigned, geomID);
    122   FOR_EACH(id, 0, RTC_MAX_INSTANCE_LEVEL_COUNT) {
    123     hit->instID[id] = HITN_GRAB(hitN, N, i, unsigned, instID[id]);
    124   }
    125 }
    126 
    127 static FINLINE void
    128 rtc_rayN_set_ray
    129   (struct RTCRayN* rayN,
    130    const size_t N,
    131    const size_t i,
    132    const struct RTCRay* ray)
    133 {
    134   ASSERT(rayN && ray && i < N);
    135   RAYN_GRAB(rayN, N, i, float, org_x) = ray->org_x;
    136   RAYN_GRAB(rayN, N, i, float, org_y) = ray->org_y;
    137   RAYN_GRAB(rayN, N, i, float, org_z) = ray->org_z;
    138   RAYN_GRAB(rayN, N, i, float, tnear) = ray->tnear;
    139   RAYN_GRAB(rayN, N, i, float, dir_x) = ray->dir_x;
    140   RAYN_GRAB(rayN, N, i, float, dir_y) = ray->dir_y;
    141   RAYN_GRAB(rayN, N, i, float, dir_z) = ray->dir_z;
    142   RAYN_GRAB(rayN, N, i, float, time) = ray->time;
    143   RAYN_GRAB(rayN, N, i, float, tfar) = ray->tfar;
    144   RAYN_GRAB(rayN, N, i, unsigned, mask) = ray->mask;
    145   RAYN_GRAB(rayN, N, i, unsigned, id) = ray->id;
    146   RAYN_GRAB(rayN, N, i, unsigned, flags) = ray->flags;
    147 }
    148 
    149 static FINLINE void
    150 rtc_hitN_set_hit
    151   (const struct RTCHitN* hitN,
    152    const size_t N,
    153    const size_t i,
    154    struct RTCHit* hit)
    155 {
    156   size_t id;
    157   ASSERT(hitN && hit && i < N);
    158   HITN_GRAB(hitN, N, i, float, Ng_x) = hit->Ng_x;
    159   HITN_GRAB(hitN, N, i, float, Ng_y) = hit->Ng_y;
    160   HITN_GRAB(hitN, N, i, float, Ng_z) = hit->Ng_z;
    161   HITN_GRAB(hitN, N, i, float, u) = hit->u;
    162   HITN_GRAB(hitN, N, i, float, v) = hit->v;
    163   HITN_GRAB(hitN, N, i, unsigned, primID) = hit->primID;
    164   HITN_GRAB(hitN, N, i, unsigned, geomID) = hit->geomID;
    165   FOR_EACH(id, 0, RTC_MAX_INSTANCE_LEVEL_COUNT) {
    166     HITN_GRAB(hitN, N, i, unsigned, instID[id]) = hit->instID[id];
    167   }
    168 }
    169 
    170 #endif /* S3D_C_H */
    171