cd_hull.h
Go to the documentation of this file.
1 #ifndef HULL_LIB_H
2 
3 #define HULL_LIB_H
4 
5 
62 namespace ConvexDecomposition
63 {
64 
66 {
67 public:
68  HullResult(void)
69  {
70  mPolygons = true;
72  mOutputVertices = 0;
73  mNumFaces = 0;
74  mNumIndices = 0;
75  mIndices = 0;
76  }
77  bool mPolygons; // true if indices represents polygons, false indices are triangles
78  unsigned int mNumOutputVertices; // number of vertices in the output hull
79  double *mOutputVertices; // array of vertices, 3 doubles each x,y,z
80  unsigned int mNumFaces; // the number of faces produced
81  unsigned int mNumIndices; // the total number of indices
82  unsigned int *mIndices; // pointer to indices.
83 
84 // If triangles, then indices are array indexes into the vertex list.
85 // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
86 };
87 
89 {
90 public:
92  {
93  mPolygons = r.mPolygons;
95  mNumFaces = r.mNumFaces;
97  mIndices = 0;
98  mOutputVertices = 0;
99  if ( mNumIndices )
100  {
101  mIndices = new unsigned int[mNumIndices];
102  memcpy(mIndices,r.mIndices,sizeof(unsigned int)*mNumIndices);
103  }
104  if ( mNumOutputVertices )
105  {
106  mOutputVertices = new float[mNumOutputVertices*3];
107  const double *src = r.mOutputVertices;
108  float *dst = mOutputVertices;
109  for (unsigned int i=0; i<mNumOutputVertices; i++)
110  {
111  dst[0] = (float) src[0];
112  dst[1] = (float) src[1];
113  dst[2] = (float) src[2];
114  dst+=3;
115  src+=3;
116  }
117  }
118  }
120  {
121  delete mIndices;
122  delete mOutputVertices;
123  }
124  bool mPolygons; // true if indices represents polygons, false indices are triangles
125  unsigned int mNumOutputVertices; // number of vertices in the output hull
126  float *mOutputVertices; // array of vertices, 3 doubles each x,y,z
127  unsigned int mNumFaces; // the number of faces produced
128  unsigned int mNumIndices; // the total number of indices
129  unsigned int *mIndices; // pointer to indices.
130 
131 // If triangles, then indices are array indexes into the vertex list.
132 // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
133 };
134 
136 {
137  QF_TRIANGLES = (1<<0), // report results as triangles, not polygons.
138  QF_REVERSE_ORDER = (1<<1), // reverse order of the triangle indices.
139  QF_SKIN_WIDTH = (1<<2), // extrude hull based on this skin width
141 };
142 
143 
144 class HullDesc
145 {
146 public:
147  HullDesc(void)
148  {
149  mFlags = QF_DEFAULT;
150  mVcount = 0;
151  mVertices = 0;
152  mVertexStride = 0;
153  mNormalEpsilon = 0.001f;
154  mMaxVertices = 4096; // maximum number of points to be considered for a convex hull.
155  mSkinWidth = 0.01f; // default is one centimeter
156  };
157 
159  unsigned int vcount,
160  const double *vertices,
161  unsigned int stride)
162  {
163  mFlags = flag;
164  mVcount = vcount;
165  mVertices = vertices;
166  mVertexStride = stride;
167  mNormalEpsilon = 0.001f;
168  mMaxVertices = 4096;
169  mSkinWidth = 0.01f; // default is one centimeter
170  }
171 
172  bool HasHullFlag(HullFlag flag) const
173  {
174  if ( mFlags & flag ) return true;
175  return false;
176  }
177 
179  {
180  mFlags|=flag;
181  }
182 
184  {
185  mFlags&=~flag;
186  }
187 
188  unsigned int mFlags; // flags to use when generating the convex hull.
189  unsigned int mVcount; // number of vertices in the input point cloud
190  const double *mVertices; // the array of vertices.
191  unsigned int mVertexStride; // the stride of each vertex, in bytes.
192  double mNormalEpsilon; // the epsilon for removing duplicates. This is a normalized value, if normalized bit is on.
193  double mSkinWidth;
194  unsigned int mMaxVertices; // maximum number of vertices to be considered for the hull!
195 };
196 
198 {
199  QE_OK, // success!
200  QE_FAIL // failed.
201 };
202 
203 // This class is used when converting a convex hull into a triangle mesh.
205 {
206 public:
207  double mPos[3];
208  double mNormal[3];
209  double mTexel[2];
210 };
211 
212 // A virtual interface to receive the triangles from the convex hull.
214 {
215 public:
216  virtual void ConvexHullTriangle(const ConvexHullVertex &v1,const ConvexHullVertex &v2,const ConvexHullVertex &v3) = 0;
217 };
218 
219 
220 
222 {
223 public:
224 
225  HullError CreateConvexHull(const HullDesc &desc, // describes the input request
226  HullResult &result); // contains the resulst
227 
228  HullError ReleaseResult(HullResult &result); // release memory allocated for this result, we are done with it.
229 
230  // Utility function to convert the output convex hull as a renderable set of triangles. Unfolds the polygons into
231  // individual triangles, compute the vertex normals, and projects some texture co-ordinates.
232  HullError CreateTriangleMesh(HullResult &answer,ConvexHullTriangleInterface *iface);
233 private:
234  double ComputeNormal(double *n,const double *A,const double *B,const double *C);
235  void AddConvexTriangle(ConvexHullTriangleInterface *callback,const double *p1,const double *p2,const double *p3);
236 
237  void BringOutYourDead(const double *verts,unsigned int vcount, double *overts,unsigned int &ocount,unsigned int *indices,unsigned indexcount);
238 
239  bool CleanupVertices(unsigned int svcount,
240  const double *svertices,
241  unsigned int stride,
242  unsigned int &vcount, // output number of vertices
243  double *vertices, // location to store the results.
244  double normalepsilon,
245  double *scale);
246 };
247 
248 };
249 
250 #endif
void SetHullFlag(HullFlag flag)
Definition: cd_hull.h:178
unsigned int mVertexStride
Definition: cd_hull.h:191
HullDesc(HullFlag flag, unsigned int vcount, const double *vertices, unsigned int stride)
Definition: cd_hull.h:158
FHullResult(const HullResult &r)
Definition: cd_hull.h:91
const double * mVertices
Definition: cd_hull.h:190
unsigned int mNumOutputVertices
Definition: cd_hull.h:78
bool HasHullFlag(HullFlag flag) const
Definition: cd_hull.h:172
void ClearHullFlag(HullFlag flag)
Definition: cd_hull.h:183


convex_decomposition
Author(s): John W. Ratcliff
autogenerated on Mon Feb 28 2022 22:06:34