-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOpenVDBToPolygonsNode.cc
372 lines (292 loc) · 12.4 KB
/
OpenVDBToPolygonsNode.cc
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
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2015 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// * Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////
/// @author Fredrik Salomonsson ([email protected])
#include "stdafx.h"
// #include "OpenVDBToPolygonsNode.h"
// #include "OpenVDBData.h"
// #include "OpenVDBUtil.h"
#include <openvdb/tools/VolumeToMesh.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFloatPointArray.h>
#include <maya/MPointArray.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MFnMeshData.h>
#include <maya/MFnStringData.h>
#include <maya/MFnPluginData.h>
#include <maya/MGlobal.h>
#include <maya/MFnMesh.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MArrayDataHandle.h>
#include <maya/MArrayDataBuilder.h>
#include <boost/scoped_array.hpp>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
namespace mvdb = openvdb_maya;
MTypeId OpenVDBToPolygonsNode::id(0x00108A59);
MObject OpenVDBToPolygonsNode::aVdbInput;
MObject OpenVDBToPolygonsNode::aIsovalue;
MObject OpenVDBToPolygonsNode::aAdaptivity;
MObject OpenVDBToPolygonsNode::aVdbAllGridNames;
MObject OpenVDBToPolygonsNode::aVdbSelectedGridNames;
MObject OpenVDBToPolygonsNode::aMeshOutput;
namespace {
// Internal utility methods
class VDBToMayaMesh
{
public:
MObject mesh;
VDBToMayaMesh(openvdb::tools::VolumeToMesh& mesher): mesh(), mMesher(&mesher) { }
template<typename GridType>
void operator()(typename GridType::ConstPtr grid)
{
// extract polygonal surface
(*mMesher)(*grid);
// transfer quads and triangles
MIntArray polygonCounts, polygonConnects;
{
const size_t polygonPoolListSize = mMesher->polygonPoolListSize();
boost::scoped_array<uint32_t> numQuadsPrefix(new uint32_t[polygonPoolListSize]);
boost::scoped_array<uint32_t> numTrianglesPrefix(new uint32_t[polygonPoolListSize]);
uint32_t numQuads = 0, numTriangles = 0;
openvdb::tools::PolygonPoolList& polygonPoolList = mMesher->polygonPoolList();
for (size_t n = 0; n < polygonPoolListSize; ++n) {
numQuadsPrefix[n] = numQuads;
numTrianglesPrefix[n] = numTriangles;
numQuads += uint32_t(polygonPoolList[n].numQuads());
numTriangles += uint32_t(polygonPoolList[n].numTriangles());
}
polygonCounts.setLength(numQuads + numTriangles);
polygonConnects.setLength(4*numQuads + 3*numTriangles);
tbb::parallel_for(tbb::blocked_range<size_t>(0, polygonPoolListSize),
FaceCopyOp(polygonConnects, polygonCounts,
numQuadsPrefix, numTrianglesPrefix, polygonPoolList));
polygonPoolList.reset(); // delete polygons
}
// transfer points
const size_t numPoints = mMesher->pointListSize();
MFloatPointArray vertexArray(numPoints);
tbb::parallel_for(tbb::blocked_range<size_t>(0, numPoints),
PointCopyOp(vertexArray, mMesher->pointList()));
mMesher->pointList().reset(); // delete points
mesh = MFnMeshData().create();
MFnMesh().create(vertexArray.length(), polygonCounts.length(),
vertexArray, polygonCounts, polygonConnects, mesh);
}
private:
openvdb::tools::VolumeToMesh * const mMesher;
struct PointCopyOp;
struct FaceCopyOp;
}; // VDBToMayaMesh
struct VDBToMayaMesh::PointCopyOp
{
PointCopyOp(MFloatPointArray& mayaPoints, const openvdb::tools::PointList& vdbPoints)
: mMayaPoints(&mayaPoints) , mVdbPoints(&vdbPoints) { }
void operator()(const tbb::blocked_range<size_t>& range) const {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
const openvdb::Vec3s& p_vdb = (*mVdbPoints)[n];
MFloatPoint& p_maya = (*mMayaPoints)[n];
p_maya[0] = p_vdb[0];
p_maya[1] = p_vdb[1];
p_maya[2] = p_vdb[2];
}
}
private:
MFloatPointArray * const mMayaPoints;
openvdb::tools::PointList const * const mVdbPoints;
};
struct VDBToMayaMesh::FaceCopyOp
{
typedef boost::scoped_array<uint32_t> UInt32Array;
FaceCopyOp(MIntArray& indices, MIntArray& polyCount,
const UInt32Array& numQuadsPrefix, const UInt32Array& numTrianglesPrefix,
const openvdb::tools::PolygonPoolList& polygonPoolList)
: mIndices(&indices)
, mPolyCount(&polyCount)
, mNumQuadsPrefix(numQuadsPrefix.get())
, mNumTrianglesPrefix(numTrianglesPrefix.get())
, mPolygonPoolList(&polygonPoolList)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
const uint32_t numQuads = mNumQuadsPrefix[range.begin()];
const uint32_t numTriangles = mNumTrianglesPrefix[range.begin()];
uint32_t face = numQuads + numTriangles;
uint32_t vertex = 4*numQuads + 3*numTriangles;
MIntArray& indices = *mIndices;
MIntArray& polyCount = *mPolyCount;
// Setup the polygon count and polygon indices
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
const openvdb::tools::PolygonPool& polygons = (*mPolygonPoolList)[n];
// Add all quads in the polygon pool
for (size_t i = 0, I = polygons.numQuads(); i < I; ++i) {
polyCount[face++] = 4;
const openvdb::Vec4I& quad = polygons.quad(i);
indices[vertex++] = quad[0];
indices[vertex++] = quad[1];
indices[vertex++] = quad[2];
indices[vertex++] = quad[3];
}
// Add all triangles in the polygon pool
for (size_t i = 0, I = polygons.numTriangles(); i < I; ++i) {
polyCount[face++] = 3;
const openvdb::Vec3I& triangle = polygons.triangle(i);
indices[vertex++] = triangle[0];
indices[vertex++] = triangle[1];
indices[vertex++] = triangle[2];
}
}
}
private:
MIntArray * const mIndices;
MIntArray * const mPolyCount;
uint32_t const * const mNumQuadsPrefix;
uint32_t const * const mNumTrianglesPrefix;
openvdb::tools::PolygonPoolList const * const mPolygonPoolList;
};
} // unnamed namespace
////////////////////////////////////////
void* OpenVDBToPolygonsNode::creator()
{
return new OpenVDBToPolygonsNode();
}
MStatus OpenVDBToPolygonsNode::initialize()
{
MStatus stat;
MFnNumericAttribute nAttr;
MFnTypedAttribute tAttr;
// Setup input / output attributes
aVdbInput = tAttr.create( "vdbInput", "input", OpenVDBData::id, MObject::kNullObj,
&stat );
if (stat != MS::kSuccess) return stat;
tAttr.setReadable(false);
stat = addAttribute(aVdbInput);
if (stat != MS::kSuccess) return stat;
aMeshOutput = tAttr.create("meshOutput", "mesh", MFnData::kMesh, MObject::kNullObj, &stat);
if (stat != MS::kSuccess) return stat;
tAttr.setReadable(true);
tAttr.setWritable(false);
tAttr.setStorable(false);
tAttr.setArray(true);
tAttr.setUsesArrayDataBuilder(true);
stat = addAttribute(aMeshOutput);
if (stat != MS::kSuccess) return stat;
// Setup UI attributes
aIsovalue = nAttr.create("isovalue", "iso", MFnNumericData::kFloat);
nAttr.setDefault(0.0);
nAttr.setSoftMin(-1.0);
nAttr.setSoftMax( 1.0);
nAttr.setConnectable(false);
stat = addAttribute(aIsovalue);
if (stat != MS::kSuccess) return stat;
aAdaptivity = nAttr.create("adaptivity", "adapt", MFnNumericData::kFloat);
nAttr.setDefault(0.0);
nAttr.setMin(0.0);
nAttr.setMax( 1.0);
nAttr.setConnectable(false);
stat = addAttribute(aAdaptivity);
if (stat != MS::kSuccess) return stat;
MFnStringData fnStringData;
MObject defaultStringData = fnStringData.create("");
aVdbAllGridNames = tAttr.create("vdbAllGridNames", "allgrids",
MFnData::kString, defaultStringData, &stat);
if (stat != MS::kSuccess) return stat;
tAttr.setConnectable(false);
tAttr.setWritable(false);
tAttr.setReadable(false);
tAttr.setHidden(true);
stat = addAttribute(aVdbAllGridNames);
if (stat != MS::kSuccess) return stat;
aVdbSelectedGridNames = tAttr.create("vdbSelectedGridNames", "selectedgrids",
MFnData::kString, defaultStringData, &stat);
if (stat != MS::kSuccess) return stat;
tAttr.setConnectable(false);
tAttr.setWritable(false);
tAttr.setReadable(false);
tAttr.setHidden(true);
stat = addAttribute(aVdbSelectedGridNames);
if (stat != MS::kSuccess) return stat;
// Setup dependencies
attributeAffects(aVdbInput, aVdbAllGridNames);
attributeAffects(aVdbInput, aMeshOutput);
attributeAffects(aIsovalue, aMeshOutput);
attributeAffects(aAdaptivity, aMeshOutput);
attributeAffects(aVdbSelectedGridNames, aMeshOutput);
return MS::kSuccess;
}
////////////////////////////////////////
MStatus OpenVDBToPolygonsNode::compute(const MPlug& plug, MDataBlock& data)
{
MStatus status;
const OpenVDBData* inputVdb = mvdb::getInputVDB(aVdbInput, data);
if (!inputVdb) return MS::kFailure;
if (plug == aVdbAllGridNames) {
MString names = mvdb::getGridNames(*inputVdb).c_str();
MDataHandle outHandle = data.outputValue(aVdbAllGridNames);
outHandle.set(names);
return data.setClean(plug);
}
// Get selected grids
MDataHandle selectionHandle = data.inputValue(aVdbSelectedGridNames, &status);
if (status != MS::kSuccess) return status;
std::string names = selectionHandle.asString().asChar();
std::vector<openvdb::GridBase::ConstPtr> grids;
mvdb::getGrids(grids, *inputVdb, names);
if (grids.empty()) {
return MS::kUnknownParameter;
}
// Convert Openvdbs to meshes
if (plug == aMeshOutput) {
MDataHandle isoHandle = data.inputValue(aIsovalue, &status);
if (status != MS::kSuccess) return status;
MDataHandle adaptHandle = data.inputValue(aAdaptivity, &status);
if (status != MS::kSuccess) return status;
openvdb::tools::VolumeToMesh mesher(isoHandle.asFloat(), adaptHandle.asFloat());
MArrayDataHandle outArrayHandle = data.outputArrayValue(aMeshOutput, &status);
if (status != MS::kSuccess) return status;
MArrayDataBuilder builder(aMeshOutput, grids.size(), &status);
for (size_t n = 0, N = grids.size(); n < N; ++n) {
VDBToMayaMesh converter(mesher);
if (mvdb::processTypedScalarGrid(grids[n], converter)) {
MDataHandle outHandle = builder.addElement(n);
outHandle.set(converter.mesh);
}
}
status = outArrayHandle.set(builder);
if (status != MS::kSuccess) return status;
status = outArrayHandle.setAllClean();
if (status != MS::kSuccess) return status;
} else {
return MS::kUnknownParameter;
}
return data.setClean(plug);
}
// Copyright (c) 2012-2015 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )