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_utils.h (3641B)


      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 #ifndef TEST_CPR_UTILS_H
     17 #define TEST_CPR_UTILS_H
     18 
     19 #define _POSIX_C_SOURCE 200112L
     20 
     21 #include "scpr.h"
     22 #include <rsys/mem_allocator.h>
     23 #include <stdio.h>
     24 #include <math.h>
     25 
     26 #define ERR(Expr) { if((res = (Expr)) != RES_OK) goto error; } (void)0
     27 #define BAD(Expr) CHK((Expr) == RES_BAD_ARG);
     28 #define OK(Expr) CHK((Expr) == RES_OK);
     29 
     30 struct polygon_context {
     31   double** coords;
     32   size_t* nverts;
     33   size_t ncomps;
     34 };
     35 
     36 static INLINE void
     37 pget_nverts(const size_t icomp, size_t* nverts, void* context)
     38 {
     39   const struct polygon_context* ctx = (const struct polygon_context*)context;
     40   CHK(nverts != NULL);
     41   CHK(context != NULL);
     42   CHK(icomp < ctx->ncomps);
     43   *nverts = ctx->nverts[icomp];
     44 }
     45 
     46 static INLINE void
     47 pget_pos(const size_t icomp, const size_t ivert, double pos[2], void* context)
     48 {
     49   const struct polygon_context* ctx = (const struct polygon_context*)context;
     50   CHK(pos != NULL);
     51   CHK(context != NULL);
     52   CHK(ctx->coords != NULL);
     53   CHK(icomp < ctx->ncomps);
     54   CHK(ivert < ctx->nverts[icomp]);
     55   pos[0] = ctx->coords[icomp][ivert*2 + 0];
     56   pos[1] = ctx->coords[icomp][ivert*2 + 1];
     57 }
     58 
     59 struct mesh_context {
     60   const double* coords;
     61   size_t nverts;
     62   const size_t* indices;
     63   size_t ntris;
     64 };
     65 
     66 static INLINE void
     67 mget_pos(const size_t ivert, double pos[2], void* context)
     68 {
     69   const struct mesh_context* ctx = (const struct mesh_context*)context;
     70   CHK(pos != NULL);
     71   CHK(context != NULL);
     72   CHK(ctx->coords != NULL);
     73   CHK(ivert < ctx->nverts);
     74   pos[0] = ctx->coords[ivert*2 + 0];
     75   pos[1] = ctx->coords[ivert*2 + 1];
     76 }
     77 
     78 static INLINE void
     79 mget_ids(const size_t itri, size_t ids[3], void* context)
     80 {
     81   const struct mesh_context* ctx = (const struct mesh_context*)context;
     82   CHK(ids != NULL);
     83   CHK(context != NULL);
     84   CHK(ctx->indices != NULL);
     85   CHK(itri < ctx->ntris);
     86   ids[0] = ctx->indices[itri*3 + 0];
     87   ids[1] = ctx->indices[itri*3 + 1];
     88   ids[2] = ctx->indices[itri*3 + 2];
     89 }
     90 
     91 static void
     92 check_memory_allocator(struct mem_allocator* allocator)
     93 {
     94   if(MEM_ALLOCATED_SIZE(allocator)) {
     95     char dump[512];
     96     MEM_DUMP(allocator, dump, sizeof(dump)/sizeof(char));
     97     fprintf(stderr, "%s\n", dump);
     98     FATAL("Memory leaks\n");
     99   }
    100 }
    101 
    102 INLINE int
    103 check_stability
    104   (struct scpr_device* dev,
    105    const struct scpr_polygon* polygon)
    106 {
    107   res_T res = RES_OK;
    108   size_t i, j, ccount, vcount;
    109   ASSERT(dev && polygon);
    110 
    111   ERR(scpr_polygon_get_components_count(polygon, &ccount));
    112 
    113   for(i = 0; i < ccount; i++) {
    114     ERR(scpr_polygon_get_vertices_count(polygon, i, &vcount));
    115     for(j = 0; j < vcount; j++) {
    116       double pt[2], tmp[2];
    117       int64_t tmp2[2];
    118       ERR(scpr_polygon_get_position(polygon, i, j, pt));
    119       ERR(scpr_device_scale(dev, pt, 2, tmp2));
    120       ERR(scpr_device_unscale(dev, tmp2, 2, tmp));
    121       if(tmp[0] != pt[0] || tmp[1] != pt[1]) {
    122         res = RES_BAD_ARG;
    123         goto error;
    124       }
    125     }
    126   }
    127 end:
    128   return (res == RES_OK);
    129 error:
    130   goto end;
    131 }
    132 
    133 #endif /* TEST_CPR_UTILS_H */