1 #ifndef HULL_H 2 3 #define HULL_H 4 5 /*---------------------------------------------------------------------- 6 Copyright (c) 2004 Open Dynamics Framework Group 7 www.physicstools.org 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without modification, are permitted provided 11 that the following conditions are met: 12 13 Redistributions of source code must retain the above copyright notice, this list of conditions 14 and the following disclaimer. 15 16 Redistributions in binary form must reproduce the above copyright notice, 17 this list of conditions and the following disclaimer in the documentation 18 and/or other materials provided with the distribution. 19 20 Neither the name of the Open Dynamics Framework Group nor the names of its contributors may 21 be used to endorse or promote products derived from this software without specific prior written permission. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 28 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 -----------------------------------------------------------------------*/ 31 32 // Modified by Lasse Oorni for Urho3D 33 34 // Urho3D: use a namespace to not clash with Urho3D's inbuilt math types 35 namespace StanHull 36 { 37 38 class HullResult 39 { 40 public: HullResult(void)41 HullResult(void) 42 { 43 mPolygons = true; 44 mNumOutputVertices = 0; 45 mOutputVertices = 0; 46 mNumFaces = 0; 47 mNumIndices = 0; 48 mIndices = 0; 49 } 50 bool mPolygons; // true if indices represents polygons, false indices are triangles 51 unsigned int mNumOutputVertices; // number of vertices in the output hull 52 float *mOutputVertices; // array of vertices, 3 floats each x,y,z 53 unsigned int mNumFaces; // the number of faces produced 54 unsigned int mNumIndices; // the total number of indices 55 unsigned int *mIndices; // pointer to indices. 56 57 // If triangles, then indices are array indexes into the vertex list. 58 // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc.. 59 }; 60 61 enum HullFlag 62 { 63 QF_TRIANGLES = (1<<0), // report results as triangles, not polygons. 64 QF_REVERSE_ORDER = (1<<1), // reverse order of the triangle indices. 65 QF_SKIN_WIDTH = (1<<2), // extrude hull based on this skin width 66 QF_DEFAULT = 0 67 }; 68 69 70 class HullDesc 71 { 72 public: HullDesc(void)73 HullDesc(void) 74 { 75 mFlags = QF_DEFAULT; 76 mVcount = 0; 77 mVertices = 0; 78 mVertexStride = sizeof(float)*3; 79 mNormalEpsilon = 0.001f; 80 mMaxVertices = 4096; // maximum number of points to be considered for a convex hull. 81 mMaxFaces = 4096; 82 mSkinWidth = 0.01f; // default is one centimeter 83 }; 84 HullDesc(HullFlag flag,unsigned int vcount,const float * vertices,unsigned int stride)85 HullDesc(HullFlag flag, 86 unsigned int vcount, 87 const float *vertices, 88 unsigned int stride) 89 { 90 mFlags = flag; 91 mVcount = vcount; 92 mVertices = vertices; 93 mVertexStride = stride; 94 mNormalEpsilon = 0.001f; 95 mMaxVertices = 4096; 96 mSkinWidth = 0.01f; // default is one centimeter 97 } 98 HasHullFlag(HullFlag flag)99 bool HasHullFlag(HullFlag flag) const 100 { 101 if ( mFlags & flag ) return true; 102 return false; 103 } 104 SetHullFlag(HullFlag flag)105 void SetHullFlag(HullFlag flag) 106 { 107 mFlags|=flag; 108 } 109 ClearHullFlag(HullFlag flag)110 void ClearHullFlag(HullFlag flag) 111 { 112 mFlags&=~flag; 113 } 114 115 unsigned int mFlags; // flags to use when generating the convex hull. 116 unsigned int mVcount; // number of vertices in the input point cloud 117 const float *mVertices; // the array of vertices. 118 unsigned int mVertexStride; // the stride of each vertex, in bytes. 119 float mNormalEpsilon; // the epsilon for removing duplicates. This is a normalized value, if normalized bit is on. 120 float mSkinWidth; 121 unsigned int mMaxVertices; // maximum number of vertices to be considered for the hull! 122 unsigned int mMaxFaces; 123 }; 124 125 enum HullError 126 { 127 QE_OK, // success! 128 QE_FAIL // failed. 129 }; 130 131 class HullLibrary 132 { 133 public: 134 135 HullError CreateConvexHull(const HullDesc &desc, // describes the input request 136 HullResult &result); // contains the resulst 137 138 HullError ReleaseResult(HullResult &result); // release memory allocated for this result, we are done with it. 139 140 private: 141 142 void BringOutYourDead(const float *verts,unsigned int vcount, float *overts,unsigned int &ocount,unsigned int *indices,unsigned indexcount); 143 144 bool CleanupVertices(unsigned int svcount, 145 const float *svertices, 146 unsigned int stride, 147 unsigned int &vcount, // output number of vertices 148 float *vertices, // location to store the results. 149 float normalepsilon, 150 float *scale); 151 }; 152 153 } 154 155 #endif 156 157