star-enclosures-3d

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

test_senc3d_utils2.h (4082B)


      1 /* Copyright (C) 2018-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 TEST_UTILS2_H
     17 #define TEST_UTILS2_H
     18 
     19 #if !defined(NB_CYL_X) ||  !defined(NB_CYL_Y) || !defined(NB_CYL_Z) | !defined(NB_CYL)
     20 #error "Macro definitions are missing"
     21 #endif
     22 
     23 #include "test_senc3d_utils.h"
     24 
     25 #include <star/s3dut.h>
     26 #include <rsys/double3.h>
     27 
     28 struct s3dut_context {
     29   struct s3dut_mesh_data data;
     30   struct context ctx;
     31 };
     32 
     33 static void
     34 get_s3dut_indices(const unsigned itri, unsigned ids[3], void* context)
     35 {
     36   struct s3dut_context* ctx = context;
     37   unsigned s3dut_itri, cyl_idx, v_offset;
     38   ASSERT(ids && ctx);
     39   ASSERT(itri < NB_CYL * ctx->data.nprimitives);
     40   /* Get cyl_idx along with the s3dut vertice index */
     41   s3dut_itri = itri % (unsigned)ctx->data.nprimitives;
     42   cyl_idx = itri / (unsigned)ctx->data.nprimitives;
     43   ASSERT(ctx->data.indices[s3dut_itri * 3 + 0] <= UINT_MAX
     44     && ctx->data.indices[s3dut_itri * 3 + 1] <= UINT_MAX
     45     && ctx->data.indices[s3dut_itri * 3 + 2] <= UINT_MAX);
     46   /* Compute the vertex index in the user numbering
     47    * from cyl_idx and s3dut data; vertex related getters
     48    * will have to get the s3dut index back */
     49   v_offset = cyl_idx * (unsigned)ctx->data.nvertices;
     50   ids[0] = v_offset + (unsigned)ctx->data.indices[s3dut_itri * 3 + 0];
     51   ids[ctx->ctx.reverse_vrtx ? 2 : 1]
     52     = v_offset + (unsigned)ctx->data.indices[s3dut_itri * 3 + 1];
     53   ids[ctx->ctx.reverse_vrtx ? 1 : 2]
     54     = v_offset + (unsigned)ctx->data.indices[s3dut_itri * 3 + 2];
     55 }
     56 
     57 static void
     58 get_s3dut_position(const unsigned ivert, double pos[3], void* context)
     59 {
     60   struct s3dut_context* ctx = context;
     61   unsigned s3dut_ivert, cyl_idx;
     62   int i, j, k;
     63   double offset[3], tmp[3];
     64   double center_x, center_y, scale, misalignment = 0;
     65   ASSERT(pos && ctx);
     66   ASSERT(ivert < NB_CYL * ctx->data.nvertices);
     67   /* Get cyl_idx and cylinder imbrication along with the s3dut vertice index */
     68   s3dut_ivert = ivert % (unsigned)ctx->data.nvertices;
     69   cyl_idx = ivert / (unsigned)ctx->data.nvertices;
     70   /* k th cylinder of the imbrication at grid position i,j */
     71   i = (int)cyl_idx / (NB_CYL_Y * NB_CYL_Z);
     72   j = (cyl_idx / NB_CYL_Z) % NB_CYL_Y;
     73   k = cyl_idx % NB_CYL_Z;
     74   ASSERT(i < NB_CYL_X && j < NB_CYL_Y && k < NB_CYL_Z);
     75   ASSERT((unsigned)(i * NB_CYL_Y * NB_CYL_Z + j * NB_CYL_Z + k)
     76     * ctx->data.nvertices + s3dut_ivert == ivert);
     77   center_x = 2 * (1 + NB_CYL_Z) * (i - NB_CYL_X / 2);
     78   center_y = 2 * (1 + NB_CYL_Z) * (j - NB_CYL_Y / 2);
     79   /* Compute scale and offset from imbrication */
     80   scale = k + 1;
     81 #ifdef MITIGATE_EMBREE_181
     82   /* Mitigate Embree issue #181
     83    * We cannot keep perfect alignment of cylinders
     84    * or some hits are missed */
     85   misalignment = (k % 2) ? -0.01 : +0.01;
     86 #endif
     87   d3(offset, center_x + misalignment, center_y + misalignment, 0);
     88   d3_add(pos, d3_muld(tmp, ctx->data.positions + s3dut_ivert * 3, scale),
     89     offset);
     90 }
     91 
     92 static void
     93 get_s3dut_media(const unsigned itri, unsigned medium[2], void* context)
     94 {
     95   struct s3dut_context* ctx = context;
     96   unsigned cyl_idx;
     97   int k;
     98   ASSERT(medium && ctx);
     99   ASSERT(itri < NB_CYL * ctx->data.nprimitives);
    100   /* Get cyl_idx */
    101   cyl_idx = itri / (unsigned)ctx->data.nprimitives;
    102   /* k th cylinder of the imbrication at some grid position */
    103   k = cyl_idx % NB_CYL_Z;
    104   medium[ctx->ctx.reverse_med ? SENC3D_BACK : SENC3D_FRONT] = (unsigned)k;
    105   medium[ctx->ctx.reverse_med ? SENC3D_FRONT : SENC3D_BACK] = (unsigned)(k + 1);
    106 }
    107 
    108 #endif /* TEST_UTILS2_H */