-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathMeshMaya.h
199 lines (181 loc) · 6.88 KB
/
MeshMaya.h
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
/**
* @file MeshMaya.h
* @brief Mesh manipulation functions for Maya
* @section LICENSE The MIT License
* @section requirements: Eigen library, Maya
* @version 0.10
* @date Aug. 2014
* @author Shizuo KAJI
*/
#pragma once
#include "tetrise.h"
#include <set>
using namespace Tetrise;
// get face list
int makeFaceTet(MDataBlock& data, MObject& input, MObject& inputGeom, unsigned int mIndex, const std::vector<Vector3d>& pts,
std::vector<int>& tetList, std::vector<Matrix4d>& tetMatrix, std::vector<double>& tetWeight){
// returns total number of pts including ghost ones
// read mesh data
int numPts = (int) pts.size();
MStatus status;
MArrayDataHandle hInput = data.outputArrayValue( input, &status );
CHECK_MSTATUS_AND_RETURN_IT( status );
status = hInput.jumpToElement( mIndex );
CHECK_MSTATUS_AND_RETURN_IT( status );
MObject oInputGeom = hInput.outputValue().child( inputGeom ).asMesh();
MFnMesh inputMesh(oInputGeom);
// face list
MIntArray count, triangles;
inputMesh.getTriangles( count, triangles );
std::vector<int> faceList(triangles.length());
for(int i=0;i<triangles.length();i++){
faceList[i]=triangles[i];
}
//
std::vector<vertex> vList;
std::vector<edge> eList;
makeTetList(TM_FACE, numPts, faceList, eList, vList, tetList);
makeTetMatrix(TM_FACE, pts, tetList, faceList, eList, vList, tetMatrix, tetWeight);
return numPts + (int)tetList.size()/4;
}
// make face list
void makeFaceList(MObject& mesh, std::vector<int>& faceList, std::vector<int>& faceCount,
bool isSymmetric=false){
if( isSymmetric ){
MItMeshPolygon iter(mesh);
MIntArray faceVertices;
faceList.clear();
for(int i=0; ! iter.isDone(); i++){
iter.getVertices(faceVertices);
int count=faceVertices.length();
if(count==3){
faceCount.push_back(1);
faceList.push_back(faceVertices[0]);
faceList.push_back(faceVertices[1]);
faceList.push_back(faceVertices[2]);
}else{
for(int j=0;j<count;j++){
faceCount.push_back(count);
faceList.push_back(faceVertices[j]);
faceList.push_back(faceVertices[(j+1) % count]);
faceList.push_back(faceVertices[(j+2) % count]);
}
}
iter.next();
}
}else{
MFnMesh fnMesh(mesh);
MIntArray count, triangles;
fnMesh.getTriangles( count, triangles );
faceCount.resize(triangles.length()/3,1);
faceList.resize(triangles.length());
for(int i=0;i<triangles.length();i++){
faceList[i]=triangles[i];
}
}
}
// vertex list
void makeVertexList(MObject& mesh, std::vector<vertex>& vertexList){
int numPts = MFnMesh(mesh).numVertices();
MItMeshPolygon iter(mesh);
MIntArray faceVertices;
vertexList.resize(numPts);
for(int i=0;i<numPts;i++){
vertexList[i].index = i;
vertexList[i].connectedTriangles.clear();
}
for( ; ! iter.isDone(); iter.next()){
iter.getVertices(faceVertices);
int count = (int) faceVertices.length();
for(int j=0;j<count;j++){
vertexList[faceVertices[j]].connectedTriangles.push_back(faceVertices[(j+1)%count]);
vertexList[faceVertices[j]].connectedTriangles.push_back(faceVertices[(j+count-1)%count]);
}
}
}
// get mesh data
int getMeshData(MDataBlock& data, MObject& input, MObject& inputGeom, unsigned int mIndex,
short tetMode, const std::vector<Vector3d>& pts, std::vector<int>& tetList,
std::vector<int>& faceList, std::vector<edge>& edgeList,
std::vector<vertex>& vertexList, std::vector<Matrix4d>& tetMat, std::vector<double>& tetWeight){
// returns total number of pts including ghost ones
// read mesh data
int numPts = (int) pts.size();
MStatus status;
MArrayDataHandle hInput = data.outputArrayValue( input, &status );
CHECK_MSTATUS_AND_RETURN_IT( status );
status = hInput.jumpToElement( mIndex );
CHECK_MSTATUS_AND_RETURN_IT( status );
MObject oInputGeom = hInput.outputValue().child( inputGeom ).asMesh();
std::vector<int> faceCount;
makeFaceList(oInputGeom, faceList, faceCount);
makeVertexList(oInputGeom, vertexList);
makeEdgeList(faceList, edgeList);
int dim=makeTetList(tetMode, numPts, faceList, edgeList, vertexList, tetList);
makeTetMatrix(tetMode, pts, tetList, faceList, edgeList, vertexList, tetMat, tetWeight);
return dim;
}
// visualise vertex assigned values
void visualise(MDataBlock& data, MObject& outputGeom, std::vector<double>& ptsColour){
// load target mesh output
MStatus status;
MArrayDataHandle outputArray = data.outputArrayValue(outputGeom , &status );
MDataHandle hOutput = outputArray.inputValue(&status);
MFnMesh outMesh(hOutput.data());
MColorArray Colours;
MIntArray Index;
// set vertex colour
for(int i=0;i<ptsColour.size();i++){
MColor colour(MColor::kHSV, 0.0, ptsColour[i], 1.0);
Colours.append( colour );
Index.append(i);
}
outMesh.setVertexColors(Colours, Index);
}
// read array of matrix attributes and convert them to Eigen matrices
void readMatrixArray(MArrayDataHandle& handle, std::vector<Matrix4d>& m){
int numPrb=handle.elementCount();
m.resize(numPrb);
MMatrix mat;
for(int i=0;i<numPrb;i++){
handle.jumpToArrayElement(i);
mat=handle.inputValue().asMatrix();
m[i] << mat(0,0), mat(0,1), mat(0,2), mat(0,3),
mat(1,0), mat(1,1), mat(1,2), mat(1,3),
mat(2,0), mat(2,1), mat(2,2), mat(2,3),
mat(3,0), mat(3,1), mat(3,2), mat(3,3);
}
}
// read array of vector attributes into Eigen vectors
void readVectorArray(MArrayDataHandle& handle, std::vector<Vector3d>& V){
int num=handle.elementCount();
V.resize(num);
MVector v;
for(int i=0;i<num;i++){
handle.jumpToArrayElement(i);
v=handle.inputValue().asVector();
V[i] << v(0), v(1), v(2);
}
}
////
void outputAttr(MDataBlock& data, MObject& attribute, std::vector<double>& values){
MStatus status;
MArrayDataBuilder builder(attribute, (int)values.size(), &status);
for(int i=0;i<values.size();i++){
MDataHandle outHandle = builder.addElement(i);
outHandle.set(values[i]);
}
MArrayDataHandle outputArray = data.outputArrayValue(attribute);
outputArray.set(builder);
}
////
void deleteAttr(MDataBlock& data, MObject& attribute, std::set<int>& indices){
MStatus status;
MArrayDataHandle outputArray = data.outputArrayValue(attribute);
MArrayDataBuilder builder=outputArray.builder();
std::set<int>::iterator iter;
for(iter = indices.begin(); iter != indices.end(); iter++){
builder.removeElement(*iter);
}
outputArray.set(builder);
}