solparser_entity.h (6810B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016-2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #ifndef SOLPARSER_ENTITY_H 18 #define SOLPARSER_ENTITY_H 19 20 #include "solparser_geometry.h" 21 #include "solparser_pivot.h" 22 #include "solparser_shape.h" 23 24 #include <rsys/double3.h> 25 #include <rsys/dynamic_array.h> 26 #include <rsys/hash_table.h> 27 #include <rsys/list.h> 28 #include <rsys/str.h> 29 30 enum solparser_entity_type { 31 SOLPARSER_ENTITY_EMPTY, 32 SOLPARSER_ENTITY_GEOMETRY, 33 SOLPARSER_ENTITY_X_PIVOT, 34 SOLPARSER_ENTITY_ZX_PIVOT 35 }; 36 37 struct solparser_entity_id { size_t i; }; 38 39 #define DARRAY_NAME child_id 40 #define DARRAY_DATA struct solparser_entity_id 41 #include <rsys/dynamic_array.h> 42 43 #define DARRAY_NAME anchor_id 44 #define DARRAY_DATA struct solparser_anchor_id 45 #include <rsys/dynamic_array.h> 46 47 /* Declare the hash table that map an entity name to the index of its in memory 48 * solstice representation. */ 49 #define HTABLE_NAME str2sols 50 #define HTABLE_KEY struct str 51 #define HTABLE_KEY_FUNCTOR_INIT str_init 52 #define HTABLE_KEY_FUNCTOR_RELEASE str_release 53 #define HTABLE_KEY_FUNCTOR_COPY str_copy 54 #define HTABLE_KEY_FUNCTOR_COPY_AND_RELEASE str_copy_and_release 55 #define HTABLE_KEY_FUNCTOR_EQ str_eq 56 #define HTABLE_KEY_FUNCTOR_HASH str_hash 57 #define HTABLE_DATA size_t 58 #include <rsys/hash_table.h> 59 60 struct solparser_entity { 61 double rotation[3]; /* In degrees */ 62 double translation[3]; 63 64 struct str name; 65 66 int primary; 67 68 enum solparser_entity_type type; 69 union { 70 struct solparser_geometry_id geometry; 71 struct solparser_pivot_id x_pivot; 72 struct solparser_pivot_id zx_pivot; 73 } data; 74 75 /* Internal data. Should not be acceded directly. */ 76 struct htable_str2sols str2anchors; 77 struct htable_str2sols str2children; 78 struct darray_anchor_id anchors; /* List of anchors */ 79 struct darray_child_id children; /* List of children nodes */ 80 }; 81 82 static INLINE void 83 solparser_entity_init 84 (struct mem_allocator* allocator, struct solparser_entity* entity) 85 { 86 ASSERT(entity); 87 d3_splat(entity->rotation, 0); 88 d3_splat(entity->translation, 0); 89 entity->type = SOLPARSER_ENTITY_GEOMETRY; 90 entity->data.geometry.i = SIZE_MAX; 91 str_init(allocator, &entity->name); 92 entity->primary = 2; 93 htable_str2sols_init(allocator, &entity->str2anchors); 94 htable_str2sols_init(allocator, &entity->str2children); 95 darray_anchor_id_init(allocator, &entity->anchors); 96 darray_child_id_init(allocator, &entity->children); 97 } 98 99 static INLINE void 100 solparser_entity_release(struct solparser_entity* entity) 101 { 102 ASSERT(entity); 103 str_release(&entity->name); 104 htable_str2sols_release(&entity->str2anchors); 105 htable_str2sols_release(&entity->str2children); 106 darray_anchor_id_release(&entity->anchors); 107 darray_child_id_release(&entity->children); 108 } 109 110 static INLINE res_T 111 solparser_entity_copy 112 (struct solparser_entity* dst, const struct solparser_entity* src) 113 { 114 res_T res = RES_OK; 115 ASSERT(dst && src); 116 d3_set(dst->translation, src->translation); 117 d3_set(dst->rotation, src->rotation); 118 dst->type = src->type; 119 dst->data = src->data; 120 res = str_copy(&dst->name, &src->name); 121 dst->primary = src->primary; 122 if(res != RES_OK) return res; 123 res = htable_str2sols_copy(&dst->str2anchors, &src->str2anchors); 124 if(res != RES_OK) return res; 125 res = htable_str2sols_copy(&dst->str2children, &src->str2children); 126 if(res != RES_OK) return res; 127 res = darray_anchor_id_copy(&dst->anchors, &src->anchors); 128 if(res != RES_OK) return res; 129 res = darray_child_id_copy(&dst->children, &src->children); 130 if(res != RES_OK) return res; 131 return RES_OK; 132 } 133 134 static INLINE res_T 135 solparser_entity_copy_and_release 136 (struct solparser_entity* dst, struct solparser_entity* src) 137 { 138 res_T res = RES_OK; 139 ASSERT(dst && src); 140 d3_set(dst->translation, src->translation); 141 d3_set(dst->rotation, src->rotation); 142 dst->type = src->type; 143 dst->data = src->data; 144 res = str_copy_and_release(&dst->name, &src->name); 145 dst->primary = src->primary; 146 if(res != RES_OK) return res; 147 res = htable_str2sols_copy_and_release(&dst->str2anchors, &src->str2anchors); 148 if(res != RES_OK) return res; 149 res = htable_str2sols_copy_and_release(&dst->str2children, &src->str2children); 150 if(res != RES_OK) return res; 151 res = darray_anchor_id_copy_and_release(&dst->anchors, &src->anchors); 152 if(res != RES_OK) return res; 153 res = darray_child_id_copy_and_release(&dst->children, &src->children); 154 if(res != RES_OK) return res; 155 return RES_OK; 156 } 157 158 static INLINE res_T 159 solparser_entity_copy_and_clear 160 (struct solparser_entity* dst, struct solparser_entity* src) 161 { 162 res_T res = RES_OK; 163 ASSERT(dst && src); 164 d3_set(dst->translation, src->translation); 165 d3_set(dst->rotation, src->rotation); 166 dst->type = src->type; 167 dst->data = src->data; 168 res = str_copy_and_clear(&dst->name, &src->name); 169 dst->primary = src->primary; 170 if(res != RES_OK) return res; 171 res = htable_str2sols_copy_and_clear(&dst->str2anchors, &src->str2anchors); 172 if(res != RES_OK) return res; 173 res = htable_str2sols_copy_and_clear(&dst->str2children, &src->str2children); 174 if(res != RES_OK) return res; 175 res = darray_anchor_id_copy_and_clear(&dst->anchors, &src->anchors); 176 if(res != RES_OK) return res; 177 res = darray_child_id_copy_and_clear(&dst->children, &src->children); 178 if(res != RES_OK) return res; 179 return RES_OK; 180 } 181 182 static INLINE size_t 183 solparser_entity_get_anchors_count(const struct solparser_entity* entity) 184 { 185 ASSERT(entity); 186 return darray_anchor_id_size_get(&entity->anchors); 187 } 188 189 static INLINE struct solparser_anchor_id 190 solparser_entity_get_anchor(const struct solparser_entity* entity, const size_t i) 191 { 192 ASSERT(entity && i < solparser_entity_get_anchors_count(entity)); 193 return darray_anchor_id_cdata_get(&entity->anchors)[i]; 194 } 195 196 static INLINE size_t 197 solparser_entity_get_children_count(const struct solparser_entity* entity) 198 { 199 ASSERT(entity); 200 return darray_child_id_size_get(&entity->children); 201 } 202 203 static INLINE struct solparser_entity_id 204 solparser_entity_get_child(const struct solparser_entity* entity, const size_t i) 205 { 206 ASSERT(entity && i < solparser_entity_get_children_count(entity)); 207 return darray_child_id_cdata_get(&entity->children)[i]; 208 } 209 210 #endif /* SOLPARSER_ENTITY_H */ 211