star-2d

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

s2d_c.h (5617B)


      1 /* Copyright (C) 2016-2021, 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 S2D_C_H
     17 #define S2D_C_H
     18 
     19 #include "s2d.h"
     20 #include "s2d_backend.h"
     21 
     22 #include <rsys/rsys.h>
     23 
     24 static FINLINE res_T
     25 rtc_error_to_res_T(const enum RTCError err)
     26 {
     27   switch(err) {
     28     case RTC_ERROR_NONE: return RES_OK;
     29     case RTC_ERROR_UNKNOWN: return RES_UNKNOWN_ERR;
     30     case RTC_ERROR_INVALID_ARGUMENT: return RES_BAD_ARG;
     31     case RTC_ERROR_INVALID_OPERATION: return RES_BAD_ARG;
     32     case RTC_ERROR_OUT_OF_MEMORY: return RES_MEM_ERR;
     33     case RTC_ERROR_UNSUPPORTED_CPU: return RES_BAD_ARG;
     34     case RTC_ERROR_CANCELLED: return RES_UNKNOWN_ERR;
     35     default: FATAL("Unreachable code\n"); break;
     36   }
     37 }
     38 
     39 static INLINE const char*
     40 rtc_error_string(const enum RTCError err)
     41 {
     42   const char* str = NULL;
     43   switch(err) {
     44     case RTC_ERROR_NONE: str = "No error"; break;
     45     case RTC_ERROR_UNKNOWN: str = "Unknown error"; break;
     46     case RTC_ERROR_INVALID_ARGUMENT: str = "Invalid argument"; break;
     47     case RTC_ERROR_INVALID_OPERATION: str = "Invalid operation"; break;
     48     case RTC_ERROR_OUT_OF_MEMORY: str = "Out of memory"; break;
     49     case RTC_ERROR_UNSUPPORTED_CPU: str = "Unsupported CPU"; break;
     50     case RTC_ERROR_CANCELLED: str = "Cancelled operation"; break;
     51     default: FATAL("Unreachable code\n"); break;
     52   }
     53   return str;
     54 }
     55 
     56 static INLINE unsigned
     57 s2d_type_get_dimension(const enum s2d_type type)
     58 {
     59   switch(type) {
     60     case S2D_FLOAT: return 1;
     61     case S2D_FLOAT2: return 2;
     62     case S2D_FLOAT3: return 3;
     63     default: FATAL("Unreachable code\n"); break;
     64   }
     65 }
     66 
     67 #define RAYN_GRAB(RayN, N, i, Type, Attr) \
     68   (((Type*)((char*)(RayN)+(offsetof(struct RTCRay, Attr)*N)))[i])
     69 #define HITN_GRAB(HitN, N, i, Type, Attr) \
     70   (((Type*)((char*)(HitN)+(offsetof(struct RTCHit, Attr)*N)))[i])
     71 #define RAYHITN_GET_RAYN(RayHitN, N) \
     72   ((struct RTCRayN*)((char*)RayHitN+offsetof(struct RTCRayHit, ray)*N))
     73 #define RAYHITN_GET_HITN(RayHitN, N) \
     74   ((struct RTCHitN*)((char*)RayHitN+offsetof(struct RTCRayHit, hit)*N))
     75 
     76 static FINLINE void
     77 rtc_rayN_get_ray
     78   (const struct RTCRayN* rayN, /* SoA layout */
     79    const size_t N, /* SoA width */
     80    const size_t i, /* Id of the ray */
     81    struct RTCRay* ray)
     82 {
     83   ASSERT(rayN && ray && i < N);
     84   ray->org_x = RAYN_GRAB(rayN, N, i, float, org_x);
     85   ray->org_y = RAYN_GRAB(rayN, N, i, float, org_y);
     86   ray->org_z = RAYN_GRAB(rayN, N, i, float, org_z);
     87   ray->tnear = RAYN_GRAB(rayN, N, i, float, tnear);
     88   ray->dir_x = RAYN_GRAB(rayN, N, i, float, dir_x);
     89   ray->dir_y = RAYN_GRAB(rayN, N, i, float, dir_y);
     90   ray->dir_z = RAYN_GRAB(rayN, N, i, float, dir_z);
     91   ray->time  = RAYN_GRAB(rayN, N, i, float, time);
     92   ray->tfar  = RAYN_GRAB(rayN, N, i, float, tfar);
     93   ray->mask  = RAYN_GRAB(rayN, N, i, unsigned, mask);
     94   ray->id    = RAYN_GRAB(rayN, N, i, unsigned, id);
     95   ray->flags = RAYN_GRAB(rayN, N, i, unsigned, flags);
     96 }
     97 
     98 static FINLINE void
     99 rtc_hitN_get_hit
    100   (const struct RTCHitN* hitN,
    101    const size_t N,
    102    const size_t i,
    103    struct RTCHit* hit)
    104 {
    105   size_t id;
    106   ASSERT(hitN && hit && i < N);
    107   hit->Ng_x   = HITN_GRAB(hitN, N, i, float, Ng_x);
    108   hit->Ng_y   = HITN_GRAB(hitN, N, i, float, Ng_y);
    109   hit->Ng_z   = HITN_GRAB(hitN, N, i, float, Ng_z);
    110   hit->u      = HITN_GRAB(hitN, N, i, float, u);
    111   hit->v      = HITN_GRAB(hitN, N, i, float, v);
    112   hit->primID = HITN_GRAB(hitN, N, i, unsigned, primID);
    113   hit->geomID = HITN_GRAB(hitN, N, i, unsigned, geomID);
    114   FOR_EACH(id, 0, RTC_MAX_INSTANCE_LEVEL_COUNT) {
    115     hit->instID[id] = HITN_GRAB(hitN, N, i, unsigned, instID[id]);
    116   }
    117 }
    118 
    119 static FINLINE void
    120 rtc_rayN_set_ray
    121   (struct RTCRayN* rayN,
    122    const size_t N,
    123    const size_t i,
    124    const struct RTCRay* ray)
    125 {
    126   ASSERT(rayN && ray && i < N);
    127   RAYN_GRAB(rayN, N, i, float, org_x) = ray->org_x;
    128   RAYN_GRAB(rayN, N, i, float, org_y) = ray->org_y;
    129   RAYN_GRAB(rayN, N, i, float, org_z) = ray->org_z;
    130   RAYN_GRAB(rayN, N, i, float, tnear) = ray->tnear;
    131   RAYN_GRAB(rayN, N, i, float, dir_x) = ray->dir_x;
    132   RAYN_GRAB(rayN, N, i, float, dir_y) = ray->dir_y;
    133   RAYN_GRAB(rayN, N, i, float, dir_z) = ray->dir_z;
    134   RAYN_GRAB(rayN, N, i, float, time) = ray->time;
    135   RAYN_GRAB(rayN, N, i, float, tfar) = ray->tfar;
    136   RAYN_GRAB(rayN, N, i, unsigned, mask) = ray->mask;
    137   RAYN_GRAB(rayN, N, i, unsigned, id) = ray->id;
    138   RAYN_GRAB(rayN, N, i, unsigned, flags) = ray->flags;
    139 }
    140 
    141 static FINLINE void
    142 rtc_hitN_set_hit
    143   (const struct RTCHitN* hitN,
    144    const size_t N,
    145    const size_t i,
    146    struct RTCHit* hit)
    147 {
    148   size_t id;
    149   ASSERT(hitN && hit && i < N);
    150   HITN_GRAB(hitN, N, i, float, Ng_x) = hit->Ng_x;
    151   HITN_GRAB(hitN, N, i, float, Ng_y) = hit->Ng_y;
    152   HITN_GRAB(hitN, N, i, float, Ng_z) = hit->Ng_z;
    153   HITN_GRAB(hitN, N, i, float, u) = hit->u;
    154   HITN_GRAB(hitN, N, i, float, v) = hit->v;
    155   HITN_GRAB(hitN, N, i, unsigned, primID) = hit->primID;
    156   HITN_GRAB(hitN, N, i, unsigned, geomID) = hit->geomID;
    157   FOR_EACH(id, 0, RTC_MAX_INSTANCE_LEVEL_COUNT) {
    158     HITN_GRAB(hitN, N, i, unsigned, instID[id]) = hit->instID[id];
    159   }
    160 }
    161 
    162 #endif /* S2D_C_H */