star-cpr

Clip 2D meshes with 2D polygons
git clone git://git.meso-star.fr/star-cpr.git
Log | Files | Refs | README | LICENSE

test_scpr_is_in.c (3005B)


      1 /* Copyright (C) 2016-2018, 2021-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 #define _POSIX_C_SOURCE 200112L
     17 
     18 #include "scpr.h"
     19 #include "test_scpr_utils.h"
     20 
     21 #include <rsys/rsys.h>
     22 #include <rsys/mem_allocator.h>
     23 
     24 #include <memory.h>
     25 
     26 
     27 int
     28 main(int argc, char** argv)
     29 {
     30   struct mem_allocator allocator;
     31   struct scpr_device_create_args args = SCPR_DEVICE_CREATE_ARGS_DEFAULT;
     32   struct scpr_device* dev;
     33   double c0[] = {0, 5, 8, 5, 8, 9, 0, 9 };
     34   double c1[] = {1, 1, 3, 1, 3, 9, 1, 9 };
     35   double c2[] = {3, 6, 5, 6, 5, 8, 4, 7, 3, 8 };
     36   size_t n0[] = { 4 };
     37   size_t n1[] = { 4 };
     38   size_t n2[] = { 5 };
     39   struct scpr_polygon* p0 = NULL;
     40   struct scpr_polygon* p1 = NULL;
     41   struct scpr_polygon* p2 = NULL;
     42   struct polygon_context context;
     43   double** pos;
     44   int in;
     45   (void)argc, (void)argv;
     46 
     47   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     48 
     49   pos = (double**)MEM_CALLOC(&allocator, 1, sizeof(*pos));
     50   *pos = (double*)MEM_CALLOC(&allocator, 10, sizeof(**pos));
     51 
     52   args.allocator = &allocator;
     53   args.precision = 2;
     54   OK(scpr_device_create(&args, &dev));
     55 
     56   OK(scpr_polygon_create(dev, &p0));
     57   OK(scpr_polygon_create(dev, &p1));
     58   OK(scpr_polygon_create(dev, &p2));
     59 
     60   context.ncomps = 1;
     61   context.coords = pos;
     62 
     63   context.nverts = n0;
     64   memcpy(*pos, c0, 2*n0[0]*sizeof(**pos));
     65   OK(scpr_polygon_setup_indexed_vertices(p0, 1, pget_nverts, pget_pos, &context));
     66 
     67   context.nverts = n1;
     68   memcpy(*pos, c1, 2*n1[0]*sizeof(**pos));
     69   OK(scpr_polygon_setup_indexed_vertices(p1, 1, pget_nverts, pget_pos, &context));
     70 
     71   context.nverts = n2;
     72   memcpy(*pos, c2, 2*n2[0]*sizeof(**pos));
     73   OK(scpr_polygon_setup_indexed_vertices(p2, 1, pget_nverts, pget_pos, &context));
     74 
     75   BAD(scpr_is_component_in_component(NULL, 0, NULL, 0, NULL));
     76   OK(scpr_is_component_in_component(p1, 0, p2, 0, &in));
     77   CHK(in == 0);
     78   OK(scpr_is_component_in_component(p2, 0, p1, 0, &in));
     79   CHK(in == 0);
     80   OK(scpr_is_component_in_component(p2, 0, p0, 0, &in));
     81   CHK(in == 1);
     82   OK(scpr_is_component_in_component(p0, 0, p2, 0, &in));
     83   CHK(in == 0);
     84 
     85   OK(scpr_device_ref_put(dev));
     86   OK(scpr_polygon_ref_put(p0));
     87   OK(scpr_polygon_ref_put(p1));
     88   OK(scpr_polygon_ref_put(p2));
     89 
     90   MEM_RM(&allocator, *pos);
     91   MEM_RM(&allocator, pos);
     92 
     93   check_memory_allocator(&allocator);
     94   mem_shutdown_proxy_allocator(&allocator);
     95   CHK(mem_allocated_size() == 0);
     96   return 0;
     97 }