-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpushDeformer.cpp
387 lines (338 loc) · 12.5 KB
/
pushDeformer.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "pushDeformer.h"
#include <maya/MFloatPointArray.h>
#include <maya/MAnimControl.h>
#include <chrono>
#include <ctime>
MTypeId PushDeformer::id(0x00124502);
MObject PushDeformer::aDriverGeo;
MObject PushDeformer::aAmount;
MObject PushDeformer::aStressMap;
MObject PushDeformer::aUseStress;
MObject PushDeformer::aMultiThreadingType;
#define NUM_TASKS 16
PushDeformer::PushDeformer()
{
MThreadPool::init();
}
PushDeformer::~PushDeformer()
{
MThreadPool::release();
}
void* PushDeformer::creator()
{
return new PushDeformer();
}
ThreadData* PushDeformer::createThreadData( int numTasks, TaskData* pTaskData )
{
ThreadData* pThreadData = new ThreadData[numTasks];
unsigned int numPoints = pTaskData->points.length();
unsigned int taskLength = (numPoints + numTasks - 1) / numTasks;
unsigned int start = 0;
unsigned int end = taskLength;
int lastTask = numTasks - 1;
for( int i = 0; i < numTasks; ++i )
{
if ( i == lastTask )
{
end = numPoints;
}
pThreadData[i].start = start;
pThreadData[i].end = end;
pThreadData[i].numTasks = numTasks;
pThreadData[i].pData = pTaskData;
start += taskLength;
end += taskLength;
}
return pThreadData;
}
void PushDeformer::createTasks( void* pData, MThreadRootTask* pRoot )
{
ThreadData* pThreadData = (ThreadData*)pData;
if ( pThreadData )
{
int numTasks = pThreadData->numTasks;
for( int i = 0; i < numTasks; ++i )
{
MThreadPool::createTask( threadEvaluate, (void*)&pThreadData[i], pRoot );
}
MThreadPool::executeAndJoin( pRoot );
}
}
MThreadRetVal PushDeformer::threadEvaluate( void *pParam )
{
MStatus status;
ThreadData* pThreadData = (ThreadData*)(pParam);
TaskData* pData = pThreadData->pData;
unsigned int start = pThreadData->start;
unsigned int end = pThreadData->end;
MPointArray& points = pData->points;
MFloatVectorArray& normals = pData->normals;
//MDataBlock& dataBlock = pData->dataBlock;
MDoubleArray& stressV = pData->stressV;
double bulgeAmount = pData->bulgeAmount;
bool useStressV = pData->useStressV;
float envelope = pData->envelope;
int geomIndex = pData->geomIndex;
float w = 1.0;
for ( unsigned int i = start; i < end; ++i )
{
//float w = weightValue(dataBlock, geomIndex, i);
double stressFactor = (useStressV == 1 ? stressV[i] : 1.0);
double factor = bulgeAmount * envelope * w * stressFactor;
points[i].x += normals[i].x * factor;
points[i].y += normals[i].y * factor;
points[i].z += normals[i].z * factor;
}
return 0;
}
MStatus PushDeformer::deform(MDataBlock& dataBlock,
MItGeometry& itGeo,
const MMatrix& localToWorldMatrix,
unsigned int geomIndex)
{
MStatus status;
//get attribute handles
double bulgeAmount = dataBlock.inputValue(aAmount, &status).asDouble();
CHECK_MSTATUS_AND_RETURN_IT(status);
m_taskData.envelope = dataBlock.inputValue(envelope, &status).asFloat();
CHECK_MSTATUS_AND_RETURN_IT(status);
bool useStressV = dataBlock.inputValue(aUseStress, &status).asBool();
CHECK_MSTATUS_AND_RETURN_IT(status);
int multiThreadingType = dataBlock.inputValue(aMultiThreadingType, &status).asBool();
CHECK_MSTATUS_AND_RETURN_IT(status);
if (m_taskData.envelope <= 0.001)
{
return MS::kSuccess;
}
// if the use stress plug is turned on pull
MDoubleArray stressV;
if (useStressV == true)
{
//pull out the raw data as an Mobject
MObject stressMap = dataBlock.inputValue(aStressMap, &status).data();
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnDoubleArrayData stressDataFn(stressMap);
m_taskData.stressV = stressDataFn.array();
}
//retrieve the handle to the output array attribute
MArrayDataHandle hInput = dataBlock.outputArrayValue(input, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
//get the input array index handle
status = hInput.jumpToElement(geomIndex);
//get the handle of geomIndex attribute
MDataHandle hInputElement = hInput.outputValue(&status);
CHECK_MSTATUS_AND_RETURN_IT(status);
//Get the MObject of the input geometry of geomindex
MObject oInputGeom = hInputElement.child(inputGeom).asMesh();
MFnMesh fnMesh(oInputGeom, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
//MGlobal::displayInfo( "test" );
/*for (int i = 0; i < itGeo.count(); i++)
{
MGlobal::displayInfo( MFnAttribute(weightList).isArray );
}*/
m_taskData.bulgeAmount = bulgeAmount;
double time = MAnimControl().currentTime().as(MTime::kFilm);
MGlobal::displayInfo(MString()+time);
if (time == 1)
{
m_MThreadPoolTime = 0;
}
auto startTime =std::chrono::steady_clock::now();
auto endTime = std::chrono::steady_clock::now();
if(multiThreadingType == 1)
{
itGeo.allPositions(m_taskData.points, MSpace::kWorld);
fnMesh.getVertexNormals(false, m_taskData.normals, MSpace::kWorld);
startTime = std::chrono::steady_clock::now();
ThreadData* pThreadData = createThreadData( NUM_TASKS, &m_taskData );
MThreadPool::newParallelRegion( createTasks, (void*)pThreadData );
endTime = std::chrono::steady_clock::now();
auto elapsed = endTime - startTime;
auto elapsedCount = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
m_MThreadPoolTime += (double)elapsedCount / 1000000.0;
MGlobal::displayInfo(MString("compute time: ") + (MString() + (m_MThreadPoolTime/time)) + MString(" s"));
itGeo.setAllPositions(m_taskData.points);
delete [] pThreadData;
return MS::kSuccess;
}
else if(multiThreadingType == 2)
{
// TBB
itGeo.allPositions(m_taskData.points, MSpace::kWorld);
fnMesh.getVertexNormals(false, m_taskData.normals, MSpace::kWorld);
bool useStressV = m_taskData.useStressV == true && (m_taskData.stressV.length() > 0);
const float w = 1.0;
startTime = std::chrono::steady_clock::now();
tbb::parallel_for(size_t(0), size_t(itGeo.count()), [this, useStressV, w](size_t i)
{
//const float w = weightValue(dataBlock, geomIndex, i);
double stressFactor = (useStressV == 1 ? m_taskData.stressV[i] : 1.0);
double factor = m_taskData.bulgeAmount * m_taskData.envelope * w * stressFactor;
//deform
m_taskData.points[i].x += m_taskData.normals[i].x * factor;
m_taskData.points[i].y += m_taskData.normals[i].y * factor;
m_taskData.points[i].z += m_taskData.normals[i].z * factor;
});
endTime = std::chrono::steady_clock::now();
}
else if (multiThreadingType == 3)
{
// OMP Maya Array access
const float w = 1.0;
itGeo.allPositions(m_taskData.points, MSpace::kWorld);
fnMesh.getVertexNormals(false, m_taskData.normals, MSpace::kWorld);
bool useStressV = m_taskData.useStressV == true && (m_taskData.stressV.length() > 0);
startTime = std::chrono::steady_clock::now();
#pragma omp parallel for
for (int i = 0; i < itGeo.count(); i++)
{
//float w = weightValue(dataBlock, geomIndex, itGeo.index());
double stressFactor = (useStressV == 1 ? m_taskData.stressV[i] : 1.0);
double factor = bulgeAmount * m_taskData.envelope * w * stressFactor;
m_taskData.points[i].x += m_taskData.normals[i].x * factor;
m_taskData.points[i].y += m_taskData.normals[i].y * factor;
m_taskData.points[i].z += m_taskData.normals[i].z * factor;
}
endTime = std::chrono::steady_clock::now();
}
else if (multiThreadingType == 4)
{
unsigned int start = 0;
unsigned int end = itGeo.count();
float w = 1.0;
float ba = float(bulgeAmount);
float factor = float(bulgeAmount) * m_taskData.envelope * w;
MFloatVectorArray norm;
fnMesh.getVertexNormals(false, norm, MSpace::kWorld);
MFloatPointArray pts;
fnMesh.getPoints(pts);
float * a = &pts[0].x;
float * b = &norm[0].x;
int vEnd = end;
int offset = 0;
startTime = std::chrono::steady_clock::now();
// outerloop can't be parallelized
for (int i = 0; i < end; i++)
{
int lBound = i * 4;
int uBound = lBound + 4;
#pragma simd
for (int j = lBound; j < uBound; j++)
{
if(lBound -i != 4)
a[j] += b[j - offset] * factor;
}
offset++;
m_taskData.points[i] = MFloatPoint(a[lBound], a[lBound + 1], a[lBound + 2], 1.0);
}
endTime = std::chrono::steady_clock::now();
}
else if (multiThreadingType == 5)
{
unsigned int start = 0;
unsigned int end = itGeo.count();
float w = 1.0;
float ba = float(bulgeAmount);
float factor = float(bulgeAmount) * m_taskData.envelope * w;
MFloatVectorArray norm;
fnMesh.getVertexNormals(false, norm, MSpace::kWorld);
const float * pts = fnMesh.getRawPoints(0);
std::vector<float> a(end*3);
float * b = &norm[0].x;
int vEnd = end;
startTime = std::chrono::steady_clock::now();
#pragma omp parallel for
for (int i = 0; i < end; i++)
{
int k = i * 3;
int l = k + 3;
#pragma simd
for (int j = k; j < l; j++)
{
a[j] = pts[j] + (b[j] * factor);
}
m_taskData.points[i] = MFloatPoint(a[k], a[k + 1], a[k + 2], 1.0);
}
endTime = std::chrono::steady_clock::now();
}
else if (multiThreadingType == 6)
{
unsigned int start = 0;
unsigned int end = itGeo.count();
float w = 1.0;
float ba = float(bulgeAmount);
float factor = float(bulgeAmount) * m_taskData.envelope * w;
MFloatVectorArray norm;
fnMesh.getVertexNormals(false, norm, MSpace::kWorld);
const float * pts = fnMesh.getRawPoints(0);
std::vector<float> a(end * 3);
float * b = &norm[0].x;
int vEnd = end*3;
startTime = std::chrono::steady_clock::now();
#pragma omp parallel for
//#pragma simd
for (int i = 0; i < vEnd; i++)
{
a[i] = pts[i] + (b[i] * factor);
}
#pragma omp parallel for
for (int k = 0; k < end; k++)
{
m_taskData.points[k] = MFloatVector(a[k * 3], a[k * 3 + 1], a[k * 3 + 2]);
}
endTime = std::chrono::steady_clock::now();
}
else
{
const float w = 1.0;
itGeo.allPositions(m_taskData.points, MSpace::kWorld);
fnMesh.getVertexNormals(false, m_taskData.normals, MSpace::kWorld);
startTime = std::chrono::steady_clock::now();
for (; !itGeo.isDone(); itGeo.next())
{
double stressFactor = (useStressV == 1 ? m_taskData.stressV[itGeo.index()] : 1.0);
double factor = m_taskData.bulgeAmount * m_taskData.envelope * w * stressFactor;
//deform
m_taskData.points[itGeo.index()].x += m_taskData.normals[itGeo.index()].x * factor;
m_taskData.points[itGeo.index()].y += m_taskData.normals[itGeo.index()].y * factor;
m_taskData.points[itGeo.index()].z += m_taskData.normals[itGeo.index()].z * factor;
}
endTime = std::chrono::steady_clock::now();
}
auto elapsed = endTime - startTime;
auto elapsedCount = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
m_MThreadPoolTime += (double)elapsedCount / 1000000.0;
MGlobal::displayInfo(MString("compute time: ") + (MString() + (m_MThreadPoolTime / time)) + MString(" s"));
itGeo.setAllPositions(m_taskData.points);
return MS::kSuccess;
}
MStatus PushDeformer::initialize()
{
MStatus status;
MFnNumericAttribute nAttr;
MFnTypedAttribute tAttr;
//add bulge attribute which affects the output geom
aAmount = nAttr.create("amount", "amt", MFnNumericData::kDouble, 0.0);
nAttr.setKeyable(true);
addAttribute(aAmount);
aStressMap = tAttr.create("aStressMap", "stMap", MFnData::kDoubleArray);
tAttr.setKeyable(true);
tAttr.setWritable(true);
tAttr.setStorable(true);
addAttribute(aStressMap);
aUseStress = nAttr.create("useStress", "useStress", MFnNumericData::kBoolean, 0);
nAttr.setKeyable(true);
nAttr.setStorable(true);
addAttribute(aUseStress);
aMultiThreadingType = nAttr.create("multiThreadingType", "multiThreadingType", MFnNumericData::kInt, 0);
nAttr.setKeyable(true);
addAttribute(aMultiThreadingType);
attributeAffects(aAmount, outputGeom);
attributeAffects(aStressMap, outputGeom);
attributeAffects(aUseStress, outputGeom);
attributeAffects(aMultiThreadingType, outputGeom);
// make paintable deformer
MGlobal::executeCommand("makePaintable -attrType multiFloat -sm deformer pushDeformer weights");
return MS::kSuccess;
}