DecomposeSample.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <float.h>
6 
7 #include <vector>
8 
11 
12 using namespace ConvexDecomposition;
13 
14 // Test application to demonstrate how to use the ConvexDecomposition system.
15 
16 typedef std::vector< ConvexResult * > ConvexResultVector;
17 
18 static const char * fstring(float v)
19 {
20  static char data[64*16];
21  static int index=0;
22 
23  char *ret = &data[index*64];
24  index++;
25  if (index == 16 ) index = 0;
26 
27  if ( v == FLT_MIN ) return "-INF"; // collada notation for FLT_MIN and FLT_MAX
28  if ( v == FLT_MAX ) return "INF";
29 
30  if ( v == 1 )
31  {
32  strcpy(ret,"1");
33  }
34  else if ( v == 0 )
35  {
36  strcpy(ret,"0");
37  }
38  else if ( v == - 1 )
39  {
40  strcpy(ret,"-1");
41  }
42  else
43  {
44  sprintf(ret,"%.9f", v );
45  const char *dot = strstr(ret,".");
46  if ( dot )
47  {
48  int len = (int)strlen(ret);
49  char *foo = &ret[len-1];
50  while ( *foo == '0' ) foo--;
51  if ( *foo == '.' )
52  *foo = 0;
53  else
54  foo[1] = 0;
55  }
56  }
57 
58  return ret;
59 }
60 
62 {
63 public:
64 
65  CBuilder(const char *fname,const DecompDesc &d)
66  {
67 
68  strcpy(mBaseName,fname);
69  char *dot = strstr(mBaseName,".obj");
70  if ( dot ) *dot = 0;
71 
72  sprintf(mObjName,"%s_convex.obj", mBaseName );
73 
74  mBaseCount = 0;
75  mHullCount = 0;
76  mSkinWidth = (float)d.mSkinWidth;
77 
78  mFph = fopen(mObjName,"wb");
79 
80 
81  printf("########################################################################\r\n");
82  printf("# Perfomring approximate convex decomposition for triangle mesh %s\r\n", fname );
83  printf("# Input Mesh has %d vertices and %d triangles.\r\n", d.mVcount, d.mTcount );
84  printf("# Recursion depth: %d\r\n", d.mDepth );
85  printf("# Concavity Threshold Percentage: %0.2f\r\n", d.mCpercent );
86  printf("# Hull Merge Volume Percentage: %0.2f\r\n", d.mPpercent );
87  printf("# Maximum output vertices per hull: %d\r\n", d.mMaxVertices );
88  printf("# Hull Skin Width: %0.2f\r\n", d.mSkinWidth );
89  printf("########################################################################\r\n");
90 
91 
92  if ( mFph )
93  {
94  fprintf(mFph,"########################################################################\r\n");
95  fprintf(mFph,"# Approximate convex decomposition for triangle mesh %s\r\n", fname );
96  fprintf(mFph,"# Input Mesh has %d vertices and %d triangles.\r\n", d.mVcount, d.mTcount );
97  fprintf(mFph,"# Recursion depth: %d\r\n", d.mDepth );
98  fprintf(mFph,"# Concavity Threshold Percentage: %0.2f\r\n", d.mCpercent );
99  fprintf(mFph,"# Hull Merge Volume Percentage: %0.2f\r\n", d.mPpercent );
100  fprintf(mFph,"# Maximum output vertices per hull: %d\r\n", d.mMaxVertices );
101  fprintf(mFph,"# Hull Skin Width: %0.2f\r\n", d.mSkinWidth );
102  fprintf(mFph,"########################################################################\r\n");
103 
104  printf("Opened Wavefront file %s for output.\r\n", mObjName );
105  }
106  else
107  {
108  printf("Failed to open output file %s\r\n", mObjName );
109  }
110  }
111 
112  ~CBuilder(void)
113  {
114  if ( mFph )
115  {
116  fclose(mFph);
117  }
118  for (unsigned int i=0; i<mHulls.size(); i++)
119  {
120  ConvexResult *cr = mHulls[i];
121  delete cr;
122  }
123  }
124 
125  virtual void ConvexDecompResult(ConvexResult &result)
126  {
127  // we have a hull...
128  mHullCount++;
129 
130  printf("Received hull %d HullVolume(%0.3f)\r\n", mHullCount, result.mHullVolume );
131 
132  if ( mFph )
133  {
134 
135  ConvexResult *cr = new ConvexResult(result);
136  mHulls.push_back(cr);
137 
138  fprintf(mFph,"########################################################################\r\n");
139  fprintf(mFph,"## Hull Piece %d with %d vertices and %d triangles.\r\n", mHullCount, result.mHullVcount, result.mHullTcount );
140  fprintf(mFph,"########################################################################\r\n");
141 
142  for (unsigned int i=0; i<result.mHullVcount; i++)
143  {
144  const double *p = &result.mHullVertices[i*3];
145  fprintf(mFph,"v %0.9f %0.9f %0.9f\r\n", (float)p[0], (float)p[1], (float)p[2] );
146  }
147 
148  if ( 1 )
149  {
150  const unsigned int *src = result.mHullIndices;
151  for (unsigned int i=0; i<result.mHullTcount; i++)
152  {
153  unsigned int i1 = *src++;
154  unsigned int i2 = *src++;
155  unsigned int i3 = *src++;
156 
157  i1+=mBaseCount;
158  i2+=mBaseCount;
159  i3+=mBaseCount;
160 
161  fprintf(mFph,"f %d %d %d\r\n", i1+1, i2+1, i3+1 );
162  }
163  }
164  mBaseCount+=result.mHullVcount; // advance the 'base index' counter.
165  }
166 
167  }
168 
169  void saveCOLLADA(FILE *fph,unsigned int index,ConvexResult *cr)
170  {
171 
172  fprintf(fph," <geometry id=\"%s_%d-Mesh\" name=\"%s_%d-Mesh\">\r\n", mBaseName, index, mBaseName, index);
173  fprintf(fph," <convex_mesh>\r\n");
174  fprintf(fph," <source id=\"%s_%d-Position\">\r\n", mBaseName, index);
175  fprintf(fph," <float_array count=\"%d\" id=\"%s_%d-Position-array\">\r\n", cr->mHullVcount*3, mBaseName, index);
176  fprintf(fph," ");
177  for (unsigned int i=0; i<cr->mHullVcount; i++)
178  {
179  const double *p = &cr->mHullVertices[i*3];
180  fprintf(fph,"%s %s %s ", fstring((float)p[0]), fstring((float)p[1]), fstring((float)p[2]) );
181  if ( ((i+1)&3) == 0 && (i+1) < cr->mHullVcount )
182  {
183  fprintf(fph,"\r\n");
184  fprintf(fph," ");
185  }
186  }
187  fprintf(fph,"\r\n");
188  fprintf(fph," </float_array>\r\n");
189  fprintf(fph," <technique_common>\r\n");
190  fprintf(fph," <accessor count=\"%d\" source=\"#%s_%d-Position-array\" stride=\"3\">\r\n", cr->mHullVcount, mBaseName, index );
191  fprintf(fph," <param name=\"X\" type=\"float\"/>\r\n");
192  fprintf(fph," <param name=\"Y\" type=\"float\"/>\r\n");
193  fprintf(fph," <param name=\"Z\" type=\"float\"/>\r\n");
194  fprintf(fph," </accessor>\r\n");
195  fprintf(fph," </technique_common>\r\n");
196  fprintf(fph," </source>\r\n");
197  fprintf(fph," <vertices id=\"%s_%d-Vertex\">\r\n", mBaseName, index);
198  fprintf(fph," <input semantic=\"POSITION\" source=\"#%s_%d-Position\"/>\r\n", mBaseName, index);
199  fprintf(fph," </vertices>\r\n");
200  fprintf(fph," <triangles material=\"Material\" count=\"%d\">\r\n", cr->mHullTcount );
201  fprintf(fph," <input offset=\"0\" semantic=\"VERTEX\" source=\"#%s_%d-Vertex\"/>\r\n", mBaseName, index);
202  fprintf(fph," <p>\r\n");
203  fprintf(fph," ");
204  for (unsigned int i=0; i<cr->mHullTcount; i++)
205  {
206  unsigned int *tri = &cr->mHullIndices[i*3];
207  fprintf(fph,"%d %d %d ", tri[0], tri[1], tri[2] );
208  if ( ((i+1)&3) == 0 && (i+1) < cr->mHullTcount )
209  {
210  fprintf(fph,"\r\n");
211  fprintf(fph," ");
212  }
213  }
214  fprintf(fph,"\r\n");
215  fprintf(fph," </p>\r\n");
216  fprintf(fph," </triangles>\r\n");
217  fprintf(fph," </convex_mesh>\r\n");
218  fprintf(fph," </geometry>\r\n");
219 
220  }
221 
222  void saveCOLLADA(void)
223  {
224  char scratch[512];
225  sprintf(scratch,"%s.dae", mBaseName );
226  FILE *fph = fopen(scratch,"wb");
227 
228  if ( fph )
229  {
230  printf("Saving convex decomposition of %d hulls to COLLADA file '%s'\r\n", (int)mHulls.size(), scratch );
231 
232  fprintf(fph,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
233  fprintf(fph,"<COLLADA version=\"1.4.0\" xmlns=\"http://www.collada.org/2005/11/COLLADASchema\">\r\n");
234  fprintf(fph," <asset>\r\n");
235  fprintf(fph," <contributor>\r\n");
236  fprintf(fph," <author>NxuStream2 converter - http://www.ageia.com</author>\r\n");
237  fprintf(fph," <authoring_tool>PhysX Rocket, PhysX Viewer, or CreateDynamics</authoring_tool>\r\n");
238  fprintf(fph," <comments>questions to: jratcliff@ageia.com</comments>\r\n");
239  fprintf(fph," <copyright></copyright>\r\n");
240  fprintf(fph," <source_data>chair_gothic_tilted</source_data>\r\n");
241  fprintf(fph," </contributor>\r\n");
242  fprintf(fph," <unit meter=\"1\" name=\"meter\"/>\r\n");
243  fprintf(fph," <up_axis>Y_UP</up_axis>\r\n");
244  fprintf(fph," </asset>\r\n");
245  fprintf(fph," <library_materials>\r\n");
246  fprintf(fph," <material id=\"Material\" name=\"Material\">\r\n");
247  fprintf(fph," <instance_effect url=\"#Material-fx\"></instance_effect>\r\n");
248  fprintf(fph," </material>\r\n");
249  fprintf(fph," </library_materials>\r\n");
250  fprintf(fph," <library_effects>\r\n");
251  fprintf(fph," <effect id=\"Material-fx\" name=\"Material\">\r\n");
252  fprintf(fph," <profile_COMMON>\r\n");
253  fprintf(fph," <technique id=\"Material-fx-COMMON\" sid=\"COMMON\">\r\n");
254  fprintf(fph," <phong>\r\n");
255  fprintf(fph," <ambient>\r\n");
256  fprintf(fph," <color>0.803922 0.588235 0.92549 1</color>\r\n");
257  fprintf(fph," </ambient>\r\n");
258  fprintf(fph," <diffuse>\r\n");
259  fprintf(fph," <color>0.803922 0.588235 0.92549 1</color>\r\n");
260  fprintf(fph," </diffuse>\r\n");
261  fprintf(fph," <specular>\r\n");
262  fprintf(fph," <color>0.631373 0.631373 0.631373 1</color>\r\n");
263  fprintf(fph," </specular>\r\n");
264  fprintf(fph," <shininess>\r\n");
265  fprintf(fph," <float>1</float>\r\n");
266  fprintf(fph," </shininess>\r\n");
267  fprintf(fph," <reflective>\r\n");
268  fprintf(fph," <color>0 0 0 1</color>\r\n");
269  fprintf(fph," </reflective>\r\n");
270  fprintf(fph," <transparent>\r\n");
271  fprintf(fph," <color>1 1 1 1</color>\r\n");
272  fprintf(fph," </transparent>\r\n");
273  fprintf(fph," <transparency>\r\n");
274  fprintf(fph," <float>0</float>\r\n");
275  fprintf(fph," </transparency>\r\n");
276  fprintf(fph," </phong>\r\n");
277  fprintf(fph," </technique>\r\n");
278  fprintf(fph," </profile_COMMON>\r\n");
279  fprintf(fph," </effect>\r\n");
280  fprintf(fph," </library_effects>\r\n");
281  fprintf(fph," <library_geometries>\r\n");
282 
283  for (unsigned int i=0; i<mHulls.size(); i++)
284  {
285  ConvexResult *cr = mHulls[i];
286  saveCOLLADA(fph,i,cr);
287  }
288 
289 
290  fprintf(fph," </library_geometries>\r\n");
291  fprintf(fph," <library_visual_scenes>\r\n");
292  fprintf(fph," <visual_scene id=\"Scene0-Visual\" name=\"Scene0-Visual\">\r\n");
293  fprintf(fph," <node id=\"%s-Node\" name=\"%s\" type=\"NODE\">\r\n", mBaseName, mBaseName );
294  fprintf(fph," <translate>0 0 0</translate>\r\n");
295  fprintf(fph," <rotate>1 0 0 0</rotate>\r\n");
296  fprintf(fph," </node>\r\n");
297  fprintf(fph," </visual_scene>\r\n");
298  fprintf(fph," </library_visual_scenes>\r\n");
299  fprintf(fph," <library_physics_materials>\r\n");
300  fprintf(fph," <physics_material id=\"pmat0_0-PhysicsMaterial\" name=\"pmat0_0-PhysicsMaterial\">\r\n");
301  fprintf(fph," <technique_common>\r\n");
302  fprintf(fph," <dynamic_friction>0.5</dynamic_friction>\r\n");
303  fprintf(fph," <restitution>0</restitution>\r\n");
304  fprintf(fph," <static_friction>0.5</static_friction>\r\n");
305  fprintf(fph," </technique_common>\r\n");
306  fprintf(fph," </physics_material>\r\n");
307  fprintf(fph," </library_physics_materials>\r\n");
308  fprintf(fph," <library_physics_models>\r\n");
309  fprintf(fph," <physics_model id=\"Scene0-PhysicsModel\">\r\n");
310  fprintf(fph," <rigid_body sid=\"%s-RigidBody\" name=\"%s\">\r\n", mBaseName, mBaseName);
311  fprintf(fph," <technique_common>\r\n");
312  fprintf(fph," <instance_physics_material url=\"#pmat0_0-PhysicsMaterial\"/>\r\n");
313 
314  for (unsigned int i=0; i<mHulls.size(); i++)
315  {
316  ConvexResult *ch = mHulls[i];
317  fprintf(fph," <shape>\r\n");
318  fprintf(fph," <translate>0 0 0</translate>\r\n");
319  fprintf(fph," <rotate>1 0 0 0</rotate>\r\n");
320  fprintf(fph," <instance_physics_material url=\"#pmat0_0-PhysicsMaterial\"/>\r\n");
321  fprintf(fph," <density>1</density>\r\n");
322  fprintf(fph," <extra>\r\n");
323  fprintf(fph," <technique profile=\"PhysX\">\r\n");
324  fprintf(fph," <skinWidth>%s</skinWidth>\r\n", fstring( mSkinWidth ) );
325  fprintf(fph," </technique>\r\n");
326  fprintf(fph," </extra>\r\n");
327  fprintf(fph," <instance_geometry url=\"%s_%d-Mesh\"/>\r\n", mBaseName, i);
328  fprintf(fph," </shape>\r\n");
329  }
330 
331  fprintf(fph," <dynamic>true</dynamic>\r\n");
332  fprintf(fph," <mass>1</mass>\r\n");
333  fprintf(fph," </technique_common>\r\n");
334  fprintf(fph," <extra>\r\n");
335  fprintf(fph," <technique profile=\"PhysX\">\r\n");
336  fprintf(fph," <extra>\r\n");
337  fprintf(fph," <technique profile=\"PhysX\">\r\n");
338  fprintf(fph," <linearDamping>0</linearDamping>\r\n");
339  fprintf(fph," <angularDamping>0</angularDamping>\r\n");
340  fprintf(fph," <solverIterationCount>32</solverIterationCount>\r\n");
341  fprintf(fph," </technique>\r\n");
342  fprintf(fph," </extra>\r\n");
343  fprintf(fph," <disable_collision>false</disable_collision>\r\n");
344  fprintf(fph," </technique>\r\n");
345  fprintf(fph," </extra>\r\n");
346  fprintf(fph," </rigid_body>\r\n");
347  fprintf(fph," </physics_model>\r\n");
348  fprintf(fph," </library_physics_models>\r\n");
349  fprintf(fph," <library_physics_scenes>\r\n");
350  fprintf(fph," <physics_scene id=\"Scene0-Physics\">\r\n");
351  fprintf(fph," <instance_physics_model url=\"#Scene0-PhysicsModel\">\r\n");
352  fprintf(fph," <instance_rigid_body target=\"#%s-Node\" body=\"%s-RigidBody\"/>\r\n", mBaseName, mBaseName );
353  fprintf(fph," <extra>\r\n");
354  fprintf(fph," <technique profile=\"PhysX\">\r\n");
355  fprintf(fph," </technique>\r\n");
356  fprintf(fph," </extra>\r\n");
357  fprintf(fph," </instance_physics_model>\r\n");
358  fprintf(fph," <technique_common>\r\n");
359  fprintf(fph," <gravity>0 -9.800000191 0</gravity>\r\n");
360  fprintf(fph," </technique_common>\r\n");
361  fprintf(fph," </physics_scene>\r\n");
362  fprintf(fph," </library_physics_scenes>\r\n");
363  fprintf(fph," <scene>\r\n");
364  fprintf(fph," <instance_visual_scene url=\"#Scene0-Visual\"/>\r\n");
365  fprintf(fph," <instance_physics_scene url=\"#Scene0-Physics\"/>\r\n");
366  fprintf(fph," </scene>\r\n");
367  fprintf(fph,"</COLLADA>\r\n");
368 
369 
370  fclose(fph);
371  }
372  else
373  {
374  printf("Failed to open file '%s' for write access.\r\n", scratch );
375  }
376  }
377 
378  void saveXML(FILE *fph,unsigned int index,ConvexResult *cr)
379  {
380 
381  fprintf(fph," <NxConvexMeshDesc id=\"%s_%d\">\r\n", mBaseName, index);
382  fprintf(fph," <points>\r\n");
383  fprintf(fph," ");
384  for (unsigned int i=0; i<cr->mHullVcount; i++)
385  {
386  const double *p = &cr->mHullVertices[i*3];
387  fprintf(fph,"%s %s %s ", fstring((float)p[0]), fstring((float)p[1]), fstring((float)p[2]) );
388  if ( ((i+1)&3) == 0 && (i+1) < cr->mHullVcount )
389  {
390  fprintf(fph,"\r\n");
391  fprintf(fph," ");
392  }
393  }
394  fprintf(fph,"\r\n");
395  fprintf(fph," </points>\r\n");
396  fprintf(fph," <triangles>\r\n");
397  fprintf(fph," ");
398  for (unsigned int i=0; i<cr->mHullTcount; i++)
399  {
400  unsigned int *tri = &cr->mHullIndices[i*3];
401  fprintf(fph,"%d %d %d ", tri[0], tri[1], tri[2] );
402  if ( ((i+1)&3) == 0 && (i+1) < cr->mHullTcount )
403  {
404  fprintf(fph,"\r\n");
405  fprintf(fph," ");
406  }
407  }
408  fprintf(fph,"\r\n");
409  fprintf(fph," </triangles>\r\n");
410  fprintf(fph," </NxConvexMeshDesc>\r\n");
411 
412  }
413 
414  void saveNxuStream(void)
415  {
416  char scratch[512];
417  sprintf(scratch,"%s.xml", mBaseName );
418  FILE *fph = fopen(scratch,"wb");
419 
420  if ( fph )
421  {
422  printf("Saving convex decomposition of %d hulls to NxuStream file '%s'\r\n", (int)mHulls.size(), scratch );
423 
424  fprintf(fph,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
425  fprintf(fph," <NXUSTREAM2>\r\n");
426  fprintf(fph," <NxuPhysicsCollection id=\"beshon\" sdkVersion=\"244\" nxuVersion=\"100\">\r\n");
427  fprintf(fph," <NxPhysicsSDKDesc id=\"SDK\">\r\n");
428  fprintf(fph," <hwPageSize>65536</hwPageSize>\r\n");
429  fprintf(fph," <hwPageMax>256</hwPageMax>\r\n");
430  fprintf(fph," <hwConvexMax>2048</hwConvexMax>\r\n");
431  fprintf(fph," </NxPhysicsSDKDesc>\r\n");
432 
433  for (unsigned int i=0; i<mHulls.size(); i++)
434  {
435  saveXML(fph, i, mHulls[i] );
436  }
437 
438 
439  fprintf(fph," <NxSceneDesc id=\"%s\">\r\n", mBaseName);
440  fprintf(fph," <filterBool>false</filterBool>\r\n");
441  fprintf(fph," <filterOp0>NX_FILTEROP_AND</filterOp0>\r\n");
442  fprintf(fph," <filterOp1>NX_FILTEROP_AND</filterOp1>\r\n");
443  fprintf(fph," <filterOp2>NX_FILTEROP_AND</filterOp2>\r\n");
444  fprintf(fph," <NxGroupsMask id=\"groupMask0\" bits0=\"0\" bits1=\"0\" bits2=\"0\" bits3=\"0\"/>\r\n");
445  fprintf(fph," <NxGroupsMask id=\"groupMask1\" bits0=\"0\" bits1=\"0\" bits2=\"0\" bits3=\"0\"/>\r\n");
446  fprintf(fph," <gravity>0 -9.800000191 0</gravity>\r\n");
447  fprintf(fph," <maxTimestep>0.016666668</maxTimestep>\r\n");
448  fprintf(fph," <maxIter>8</maxIter>\r\n");
449  fprintf(fph," <timeStepMethod>NX_TIMESTEP_FIXED</timeStepMethod>\r\n");
450  fprintf(fph," <maxBounds>FLT_MIN FLT_MIN FLT_MIN FLT_MAX FLT_MAX FLT_MAX</maxBounds>\r\n");
451  fprintf(fph," <NxSceneLimits id=\"limits\">\r\n");
452  fprintf(fph," <maxNbActors>0</maxNbActors>\r\n");
453  fprintf(fph," <maxNbBodies>0</maxNbBodies>\r\n");
454  fprintf(fph," <maxNbStaticShapes>0</maxNbStaticShapes>\r\n");
455  fprintf(fph," <maxNbDynamicShapes>0</maxNbDynamicShapes>\r\n");
456  fprintf(fph," <maxNbJoints>0</maxNbJoints>\r\n");
457  fprintf(fph," </NxSceneLimits>\r\n");
458  fprintf(fph," <simType>NX_SIMULATION_SW</simType>\r\n");
459  fprintf(fph," <groundPlane>true</groundPlane>\r\n");
460  fprintf(fph," <boundsPlanes>false</boundsPlanes>\r\n");
461  fprintf(fph," <NxSceneFlags id=\"flags\">\r\n");
462  fprintf(fph," <NX_SF_DISABLE_SSE>false</NX_SF_DISABLE_SSE>\r\n");
463  fprintf(fph," <NX_SF_DISABLE_COLLISIONS>false</NX_SF_DISABLE_COLLISIONS>\r\n");
464  fprintf(fph," <NX_SF_SIMULATE_SEPARATE_THREAD>true</NX_SF_SIMULATE_SEPARATE_THREAD>\r\n");
465  fprintf(fph," <NX_SF_ENABLE_MULTITHREAD>false</NX_SF_ENABLE_MULTITHREAD>\r\n");
466  fprintf(fph," </NxSceneFlags>\r\n");
467  fprintf(fph," <internalThreadCount>0</internalThreadCount>\r\n");
468  fprintf(fph," <backgroundThreadCount>0</backgroundThreadCount>\r\n");
469  fprintf(fph," <threadMask>1431655764</threadMask>\r\n");
470  fprintf(fph," <backgroundThreadMask>1431655764</backgroundThreadMask>\r\n");
471  fprintf(fph," <NxMaterialDesc id=\"Material_0\" userProperties=\"\" hasSpring=\"false\" materialIndex=\"0\">\r\n");
472  fprintf(fph," <dynamicFriction>0.5</dynamicFriction>\r\n");
473  fprintf(fph," <staticFriction>0.5</staticFriction>\r\n");
474  fprintf(fph," <restitution>0</restitution>\r\n");
475  fprintf(fph," <dynamicFrictionV>0</dynamicFrictionV>\r\n");
476  fprintf(fph," <staticFrictionV>0</staticFrictionV>\r\n");
477  fprintf(fph," <frictionCombineMode>NX_CM_AVERAGE</frictionCombineMode>\r\n");
478  fprintf(fph," <restitutionCombineMode>NX_CM_AVERAGE</restitutionCombineMode>\r\n");
479  fprintf(fph," <dirOfAnisotropy>1 0 0</dirOfAnisotropy>\r\n");
480  fprintf(fph," <NxMaterialFlag id=\"flags\">\r\n");
481  fprintf(fph," <NX_MF_ANISOTROPIC>false</NX_MF_ANISOTROPIC>\r\n");
482  fprintf(fph," <NX_MF_DISABLE_FRICTION>false</NX_MF_DISABLE_FRICTION>\r\n");
483  fprintf(fph," <NX_MF_DISABLE_STRONG_FRICTION>false</NX_MF_DISABLE_STRONG_FRICTION>\r\n");
484  fprintf(fph," </NxMaterialFlag>\r\n");
485  fprintf(fph," </NxMaterialDesc>\r\n");
486  fprintf(fph," <NxActorDesc id=\"%s\" userProperties=\"\" hasBody=\"true\" name=\"%s\">\r\n", mBaseName, mBaseName);
487  fprintf(fph," <globalPose>1 0 0 0 1 0 0 0 1 0 0 0 </globalPose>\r\n");
488  fprintf(fph," <NxBodyDesc>\r\n");
489  fprintf(fph," <mass>1</mass>\r\n");
490  fprintf(fph," <linearDamping>0</linearDamping>\r\n");
491  fprintf(fph," <angularDamping>0</angularDamping>\r\n");
492  fprintf(fph," <solverIterationCount>32</solverIterationCount>\r\n");
493  fprintf(fph," <NxBodyFlag id=\"flags\">\r\n");
494  fprintf(fph," <NX_BF_POSE_SLEEP_TEST>true</NX_BF_POSE_SLEEP_TEST>\r\n");
495  fprintf(fph," <NX_AF_DISABLE_COLLISION>false</NX_AF_DISABLE_COLLISION>\r\n");
496  fprintf(fph," </NxBodyFlag>\r\n");
497  fprintf(fph," </NxBodyDesc>\r\n");
498  fprintf(fph," <name>Bip01 Pelvis</name>\r\n");
499  for (unsigned int i=0; i<mHulls.size(); i++)
500  {
501  fprintf(fph," <NxConvexShapeDesc id=\"Shape_%d\" meshData=\"%s_%d\">\r\n", i, mBaseName, i);
502  fprintf(fph," <NxShapeDesc>\r\n");
503  fprintf(fph," <localPose>1 0 0 0 1 0 0 0 1 0 0 0 </localPose>\r\n");
504  fprintf(fph," <skinWidth>0</skinWidth>\r\n");
505  fprintf(fph," </NxShapeDesc>\r\n");
506  fprintf(fph," </NxConvexShapeDesc>\r\n");
507  }
508  fprintf(fph," </NxActorDesc>\r\n");
509 
510  fprintf(fph," </NxSceneDesc>\r\n");
511  fprintf(fph," <NxSceneInstance id=\"%s\">\r\n", mBaseName);
512  fprintf(fph," <sceneName>beshon</sceneName>\r\n");
513  fprintf(fph," <NxuUserProperties></NxuUserProperties>\r\n");
514  fprintf(fph," <rootNode>1 0 0 0 1 0 0 0 1 0 0 0</rootNode>\r\n");
515  fprintf(fph," <newScene>false</newScene>\r\n");
516  fprintf(fph," <ignorePlane>true</ignorePlane>\r\n");
517  fprintf(fph," <numSceneInstances>0</numSceneInstances>\r\n");
518  fprintf(fph," </NxSceneInstance>\r\n");
519  fprintf(fph," </NxuPhysicsCollection>\r\n");
520  fprintf(fph,"</NXUSTREAM2>\r\n");
521 
522  }
523  else
524  {
525  printf("Failed to open file '%s' for write access.\r\n", scratch );
526  }
527  }
528 
529  float mSkinWidth;
530  unsigned int mHullCount;
531  FILE *mFph;
532  unsigned int mBaseCount;
533  char mObjName[512];
534  char mBaseName[512];
536 
537 };
538 
539 
540 int main(int argc,const char **argv)
541 {
542  if ( argc < 2 )
543  {
544  printf("Usage: Test <meshanme.obj> (options)\r\n");
545  printf("\r\n");
546  printf("Options:\r\n");
547  printf("\r\n");
548  printf(" -d<depth> : How deep to recursively split. Values of 3-7 are reasonable.\r\n");
549  printf(" -c<percent> : Percentage of concavity to keep splitting. 0-20%% is reasonable.\r\n");
550  printf(" -p<percent> : Percentage of volume delta to collapse hulls. 0-30%% is reasonable.\r\n");
551  printf(" -v<maxverts> : Maximum number of vertices in the output hull. Default is 32.\r\n");
552  printf(" -s<skinwidth> : Skin Width inflation. Default is 0.\r\n");
553  }
554  else
555  {
556  unsigned int depth = 5;
557  float cpercent = 5;
558  float ppercent = 5;
559  unsigned int maxv = 32;
560  float skinWidth = 0;
561 
562  // process command line switches.
563  for (int i=2; i<argc; i++)
564  {
565  const char *o = argv[i];
566 
567  if ( strncmp(o,"-d",2) == 0 )
568  {
569  depth = (unsigned int) atoi( &o[2] );
570  if ( depth < 0 || depth > 10 )
571  {
572  depth = 5;
573  printf("Invalid depth value in switch, defaulting to 5.\r\n");
574  }
575  }
576 
577  if ( strncmp(o,"-c",2) == 0 )
578  {
579  cpercent = (float) atof( &o[2] );
580  if ( cpercent < 0 || cpercent > 100 )
581  {
582  cpercent = 5;
583  printf("Invalid concavity percentage in switch, defaulting to 5.\r\n");
584  }
585  }
586 
587  if ( strncmp(o,"-p",2) == 0 )
588  {
589  ppercent = (float) atof( &o[2] );
590  if ( ppercent < 0 || ppercent > 100 )
591  {
592  ppercent = 5;
593  printf("Invalid hull merge percentage in switch, defaulting to 5.\r\n");
594  }
595  }
596 
597  if ( strncmp(o,"-v",2) == 0 )
598  {
599  maxv = (unsigned int) atoi( &o[2] );
600  if ( maxv < 8 || maxv > 256 )
601  {
602  maxv = 32;
603  printf("Invalid max vertices in switch, defaulting to 32.\r\n");
604  }
605  }
606 
607  if ( strncmp(o,"-s",2) == 0 )
608  {
609  skinWidth = (float) atof( &o[2] );
610  if ( skinWidth < 0 || skinWidth > 0.1f )
611  {
612  skinWidth = 0;
613  printf("Invalid skinWidth in switch, defaulting to 0.\r\n");
614  }
615  }
616 
617  }
618 
619  WavefrontObj wo;
620 
621  unsigned int tcount = wo.loadObj(argv[1]);
622 
623  if ( tcount )
624  {
625 
626 
627  DecompDesc d;
628 
629  d.mVcount = wo.mVertexCount;
630  d.mVertices = wo.mVertices;
631  d.mTcount = wo.mTriCount;
632  d.mIndices = (unsigned int *)wo.mIndices;
633  d.mDepth = depth;
634  d.mCpercent = cpercent;
635  d.mPpercent = ppercent;
636  d.mMaxVertices = maxv;
637  d.mSkinWidth = skinWidth;
638 
639 
640  CBuilder cb(argv[1],d); // receives the answers and writes out a wavefront OBJ file.
641 
642  d.mCallback = &cb;
643 
644  unsigned int count = performConvexDecomposition(d);
645 
646  if ( count )
647  {
648  printf("Input triangle mesh with %d triangles produced %d output hulls.\r\n", d.mTcount, count );
649 
650  cb.saveNxuStream(); // save results in NxuStream format.
651  cb.saveCOLLADA(); // save results in COLLADA physics 1.4.1 format.
652 
653  }
654  else
655  {
656  printf("Failed to produce any convex hulls.\r\n");
657  }
658 
659  }
660  else
661  {
662  // sorry..no
663  printf("Sorry unable to load file '%s'\r\n", argv[1] );
664  }
665 
666  }
667 
668 }
ConvexDecomposition::DecompDesc::mDepth
unsigned int mDepth
Definition: ConvexDecomposition.h:232
ConvexDecomposition::WavefrontObj::mIndices
int * mIndices
Definition: cd_wavefront.h:76
ConvexDecomposition::WavefrontObj::loadObj
unsigned int loadObj(const char *fname)
Definition: cd_wavefront.cpp:811
CBuilder
Definition: DecomposeSample.cpp:61
CBuilder::saveCOLLADA
void saveCOLLADA(void)
Definition: DecomposeSample.cpp:222
CBuilder::mBaseCount
unsigned int mBaseCount
Definition: DecomposeSample.cpp:532
ConvexDecomposition::DecompDesc::mSkinWidth
double mSkinWidth
Definition: ConvexDecomposition.h:238
ConvexDecomposition::performConvexDecomposition
unsigned int performConvexDecomposition(const DecompDesc &desc)
Definition: ConvexDecomposition.cpp:1049
main
int main(int argc, const char **argv)
Definition: DecomposeSample.cpp:540
ConvexDecomposition::ConvexResult
Definition: ConvexDecomposition.h:64
ConvexDecomposition::WavefrontObj
Definition: cd_wavefront.h:65
ConvexDecomposition::dot
double dot(const double3 &a, const double3 &b)
Definition: cd_hull.cpp:661
ConvexDecomposition::ConvexResult::mHullTcount
unsigned int mHullTcount
Definition: ConvexDecomposition.h:135
CBuilder::saveNxuStream
void saveNxuStream(void)
Definition: DecomposeSample.cpp:414
cd_wavefront.h
CBuilder::mFph
FILE * mFph
Definition: DecomposeSample.cpp:531
ConvexDecomposition
Definition: bestfit.cpp:75
CBuilder::ConvexDecompResult
virtual void ConvexDecompResult(ConvexResult &result)
Definition: DecomposeSample.cpp:125
ConvexDecomposition::ConvexResult::mHullIndices
unsigned int * mHullIndices
Definition: ConvexDecomposition.h:136
ConvexDecomposition::DecompDesc::mIndices
unsigned int * mIndices
Definition: ConvexDecomposition.h:229
CBuilder::saveCOLLADA
void saveCOLLADA(FILE *fph, unsigned int index, ConvexResult *cr)
Definition: DecomposeSample.cpp:169
ConvexDecomposition::ConvexResult::mHullVolume
double mHullVolume
Definition: ConvexDecomposition.h:138
ConvexDecomposition::DecompDesc::mTcount
unsigned int mTcount
Definition: ConvexDecomposition.h:228
CBuilder::~CBuilder
~CBuilder(void)
Definition: DecomposeSample.cpp:112
CBuilder::CBuilder
CBuilder(const char *fname, const DecompDesc &d)
Definition: DecomposeSample.cpp:65
ConvexDecomposition::WavefrontObj::mVertexCount
int mVertexCount
Definition: cd_wavefront.h:74
ConvexDecomposition::DecompDesc::mPpercent
double mPpercent
Definition: ConvexDecomposition.h:234
ConvexDecomposition::DecompDesc::mCallback
ConvexDecompInterface * mCallback
Definition: ConvexDecomposition.h:240
CBuilder::mHullCount
unsigned int mHullCount
Definition: DecomposeSample.cpp:530
ConvexDecomposition::DecompDesc::mVcount
unsigned int mVcount
Definition: ConvexDecomposition.h:226
ConvexDecomposition::ConvexDecompInterface
Definition: ConvexDecomposition.h:191
CBuilder::saveXML
void saveXML(FILE *fph, unsigned int index, ConvexResult *cr)
Definition: DecomposeSample.cpp:378
CBuilder::mSkinWidth
float mSkinWidth
Definition: DecomposeSample.cpp:529
ConvexResultVector
std::vector< ConvexResult * > ConvexResultVector
Definition: DecomposeSample.cpp:16
ConvexDecomposition::ConvexResult::mHullVcount
unsigned int mHullVcount
Definition: ConvexDecomposition.h:133
ConvexDecomposition::DecompDesc::mVertices
const double * mVertices
Definition: ConvexDecomposition.h:227
ConvexDecomposition::WavefrontObj::mVertices
double * mVertices
Definition: cd_wavefront.h:77
fstring
static const char * fstring(float v)
Definition: DecomposeSample.cpp:18
ConvexDecomposition.h
ConvexDecomposition::ConvexResult::mHullVertices
double * mHullVertices
Definition: ConvexDecomposition.h:134
ConvexDecomposition::DecompDesc
Definition: ConvexDecomposition.h:208
ConvexDecomposition::DecompDesc::mMaxVertices
unsigned int mMaxVertices
Definition: ConvexDecomposition.h:237
ConvexDecomposition::DecompDesc::mCpercent
double mCpercent
Definition: ConvexDecomposition.h:233
CBuilder::mHulls
ConvexResultVector mHulls
Definition: DecomposeSample.cpp:535
ConvexDecomposition::WavefrontObj::mTriCount
int mTriCount
Definition: cd_wavefront.h:75


convex_decomposition
Author(s): John W. Ratcliff
autogenerated on Wed Mar 2 2022 00:04:59