star-2d

Contour structuring for efficient 2D geometric queries
git clone git://git.meso-star.fr/star-2d.git
Log | Files | Refs | README | LICENSE

test_s2d_trace_ray_3d.c (5083B)


      1 /* Copyright (C) 2016-2021, 2023 |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 "s2d.h"
     17 #include "test_s2d_utils.h"
     18 
     19 #include <rsys/float2.h>
     20 #include <rsys/float3.h>
     21 
     22 int
     23 main(int argc, char** argv)
     24 {
     25   struct mem_allocator allocator;
     26   struct s2d_device* dev;
     27   struct s2d_scene* scn;
     28   struct s2d_scene_view* scnview;
     29   struct s2d_shape* shape;
     30   struct s2d_vertex_data vdata;
     31   struct s2d_hit hit;
     32   float org[3];
     33   float dir[3];
     34   float range[2];
     35   float tmp[2];
     36   float dot;
     37   (void)argc, (void)argv;
     38 
     39   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     40 
     41   CHK(s2d_device_create(NULL, &allocator, 1, &dev) == RES_OK);
     42   CHK(s2d_scene_create(dev, &scn) == RES_OK);
     43   CHK(s2d_shape_create_line_segments(dev, &shape) == RES_OK);
     44   CHK(s2d_scene_attach_shape(scn, shape) == RES_OK);
     45 
     46   vdata.get = line_segments_get_position;
     47   vdata.type = S2D_FLOAT2;
     48   vdata.usage = S2D_POSITION;
     49   CHK(s2d_line_segments_setup_indexed_vertices
     50     (shape, square_nsegs, line_segments_get_ids, square_nverts, &vdata, 1,
     51      (void*)&square_desc) == RES_OK);
     52 
     53   f3_splat(org, 10.f);
     54   f3(dir, 1, 0, 0);
     55   f2(range, 0, FLT_MAX);
     56 
     57   CHK(s2d_scene_view_create(scn, S2D_TRACE, &scnview) == RES_OK);
     58 
     59   #define RT_3D s2d_scene_view_trace_ray_3d
     60   CHK(RT_3D(NULL, NULL, NULL, NULL, NULL, NULL) == RES_BAD_ARG);
     61   CHK(RT_3D(scnview, NULL, NULL, NULL, NULL, NULL) == RES_BAD_ARG);
     62   CHK(RT_3D(NULL, org, NULL, NULL, NULL, NULL) == RES_BAD_ARG);
     63   CHK(RT_3D(scnview, org, NULL, NULL, NULL, NULL) == RES_BAD_ARG);
     64   CHK(RT_3D(NULL, NULL, dir, NULL, NULL, NULL) == RES_BAD_ARG);
     65   CHK(RT_3D(scnview, NULL, dir, NULL, NULL, NULL) == RES_BAD_ARG);
     66   CHK(RT_3D(NULL, org, dir, NULL, NULL, NULL) == RES_BAD_ARG);
     67   CHK(RT_3D(scnview, org, dir, NULL, NULL, NULL) == RES_BAD_ARG);
     68   CHK(RT_3D(NULL, NULL, NULL, range, NULL, NULL) == RES_BAD_ARG);
     69   CHK(RT_3D(scnview, NULL, NULL, range, NULL, NULL) == RES_BAD_ARG);
     70   CHK(RT_3D(NULL, org, NULL, range, NULL, NULL) == RES_BAD_ARG);
     71   CHK(RT_3D(scnview, org, NULL, range, NULL, NULL) == RES_BAD_ARG);
     72   CHK(RT_3D(NULL, NULL, dir, range, NULL, NULL) == RES_BAD_ARG);
     73   CHK(RT_3D(scnview, NULL, dir, range, NULL, NULL) == RES_BAD_ARG);
     74   CHK(RT_3D(NULL, org, dir, range, NULL, NULL) == RES_BAD_ARG);
     75   CHK(RT_3D(scnview, org, dir, range, NULL, NULL) == RES_BAD_ARG);
     76   CHK(RT_3D(NULL, NULL, NULL, NULL, NULL, &hit) == RES_BAD_ARG);
     77   CHK(RT_3D(scnview, NULL, NULL, NULL, NULL, &hit) == RES_BAD_ARG);
     78   CHK(RT_3D(NULL, org, NULL, NULL, NULL, &hit) == RES_BAD_ARG);
     79   CHK(RT_3D(scnview, org, NULL, NULL, NULL, &hit) == RES_BAD_ARG);
     80   CHK(RT_3D(NULL, NULL, dir, NULL, NULL, &hit) == RES_BAD_ARG);
     81   CHK(RT_3D(scnview, NULL, dir, NULL, NULL, &hit) == RES_BAD_ARG);
     82   CHK(RT_3D(NULL, org, dir, NULL, NULL, &hit) == RES_BAD_ARG);
     83   CHK(RT_3D(scnview, org, dir, NULL, NULL, &hit) == RES_BAD_ARG);
     84   CHK(RT_3D(NULL, NULL, NULL, range, NULL, &hit) == RES_BAD_ARG);
     85   CHK(RT_3D(scnview, NULL, NULL, range, NULL, &hit) == RES_BAD_ARG);
     86   CHK(RT_3D(NULL, org, NULL, range, NULL, &hit) == RES_BAD_ARG);
     87   CHK(RT_3D(scnview, org, NULL, range, NULL, &hit) == RES_BAD_ARG);
     88   CHK(RT_3D(NULL, NULL, dir, range, NULL, &hit) == RES_BAD_ARG);
     89   CHK(RT_3D(scnview, NULL, dir, range, NULL, &hit) == RES_BAD_ARG);
     90   CHK(RT_3D(NULL, org, dir, range, NULL, &hit) == RES_BAD_ARG);
     91   CHK(RT_3D(scnview, org, dir, range, NULL, &hit) == RES_OK);
     92 
     93   CHK(S2D_HIT_NONE(&hit) == 0);
     94   f2_normalize(hit.normal, hit.normal);
     95   CHK(f2_eq_eps(hit.normal, f2(tmp, -1.f, 0.f), 1.e-6f) == 1);
     96   CHK(eq_epsf(hit.distance, 1.f, 1.e-6f) == 1);
     97   CHK(eq_epsf(hit.u, 0.5f, 1.e-6f) == 1);
     98 
     99   f3_normalize(dir, f3(dir, 1.f, 0.f, 1.f));
    100   CHK(RT_3D(scnview, org, dir, range, NULL, &hit) == RES_OK);
    101   CHK(S2D_HIT_NONE(&hit) == 0);
    102   f2_normalize(hit.normal, hit.normal);
    103   CHK(f2_eq_eps(hit.normal, f2(tmp, -1.f, 0.f), 1.e-6f) == 1);
    104   CHK(eq_epsf(hit.u, 0.5f, 1.e-6f) == 1);
    105 
    106   dot = dir[0];
    107   CHK(eq_epsf(hit.distance, 1.f / dot, 1.e-6f) == 1);
    108 
    109   range[1] = 1.2f;
    110   CHK(RT_3D(scnview, org, dir, range, NULL, &hit) == RES_OK);
    111   CHK(S2D_HIT_NONE(&hit) == 1);
    112 
    113   CHK(s2d_scene_view_ref_put(scnview) == RES_OK);
    114 
    115   CHK(RT_3D(scnview, org, dir, range, NULL, &hit) == RES_BAD_OP);
    116   #undef RT_3D
    117 
    118   CHK(s2d_scene_ref_put(scn) == RES_OK);
    119   CHK(s2d_shape_ref_put(shape) == RES_OK);
    120   CHK(s2d_device_ref_put(dev) == RES_OK);
    121 
    122   check_memory_allocator(&allocator);
    123   mem_shutdown_proxy_allocator(&allocator);
    124   CHK(mem_allocated_size() == 0);
    125 
    126   return 0;
    127 }