star-enclosures-2d

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

senc2d_scene_analyze_c.h (4887B)


      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_SCNENE_ANALYZE_C_H
     17 #define SENC2D_SCNENE_ANALYZE_C_H
     18 
     19 #include "senc2d_internal_types.h"
     20 #include "senc2d.h"
     21 
     22 #include <rsys/mem_allocator.h>
     23 #include <rsys/hash_table.h>
     24 #include <rsys/double2.h>
     25 
     26 struct senc2d_scene;
     27 
     28 static FINLINE void
     29 init_segside(struct mem_allocator* alloc, struct segside* data)
     30 {
     31   int i;
     32   ASSERT(data); (void)alloc;
     33   FOR_EACH(i, 0, 3) data->facing_side_id[i] = SIDE_NULL__;
     34   data->medium = MEDIUM_NULL__;
     35 }
     36 
     37 #define DARRAY_NAME side_id
     38 #define DARRAY_DATA side_id_t
     39 #include <rsys/dynamic_array.h>
     40 
     41 /* Descriptors for connex component.
     42  * Define lists of seg sides starting from a given head.
     43  * Also keeps the maximum z info of the component
     44  * and the list of media found  */
     45 struct cc_descriptor {
     46   /* The area of the component */
     47   double area;
     48   /* Twice the signed volume of the component */
     49   double _2volume;
     50   /* Does this component is an outer border of an enclosure or an inner one? */
     51   int is_outer_border;
     52   vrtx_id_t max_y_vrtx_id; /* id of the vrtx with max z value */
     53   side_id_t side_count;
     54   /* Used when grouping components to form enclosures */
     55   component_id_t cc_id;
     56   component_id_t cc_group_root;
     57   enclosure_id_t enclosure_id;
     58   /* Range of sides member of this component */
     59   struct side_range side_range;
     60   /* Media used by this component */
     61   uchar* media;
     62 };
     63 extern const struct cc_descriptor CC_DESCRIPTOR_NULL;
     64 
     65 static FINLINE void
     66 cc_descriptor_init
     67   (struct mem_allocator* alloc,
     68    struct cc_descriptor* data)
     69 {
     70   ASSERT(data);
     71   (void)alloc;
     72   *data = CC_DESCRIPTOR_NULL;
     73 }
     74 
     75 static FINLINE void
     76 ptr_component_descriptor_init
     77   (struct mem_allocator* alloc,
     78    struct cc_descriptor** data)
     79 {
     80   (void)alloc;
     81   ASSERT(data);
     82   *data = NULL;
     83 }
     84 
     85 #define DARRAY_NAME ptr_component_descriptor
     86 #define DARRAY_DATA struct cc_descriptor*
     87 #define DARRAY_FUNCTOR_INIT ptr_component_descriptor_init
     88 #include <rsys/dynamic_array.h>
     89 
     90 /* Need allocator to free array elts: cannot rely on standard
     91  * darray release stuff */
     92 static FINLINE void
     93 custom_darray_ptr_component_descriptor_release
     94   (struct darray_ptr_component_descriptor* array)
     95 {
     96   size_t c, cc_count;
     97   struct cc_descriptor** components;
     98   if(!array) return;
     99   cc_count = darray_ptr_component_descriptor_size_get(array);
    100   components = darray_ptr_component_descriptor_data_get(array);
    101   FOR_EACH(c, 0, cc_count) {
    102     if(!components[c]) continue;
    103     MEM_RM(array->allocator, components[c]->media);
    104     MEM_RM(array->allocator, components[c]);
    105   }
    106   darray_ptr_component_descriptor_release(array);
    107 }
    108 
    109 /* Segment information.
    110  * Depending on lifespan, information is kept in different places:
    111  * - segment_in for user provided information (kept in scene)
    112  * - segment_tmp for tmp information (kept until segment_comp is ready)
    113  * - segment_comp for information describing components (kept until
    114  *   segment_enc is ready)
    115  * - segment_enc for information describing enclosures (kept in
    116  *   struct descriptor). */
    117 struct segment_tmp {
    118   double max_y;
    119 };
    120 
    121 #ifndef NDEBUG
    122 static FINLINE void
    123 segment_tmp_init(struct mem_allocator* alloc, struct segment_tmp* seg) {
    124   (void)alloc;
    125   ASSERT(seg);
    126   seg->max_y = -DBL_MAX;
    127 }
    128 #define DARRAY_FUNCTOR_INIT segment_tmp_init
    129 #endif
    130 
    131 #define DARRAY_NAME segment_tmp
    132 #define DARRAY_DATA struct segment_tmp
    133 #include <rsys/dynamic_array.h>
    134 
    135 struct neighbour_info {
    136   double angle;
    137   seg_id_t seg_id;
    138   /* Rank of the vertex in the segment (in [0 1]) */
    139   uchar common_vertex_rank;
    140   /* Does the geometrical normal point towards the next neighbour
    141    * (if not, it points towards the previous one)? */
    142   uchar normal_toward_next_neighbour;
    143 };
    144 #define DARRAY_NAME neighbour
    145 #define DARRAY_DATA struct neighbour_info
    146 #include <rsys/dynamic_array.h>
    147 
    148 #define DARRAY_NAME neighbourhood
    149 #define DARRAY_DATA struct darray_neighbour
    150 #define DARRAY_FUNCTOR_INIT darray_neighbour_init
    151 #define DARRAY_FUNCTOR_COPY darray_neighbour_copy
    152 #define DARRAY_FUNCTOR_RELEASE darray_neighbour_release
    153 #define DARRAY_FUNCTOR_COPY_AND_RELEASE darray_neighbour_copy_and_release
    154 #include <rsys/dynamic_array.h>
    155 
    156 extern LOCAL_SYM res_T
    157 scene_analyze(struct senc2d_scene* scene);
    158 
    159 #endif /* SENC2D_SCNENE_ANALYZE_C_H */