/
finalproject.cpp
355 lines (280 loc) · 11.1 KB
/
finalproject.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
//
// File: finalproject.cpp
//
// Authors: Eric Dazet and Arnav Muruildhar
//
// Description:
// Magnet mesh plugin source code.
//
//
#include <maya/MIOStream.h>
#include <maya/MPxDeformerNode.h>
#include <maya/MItGeometry.h>
#include <maya/MFnPlugin.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/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnMeshData.h>
#include <maya/MThreadUtils.h>
#include "finalproject.h"
#include "float.h"
#include "math.h"
// Macros
//
#define MCheckStatus(status,message) \
if( MStatus::kSuccess != status ) { \
cerr << message << "\n"; \
return status; \
}
class finalproject : public MPxDeformerNode
{
public:
finalproject();
virtual ~finalproject();
static void* creator();
static MStatus initialize();
// deformation function
//
virtual MStatus compute(const MPlug& plug, MDataBlock& dataBlock);
public:
// local node attributes
static MTypeId id;
static MObject deformingMesh;
static MObject transX; //attribute to store x-value of object center after moved by the magnet
static MObject transY; //attribute to store y-value of object center after moved by the magnet
static MObject transZ; //attribute to store z-value of object center after moved by the magnet
static MObject offload; //attribute to toggle Xeon Phi offload
static MObject tesla; //attribute representing magnetic strength value
static MObject positivelycharged; //attribute representing polarity of the object
private:
};
MTypeId finalproject::id( 0x8104D );
MObject finalproject::deformingMesh;
MObject finalproject::transX;
MObject finalproject::transY;
MObject finalproject::transZ;
MObject finalproject::offload;
MObject finalproject::tesla;
MObject finalproject::positivelycharged;
finalproject::finalproject() {}
finalproject::~finalproject() {}
void* finalproject::creator()
{
return new finalproject();
}
MStatus finalproject::initialize()
{
// local attribute initialization
MStatus status;
MFnTypedAttribute mAttr;
deformingMesh=mAttr.create( "deformingMesh", "dm", MFnMeshData::kMesh);
MFnNumericAttribute nAttrt;
transX = nAttrt.create( "transX", "tx", MFnNumericData::kDouble);
nAttrt.setStorable(true);
status = addAttribute( transX );
MCheckStatus(status, "ERROR in addAttribute\n");
status = attributeAffects( transX, outputGeom );
MCheckStatus(status, "ERROR in attributeAffects\n");
transY = nAttrt.create( "transY", "ty", MFnNumericData::kDouble);
nAttrt.setStorable(true);
status = addAttribute( transY );
MCheckStatus(status, "ERROR in addAttribute\n");
status = attributeAffects( transY, outputGeom );
MCheckStatus(status, "ERROR in attributeAffects\n");
transZ = nAttrt.create( "transZ", "tz", MFnNumericData::kDouble);
nAttrt.setStorable(true);
status = addAttribute( transZ );
MCheckStatus(status, "ERROR in addAttribute\n");
status = attributeAffects( transZ, outputGeom );
MCheckStatus(status, "ERROR in attributeAffects\n");
tesla = nAttrt.create( "tesla", "tes", MFnNumericData::kDouble);
nAttrt.setStorable(true);
nAttrt.setKeyable(true);
nAttrt.setDefault(0.0);
nAttrt.setMin(0.0);
nAttrt.setMax(300.0);
status = addAttribute( tesla );
MCheckStatus(status, "ERROR in addAttribute\n");
status = attributeAffects( tesla, outputGeom );
MCheckStatus(status, "ERROR in attributeAffects\n");
positivelycharged = nAttrt.create( "positivelycharged", "pc", MFnNumericData::kBoolean);
nAttrt.setStorable(true);
nAttrt.setKeyable(true);
nAttrt.setDefault(true);
status = addAttribute( positivelycharged );
MCheckStatus(status, "ERROR in addAttribute\n");
MCheckStatus(status, "ERROR in attributeAffects\n");
MFnNumericAttribute nAttrO;
offload=nAttrO.create( "offload", "ol", MFnNumericData::kBoolean);
nAttrO.setStorable(true);
nAttrO.setDefault(false);
nAttrO.setKeyable(true);
// deformation attributes
status = addAttribute( deformingMesh );
MCheckStatus(status, "ERROR in addAttribute\n");
status = attributeAffects( deformingMesh, outputGeom );
MCheckStatus(status, "ERROR in attributeAffects\n");
status = addAttribute( offload );
MCheckStatus(status, "ERROR in addAttribute\n");
status = attributeAffects( offload, outputGeom );
MCheckStatus(status, "ERROR in attributeAffects\n");
return MStatus::kSuccess;
}
MStatus finalproject::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;
}
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 deforming mesh
MDataHandle deformData = data.inputValue(deformingMesh, &status);
MCheckStatus(status, "ERROR getting deforming mesh\n");
if (deformData.type() != MFnData::kMesh) {
printf("Incorrect deformer geometry type %d\n", deformData.type());
return MStatus::kFailure;
}
MDataHandle offloadData = data.inputValue(offload, &status);
//gathers world space positions of the object and the magnet
MObject dSurf = deformData.asMeshTransformed();
MObject iSurf = inputData.asMeshTransformed();
MFnMesh fnDeformingMesh, fnInputMesh;
fnDeformingMesh.setObject( dSurf ) ;
fnInputMesh.setObject( iSurf ) ;
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 objVerts;
iter.allPositions(objVerts);
int objNumPoints = objVerts.length();
MPointArray magVerts, tempverts;
fnDeformingMesh.getPoints(magVerts);
fnInputMesh.getPoints(tempverts);
int magNumPoints = magVerts.length();
double min = DBL_MAX, max = -DBL_MAX;
//finds min and max z-coordinate values to determine middle point (choice of z-axis was ours)
for (int i = 0; i < magNumPoints; i++) {
min = magVerts[i].z < min ? magVerts[i].z : min;
max = magVerts[i].z > max ? magVerts[i].z : max;
}
double middle = (min + max) / 2;
double polarity[magNumPoints];
//assigns polarity based on middle point of mesh
for (int i = 0; i < magNumPoints; i++) {
polarity[i] = magVerts[i].z > middle ? max / magVerts[i].z : -min / magVerts[i].z;
}
double* objdVerts = (double *)malloc(sizeof(double) * objNumPoints * 3);
double* magdVerts = (double *)malloc(sizeof(double) * magNumPoints * 3);
//creates handles to use attribute data
MDataHandle vecX = data.inputValue(transX, &status);
MDataHandle vecY = data.inputValue(transY, &status);
MDataHandle vecZ = data.inputValue(transZ, &status);
//gathers previously stored coordinates of the center of the object
double moveX = vecX.asFloat();
double moveY = vecY.asFloat();
double moveZ = vecZ.asFloat();
//translates object based on the position stored in the attribute values
for (int i=0; i<objNumPoints; i++) {
objdVerts[i * 3] = tempverts[i].x + moveX;
objdVerts[i * 3 + 1] = tempverts[i].y + moveY;
objdVerts[i * 3 + 2] = tempverts[i].z + moveZ;
}
for (int i=0; i<magNumPoints; i++) {
magdVerts[i * 3] = magVerts[i].x;
magdVerts[i * 3 + 1] = magVerts[i].y;
magdVerts[i * 3 + 2] = magVerts[i].z;
}
double teslaData = data.inputValue(tesla, &status).asDouble();
MDataHandle posiData = data.inputValue(positivelycharged, &status);
double pivot[6] = {DBL_MAX, -DBL_MAX, DBL_MAX, -DBL_MAX, DBL_MAX, -DBL_MAX};
//finds the pivot point of the object in world space prior to being affected by the magnet
for (int i = 0; i < tempverts.length(); i++) {
pivot[0] = tempverts[i].x < pivot[0] ? tempverts[i].x : pivot[0];
pivot[1] = tempverts[i].x > pivot[1] ? tempverts[i].x : pivot[1];
pivot[2] = tempverts[i].y < pivot[2] ? tempverts[i].y : pivot[2];
pivot[3] = tempverts[i].y > pivot[3] ? tempverts[i].y : pivot[3];
pivot[4] = tempverts[i].z < pivot[4] ? tempverts[i].z : pivot[4];
pivot[5] = tempverts[i].z > pivot[5] ? tempverts[i].z : pivot[5];
}
MTimer timer; timer.beginTimer();
//main function call
magnetForce(magNumPoints, objNumPoints, teslaData, magdVerts,
objdVerts, polarity, posiData.asBool(), offloadData.asBool());
timer.endTimer(); printf("Runtime for threaded loop %f\n", timer.elapsedTime());
for (int i=0; i<objNumPoints; i++) {
objVerts[i].x = objdVerts[i * 3 + 0];
objVerts[i].y = objdVerts[i * 3 + 1];
objVerts[i].z = objdVerts[i * 3 + 2];
}
//finds the pivot point of object in world space after being affected by the magnet
double objCenter[6] = {DBL_MAX, -DBL_MAX, DBL_MAX, -DBL_MAX, DBL_MAX, -DBL_MAX};
for (int i = 0; i < tempverts.length(); i++) {
objCenter[0] = objVerts[i].x < objCenter[0] ? objVerts[i].x : objCenter[0];
objCenter[1] = objVerts[i].x > objCenter[1] ? objVerts[i].x : objCenter[1];
objCenter[2] = objVerts[i].y < objCenter[2] ? objVerts[i].y : objCenter[2];
objCenter[3] = objVerts[i].y > objCenter[3] ? objVerts[i].y : objCenter[3];
objCenter[4] = objVerts[i].z < objCenter[4] ? objVerts[i].z : objCenter[4];
objCenter[5] = objVerts[i].z > objCenter[5] ? objVerts[i].z : objCenter[5];
}
//creates vector based on the two calculated pivot points
moveX = (objCenter[0] + objCenter[1]) / 2 - (pivot[0] + pivot[1]) / 2;
moveY = (objCenter[2] + objCenter[3]) / 2 - (pivot[2] + pivot[3]) / 2;
moveZ = (objCenter[4] + objCenter[5]) / 2 - (pivot[4] + pivot[5]) / 2;
//stores pivot vector for next computation
if (teslaData) {
vecX.setFloat(moveX);
vecY.setFloat(moveY);
vecZ.setFloat(moveZ);
}
// write values back onto output using fast set method on iterator
iter.setAllPositions(objVerts, MSpace::kWorld);
free(objdVerts);
free(magdVerts);
return status;
}
// standard initialization procedures
//
MStatus initializePlugin( MObject obj )
{
MStatus result;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "1.0", "Any");
result = plugin.registerNode( "finalproject", finalproject::id, finalproject::creator,
finalproject::initialize, MPxNode::kDeformerNode );
return result;
}
MStatus uninitializePlugin( MObject obj)
{
MStatus result;
MFnPlugin plugin( obj );
result = plugin.deregisterNode( finalproject::id );
return result;
}