star-enclosures-3d

Extract enclosures from 3D geometry
git clone git://git.meso-star.fr/star-enclosures-3d.git
Log | Files | Refs | README | LICENSE

senc3d_internal_types.h (4611B)


      1 /* Copyright (C) 2018-2020, 2023, 2024 |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 SENC3D_INTERNAL_TYPES_H
     17 #define SENC3D_INTERNAL_TYPES_H
     18 
     19 #include "senc3d.h"
     20 
     21 #include <rsys/math.h>
     22 
     23 #include <stdio.h>
     24 #include <stdint.h>
     25 
     26 /* Utility macros */
     27 #define LIB_NAME "star-enclosures-3d"
     28 
     29 #ifdef NDEBUG
     30 #define OK2(Expr)\
     31   if((tmp_res = (Expr)) != RES_OK) goto tmp_error;
     32 
     33 #define OK(Expr)\
     34   if((res = (Expr)) != RES_OK) goto error;
     35 #else
     36 #define OK2(Expr)\
     37   if((tmp_res = (Expr)) != RES_OK) {\
     38     fprintf(stderr, LIB_NAME":%s: error code set to %d at line %d\n",\
     39       FUNC_NAME, tmp_res, __LINE__);\
     40     goto tmp_error;\
     41   }
     42 
     43 #define OK(Expr)\
     44   if((res = (Expr)) != RES_OK) {\
     45     fprintf(stderr, LIB_NAME":%s: error code set to %d at line %d\n",\
     46       FUNC_NAME, res, __LINE__);\
     47     goto error;\
     48   }
     49 #endif
     50 
     51 /* Helper type */
     52 typedef unsigned char uchar;
     53 
     54 /* The following types must be defined accordingly with the types
     55  * used in senc2d.h */
     56 
     57 /* Trg IDs use the same type than Side IDs */
     58 typedef unsigned trg_id_t;
     59 /* TRG_MAX__ is limited to half the max of the base type to allow to count
     60  * sides */
     61 #define TRG_MAX__ (UINT_MAX/2)
     62 #define TRG_NULL__ UINT_MAX
     63 #define PRTF_TRG "%u"
     64 static INLINE int
     65 cmp_trg_id
     66   (const void* ptr1, const void* ptr2)
     67 {
     68   const trg_id_t* t1 = ptr1;
     69   const trg_id_t* t2 = ptr2;
     70   return (int)(*t1) - (int)(*t2);
     71 }
     72 
     73 /* Side IDs type  use the same base type than Trg IDs */
     74 typedef trg_id_t side_id_t;
     75 #define SIDE_MAX__ (2*TRG_MAX__)
     76 #define SIDE_NULL__ TRG_NULL__
     77 
     78 /* Vertex IDs type */
     79 typedef unsigned vrtx_id_t;
     80 #define VRTX_MAX__ (UINT_MAX-1)
     81 #define VRTX_NULL__ UINT_MAX
     82 #define PRTF_VRTX "%u"
     83 
     84 /* Edge IDs use the same type than vertex IDs */
     85 typedef vrtx_id_t edge_id_t;
     86 #define EDGE_MAX__ VRTX_MAX__
     87 #define EDGE_NULL__ VRTX_NULL__
     88 
     89 /* Medium IDs type */
     90 typedef unsigned medium_id_t;
     91 #define MEDIUM_MAX__ (UINT_MAX-1) /* MAX is for unspecified medium */
     92 #define MEDIUM_NULL__ UINT_MAX
     93 #define PRTF_MDM "%u"
     94 
     95 static FINLINE medium_id_t
     96 medium_idx_2_medium_id(int64_t m_idx) {
     97   return m_idx ? (medium_id_t)(m_idx - 1) : SENC3D_UNSPECIFIED_MEDIUM;
     98 }
     99 
    100 static FINLINE unsigned
    101 medium_id_2_medium_idx(medium_id_t medium) {
    102   uint64_t tmp = (medium == SENC3D_UNSPECIFIED_MEDIUM) ? 0 : medium + 1;
    103   ASSERT(tmp <= UINT_MAX);
    104   return (unsigned)tmp;
    105 }
    106 
    107 /* Enclosure IDs type */
    108 typedef unsigned enclosure_id_t;
    109 #define ENCLOSURE_MAX__ (UINT_MAX-1)
    110 #define ENCLOSURE_NULL__ UINT_MAX
    111 
    112 /* Component IDs use the same type than enclosure IDs */
    113 typedef enclosure_id_t component_id_t;
    114 #define COMPONENT_MAX__ (UINT_MAX-2) /* To allow special values */
    115 #define COMPONENT_NULL__ UINT_MAX
    116 /* Special values */
    117 #define CC_GROUP_ROOT_NONE UINT_MAX
    118 #define CC_GROUP_ROOT_INFINITE (UINT_MAX-1)
    119 #define CC_GROUP_ID_NONE UINT_MAX
    120 #define CC_ID_NONE UINT_MAX
    121 
    122 #if (MEDIUM_MAX__+1 != SENC3D_UNSPECIFIED_MEDIUM)
    123 #error "Inconsistant values"
    124 #endif
    125 
    126 /* This one is used as flag */
    127 enum side_flag {
    128   FLAG_FRONT = BIT(0),
    129   FLAG_BACK = BIT(1)
    130 };
    131 
    132 /* Utility macros */
    133 static FINLINE trg_id_t
    134 TRGSIDE_2_TRG(side_id_t s) {
    135   ASSERT(((size_t)s >> 1) <= TRG_MAX__);
    136   return s >> 1;
    137 }
    138 
    139 static FINLINE int
    140 TRGSIDE_IS_FRONT(side_id_t s) {
    141   return (s & 1) == 0;
    142 }
    143 
    144 static FINLINE enum senc3d_side
    145 TRGSIDE_2_SIDE(side_id_t s) {
    146   return (s & 1) ? SENC3D_BACK : SENC3D_FRONT;
    147 }
    148 
    149 static FINLINE enum side_flag
    150 TRGSIDE_2_SIDEFLAG(side_id_t s) {
    151   return (s & 1) ? FLAG_BACK : FLAG_FRONT;
    152 }
    153 
    154 static FINLINE uchar
    155 SIDE_CANCELED_FLAG(enum side_flag f) {
    156   ASSERT((f << 4) <= UCHAR_MAX);
    157   return (uchar)(f << 4);
    158 }
    159 
    160 static FINLINE side_id_t
    161 TRGIDxSIDE_2_TRGSIDE(trg_id_t t, enum senc3d_side i) {
    162   size_t r;
    163   ASSERT(i == SENC3D_FRONT || i == SENC3D_BACK);
    164   r = (t << 1) | (i == SENC3D_BACK);
    165   ASSERT(r <= SIDE_MAX__);
    166   return (side_id_t)r;
    167 }
    168 
    169 static FINLINE side_id_t
    170 TRGSIDE_OPPOSITE(side_id_t s) {
    171   return TRGIDxSIDE_2_TRGSIDE(TRGSIDE_2_TRG(s),
    172     TRGSIDE_IS_FRONT(s) ? SENC3D_BACK : SENC3D_FRONT);
    173 }
    174 
    175 #endif /* SENC3D_INTERNAL_TYPES_H */