/* Copyright (c) 2017-2018 Leo McCormack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* * Filename: * convhull_3d.h * Description: * A header only C implementation of the 3-D quickhull algorithm. * The code is largely derived from the "computational-geometry-toolbox" * by George Papazafeiropoulos (c) 2014, originally distributed under * the BSD (2-clause) license. * To include this implementation in a project, simply add this: * #define CONVHULL_3D_ENABLE * #include "convhull_3d.h" * By default, the algorithm uses double floating point precision. To * use single precision (less accurate but quicker), also add this: * #define CONVHULL_3D_USE_FLOAT_PRECISION * If your project has CBLAS linked, then you can also speed things up * a tad by adding this: * #define CONVHULL_3D_USE_CBLAS * The code is C++ compiler safe. * Reference: "The Quickhull Algorithm for Convex Hull, C. Bradford * Barber, David P. Dobkin and Hannu Huhdanpaa, Geometry * Center Technical Report GCG53, July 30, 1993" * Dependencies: * cblas (optional for speed ups, especially for very large meshes) * Author, date created: * Leo McCormack, 02.10.2017 */ /********** * PUBLIC: *********/ #ifndef CONVHULL_3D_INCLUDED #define CONVHULL_3D_INCLUDED #ifdef __cplusplus extern "C" { #endif #ifdef CONVHULL_3D_USE_FLOAT_PRECISION typedef float CH_FLOAT; #else typedef double CH_FLOAT; #endif typedef struct _ch_vertex { union { CH_FLOAT v[3]; struct{ CH_FLOAT x, y, z; }; }; } ch_vertex; typedef ch_vertex ch_vec3; /* builds the convexhull, returning the face indices corresponding to "in_vertices" */ void convhull_3d_build(/* input arguments */ ch_vertex* const in_vertices, /* vector of input vertices; nVert x 1 */ const int nVert, /* number of vertices */ /* output arguments */ int** out_faces, /* & of empty int*, output face indices; flat: nOut_faces x 3 */ int* nOut_faces); /* & of int, number of output face indices */ #ifdef __cplusplus } /*extern "C"*/ #endif #endif /* CONVHULL_3D_INCLUDED */ /************ * INTERNAL: ***********/ #include #include #include #include #include #ifdef CONVHULL_3D_USE_FLOAT_PRECISION #define CH_FLT_MIN FLT_MIN #define CH_FLT_MAX FLT_MAX #define CH_NOISE_VAL 0.00001f #else #define CH_FLT_MIN DBL_MIN #define CH_FLT_MAX DBL_MAX #define CH_NOISE_VAL 0.0000001 #endif #ifndef MIN #define MIN(a,b) (( (a) < (b) ) ? (a) : (b) ) #endif #ifndef MAX #define MAX(a,b) (( (a) > (b) ) ? (a) : (b) ) #endif #define CH_MAX_NUM_FACES 50000 /* structs for qsort */ typedef struct float_w_idx { CH_FLOAT val; int idx; }float_w_idx; typedef struct int_w_idx { int val; int idx; }int_w_idx; /* internal functions prototypes: */ static int cmp_asc_float(const void*, const void*); static int cmp_desc_float(const void*, const void*); static int cmp_asc_int(const void*, const void*); static int cmp_desc_int(const void*, const void*); static void sort_float(CH_FLOAT*, CH_FLOAT*, int*, int, int); static void sort_int(int*, int*, int*, int, int); static CH_FLOAT det_4x4(CH_FLOAT*); static void plane_3d(CH_FLOAT*, CH_FLOAT*, CH_FLOAT*); static void ismember(int*, int*, int*, int, int); /* internal functions definitions: */ static int cmp_asc_float(const void *a,const void *b) { const struct float_w_idx *a1 = (const struct float_w_idx*)a; const struct float_w_idx *a2 = (const struct float_w_idx*)b; if((*a1).val<(*a2).val)return -1; else if((*a1).val>(*a2).val)return 1; else return 0; } static int cmp_desc_float(const void *a,const void *b) { const struct float_w_idx *a1 = (const struct float_w_idx*)a; const struct float_w_idx *a2 = (const struct float_w_idx*)b; if((*a1).val>(*a2).val)return -1; else if((*a1).val<(*a2).val)return 1; else return 0; } static int cmp_asc_int(const void *a,const void *b) { const struct int_w_idx *a1 = (const struct int_w_idx*)a; const struct int_w_idx *a2 = (const struct int_w_idx*)b; if((*a1).val<(*a2).val)return -1; else if((*a1).val>(*a2).val)return 1; else return 0; } static int cmp_desc_int(const void *a,const void *b) { const struct int_w_idx *a1 = (const struct int_w_idx*)a; const struct int_w_idx *a2 = (const struct int_w_idx*)b; if((*a1).val>(*a2).val)return -1; else if((*a1).val<(*a2).val)return 1; else return 0; } static void sort_float ( CH_FLOAT* in_vec, /* vector[len] to be sorted */ CH_FLOAT* out_vec, /* if NULL, then in_vec is sorted "in-place" */ int* new_idices, /* set to NULL if you don't need them */ int len, /* number of elements in vectors, must be consistent with the input data */ int descendFLAG /* !1:ascending, 1:descending */ ) { int i; struct float_w_idx *data; data = (float_w_idx*)malloc(len*sizeof(float_w_idx)); for(i=0;i=2 dimensions, but "plane_3d" and "det_4x4" are hardcoded for 3, * so would need to be rewritten */ d = 3; span = (CH_FLOAT*)malloc(d*sizeof(CH_FLOAT)); for(j=0; j0) ){ /* i is the first point of the points left */ i = pleft[0]; /* Delete the point selected */ for(j=0; j 0.0){ num_visible_ind++; /* will sum to 0 if none are visible */ visible_ind[j] = 1; } else visible_ind[j] = 0; } num_nonvisible_faces = nFaces - num_visible_ind; /* proceed if there are any visible faces */ if(num_visible_ind!=0){ /* Find visible face indices */ visible = (int*)malloc(num_visible_ind*sizeof(int)); for(j=0, k=0; j CH_MAX_NUM_FACES){ FUCKED = 1; nFaces = 0; break; } } /* Orient each new face properly */ hVec = (int*)malloc( nFaces*sizeof(int)); hVec_mem_face = (int*)malloc( nFaces*sizeof(int)); for(j=0; j