AGX Dynamics  2.35.0.1
ConvexDecomposition.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 */
21 
22 #ifndef CONVEX_DECOMPOSITION_H
23 #define CONVEX_DECOMPOSITION_H
24 
26 #include <agx/debug.h>
27 
29 
30 namespace ConvexDecomposition
31 {
32 
89  class CONVEX_EXPORT ConvexResult
90  {
91  public:
92  ConvexResult(void)
93  {
94  mHullVcount = 0;
95  mHullVertices = 0;
96  mHullTcount = 0;
97  mHullIndices = 0;
98  }
99 
100  ConvexResult(unsigned int hvcount,const double *hvertices,unsigned int htcount,const unsigned int *hindices)
101  {
102 
103  mHullVcount = hvcount;
104  if ( mHullVcount )
105  {
106  agxAssert(hvertices);
107  mHullVertices = new double[mHullVcount*sizeof(double)*3];
108  memcpy(mHullVertices, hvertices, sizeof(double)*3*mHullVcount );
109  }
110  else
111  {
112  mHullVertices = 0;
113  }
114 
115  mHullTcount = htcount;
116 
117  if ( mHullTcount )
118  {
119  agxAssert(hindices);
120  mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
121  memcpy(mHullIndices,hindices, sizeof(unsigned int)*mHullTcount*3 );
122  }
123  else
124  {
125  mHullIndices = 0;
126  }
127 
128  }
129 
130  ConvexResult(const ConvexResult &r) // copy constructor, perform a deep copy of the data.
131  {
132  mHullVcount = r.mHullVcount;
133  if ( mHullVcount )
134  {
135  agxAssert(r.mHullVertices);
136  mHullVertices = new double[mHullVcount*sizeof(double)*3];
137  memcpy(mHullVertices, r.mHullVertices, sizeof(double)*3*mHullVcount );
138  }
139  else
140  {
141  mHullVertices = 0;
142  }
143  mHullTcount = r.mHullTcount;
144  if ( mHullTcount )
145  {
146  agxAssert(r.mHullIndices);
147  mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
148  memcpy(mHullIndices, r.mHullIndices, sizeof(unsigned int)*mHullTcount*3 );
149  }
150  else
151  {
152  mHullIndices = 0;
153  }
154  }
155 
156  ~ConvexResult(void)
157  {
158  delete[] mHullVertices;
159  delete[] mHullIndices;
160  }
161 
162  // the convex hull.
163  unsigned int mHullVcount;
164  double * mHullVertices;
165  unsigned int mHullTcount;
166  unsigned int *mHullIndices;
167 
168  double mHullVolume; // the volume of the convex hull.
169 
170  };
171 
172  // convert from doubles back down to floats.
173  class CONVEX_EXPORT FConvexResult
174  {
175  public:
176  FConvexResult(const ConvexResult &r)
177  {
178  mHullVcount = r.mHullVcount;
179  mHullVertices = 0;
180  if ( mHullVcount )
181  {
182  mHullVertices = new float[mHullVcount*3];
183 
184  const double *src = r.mHullVertices;
185  float * dest = mHullVertices;
186  for (unsigned int i=0; i<mHullVcount; i++)
187  {
188  dest[0] = (float) src[0];
189  dest[1] = (float) src[1];
190  dest[2] = (float) src[2];
191  src+=3;
192  dest+=3;
193  }
194  }
195  mHullTcount = r.mHullTcount;
196  if ( mHullTcount )
197  {
198  mHullIndices = new unsigned int[mHullTcount*3];
199  memcpy(mHullIndices,r.mHullIndices,sizeof(unsigned int)*mHullTcount*3);
200  }
201  else
202  {
203  mHullIndices = 0;
204  }
205  mHullVolume = (float)r.mHullVolume;
206  }
207 
208  ~FConvexResult(void)
209  {
210  delete[] mHullVertices;
211  delete[] mHullIndices;
212  }
213 
214  unsigned int mHullVcount;
215  float * mHullVertices;
216  unsigned int mHullTcount;
217  unsigned int *mHullIndices;
218  float mHullVolume; // the volume of the convex hull.
219  };
220 
221  class CONVEX_EXPORT ConvexDecompInterface
222  {
223  public:
224 
225  virtual void ConvexDebugTri(const double * /*p1*/,const double * /*p2*/,const double * /*p3*/,unsigned int /*color*/) { }
226  virtual void ConvexDebugPoint(const double * /*p*/,double /*dist*/,unsigned int /*color*/) { }
227  virtual void ConvexDebugBound(const double * /*bmin*/,const double * /*bmax*/,unsigned int /*color*/) { }
228  virtual void ConvexDebugOBB(const double * /*sides*/, const double * /*matrix*/,unsigned int /*color*/) { }
229 
230  virtual void ConvexDecompResult(ConvexResult &result) = 0;
231 
232 
233 
234  };
235 
236  // just to avoid passing a zillion parameters to the method the
237  // options are packed into this descriptor.
238  class CONVEX_EXPORT DecompDesc
239  {
240  public:
241  DecompDesc(void)
242  {
243  mVcount = 0;
244  mVertices = 0;
245  mTcount = 0;
246  mIndices = 0;
247  mDepth = 5;
248  mCpercent = 5;
249  mPpercent = 5;
250  mMaxVertices = 32;
251  mSkinWidth = 0;
252  mCallback = 0;
253  }
254 
255  // describes the input triangle.
256  unsigned int mVcount; // the number of vertices in the source mesh.
257  const double *mVertices; // start of the vertex position array. Assumes a stride of 3 doubles.
258  unsigned int mTcount; // the number of triangles in the source mesh.
259  unsigned int *mIndices; // the indexed triangle list array (zero index based)
260 
261  // options
262  unsigned int mDepth; // depth to split, a maximum of 10, generally not over 7.
263  double mCpercent; // the concavity threshold percentage. 0=20 is reasonable.
264  double mPpercent; // the percentage volume conservation threshold to collapse hulls. 0-30 is reasonable.
265 
266  // hull output limits.
267  unsigned int mMaxVertices; // maximum number of vertices in the output hull. Recommended 32 or less.
268  double mSkinWidth; // a skin width to apply to the output hulls.
269 
270  ConvexDecompInterface *mCallback; // the interface to receive back the results.
271 
272  };
273 
274  // perform approximate convex decomposition on a mesh.
275  CONVEX_EXPORT unsigned int performConvexDecomposition(const DecompDesc &desc); // returns the number of hulls produced.
276 
277 }
278 
279 #endif
#define agxAssert(expr)
Definition: debug.h:143