senc2d_internal_types.h (4621B)
1 /* Copyright (C) 2018-2021, 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 SENC2D_INTERNAL_TYPES_H 17 #define SENC2D_INTERNAL_TYPES_H 18 19 #include "senc2d.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-2d" 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 /* Segment IDs use the same type than Side IDs */ 58 typedef unsigned seg_id_t; 59 /* SEG_MAX__ is limited to half the max of the base type to allow to count 60 * sides */ 61 #define SEG_MAX__ (UINT_MAX/2) 62 #define SEG_NULL__ UINT_MAX 63 #define PRTF_SEG "%u" 64 static INLINE int 65 cmp_seg_id 66 (const void* ptr1, const void* ptr2) 67 { 68 const seg_id_t* t1 = ptr1; 69 const seg_id_t* t2 = ptr2; 70 return (int)(*t1) - (int)(*t2); 71 } 72 73 /* Side IDs type use the same base type than Segment IDs */ 74 typedef seg_id_t side_id_t; 75 #define SIDE_MAX__ (2*SEG_MAX__) 76 #define SIDE_NULL__ SEG_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) : SENC2D_UNSPECIFIED_MEDIUM; 98 } 99 100 static FINLINE unsigned 101 medium_id_2_medium_idx(medium_id_t medium) { 102 uint64_t tmp = (medium == SENC2D_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 != SENC2D_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 seg_id_t 134 SEGSIDE_2_SEG(side_id_t s) { 135 ASSERT(((size_t)s >> 1) <= SEG_MAX__); 136 return s >> 1; 137 } 138 139 static FINLINE int 140 SEGSIDE_IS_FRONT(side_id_t s) { 141 return (s & 1) == 0; 142 } 143 144 static FINLINE enum senc2d_side 145 SEGSIDE_2_SIDE(side_id_t s) { 146 return (s & 1) ? SENC2D_BACK : SENC2D_FRONT; 147 } 148 149 static FINLINE enum side_flag 150 SEGSIDE_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 SEGIDxSIDE_2_SEGSIDE(seg_id_t s, enum senc2d_side i) { 162 size_t r; 163 ASSERT(i == SENC2D_FRONT || i == SENC2D_BACK); 164 r = (s << 1) | (i == SENC2D_BACK); 165 ASSERT(r <= SIDE_MAX__); 166 return (side_id_t)r; 167 } 168 169 static FINLINE side_id_t 170 SEGSIDE_OPPOSITE(side_id_t s) { 171 return SEGIDxSIDE_2_SEGSIDE(SEGSIDE_2_SEG(s), 172 SEGSIDE_IS_FRONT(s) ? SENC2D_BACK : SENC2D_FRONT); 173 } 174 175 #endif /* SENC2D_INTERNAL_TYPES_H */