From af720a7f32716859fd57a6beaa81a2c1a04a45e6 Mon Sep 17 00:00:00 2001 From: Garux Date: Thu, 28 Feb 2019 00:00:54 +0300 Subject: [PATCH] remove convhull_3d.h --- libs/convhull_3d.h | 778 --------------------------------------------- radiant/csg.cpp | 60 +--- 2 files changed, 17 insertions(+), 821 deletions(-) delete mode 100644 libs/convhull_3d.h diff --git a/libs/convhull_3d.h b/libs/convhull_3d.h deleted file mode 100644 index 8951807a..00000000 --- a/libs/convhull_3d.h +++ /dev/null @@ -1,778 +0,0 @@ -/* - 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 quickhull; - std::vector> pointCloud; - pointCloud.reserve( mergeVertices.size() ); - for( std::size_t i = 0; i < mergeVertices.size(); ++i ){ - pointCloud.push_back( quickhull::Vector3( static_cast( mergeVertices[i].x() ), - static_cast( mergeVertices[i].y() ), - static_cast( mergeVertices[i].z() ) ) ); - } - auto hull = quickhull.getConvexHull( pointCloud, false, true, 0.0001 ); - const auto& indexBuffer = hull.getIndexBuffer(); - const size_t triangleCount = indexBuffer.size() / 3; - for( size_t i = 0; i < triangleCount; ++i ) { - Vector3 p[3]; - for( size_t j = 0; j < 3; ++j ){ - p[j] = mergeVertices[indexBuffer[i * 3 + j]]; - } - const Plane3 plane = plane3_for_points( p[0], p[1], p[2] ); - if( plane3_valid( plane ) ){ - mergePlanes.insert( MergePlane( plane, p[0], p[2], p[1] ) ); - } - } -#else - if( mergeVertices.size() < 130 ){ // use reliable path, when possible, as convhull_3d.h is not too much. +#if 0 + if( mergeVertices.size() < 130 ){ // use reliable bruteforce path, when possible /* bruteforce new planes */ for( MergeVertices::const_iterator i = mergeVertices.begin() + 0; i != mergeVertices.end() - 2; ++i ) for( MergeVertices::const_iterator j = i + 1; j != mergeVertices.end() - 1; ++j ) @@ -1127,34 +1104,31 @@ void CSG_build_hull( const MergeVertices& mergeVertices, MergePlanes& mergePlane } } } - else{ - const int nVertices = mergeVertices.size(); - ch_vertex* vertices = ( ch_vertex* )malloc( mergeVertices.size() * sizeof( ch_vertex ) ); + else +#endif + { + quickhull::QuickHull quickhull; + std::vector> pointCloud; + pointCloud.reserve( mergeVertices.size() ); for( std::size_t i = 0; i < mergeVertices.size(); ++i ){ - vertices[i].x = static_cast( mergeVertices[i].x() ); - vertices[i].y = static_cast( mergeVertices[i].y() ); - vertices[i].z = static_cast( mergeVertices[i].z() ); + pointCloud.push_back( quickhull::Vector3( static_cast( mergeVertices[i].x() ), + static_cast( mergeVertices[i].y() ), + static_cast( mergeVertices[i].z() ) ) ); } - int* faceIndices = NULL; - int nFaces; - convhull_3d_build( vertices, nVertices, &faceIndices, &nFaces ); - /* Where 'faceIndices' is a flat 2D matrix [nFaces x 3] */ - for( int i = 0; i < nFaces; ++i ){ + auto hull = quickhull.getConvexHull( pointCloud, false, true, 0.0001 ); + const auto& indexBuffer = hull.getIndexBuffer(); + const size_t triangleCount = indexBuffer.size() / 3; + for( size_t i = 0; i < triangleCount; ++i ) { Vector3 p[3]; - for( int j = 0; j < 3; ++j ){ -// p[j] = Vector3( vertices[faceIndices[i * 3 + j]].x, vertices[faceIndices[i * 3 + j]].y, vertices[faceIndices[i * 3 + j]].z ); - p[j] = mergeVertices[faceIndices[i * 3 + j]]; + for( size_t j = 0; j < 3; ++j ){ + p[j] = mergeVertices[indexBuffer[i * 3 + j]]; } const Plane3 plane = plane3_for_points( p[0], p[1], p[2] ); if( plane3_valid( plane ) ){ mergePlanes.insert( MergePlane( plane, p[0], p[2], p[1] ) ); } } - - free( vertices ); - free( faceIndices ); } -#endif } void CSG_WrapMerge( const ClipperPoints& clipperPoints ){