-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestHACD.cpp
320 lines (299 loc) · 9.41 KB
/
TestHACD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <iostream>
/*!
**
** Copyright (c) 20011 by John W. Ratcliff mailto:[email protected]
**
**
** The MIT license:
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is furnished
** to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all
** copies or substantial portions of the Software.
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma warning(disable:4996 4100)
#include "PlatformConfigHACD.h"
#include "wavefront.h"
#include "HACD.h"
#include "JobSwarm.h"
namespace HACD
{
HACD_API *gHACD = NULL;
};
float getFloatArg(int arg,int argc,const char **argv)
{
float ret = 0;
if ( arg < argc )
{
ret = (float)atof(argv[arg]);
}
else
{
printf("Error: Missing input argument value at argument location %d.\r\n",arg+1);
}
return ret;
}
int getIntArg(int arg,int argc,const char **argv)
{
int ret = 0;
if ( arg < argc )
{
ret = atoi(argv[arg]);
}
else
{
printf("Error: Missing input argument value at argument location %d.\r\n",arg+1);
}
return ret;
}
class MyCallback : public hacd::ICallback
{
public:
MyCallback(void)
{
mLastPercent = 0xFFFFFFFF;
mLastMessage = NULL;
}
virtual bool Cancelled()
{
// Don't have a cancel button in the test console app.
return false;
}
virtual void ReportProgress(const char* message, hacd::HaF32 progress)
{
hacd::HaU32 percent = (hacd::HaU32)(progress*100.0f);
if ( percent != mLastPercent || message != mLastMessage )
{
if ( message == mLastMessage )
{
printf("%c%c%c%c",8,8,8,8);
printf("%3d%c", percent,'%' );
}
else
{
printf("\r\n%s : %3d", message, percent);
}
mLastMessage = message;
mLastPercent = percent;
}
}
const char * mLastMessage;
hacd::HaU32 mLastPercent;
};
void main(int argc,const char ** argv)
{
if ( argc == 1 )
{
printf("Usage: TestHACD <wavefront.obj> (options)\r\n");
printf("\r\n");
printf("Options:\r\n");
printf("-v <count> : Max Hull Vertices (default 64)\r\n");
printf("-m <count> : Maximum number of hulls output from HACD (default 256)\r\n");
printf("-merge <count> : Maximum number of hulls after merging the HACD result.\r\n");
printf("-mergethreshold <volume> : Threshold below which hulls are merged if they are smaller than the given volume.\r\n");
printf("-c <concavity> : Between 0 and 1 are good ranges to try; default is 0.2. The smaller the number, the more convex hulls are produced.\r\n");
printf("-b <backFaceDistanceFactor : The back face distance factor, default is 0.2\r\n");
printf("-t <threadCount> : Specifies the number of threads to use for a multi-threaded implementation\r\n");
printf("-d <decompositionDepth> : Specifies the decomposition depth; uses legacy ACD instead of HACD\r\n");
printf("-n : Normalize the input mesh.\r\n");
printf("-f : Use legacy 'fast' version of HACD\r\n");
printf("\r\n");
printf("Example: TestHACD hornbug.obj -m 40 -v 64\r\n");
printf("\r\n");
}
else
{
HACD::HACD_API::Desc desc;
const char *wavefront = argv[1];
int scan = 2;
int threadCount = 0;
while ( scan < argc )
{
const char *option = argv[scan];
if ( strcmp(option,"-v") == 0 )
{
desc.mMaxHullVertices = getIntArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-t") == 0 )
{
threadCount = getIntArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-d") == 0 )
{
desc.mDecompositionDepth = getIntArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-b") == 0 )
{
desc.mBackFaceDistanceFactor = getFloatArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-m") == 0 )
{
desc.mMaxHullCount = getIntArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-merge") == 0 )
{
desc.mMaxMergeHullCount = getIntArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-mergethreshold") == 0 )
{
desc.mSmallClusterThreshold = getFloatArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-c") == 0 )
{
desc.mConcavity = getFloatArg(scan+1,argc,argv);
scan+=2;
}
else if ( strcmp(option,"-n") == 0 )
{
desc.mNormalizeInputMesh = true;
scan++;
}
else if ( strcmp(option,"-f") == 0 )
{
desc.mUseFastVersion = true;
scan++;
}
else
{
scan++;
}
}
JOB_SWARM::JobSwarmContext *jobSwarmContext = NULL;
if ( threadCount )
{
jobSwarmContext = JOB_SWARM::createJobSwarmContext(threadCount);
// jobSwarmContext->setUseThreads(false);
}
HACD::gHACD = HACD::createHACD_API();
if ( HACD::gHACD )
{
MyCallback callback;
WavefrontObj obj;
unsigned int tcount = obj.loadObj(wavefront,false);
if ( tcount )
{
desc.mTriangleCount = obj.mTriCount;
desc.mVertexCount = obj.mVertexCount;
desc.mIndices = (hacd::HaU32 *)obj.mIndices;
desc.mVertices = obj.mVertices;
}
desc.mCallback = static_cast<hacd::ICallback*>(&callback);
if ( desc.mTriangleCount )
{
printf("Performing HACD on %d input triangles.\r\n", desc.mTriangleCount );
printf("\r\n");
printf("TriangleCount : %d\r\n", desc.mTriangleCount );
printf("VertexCount : %d\r\n", desc.mVertexCount );
printf("Concavity : %0.2f\r\n", desc.mConcavity );
printf("Max Hull Vertex Count : %3d\r\n", desc.mMaxHullVertices );
printf("Max Convex Hulls : %3d\r\n", desc.mMaxHullCount );
printf("Max Merged Convex Hulls : %3d\r\n", desc.mMaxMergeHullCount );
printf("Merge Threshold : %0.2f\r\n", desc.mSmallClusterThreshold);
printf("Back Face Distance Factor: %0.2f\r\n", desc.mBackFaceDistanceFactor );
printf("Small Cluster Threshold : %0.2f\r\n", desc.mSmallClusterThreshold );
if ( desc.mDecompositionDepth )
{
printf("DecompositionDepth : %d\r\n", desc.mDecompositionDepth );
}
if ( jobSwarmContext )
{
printf("Parallel Solver TheadCount: %d\r\n", threadCount );
}
if ( desc.mNormalizeInputMesh )
{
printf("Normalizing input mesh.\r\n");
}
if ( desc.mUseFastVersion )
{
printf("Using 'fast' version of HACD.\r\n");
}
printf("\r\n");
desc.mJobSwarmContext = jobSwarmContext;
hacd::HaU32 hullCount = HACD::gHACD->performHACD(desc);
if ( hullCount != 0 )
{
printf("\r\n");
printf("Produced %d output convex hulls. Saving output to 'ConvexDecomposition.obj' for review.\r\n", hullCount );
FILE *fph = fopen("ConvexDecomposition.obj", "wb");
if ( fph )
{
fprintf(fph,"# Input mesh '%s' produced %d convex hulls.\r\n", wavefront, hullCount );
hacd::HaU32 *baseVertex = new hacd::HaU32[hullCount];
hacd::HaU32 vertexCount = 0;
for (hacd::HaU32 i=0; i<hullCount; i++)
{
const HACD::HACD_API::Hull *hull = HACD::gHACD->getHull(i);
if ( hull )
{
baseVertex[i] = vertexCount;
fprintf(fph,"## Hull %d has %d vertices.\r\n", i+1, hull->mVertexCount );
for (hacd::HaU32 i=0; i<hull->mVertexCount; i++)
{
const hacd::HaF32 *p = &hull->mVertices[i*3];
fprintf(fph,"v %0.9f %0.9f %0.9f\r\n", p[0], p[1], p[2] );
}
vertexCount+=hull->mVertexCount;
}
}
for (hacd::HaU32 i=0; i<hullCount; i++)
{
const HACD::HACD_API::Hull *hull = HACD::gHACD->getHull(i);
if ( hull )
{
hacd::HaU32 startVertex = baseVertex[i];
fprintf(fph,"# Convex Hull %d contains %d triangles and %d vertices. Starting vertex index is: %d It has a volume of: %0.9f\r\n", i+1, hull->mTriangleCount, hull->mVertexCount, startVertex);
fprintf(fph,"g convex_hull%d\r\n", i+1);
for (hacd::HaU32 j=0; j<hull->mTriangleCount; j++)
{
hacd::HaU32 i1 = hull->mIndices[j*3+0]+startVertex+1;
hacd::HaU32 i2 = hull->mIndices[j*3+1]+startVertex+1;
hacd::HaU32 i3 = hull->mIndices[j*3+2]+startVertex+1;
fprintf(fph,"f %d %d %d\r\n", i1, i2, i3 );
}
}
}
}
else
{
printf("Failed to open output file.\r\n");
}
}
}
else
{
printf("Failed to load Wavefront OBJ file '%s'\r\n",wavefront);
}
}
else
{
printf("Failed to load the HACD DLL\r\n");
}
if ( jobSwarmContext )
{
JOB_SWARM::releaseJobSwarmContext(jobSwarmContext);
}
}
}