AGX Dynamics  2.35.0.1
cd_hull.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 /*
18 This source code has been taken and modified by Algoryx Simulation AB
19 from the source and under the license given below.
20 */
22 
23 #ifndef HULL_LIB_H
24 
25 #define HULL_LIB_H
26 
27 
84 namespace ConvexDecomposition
85 {
86 
87  class HullResult
88  {
89  public:
90  HullResult(void)
91  {
92  mPolygons = true;
93  mNumOutputVertices = 0;
94  mOutputVertices = 0;
95  mNumFaces = 0;
96  mNumIndices = 0;
97  mIndices = 0;
98  }
99  bool mPolygons; // true if indices represents polygons, false indices are triangles
100  unsigned int mNumOutputVertices; // number of vertices in the output hull
101  double *mOutputVertices; // array of vertices, 3 doubles each x,y,z
102  unsigned int mNumFaces; // the number of faces produced
103  unsigned int mNumIndices; // the total number of indices
104  unsigned int *mIndices; // pointer to indices.
105 
106  // If triangles, then indices are array indexes into the vertex list.
107  // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
108  };
109 
110  class FHullResult
111  {
112  public:
113  FHullResult(const HullResult &r)
114  {
115  mPolygons = r.mPolygons;
116  mNumOutputVertices = r.mNumOutputVertices;
117  mNumFaces = r.mNumFaces;
118  mNumIndices = r.mNumIndices;
119  mIndices = 0;
120  mOutputVertices = 0;
121  if ( mNumIndices )
122  {
123  mIndices = new unsigned int[mNumIndices];
124  memcpy(mIndices,r.mIndices,sizeof(unsigned int)*mNumIndices);
125  }
126  if ( mNumOutputVertices )
127  {
128  mOutputVertices = new float[mNumOutputVertices*3];
129  const double *src = r.mOutputVertices;
130  float *dst = mOutputVertices;
131  for (unsigned int i=0; i<mNumOutputVertices; i++)
132  {
133  dst[0] = (float) src[0];
134  dst[1] = (float) src[1];
135  dst[2] = (float) src[2];
136  dst+=3;
137  src+=3;
138  }
139  }
140  }
141  ~FHullResult(void)
142  {
143  delete mIndices;
144  delete mOutputVertices;
145  }
146  bool mPolygons; // true if indices represents polygons, false indices are triangles
147  unsigned int mNumOutputVertices; // number of vertices in the output hull
148  float *mOutputVertices; // array of vertices, 3 doubles each x,y,z
149  unsigned int mNumFaces; // the number of faces produced
150  unsigned int mNumIndices; // the total number of indices
151  unsigned int *mIndices; // pointer to indices.
152 
153  // If triangles, then indices are array indexes into the vertex list.
154  // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
155  };
156 
157  enum HullFlag
158  {
159  QF_TRIANGLES = (1<<0), // report results as triangles, not polygons.
160  QF_REVERSE_ORDER = (1<<1), // reverse order of the triangle indices.
161  QF_SKIN_WIDTH = (1<<2), // extrude hull based on this skin width
162  QF_DEFAULT = 0
163  };
164 
165 
166  class HullDesc
167  {
168  public:
169  HullDesc(void)
170  {
171  mFlags = QF_DEFAULT;
172  mVcount = 0;
173  mVertices = 0;
174  mVertexStride = 0;
175  mNormalEpsilon = 0.001f;
176  mMaxVertices = 4096; // maximum number of points to be considered for a convex hull.
177  mSkinWidth = 0.01f; // default is one centimeter
178  }
179 
180  HullDesc(HullFlag flag,
181  unsigned int vcount,
182  const double *vertices,
183  unsigned int stride)
184  {
185  mFlags = flag;
186  mVcount = vcount;
187  mVertices = vertices;
188  mVertexStride = stride;
189  mNormalEpsilon = 0.001f;
190  mMaxVertices = 4096;
191  mSkinWidth = 0.01f; // default is one centimeter
192  }
193 
194  bool HasHullFlag(HullFlag flag) const
195  {
196  if ( mFlags & flag ) return true;
197  return false;
198  }
199 
200  void SetHullFlag(HullFlag flag)
201  {
202  mFlags|=flag;
203  }
204 
205  void ClearHullFlag(HullFlag flag)
206  {
207  mFlags&=~flag;
208  }
209 
210  unsigned int mFlags; // flags to use when generating the convex hull.
211  unsigned int mVcount; // number of vertices in the input point cloud
212  const double *mVertices; // the array of vertices.
213  unsigned int mVertexStride; // the stride of each vertex, in bytes.
214  double mNormalEpsilon; // the epsilon for removing duplicates. This is a normalized value, if normalized bit is on.
215  double mSkinWidth;
216  unsigned int mMaxVertices; // maximum number of vertices to be considered for the hull!
217  };
218 
219  enum HullError
220  {
221  QE_OK, // success!
222  QE_FAIL // failed.
223  };
224 
225  // This class is used when converting a convex hull into a triangle mesh.
226  class ConvexHullVertex
227  {
228  public:
229  double mPos[3];
230  double mNormal[3];
231  double mTexel[2];
232  };
233 
234  // A virtual interface to receive the triangles from the convex hull.
235  class ConvexHullTriangleInterface
236  {
237  public:
238  virtual void ConvexHullTriangle(const ConvexHullVertex &v1,const ConvexHullVertex &v2,const ConvexHullVertex &v3) = 0;
239  };
240 
241 
242 
243  class HullLibrary
244  {
245  public:
246 
247  HullError CreateConvexHull(const HullDesc &desc, // describes the input request
248  HullResult &result); // contains the results
249 
250  HullError ReleaseResult(HullResult &result); // release memory allocated for this result, we are done with it.
251 
252  // Utility function to convert the output convex hull as a renderable set of triangles. Unfolds the polygons into
253  // individual triangles, compute the vertex normals, and projects some texture co-ordinates.
254  HullError CreateTriangleMesh(HullResult &answer,ConvexHullTriangleInterface *iface);
255  private:
256  double ComputeNormal(double *n,const double *A,const double *B,const double *C);
257  void AddConvexTriangle(ConvexHullTriangleInterface *callback,const double *p1,const double *p2,const double *p3);
258 
259  void BringOutYourDead(const double *verts,unsigned int vcount, double *overts,unsigned int &ocount,unsigned int *indices,unsigned indexcount);
260 
261  bool CleanupVertices(unsigned int svcount,
262  const double *svertices,
263  unsigned int stride,
264  unsigned int &vcount, // output number of vertices
265  double *vertices, // location to store the results.
266  double normalepsilon,
267  double *scale);
268  };
269 
270 }
271 
272 #endif