solstice-solver

Solver library of the solstice app
git clone git://git.meso-star.com/solstice-solver.git
Log | Files | Refs | README | LICENSE

test_ssol_shape.c (10192B)


      1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2016, 2018 CNRS
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     16 
     17 #include "ssol.h"
     18 #include "test_ssol_utils.h"
     19 
     20 #define PLANE_NAME SQUARE
     21 #define HALF_X 1
     22 #define HALF_Y 1
     23 #include "test_ssol_rect_geometry.h"
     24 
     25 int
     26 main(int argc, char** argv)
     27 {
     28   struct mem_allocator allocator;
     29   struct ssol_device* dev;
     30   struct ssol_shape* shape;
     31   struct ssol_vertex_data attribs[3] =
     32     {SSOL_VERTEX_DATA_NULL__, SSOL_VERTEX_DATA_NULL__, SSOL_VERTEX_DATA_NULL__};
     33   struct ssol_punched_surface punched_surface = SSOL_PUNCHED_SURFACE_NULL;
     34   struct ssol_carving carving = SSOL_CARVING_NULL;
     35   struct ssol_quadric quadric = SSOL_QUADRIC_DEFAULT;
     36   double polygon[] = {
     37     -1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 0.f, -2.f
     38   };
     39   const size_t npolygon_verts = sizeof(polygon)/(2*sizeof(double));
     40   double val[3];
     41   unsigned ids[3];
     42   unsigned i, n;
     43   (void) argc, (void) argv;
     44 
     45   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     46 
     47   CHK(ssol_device_create
     48     (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK);
     49 
     50   CHK(ssol_shape_create_mesh(NULL, NULL) == RES_BAD_ARG);
     51   CHK(ssol_shape_create_mesh(dev, NULL) == RES_BAD_ARG);
     52   CHK(ssol_shape_create_mesh(NULL, &shape) == RES_BAD_ARG);
     53   CHK(ssol_shape_create_mesh(dev, &shape) == RES_OK);
     54 
     55   CHK(ssol_shape_ref_get(NULL) == RES_BAD_ARG);
     56   CHK(ssol_shape_ref_get(shape) == RES_OK);
     57 
     58   CHK(ssol_shape_ref_put(NULL) == RES_BAD_ARG);
     59   CHK(ssol_shape_ref_put(shape) == RES_OK);
     60 
     61   attribs[0].usage = SSOL_POSITION;
     62   attribs[0].get = get_position;
     63   attribs[1].usage = SSOL_NORMAL;
     64   attribs[1].get = get_normal;
     65   attribs[2].usage = SSOL_TEXCOORD;
     66   attribs[2].get = get_uv;
     67 
     68   CHK(ssol_mesh_setup(NULL, SQUARE_NTRIS__, get_ids, SQUARE_NVERTS__,
     69     attribs, 1, (void*)&SQUARE_DESC__) == RES_BAD_ARG);
     70   CHK(ssol_mesh_setup(shape, 0, get_ids, SQUARE_NVERTS__, attribs, 1,
     71     (void*)&SQUARE_DESC__) == RES_BAD_ARG);
     72   CHK(ssol_mesh_setup(shape, SQUARE_NTRIS__, NULL, SQUARE_NVERTS__,
     73     attribs, 1, (void*)&SQUARE_DESC__) == RES_BAD_ARG);
     74   CHK(ssol_mesh_setup(shape, SQUARE_NTRIS__, get_ids, 0, attribs, 1,
     75     (void*)&SQUARE_DESC__) == RES_BAD_ARG);
     76   CHK(ssol_mesh_setup(shape, SQUARE_NTRIS__, get_ids, SQUARE_NVERTS__,
     77     NULL, 1, (void*)&SQUARE_DESC__) == RES_BAD_ARG);
     78   CHK(ssol_mesh_setup(shape, SQUARE_NTRIS__, get_ids, SQUARE_NVERTS__,
     79     attribs, 0, (void*)&SQUARE_DESC__) == RES_BAD_ARG);
     80   CHK(ssol_mesh_setup(shape, SQUARE_NTRIS__, get_ids, SQUARE_NVERTS__,
     81     attribs, 3, (void*)&SQUARE_DESC__) == RES_OK);
     82 
     83   CHK(ssol_shape_get_vertices_count(NULL, NULL) == RES_BAD_ARG);
     84   CHK(ssol_shape_get_vertices_count(shape, NULL) == RES_BAD_ARG);
     85   CHK(ssol_shape_get_vertices_count(NULL, &n) == RES_BAD_ARG);
     86   CHK(ssol_shape_get_vertices_count(shape, &n) == RES_OK);
     87   CHK(n == SQUARE_NVERTS__);
     88 
     89   CHK(ssol_shape_get_vertex_attrib(NULL, n, (unsigned)-1, NULL) == RES_BAD_ARG);
     90   CHK(ssol_shape_get_vertex_attrib(shape, n, (unsigned)-1, NULL) == RES_BAD_ARG);
     91   CHK(ssol_shape_get_vertex_attrib(NULL, 0, (unsigned)-1, NULL) == RES_BAD_ARG);
     92   CHK(ssol_shape_get_vertex_attrib(shape, 0, (unsigned)-1, NULL) == RES_BAD_ARG);
     93   CHK(ssol_shape_get_vertex_attrib(NULL, n, SSOL_POSITION, NULL) == RES_BAD_ARG);
     94   CHK(ssol_shape_get_vertex_attrib(shape, n, SSOL_POSITION, NULL) == RES_BAD_ARG);
     95   CHK(ssol_shape_get_vertex_attrib(NULL, 0, SSOL_POSITION, NULL) == RES_BAD_ARG);
     96   CHK(ssol_shape_get_vertex_attrib(shape, 0, SSOL_POSITION, NULL) == RES_BAD_ARG);
     97   CHK(ssol_shape_get_vertex_attrib(NULL, n, (unsigned)-1, val) == RES_BAD_ARG);
     98   CHK(ssol_shape_get_vertex_attrib(shape, n, (unsigned)-1, val) == RES_BAD_ARG);
     99   CHK(ssol_shape_get_vertex_attrib(NULL, 0, (unsigned)-1, val) == RES_BAD_ARG);
    100   CHK(ssol_shape_get_vertex_attrib(shape, 0, (unsigned)-1,val) == RES_BAD_ARG);
    101   CHK(ssol_shape_get_vertex_attrib(NULL, n, SSOL_POSITION, val) == RES_BAD_ARG);
    102   CHK(ssol_shape_get_vertex_attrib(shape, n, SSOL_POSITION, val) == RES_BAD_ARG);
    103   CHK(ssol_shape_get_vertex_attrib(NULL, 0, SSOL_POSITION, val) == RES_BAD_ARG);
    104 
    105   FOR_EACH(i, 0, n) {
    106     float valf[3];
    107 
    108     CHK(ssol_shape_get_vertex_attrib(shape, i, SSOL_POSITION, val) == RES_OK);
    109     get_position(i, valf, (void*)&SQUARE_DESC__);
    110     CHK((float)val[0] == valf[0]);
    111     CHK((float)val[1] == valf[1]);
    112     CHK((float)val[2] == valf[2]);
    113 
    114     CHK(ssol_shape_get_vertex_attrib(shape, i, SSOL_NORMAL, val) == RES_OK);
    115     get_normal(i, valf, (void*)&SQUARE_DESC__);
    116     CHK((float)val[0] == valf[0]);
    117     CHK((float)val[1] == valf[1]);
    118     CHK((float)val[2] == valf[2]);
    119 
    120     CHK(ssol_shape_get_vertex_attrib(shape, i, SSOL_TEXCOORD, val) == RES_OK);
    121     get_uv(i, valf, (void*)&SQUARE_DESC__);
    122     CHK((float)val[0] == valf[0]);
    123     CHK((float)val[1] == valf[1]);
    124   }
    125 
    126   CHK(ssol_shape_get_triangles_count(NULL, NULL) == RES_BAD_ARG);
    127   CHK(ssol_shape_get_triangles_count(shape, NULL) == RES_BAD_ARG);
    128   CHK(ssol_shape_get_triangles_count(NULL, &n) == RES_BAD_ARG);
    129   CHK(ssol_shape_get_triangles_count(shape, &n) == RES_OK);
    130   CHK(n == SQUARE_NTRIS__);
    131 
    132   CHK(ssol_shape_get_triangle_indices(NULL, n, NULL) == RES_BAD_ARG);
    133   CHK(ssol_shape_get_triangle_indices(shape, n, NULL) == RES_BAD_ARG);
    134   CHK(ssol_shape_get_triangle_indices(NULL, 0, NULL) == RES_BAD_ARG);
    135   CHK(ssol_shape_get_triangle_indices(shape, 0, NULL) == RES_BAD_ARG);
    136   CHK(ssol_shape_get_triangle_indices(NULL, n, ids) == RES_BAD_ARG);
    137   CHK(ssol_shape_get_triangle_indices(shape, n, ids) == RES_BAD_ARG);
    138   CHK(ssol_shape_get_triangle_indices(NULL, 0, ids) == RES_BAD_ARG);
    139 
    140   FOR_EACH(i, 0, n) {
    141     unsigned ids2[3];
    142     CHK(ssol_shape_get_triangle_indices(shape, i, ids) == RES_OK);
    143     get_ids(i, ids2, (void*)&SQUARE_DESC__);
    144     CHK(ids[0] == ids2[0]);
    145     CHK(ids[1] == ids2[1]);
    146     CHK(ids[2] == ids2[2]);
    147   }
    148 
    149   CHK(ssol_shape_ref_put(shape) == RES_OK);
    150 
    151   CHK(ssol_shape_create_punched_surface(NULL, NULL) == RES_BAD_ARG);
    152   CHK(ssol_shape_create_punched_surface(dev, NULL) == RES_BAD_ARG);
    153   CHK(ssol_shape_create_punched_surface(NULL, &shape) == RES_BAD_ARG);
    154   CHK(ssol_shape_create_punched_surface(dev, &shape) == RES_OK);
    155 
    156   carving.get = get_polygon_vertices;
    157   carving.operation = SSOL_AND;
    158   carving.nb_vertices = npolygon_verts;
    159   carving.context = &polygon;
    160   quadric.type = SSOL_QUADRIC_PLANE;
    161   punched_surface.nb_carvings = 1;
    162   punched_surface.quadric = &quadric;
    163   punched_surface.carvings = &carving;
    164   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_OK);
    165 
    166   punched_surface.nb_carvings = 0;
    167   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    168   punched_surface.nb_carvings = 1;
    169 
    170   punched_surface.carvings = NULL;
    171   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    172   punched_surface.carvings = &carving;
    173 
    174   punched_surface.quadric = NULL;
    175   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    176   punched_surface.quadric = &quadric;
    177 
    178   quadric.type = (enum ssol_quadric_type)999;
    179   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    180   quadric.type = SSOL_QUADRIC_PLANE;
    181 
    182   carving.nb_vertices = 0;
    183   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    184   carving.nb_vertices = npolygon_verts;
    185 
    186   carving.get = NULL;
    187   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    188   carving.get = get_polygon_vertices;
    189 
    190   quadric.type = SSOL_QUADRIC_PARABOL;
    191   quadric.data.parabol.focal = 1;
    192   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_OK);
    193 
    194   punched_surface.nb_carvings = 0;
    195   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    196   punched_surface.nb_carvings = 1;
    197 
    198   quadric.data.parabol.focal = 0;
    199   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    200   quadric.data.parabol.focal = 1;
    201 
    202   quadric.type = SSOL_QUADRIC_HYPERBOL;
    203   quadric.data.hyperbol.real_focal = 1;
    204   quadric.data.hyperbol.img_focal = 1;
    205   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_OK);
    206 
    207   punched_surface.nb_carvings = 0;
    208   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    209   punched_surface.nb_carvings = 1;
    210 
    211   quadric.data.hyperbol.real_focal = 0;
    212   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    213   quadric.data.hyperbol.real_focal = 1;
    214 
    215   quadric.data.hyperbol.img_focal = 0;
    216   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    217   quadric.data.hyperbol.img_focal = 1;
    218 
    219   quadric.type = SSOL_QUADRIC_PARABOLIC_CYLINDER;
    220   quadric.data.parabolic_cylinder.focal = 1;
    221   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_OK);
    222 
    223   punched_surface.nb_carvings = 0;
    224   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    225   punched_surface.nb_carvings = 1;
    226 
    227   quadric.data.parabolic_cylinder.focal = 0;
    228   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    229   quadric.data.parabolic_cylinder.focal = 1;
    230 
    231   quadric.type = SSOL_QUADRIC_HEMISPHERE;
    232   quadric.data.hemisphere.radius = 10;
    233   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_OK);
    234 
    235   punched_surface.nb_carvings = 0;
    236   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_OK);
    237   punched_surface.nb_carvings = 1;
    238 
    239   quadric.data.hemisphere.radius = 0;
    240   CHK(ssol_punched_surface_setup(shape, &punched_surface) == RES_BAD_ARG);
    241   quadric.data.hemisphere.radius = 10;
    242 
    243   CHK(ssol_shape_ref_put(shape) == RES_OK);
    244   CHK(ssol_device_ref_put(dev) == RES_OK);
    245 
    246   check_memory_allocator(&allocator);
    247   mem_shutdown_proxy_allocator(&allocator);
    248   CHK(mem_allocated_size() == 0);
    249 
    250   return 0;
    251 }