forked from ahmidou/MayaNodes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pushDeformer.cpp
270 lines (228 loc) · 7.7 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
#include "pushDeformer.h"
MTypeId PushDeformer::id(0x00124502);
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;
for ( unsigned int i = start; i < end; ++i )
{
//float w = weightValue(dataBlock, geomIndex, i);
float w = 1.0;
if (useStressV == true && (stressV.length() > 0))
{
//deform
points[i] += (MVector(normals[i]) * bulgeAmount * envelope * w * stressV[i]);
}
else
{
//deform
points[i] += normals[i] * bulgeAmount * envelope * w;
}
}
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);
fnMesh.getVertexNormals(false, m_taskData.normals, MSpace::kWorld);
itGeo.allPositions(m_taskData.points, MSpace::kWorld);
//MGlobal::displayInfo( "test" );
/*for (int i = 0; i < itGeo.count(); i++)
{
MGlobal::displayInfo( MFnAttribute(weightList).isArray );
}*/
m_taskData.bulgeAmount = bulgeAmount;
if(multiThreadingType == 1)
{
ThreadData* pThreadData = createThreadData( NUM_TASKS, &m_taskData );
MThreadPool::newParallelRegion( createTasks, (void*)pThreadData );
itGeo.setAllPositions(m_taskData.points);
delete [] pThreadData;
return MS::kSuccess;
}
else if(multiThreadingType == 2)
{
tbb::parallel_for(size_t(0), size_t(itGeo.count()), [this](size_t i)
{
//const float w = weightValue(dataBlock, geomIndex, i);
const float w = 1.0;
if (m_taskData.useStressV == true && (m_taskData.stressV.length() > 0))
{
//deform
m_taskData.points[i] += (MVector(m_taskData.normals[i]) * m_taskData.bulgeAmount * m_taskData.envelope * w * m_taskData.stressV[i]);
}
else
{
//deform
m_taskData.points[i] += m_taskData.normals[i] * m_taskData.bulgeAmount * m_taskData.envelope * w;
}
});
}
//
else if(multiThreadingType == 3)
#pragma omp parallel for
for (int i = 0; i < itGeo.count(); i++)
{
float w = weightValue(dataBlock, geomIndex, itGeo.index());
if (useStressV == true && (stressV.length() > 0))
{
//deform
m_taskData.points[i] += (MVector(m_taskData.normals[i]) * bulgeAmount * m_taskData.envelope * w * m_taskData.stressV[i]);
}
else
{
//deform
m_taskData.points[i] += m_taskData.normals[i] * bulgeAmount * m_taskData.envelope * w;
}
}
else
{
for (; !itGeo.isDone(); itGeo.next())
{
float w = weightValue(dataBlock, geomIndex, itGeo.index());
if (useStressV == true && (stressV.length() > 0))
{
//deform
m_taskData.points[itGeo.index()] += (MVector(m_taskData.normals[itGeo.index()]) * bulgeAmount * m_taskData.envelope * w * m_taskData.stressV[itGeo.index()]);
}
else
{
//deform
m_taskData.points[itGeo.index()] += m_taskData.normals[itGeo.index()] * bulgeAmount * m_taskData.envelope * w;
}
}
}
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;
}