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_sample_enclosure.c (3949B)


      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 #include "senc3d.h"
     17 #include "senc3d_sXd_helper.h"
     18 #include "test_senc3d_utils.h"
     19 
     20 #include <rsys/float3.h>
     21 #include <rsys/double3.h>
     22 
     23 #include <star/s3d.h>
     24 #include <star/ssp.h>
     25 
     26 int
     27 main(int argc, char** argv)
     28 {
     29   struct mem_allocator allocator;
     30   struct senc3d_device* dev = NULL;
     31   struct senc3d_scene* scn = NULL;
     32   struct senc3d_enclosure* enclosure = NULL;
     33   struct senc3d_enclosure_header header;
     34   struct s3d_device* s3d = NULL;
     35   struct s3d_scene* s3d_scn = NULL;
     36   struct s3d_scene_view* s3d_view = NULL;
     37   struct s3d_shape* s3d_shp = NULL;
     38   struct s3d_primitive prim;
     39   struct s3d_vertex_data vrtx_get;
     40   struct ssp_rng* rng;
     41   struct context ctx = CONTEXT_NULL__;
     42   int i;
     43   float st[2];
     44   (void)argc, (void)argv;
     45 
     46   OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
     47   OK(senc3d_device_create(NULL, &allocator, SENC3D_NTHREADS_DEFAULT, 1, &dev));
     48 
     49   /* A 3D cube, but with a hole (incomplete).
     50    * 1 single enclosure including both sides of triangles */
     51   ctx.positions = cube_vertices;
     52   ctx.indices = box_indices;
     53   ctx.front_media = medium0;
     54   ctx.back_media = medium0;
     55 
     56   OK(senc3d_scene_create(dev,
     57     SENC3D_CONVENTION_NORMAL_FRONT | SENC3D_CONVENTION_NORMAL_INSIDE,
     58     ntriangles - 1, get_indices, get_media, nvertices, get_position, &ctx,
     59     &scn));
     60 
     61   OK(senc3d_scene_get_enclosure(scn, 0, &enclosure));
     62   OK(senc3d_enclosure_get_header(enclosure, &header));
     63 
     64   /* Put enclosure in a 3D view... */
     65   vrtx_get.type = S3D_FLOAT3;
     66   vrtx_get.usage = S3D_POSITION;
     67   vrtx_get.get = senc3d_sXd_enclosure_get_position;
     68   S3D(device_create(NULL, &allocator, 0, &s3d));
     69   S3D(scene_create(s3d, &s3d_scn));
     70   S3D(shape_create_mesh(s3d, &s3d_shp));
     71   S3D(mesh_setup_indexed_vertices(s3d_shp, header.primitives_count,
     72     senc3d_sXd_enclosure_get_indices, header.vertices_count,
     73     &vrtx_get, 1, enclosure));
     74   S3D(scene_attach_shape(s3d_scn, s3d_shp));
     75   S3D(scene_view_create(s3d_scn, S3D_SAMPLE, &s3d_view));
     76 
     77   /* ... and sample it. */
     78   OK(ssp_rng_create(&allocator, SSP_RNG_THREEFRY, &rng));
     79   FOR_EACH(i, 0, 10000) {
     80     struct s3d_attrib attrib;
     81     int n, c;
     82     S3D(scene_view_sample(s3d_view,
     83       ssp_rng_canonical_float(rng),
     84       ssp_rng_canonical_float(rng),
     85       ssp_rng_canonical_float(rng),
     86       &prim, st));
     87     S3D(primitive_get_attrib(&prim, S3D_POSITION, st, &attrib));
     88     c = 0;
     89     FOR_EACH(n, 0, 3)
     90       if(eq_eps(attrib.value[n], 0, FLT_EPSILON)
     91         || eq_eps(attrib.value[n], 1, FLT_EPSILON))
     92         c++;
     93     CHK(c == 1);
     94     S3D(primitive_get_attrib(&prim, S3D_GEOMETRY_NORMAL, st, &attrib));
     95     c = 0;
     96     FOR_EACH(n, 0, 3)
     97       if(eq_eps(attrib.value[n], -1, FLT_EPSILON)
     98         || eq_eps(attrib.value[n], 1, FLT_EPSILON))
     99         c++;
    100     CHK(c == 1);
    101     c = 0;
    102     FOR_EACH(n, 0, 3)
    103       if(eq_eps(attrib.value[n], 0, FLT_EPSILON))
    104         c++;
    105     CHK(c == 2);
    106   }
    107 
    108   SENC3D(enclosure_ref_put(enclosure));
    109   SENC3D(scene_ref_put(scn));
    110   SENC3D(device_ref_put(dev));
    111 
    112   SSP(rng_ref_put(rng));
    113 
    114   S3D(shape_ref_put(s3d_shp));
    115   S3D(scene_view_ref_put(s3d_view));
    116   S3D(device_ref_put(s3d));
    117   S3D(scene_ref_put(s3d_scn));
    118 
    119   check_memory_allocator(&allocator);
    120   mem_shutdown_proxy_allocator(&allocator);
    121   CHK(mem_allocated_size() == 0);
    122 
    123   return 0;
    124 }