solparser_shape.h (11953B)
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_SHAPE_H 18 #define SOLPARSER_SHAPE_H 19 20 #include "solparser_material.h" 21 22 #include <rsys/dynamic_array_double.h> 23 #include <rsys/str.h> 24 25 enum solparser_clip_op { 26 SOLPARSER_CLIP_OP_AND, 27 SOLPARSER_CLIP_OP_SUB 28 }; 29 30 enum solparser_clip_contour_type { 31 SOLPARSER_CLIP_CONTOUR_CIRCLE, 32 SOLPARSER_CLIP_CONTOUR_POLY 33 }; 34 35 enum solparser_shape_type { 36 SOLPARSER_SHAPE_CUBOID, 37 SOLPARSER_SHAPE_CYLINDER, 38 SOLPARSER_SHAPE_OBJ, /* Imported Alias Wavefront obj */ 39 SOLPARSER_SHAPE_PARABOL, 40 SOLPARSER_SHAPE_PARABOLIC_CYLINDER, 41 SOLPARSER_SHAPE_HYPERBOL, 42 SOLPARSER_SHAPE_HEMISPHERE, 43 SOLPARSER_SHAPE_PLANE, 44 SOLPARSER_SHAPE_SPHERE, 45 SOLPARSER_SHAPE_STL /* Imported STereo Lithography */ 46 }; 47 48 /******************************************************************************* 49 * Clipping polygon 50 ******************************************************************************/ 51 struct solparser_circleclip { 52 double radius; 53 double center[2]; 54 long segments; 55 }; 56 57 struct solparser_polyclip { 58 enum solparser_clip_op op; 59 enum solparser_clip_contour_type contour_type; 60 struct darray_double vertices; 61 struct solparser_circleclip circle; 62 }; 63 64 static INLINE void 65 solparser_polyclip_init 66 (struct mem_allocator* allocator, 67 struct solparser_polyclip* polyclip) 68 { 69 ASSERT(polyclip); 70 darray_double_init(allocator, &polyclip->vertices); 71 } 72 73 static INLINE void 74 solparser_polyclip_release(struct solparser_polyclip* polyclip) 75 { 76 ASSERT(polyclip); 77 darray_double_release(&polyclip->vertices); 78 } 79 80 static INLINE res_T 81 solparser_polyclip_copy 82 (struct solparser_polyclip* dst, const struct solparser_polyclip* src) 83 { 84 ASSERT(dst && src); 85 dst->op = src->op; 86 return darray_double_copy(&dst->vertices, &src->vertices); 87 } 88 89 static INLINE res_T 90 solparser_polyclip_copy_and_release 91 (struct solparser_polyclip* dst, struct solparser_polyclip* src) 92 { 93 ASSERT(dst && src); 94 dst->op = src->op; 95 return darray_double_copy_and_release(&dst->vertices, &src->vertices); 96 } 97 98 static INLINE size_t 99 solparser_polyclip_get_vertices_count 100 (const struct solparser_polyclip* polyclip) 101 { 102 size_t n; 103 ASSERT(polyclip); 104 n = darray_double_size_get(&polyclip->vertices); 105 ASSERT((n % 2/*#coords per vertex*/) == 0); 106 return n / 2/*#coords per vertex*/; 107 } 108 109 static INLINE void 110 solparser_polyclip_get_vertex 111 (const struct solparser_polyclip* polyclip, 112 const size_t ivert, 113 double pos[2]) 114 { 115 ASSERT(polyclip && ivert < solparser_polyclip_get_vertices_count(polyclip)); 116 pos[0] = darray_double_cdata_get(&polyclip->vertices)[ivert*2+0]; 117 pos[1] = darray_double_cdata_get(&polyclip->vertices)[ivert*2+1]; 118 } 119 120 121 /* Declare the array of clipping polygons */ 122 #define DARRAY_NAME polyclip 123 #define DARRAY_DATA struct solparser_polyclip 124 #define DARRAY_FUNCTOR_INIT solparser_polyclip_init 125 #define DARRAY_FUNCTOR_RELEASE solparser_polyclip_release 126 #define DARRAY_FUNCTOR_COPY solparser_polyclip_copy 127 #define DARRAY_FUNCTOR_COPY_AND_RELEASE solparser_polyclip_copy_and_release 128 #include <rsys/dynamic_array.h> 129 130 /******************************************************************************* 131 * Imported geometry shape 132 ******************************************************************************/ 133 struct solparser_shape_imported_geometry { 134 struct str filename; 135 }; 136 137 static INLINE void 138 solparser_shape_imported_geometry_init 139 (struct mem_allocator* allocator, 140 struct solparser_shape_imported_geometry* impgeom) 141 { 142 ASSERT(impgeom); 143 str_init(allocator, &impgeom->filename); 144 } 145 146 static INLINE void 147 solparser_shape_imported_geometry_release 148 (struct solparser_shape_imported_geometry* impgeom) 149 { 150 ASSERT(impgeom); 151 str_release(&impgeom->filename); 152 } 153 154 static INLINE res_T 155 solparser_shape_imported_geometry_copy 156 (struct solparser_shape_imported_geometry* dst, 157 const struct solparser_shape_imported_geometry* src) 158 { 159 ASSERT(dst && src); 160 return str_copy(&dst->filename, &src->filename); 161 } 162 163 static INLINE res_T 164 solparser_shape_imported_geometry_copy_and_release 165 (struct solparser_shape_imported_geometry* dst, 166 struct solparser_shape_imported_geometry* src) 167 { 168 ASSERT(dst && src); 169 return str_copy_and_release(&dst->filename, &src->filename); 170 } 171 172 /******************************************************************************* 173 * Paraboloid shape 174 ******************************************************************************/ 175 struct solparser_shape_paraboloid { 176 double focal; 177 long nslices; /* < 0 if not defined */ 178 struct darray_polyclip polyclips; 179 }; 180 181 static INLINE void 182 solparser_shape_paraboloid_init 183 (struct mem_allocator* allocator, 184 struct solparser_shape_paraboloid* paraboloid) 185 { 186 ASSERT(paraboloid); 187 paraboloid->nslices = -1; 188 darray_polyclip_init(allocator, ¶boloid->polyclips); 189 } 190 191 static INLINE void 192 solparser_shape_paraboloid_release(struct solparser_shape_paraboloid* paraboloid) 193 { 194 ASSERT(paraboloid); 195 darray_polyclip_release(¶boloid->polyclips); 196 } 197 198 static INLINE res_T 199 solparser_shape_paraboloid_copy 200 (struct solparser_shape_paraboloid* dst, 201 const struct solparser_shape_paraboloid* src) 202 { 203 ASSERT(dst && src); 204 dst->focal = src->focal; 205 dst->nslices = src->nslices; 206 return darray_polyclip_copy(&dst->polyclips, &src->polyclips); 207 } 208 209 static INLINE res_T 210 solparser_shape_paraboloid_copy_and_release 211 (struct solparser_shape_paraboloid* dst, 212 struct solparser_shape_paraboloid* src) 213 { 214 ASSERT(dst && src); 215 dst->focal = src->focal; 216 dst->nslices = src->nslices; 217 return darray_polyclip_copy_and_release(&dst->polyclips, &src->polyclips); 218 } 219 220 /******************************************************************************* 221 * Hyperboloid shape 222 ******************************************************************************/ 223 struct solparser_hyperboloid_focals { 224 double real; 225 double image; 226 }; 227 228 #define SOLPARSER_HYPERBOLOID_FOCALS_NULL__ { 0, 0 } 229 static const struct solparser_hyperboloid_focals 230 SOLPARSER_HYPERBOLOID_FOCALS_NULL = SOLPARSER_HYPERBOLOID_FOCALS_NULL__; 231 232 struct solparser_shape_hyperboloid { 233 struct solparser_hyperboloid_focals focals; 234 struct darray_polyclip polyclips; 235 long nslices; /* < 0 if not defined */ 236 }; 237 238 static INLINE void 239 solparser_shape_hyperboloid_init 240 (struct mem_allocator* allocator, 241 struct solparser_shape_hyperboloid* hyperboloid) 242 { 243 ASSERT(hyperboloid); 244 hyperboloid->nslices = -1; 245 darray_polyclip_init(allocator, &hyperboloid->polyclips); 246 } 247 248 static INLINE void 249 solparser_shape_hyperboloid_release(struct solparser_shape_hyperboloid* hyperboloid) 250 { 251 ASSERT(hyperboloid); 252 darray_polyclip_release(&hyperboloid->polyclips); 253 } 254 255 static INLINE res_T 256 solparser_shape_hyperboloid_copy 257 (struct solparser_shape_hyperboloid* dst, 258 const struct solparser_shape_hyperboloid* src) 259 { 260 ASSERT(dst && src); 261 dst->focals = src->focals; 262 dst->nslices = src->nslices; 263 return darray_polyclip_copy(&dst->polyclips, &src->polyclips); 264 } 265 266 static INLINE res_T 267 solparser_shape_hyperboloid_copy_and_release 268 (struct solparser_shape_hyperboloid* dst, 269 struct solparser_shape_hyperboloid* src) 270 { 271 ASSERT(dst && src); 272 dst->focals = src->focals; 273 dst->nslices = src->nslices; 274 return darray_polyclip_copy_and_release(&dst->polyclips, &src->polyclips); 275 } 276 277 /******************************************************************************* 278 * Hemisphere shape 279 ******************************************************************************/ 280 struct solparser_shape_hemisphere { 281 double radius; 282 struct darray_polyclip polyclips; 283 long nslices; /* < 0 if not defined */ 284 }; 285 286 static INLINE void 287 solparser_shape_hemisphere_init 288 (struct mem_allocator* allocator, 289 struct solparser_shape_hemisphere* hemisphere) 290 { 291 ASSERT(hemisphere); 292 hemisphere->nslices = -1; 293 darray_polyclip_init(allocator, &hemisphere->polyclips); 294 } 295 296 static INLINE void 297 solparser_shape_hemisphere_release 298 (struct solparser_shape_hemisphere* hemisphere) 299 { 300 ASSERT(hemisphere); 301 darray_polyclip_release(&hemisphere->polyclips); 302 } 303 304 static INLINE res_T 305 solparser_shape_hemisphere_copy 306 (struct solparser_shape_hemisphere* dst, 307 const struct solparser_shape_hemisphere* src) 308 { 309 ASSERT(dst && src); 310 dst->radius = src->radius; 311 dst->nslices = src->nslices; 312 return darray_polyclip_copy(&dst->polyclips, &src->polyclips); 313 } 314 315 static INLINE res_T 316 solparser_shape_hemisphere_copy_and_release 317 (struct solparser_shape_hemisphere* dst, 318 struct solparser_shape_hemisphere* src) 319 { 320 ASSERT(dst && src); 321 dst->radius = src->radius; 322 dst->nslices = src->nslices; 323 return darray_polyclip_copy_and_release(&dst->polyclips, &src->polyclips); 324 } 325 326 /******************************************************************************* 327 * Plane shape 328 ******************************************************************************/ 329 struct solparser_shape_plane { 330 struct darray_polyclip polyclips; 331 long nslices; 332 }; 333 334 static INLINE void 335 solparser_shape_plane_init 336 (struct mem_allocator* allocator, 337 struct solparser_shape_plane* plane) 338 { 339 ASSERT(plane); 340 plane->nslices = 1; 341 darray_polyclip_init(allocator, &plane->polyclips); 342 } 343 344 static INLINE void 345 solparser_shape_plane_release(struct solparser_shape_plane* plane) 346 { 347 ASSERT(plane); 348 darray_polyclip_release(&plane->polyclips); 349 } 350 351 static INLINE res_T 352 solparser_shape_plane_copy 353 (struct solparser_shape_plane* dst, 354 const struct solparser_shape_plane* src) 355 { 356 ASSERT(dst && src); 357 dst->nslices = src->nslices; 358 return darray_polyclip_copy(&dst->polyclips, &src->polyclips); 359 } 360 361 static INLINE res_T 362 solparser_shape_plane_copy_and_release 363 (struct solparser_shape_plane* dst, 364 struct solparser_shape_plane* src) 365 { 366 ASSERT(dst && src); 367 dst->nslices = src->nslices; 368 return darray_polyclip_copy_and_release(&dst->polyclips, &src->polyclips); 369 } 370 371 /******************************************************************************* 372 * POD shape data 373 ******************************************************************************/ 374 struct solparser_shape_cuboid { 375 double size[3]; /* Size along the X, Y and Z dimension */ 376 }; 377 378 struct solparser_shape_cylinder { 379 double height; 380 double radius; 381 long nslices; 382 long nstacks; 383 }; 384 385 struct solparser_shape_sphere { 386 double radius; 387 long nslices; 388 long nstacks; 389 }; 390 391 struct solparser_shape_cuboid_id { size_t i; }; 392 struct solparser_shape_cylinder_id { size_t i; }; 393 struct solparser_shape_imported_geometry_id { size_t i; }; 394 struct solparser_shape_paraboloid_id { size_t i; }; 395 struct solparser_shape_hyperboloid_id { size_t i; }; 396 struct solparser_shape_hemisphere_id { size_t i; }; 397 struct solparser_shape_plane_id { size_t i; }; 398 struct solparser_shape_sphere_id { size_t i; }; 399 400 struct solparser_shape { 401 enum solparser_shape_type type; 402 union { 403 struct solparser_shape_cuboid_id cuboid; 404 struct solparser_shape_cylinder_id cylinder; 405 struct solparser_shape_imported_geometry_id obj; 406 struct solparser_shape_paraboloid_id parabol; 407 struct solparser_shape_paraboloid_id parabolic_cylinder; 408 struct solparser_shape_hyperboloid_id hyperbol; 409 struct solparser_shape_hemisphere_id hemisphere; 410 struct solparser_shape_plane_id plane; 411 struct solparser_shape_sphere_id sphere; 412 struct solparser_shape_imported_geometry_id stl; 413 } data; 414 }; 415 416 struct solparser_shape_id { size_t i; }; 417 418 #endif /* SOLPARSER_SHAPE_H */ 419