-
Notifications
You must be signed in to change notification settings - Fork 17
/
harmonicDeformer.cpp
405 lines (354 loc) · 12.6 KB
/
harmonicDeformer.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "harmonicDeformer.h"
#include <maya/MItGeometry.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MPoint.h>
#include <maya/MTimer.h>
#include <maya/MFnMesh.h>
#include <maya/MPointArray.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnMeshData.h>
#include <maya/MThreadUtils.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnDoubleArrayData.h>
#include <tbb/parallel_for.h>
#include "grid.h"
#include "cell.h"
#define MCheckStatus(status,message) \
if( MStatus::kSuccess != status ) { \
cerr << message << "\n"; \
return status; \
}
MTypeId HarmonicDeformer::id(0x00122C05);
MObject HarmonicDeformer::m_cage;
MObject HarmonicDeformer::m_refCage;
MObject HarmonicDeformer::m_gridData;
MObject HarmonicDeformer::m_cellSize;
MObject HarmonicDeformer::m_maxIteration;
MObject HarmonicDeformer::m_pointWeights;
MObject HarmonicDeformer::m_threshold;
MObject HarmonicDeformer::m_dynamicBinding;
const char* harmonicDeformerTemplate = "\
proc string AEgetNode(string $nodeAttr)\
{\
string $result = \"\";\
string $buffer[];\
\
tokenize($nodeAttr, \".\", $buffer);\
\
if (size($buffer) == 3)\
$result = $buffer[0] + \".\" + $buffer[1];\
else\
$result = $buffer[0];\
\
return $result;\
}\
\
global proc AEcomputeHarmonicWeightsButton(string $attr) \
{\
string $node = AEgetNode($attr + \"t\");\
button -label \"Compute harmonic weights\" -command(\"AEcomputeHarmonicWeights \" + $node) computeHarmonicWeightsButton;\
}\
\
global proc AEcomputeHarmonicWeightsButtonUpdate(string $attr)\
{\
string $node = AEgetNode($attr + \"t\");\
button -edit -command(\"AEcomputeHarmonicWeights \" + $node) computeHarmonicWeightsButton;\
}\
\
global proc AEcomputeHarmonicWeights(string $node)\
{\
float $cellSize = `getAttr ($node+\".cellSize\")`;\
float $threshold = `getAttr ($node+\".threshold\")`;\
int $iterations = `getAttr ($node+\".maxIterations\")`;\
int $dynamicBinding = `getAttr ($node+\".dynamicBinding\")`;\
if($dynamicBinding)\
{\
tcComputeHarmonicWeights -d $node -mi $iterations -cs $cellSize -ts $threshold -sg 1;\
}\
else\
{\
tcComputeHarmonicWeights -d $node -mi $iterations -cs $cellSize -ts $threshold;\
}\
}\
\
\
global proc AEtcHarmonicDeformerTemplate(string $nodeName)\
{\
editorTemplate -beginScrollLayout;\
\
editorTemplate -beginLayout \"Binding attributes\" -collapse 0;\
editorTemplate -addControl \"cellSize\";\
editorTemplate -addControl \"maxIterations\";\
editorTemplate -addControl \"threshold\";\
editorTemplate -addControl \"dynamicBinding\";\
editorTemplate -callCustom \"AEcomputeHarmonicWeightsButton\" \"AEcomputeHarmonicWeightsButtonUpdate\" \"\";\
\
editorTemplate -endLayout;\
\
AEdependNodeTemplate $nodeName;\
\
editorTemplate -addExtraControls; \
editorTemplate -endScrollLayout; \
}";
class ParallelFor
{
public:
ParallelFor(std::vector<std::map<unsigned int, double> >& weights,
MPointArray& cagePoints,
MPointArray& cageRefPoints,
MPointArray& verts,
double envelope) :
m_weights(weights),
m_cagePoints(cagePoints),
m_cageRefPoints(cageRefPoints),
m_verts(verts),
m_envelope(envelope)
{}
~ParallelFor() {}
void operator()(const tbb::blocked_range<size_t>& range) const
{
for (size_t i = range.begin(); i != range.end(); ++i)
{
for (std::map<unsigned int, double>::iterator it = m_weights[i].begin(); it != m_weights[i].end(); ++it)
{
MVector diffPos = m_cagePoints[it->first] - m_cageRefPoints[it->first];
diffPos *= it->second * m_envelope;
m_verts[i] += diffPos;
}
}
}
private:
std::vector<std::map<unsigned int, double> >& m_weights;
MPointArray& m_cagePoints;
MPointArray& m_cageRefPoints;
MPointArray& m_verts;
const double m_envelope;
};
HarmonicDeformer::HarmonicDeformer() : m_toBind(true), m_weightsUpdated(false), m_gridUpdated(false) {}
HarmonicDeformer::~HarmonicDeformer() {}
void HarmonicDeformer::postConstructor()
{
MGlobal::executeCommand(harmonicDeformerTemplate, false, false);
}
void* HarmonicDeformer::creator()
{
return new HarmonicDeformer();
}
MStatus HarmonicDeformer::initialize()
{
// local attribute initialization
MStatus status;
MFnTypedAttribute mAttr;
MFnNumericAttribute nAttr;
m_cage = mAttr.create("cage", "cg", MFnMeshData::kMesh);
mAttr.setStorable(true);
m_refCage = mAttr.create("refCage", "rcg", MFnMeshData::kMesh);
mAttr.setStorable(true);
m_gridData = mAttr.create("gridData", "gd", MFnData::kString);
mAttr.setStorable(true);
mAttr.setWritable(true);
mAttr.setHidden(true);
m_cellSize = nAttr.create("cellSize", "cs", MFnNumericData::kDouble, 1.0);
nAttr.setStorable(true);
nAttr.setWritable(true);
m_maxIteration = nAttr.create("maxIterations", "mi", MFnNumericData::kLong, 20);
nAttr.setStorable(true);
nAttr.setWritable(true);
m_threshold = nAttr.create("threshold", "ts", MFnNumericData::kDouble, 0.00001);
nAttr.setStorable(true);
nAttr.setWritable(true);
m_dynamicBinding = nAttr.create("dynamicBinding", "db", MFnNumericData::kBoolean, false);
nAttr.setStorable(true);
nAttr.setWritable(true);
m_pointWeights = mAttr.create("pointWeights", "pw", MFnData::kDoubleArray);
mAttr.setStorable(true);
mAttr.setWritable(true);
mAttr.setHidden(true);
// deformation attributes
status = addAttribute(m_cellSize); MCheckStatus(status, "ERROR in addAttribute m_cellSize\n");
status = addAttribute(m_maxIteration); MCheckStatus(status, "ERROR in addAttribute m_maxIteration\n");
status = addAttribute(m_threshold); MCheckStatus(status, "ERROR in addAttribute m_threshold\n");
status = addAttribute(m_dynamicBinding); MCheckStatus(status, "ERROR in addAttribute m_dynamicBinding\n");
status = addAttribute(m_pointWeights); MCheckStatus(status, "ERROR in addAttribute m_pointWeights\n");
status = addAttribute(m_cage); MCheckStatus(status, "ERROR in addAttribute m_cage\n");
status = addAttribute(m_refCage); MCheckStatus(status, "ERROR in addAttribute m_refCage\n");
status = addAttribute(m_gridData); MCheckStatus(status, "ERROR in addAttribute m_gridData\n");
status = attributeAffects(m_cage, outputGeom); MCheckStatus(status, "ERROR in attributeAffects m_cage\n");
status = attributeAffects(m_refCage, outputGeom); MCheckStatus(status, "ERROR in attributeAffects m_refCage\n");
status = attributeAffects(m_gridData, outputGeom); MCheckStatus(status, "ERROR in attributeAffects m_gridData\n");
status = attributeAffects(m_pointWeights, outputGeom); MCheckStatus(status, "ERROR in attributeAffects m_pointWeights\n");
return MStatus::kSuccess;
}
MStatus HarmonicDeformer::setDependentsDirty(const MPlug &plugBeingDirtied, MPlugArray &affectedPlugs)
{
MObject thisNode = this->thisMObject();
MPlug gridPlug(thisNode, m_gridData);
if (plugBeingDirtied == gridPlug)
m_gridUpdated = true;
MPlug pointsWeightPlug(thisNode, m_pointWeights);
if (plugBeingDirtied == pointsWeightPlug)
m_weightsUpdated = true;
MPlug inPlug(thisNode, input);
if ((plugBeingDirtied == inPlug) || (plugBeingDirtied.array() == inPlug) || (plugBeingDirtied.parent().array() == inPlug))
m_toBind = true;
//std::cout << plugBeingDirtied.name() << std::endl;
return MPxNode::setDependentsDirty(plugBeingDirtied, affectedPlugs);
}
MStatus HarmonicDeformer::compute(const MPlug& plug, MDataBlock& data)
{
// do this if we are using an OpenMP implementation that is not the same as Maya's.
// Even if it is the same, it does no harm to make this call.
MThreadUtils::syncNumOpenMPThreads();
MStatus status = MStatus::kUnknownParameter;
if (plug.attribute() != outputGeom) {
return status;
}
MDataHandle dynBindHandle = data.inputValue(m_dynamicBinding, &status);
bool dynBind = dynBindHandle.asBool();
MDataHandle envelopHandle = data.inputValue(envelope, &status);
double envelop = envelopHandle.asFloat();
unsigned int index = plug.logicalIndex();
MObject thisNode = this->thisMObject();
// get input value
MPlug inPlug(thisNode, input);
inPlug.selectAncestorLogicalIndex(index, input);
MDataHandle hInput = data.inputValue(inPlug, &status);
MCheckStatus(status, "ERROR getting input mesh\n");
// get the input geometry
MDataHandle inputData = hInput.child(inputGeom);
if (inputData.type() != MFnData::kMesh) {
printf("Incorrect input geometry type\n");
return MStatus::kFailure;
}
// get the input groupId - ignored for now...
MDataHandle hGroup = inputData.child(groupId);
unsigned int groupId = hGroup.asLong();
// get ref cage mesh
MDataHandle refCageData = data.inputValue(m_refCage, &status);
MCheckStatus(status, "ERROR getting ref cage mesh\n");
if (refCageData.type() != MFnData::kMesh) {
printf("Incorrect deformer geometry type %d\n", refCageData.type());
return MStatus::kFailure;
}
MObject refCageObj = refCageData.asMeshTransformed();
MFnMesh fnRefCacgeMesh(refCageObj);
MPointArray cageRefPoints;
fnRefCacgeMesh.getPoints(cageRefPoints);
// get cage mesh
MDataHandle cageData = data.inputValue(m_cage, &status);
MCheckStatus(status, "ERROR getting cage mesh\n");
if (cageData.type() != MFnData::kMesh) {
printf("Incorrect deformer geometry type %d\n", cageData.type());
return MStatus::kFailure;
}
MObject cageObj = cageData.asMeshTransformed();
MFnMesh fnCageMesh(cageObj);
MPointArray cagePoints;
fnCageMesh.getPoints(cagePoints, MSpace::kWorld);
MDataHandle outputData = data.outputValue(plug);
outputData.copy(inputData);
if (outputData.type() != MFnData::kMesh) {
printf("Incorrect output mesh type\n");
return MStatus::kFailure;
}
MItGeometry iter(outputData, groupId, false);
// get all points at once. Faster to query, and also better for
// threading than using iterator
MPointArray verts;
iter.allPositions(verts, MSpace::kWorld);
int nPoints = verts.length();
//if (dynBind && ((m_toBind) || (m_weights.size() != nPoints)))
if (dynBind && m_gridUpdated)
{
m_weights.clear();
MDataHandle gridDataString = data.inputValue(m_gridData, &status);
MString gridDataStr = gridDataString.asString();
tc::Grid grid(1.0);
if (grid.deserialise(gridDataStr.asChar()))
{
std::vector<tc::Vector> points;
points.resize(nPoints);
for (unsigned int v = 0; v < nPoints; ++v)
{
points[v] = tc::Vector(verts[v].x, verts[v].y, verts[v].z);
}
m_weights = grid.getWeights(points);
/*
m_weights.resize(nPoints);
for (unsigned int v = 0; v < nPoints; ++v)
{
const tc::Cell& cell = grid.getCell(tc::Vector(verts[v].x, verts[v].y, verts[v].z));
std::map<unsigned int, double> weights = cell.weights;
for (std::map<unsigned int, double>::iterator it = weights.begin(); it != weights.end(); ++it)
{
it->second = grid.getWeight(tc::Vector(verts[v].x, verts[v].y, verts[v].z), it->first);
}
//normalize weights
double totalWeight = 0.0;
for (std::map<unsigned int, double>::iterator it = weights.begin(); it != weights.end(); ++it)
totalWeight += it->second;
for (std::map<unsigned int, double>::iterator it = weights.begin(); it != weights.end(); ++it)
it->second = it->second / totalWeight;
m_weights[v] = weights;
}
*/
}
m_gridUpdated = false;
}
if ((m_weightsUpdated || (m_weights.size() != nPoints)) && (!dynBind))
{
MDataHandle weightsHandle = data.inputValue(m_pointWeights, &status);
MFnDoubleArrayData weightsData(weightsHandle.data(), &status);
if (status != MS::kSuccess)
{
MGlobal::displayError("Invalid weights data");
return MS::kFailure;
}
MDoubleArray wData = weightsData.array();
if (wData.length() == 0)
{
MGlobal::displayError("Invalid number of point weights");
return MS::kFailure;
}
unsigned long counter = 0;
int numPoints = wData[counter++];
if (nPoints != numPoints)
{
MGlobal::displayError("Invalid number of point weights stored, please compute again the harmonic weights");
return MS::kFailure;
}
m_weights.resize(numPoints);
unsigned int idxTmp = 0;
for (unsigned int ii = 0; ii < numPoints; ++ii)
{
unsigned int numWeights = wData[counter++];
for (unsigned int jj = 0; jj < numWeights; ++jj)
{
idxTmp = static_cast<unsigned int>(wData[counter++]);
m_weights[ii][idxTmp] = wData[counter++];
}
}
m_weightsUpdated = false;
}
if (m_weights.size() != nPoints)
{
MGlobal::displayError("invalid numebr of weights " + m_weights.size());
return MS::kFailure;
}
ParallelFor parallelData(m_weights, cagePoints, cageRefPoints, verts, envelop);
tbb::parallel_for(tbb::blocked_range<size_t>(0, nPoints), parallelData);
/*
for (int i = 0; i<nPoints; i++)
{
for (std::map<unsigned int, double>::iterator it = m_weights[i].begin(); it != m_weights[i].end(); ++it)
{
MVector diffPos = cagePoints[it->first] - cageRefPoints[it->first];
diffPos *= it->second;
verts[i] += diffPos;
}
}
*/
iter.setAllPositions(verts, MSpace::kWorld);
return status;
}