star-geometry-3d

Clean and decorate 3D geometries
git clone git://git.meso-star.fr/star-geometry-3d.git
Log | Files | Refs | README | LICENSE

sg3d_geometry.h (8201B)


      1 /* Copyright (C) 2019, 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 SG3D_GEOMETRY_H__
     17 #define SG3D_GEOMETRY_H__
     18 
     19 #include "sg3d.h"
     20 #include "sg3d_misc.h"
     21 
     22 #include <rsys/ref_count.h>
     23 #include <rsys/dynamic_array.h>
     24 #include <rsys/dynamic_array_uint.h>
     25 #include <rsys/hash_table.h>
     26 
     27 /* Forward declaration of external opaque data types */
     28 
     29 /******************************************************************************
     30  * A type to store triangles
     31  *****************************************************************************/
     32 struct triangle {
     33   vrtx_id_t vertex_ids[3];
     34   /* FRONT/BACK/INTERFACE property */
     35   prop_id_t properties[SG3D_PROP_TYPES_COUNT__];
     36   /* ID of the triangle in user world, i.e. without deduplication */
     37   trg_id_t user_id;
     38 };
     39 #define TRG_UNDEF__ {\
     40   { SG3D_UNSPECIFIED_PROPERTY, SG3D_UNSPECIFIED_PROPERTY, SG3D_UNSPECIFIED_PROPERTY },\
     41   { SG3D_UNSPECIFIED_PROPERTY, SG3D_UNSPECIFIED_PROPERTY, SG3D_UNSPECIFIED_PROPERTY },\
     42   SG3D_UNSPECIFIED_PROPERTY\
     43 }
     44 #define DARRAY_NAME triangle
     45 #define DARRAY_DATA struct triangle
     46 #include <rsys/dynamic_array.h>
     47 
     48 /******************************************************************************
     49  * A type to store vertices
     50  *****************************************************************************/
     51 struct vertex {
     52   double coord[3];
     53 };
     54 #define DARRAY_NAME vertex
     55 #define DARRAY_DATA struct vertex
     56 #include <rsys/dynamic_array.h>
     57 
     58 /******************************************************************************
     59  * A type to map triangle vertices to IDs in unique_triangles
     60  *****************************************************************************/
     61 struct vrtx_id3 { vrtx_id_t x[3]; };
     62 
     63 static FINLINE int
     64 trg_key_eq(const struct vrtx_id3* k1, const struct vrtx_id3* k2)
     65 {
     66   ASSERT(k1 && k2);
     67   ASSERT(k1->x[0] < k1->x[1] && k1->x[1] < k1->x[2]);
     68   ASSERT(k2->x[0] < k2->x[1] && k2->x[1] < k2->x[2]);
     69   return (k1->x[0] == k2->x[0])
     70     && (k1->x[1] == k2->x[1])
     71     && (k1->x[2] == k2->x[2]);
     72 }
     73 
     74 #define HTABLE_NAME trg
     75 #define HTABLE_KEY struct vrtx_id3
     76 #define HTABLE_DATA trg_id_t
     77 #define HTABLE_KEY_FUNCTOR_EQ trg_key_eq
     78 #include <rsys/hash_table.h>
     79 
     80 /******************************************************************************
     81  * A type to map vertex coordinates to IDs in unique_vertices
     82  *****************************************************************************/
     83 static FINLINE int
     84 vrtx_eq(const struct vertex* v1, const struct vertex* v2)
     85 {
     86   int i;
     87   ASSERT(v1 && v2);
     88   FOR_EACH(i, 0, 3) if(v1->coord[i] != v2->coord[i]) return 0;
     89   return 1;
     90 }
     91 
     92 #define HTABLE_NAME vrtx
     93 #define HTABLE_KEY struct vertex
     94 #define HTABLE_DATA vrtx_id_t
     95 #define HTABLE_KEY_FUNCTOR_EQ vrtx_eq
     96 #include <rsys/hash_table.h>
     97 
     98 /******************************************************************************
     99  * Types to record sources and values of triangle descriptions.
    100  *****************************************************************************/
    101 
    102 /* A type to store a value and the files defining this value
    103  * (usualy a single file) */
    104 struct definition {
    105   /* The value */
    106   prop_id_t property_value;
    107   /* The IDs of the geometry sets that defined the value */
    108   struct darray_uint set_ids;
    109 };
    110 
    111 static FINLINE void
    112 init_definition
    113   (struct mem_allocator* alloc,
    114    struct definition* data)
    115 {
    116   ASSERT(alloc && data);
    117   data->property_value = SG3D_UNSPECIFIED_PROPERTY;
    118   darray_uint_init(alloc, &data->set_ids);
    119 }
    120 
    121 static INLINE res_T
    122 copy_definition
    123   (struct definition* dst,
    124    const struct definition* src)
    125 {
    126   res_T res = RES_OK;
    127   ASSERT(dst && src);
    128   dst->property_value = src->property_value;
    129   ERR(darray_uint_copy(&dst->set_ids, &src->set_ids));
    130 exit:
    131   return res;
    132 error:
    133   goto exit;
    134 }
    135 
    136 static FINLINE void
    137 release_definition
    138   (struct definition* data)
    139 {
    140   ASSERT(data);
    141   darray_uint_release(&data->set_ids);
    142 }
    143 
    144 #define DARRAY_NAME definition
    145 #define DARRAY_DATA struct definition
    146 #define DARRAY_FUNCTOR_INIT init_definition
    147 #define DARRAY_FUNCTOR_COPY copy_definition
    148 #define DARRAY_FUNCTOR_RELEASE release_definition
    149 #include <rsys/dynamic_array.h>
    150 
    151 /* A type to accumulate information for a triangle.
    152  * If there is more than 1 definition / field, it is a conflict */
    153 struct trg_descriptions {
    154   struct darray_definition defs[SG3D_PROP_TYPES_COUNT__];
    155   int merge_conflict;
    156   int properties_conflict;
    157   char defs_include_unspecified;
    158   char property_defined[SG3D_PROP_TYPES_COUNT__];
    159 };
    160 
    161 static FINLINE void
    162 init_trg_descriptions
    163   (struct mem_allocator* alloc,
    164    struct trg_descriptions* data)
    165 {
    166   int i;
    167   ASSERT(alloc && data);
    168   FOR_EACH(i, 0, SG3D_PROP_TYPES_COUNT__)
    169     darray_definition_init(alloc, data->defs + i);
    170   data->merge_conflict = 0;
    171   data->properties_conflict = 0;
    172   data->defs_include_unspecified = 0;
    173   FOR_EACH(i, 0, SG3D_PROP_TYPES_COUNT__)
    174     data->property_defined[i] = 0;
    175 }
    176 
    177 static INLINE res_T
    178 copy_trg_descriptions
    179   (struct trg_descriptions* dst,
    180    const struct trg_descriptions* src)
    181 {
    182   res_T res = RES_OK;
    183   int i;
    184   ASSERT(dst && src);
    185   FOR_EACH(i, 0, SG3D_PROP_TYPES_COUNT__)
    186     ERR(darray_definition_copy(&dst->defs[i], &src->defs[i]));
    187   dst->merge_conflict = src->merge_conflict;
    188   dst->properties_conflict = src->properties_conflict;
    189   dst->defs_include_unspecified = src->defs_include_unspecified;
    190   FOR_EACH(i, 0, SG3D_PROP_TYPES_COUNT__)
    191     dst->property_defined[i] = src->property_defined[i];
    192 exit:
    193   return res;
    194 error:
    195   goto exit;
    196 }
    197 
    198 static FINLINE void
    199 release_trg_descriptions
    200   (struct trg_descriptions* data)
    201 {
    202   int i;
    203   ASSERT(data);
    204   FOR_EACH(i, 0, SG3D_PROP_TYPES_COUNT__)
    205     darray_definition_release(data->defs + i);
    206 }
    207 
    208 #define DARRAY_NAME trg_descriptions
    209 #define DARRAY_DATA struct trg_descriptions
    210 #define DARRAY_FUNCTOR_INIT init_trg_descriptions
    211 #define DARRAY_FUNCTOR_COPY copy_trg_descriptions
    212 #define DARRAY_FUNCTOR_RELEASE release_trg_descriptions
    213 #include <rsys/dynamic_array.h>
    214 
    215 /******************************************************************************
    216  * Types to store geometry amid sg3d_geometry_add calls.
    217  *****************************************************************************/
    218 struct sg3d_geometry {
    219   /* Record unique (i.e. deduplicated) triangles */
    220   struct darray_triangle unique_triangles;
    221   /* Record coordinates for unique (i.e. deduplicated) vertices */
    222   struct darray_vertex unique_vertices;
    223 
    224   /* A table to map triangle vertices to IDs in unique_triangles */
    225   struct htable_trg unique_triangles_ids;
    226   /* A table to map vertex coordinates to IDs in unique_vertices */
    227   struct htable_vrtx unique_vertices_ids;
    228 
    229   /* Record which set defined what */
    230   struct darray_trg_descriptions trg_descriptions;
    231 
    232   /* Counts */
    233   unsigned set_id;
    234   trg_id_t triangle_count_including_duplicates;
    235   side_id_t sides_with_defined_medium_count;
    236   trg_id_t trg_with_unspecified_sides_count;
    237   trg_id_t trg_with_unspecified_intface_count;
    238   trg_id_t merge_conflict_count;
    239   trg_id_t properties_conflict_count;
    240 
    241   struct sg3d_device* dev;
    242   ref_T ref;
    243 };
    244 
    245 /******************************************************************************
    246  * Local functions
    247  *****************************************************************************/
    248 
    249 extern LOCAL_SYM res_T
    250 geometry_register_triangle
    251   (struct sg3d_geometry* geometry,
    252    const struct triangle* triangle,
    253    const trg_id_t triangle_unique_id,
    254    const unsigned set_id,
    255    const int merge_conflict);
    256 
    257 /* Add new undefined triangle descriptions to a geometry */
    258 extern LOCAL_SYM res_T
    259 geometry_enlarge_trg_descriptions
    260   (struct sg3d_geometry* geom,
    261    const trg_id_t sz);
    262 
    263 #endif /* SG3D_GEOMETRY_H__ */