-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapiMeshData.cpp
More file actions
441 lines (379 loc) · 9.49 KB
/
apiMeshData.cpp
File metadata and controls
441 lines (379 loc) · 9.49 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//-
// ==========================================================================
// Copyright 2015 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk
// license agreement provided at the time of installation or download,
// or which otherwise accompanies this software in either electronic
// or hard copy form.
// ==========================================================================
//+
///////////////////////////////////////////////////////////////////////////////
//
// apiMeshData.cpp
//
///////////////////////////////////////////////////////////////////////////////
#include <maya/MIOStream.h>
#include "apiMeshData.h"
#include "apiMeshIterator.h"
#include <api_macros.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MArgList.h>
//////////////////////////////////////////////////////////////////////
// Ascii file IO defines
//
#define kDblQteChar "\""
#define kSpaceChar " "
#define kWrapString "\n\t\t"
#define kVertexKeyword "v"
#define kNormalKeyword "vn"
#define kTextureKeyword "vt"
#define kFaceKeyword "face"
#define kUVKeyword "uv"
//////////////////////////////////////////////////////////////////////
const MTypeId apiMeshData::id( 0x80777 );
const MString apiMeshData::typeName( "apiMeshData" );
apiMeshData::apiMeshData() : fGeometry( NULL )
{
fGeometry = new apiMeshGeom;
}
apiMeshData::~apiMeshData()
{
if ( NULL != fGeometry ) {
delete fGeometry;
fGeometry = NULL;
}
}
/* override */
MStatus apiMeshData::readASCII( const MArgList& argList, unsigned& index )
//
// Description
// ASCII file input method.
//
{
if ( ! readVerticesASCII(argList,index) ) {
return MS::kFailure;
}
else if ( ! readNormalsASCII(argList,++index) ) {
return MS::kFailure;
}
else if ( ! readFacesASCII(argList,++index) ) {
return MS::kFailure;
}
else if ( ! readUVASCII(argList,index) ) {
return MS::kFailure;
}
return MS::kSuccess;
}
/* override */
MStatus apiMeshData::readBinary( istream& /*in*/, unsigned /*length*/ )
//
// Description
// NOT IMPLEMENTED
//
{
return MS::kSuccess;
}
/* override */
MStatus apiMeshData::writeASCII( ostream& out )
//
// Description
// ASCII file output method.
//
{
if ( ! writeVerticesASCII(out) ) {
return MS::kFailure;
}
else if ( ! writeNormalsASCII(out) ) {
return MS::kFailure;
}
else if ( ! writeFacesASCII(out) ) {
return MS::kFailure;
}
writeUVASCII(out);
return MS::kSuccess;
}
/* override */
MStatus apiMeshData::writeBinary( ostream& /*out*/ )
//
// Description
// NOT IMPLEMENTED
//
{
return MS::kSuccess;
}
/* override */
void apiMeshData::copy ( const MPxData& other )
{
*fGeometry = *(((const apiMeshData &)other).fGeometry);
}
/* override */
MTypeId apiMeshData::typeId() const
//
// Description
// Binary tag used to identify this kind of data
//
{
return apiMeshData::id;
}
/* override */
MString apiMeshData::name() const
//
// Description
// String name used to identify this kind of data
//
{
return apiMeshData::typeName;
}
void * apiMeshData::creator()
{
return new apiMeshData;
}
/* override */
MPxGeometryIterator* apiMeshData::iterator( MObjectArray & componentList,
MObject & component,
bool useComponents)
//
// Description
//
{
apiMeshGeomIterator * result = NULL;
if ( useComponents ) {
result = new apiMeshGeomIterator( fGeometry, componentList );
}
else {
result = new apiMeshGeomIterator( fGeometry, component );
}
return result;
}
/* override */
MPxGeometryIterator* apiMeshData::iterator( MObjectArray & componentList,
MObject & component,
bool useComponents,
bool /*world*/) const
//
// Description
//
{
apiMeshGeomIterator * result = NULL;
if ( useComponents ) {
result = new apiMeshGeomIterator( fGeometry, componentList );
}
else {
result = new apiMeshGeomIterator( fGeometry, component );
}
return result;
}
/* override */
bool apiMeshData::updateCompleteVertexGroup( MObject & component ) const
//
// Description
// Make sure complete vertex group data is up-to-date.
// Returns true if the component was updated, false if it was already ok.
//
// This is used by deformers when deforming the "whole" object and
// not just selected components.
//
{
MStatus stat;
MFnSingleIndexedComponent fnComponent( component, &stat );
// Make sure there is non-null geometry and that the component
// is "complete". A "complete" component represents every
// vertex in the shape.
//
if ( stat && (NULL != fGeometry) && (fnComponent.isComplete()) ) {
int maxVerts ;
fnComponent.getCompleteData( maxVerts );
int numVertices = fGeometry->vertices.length();
if ( (numVertices > 0) && (maxVerts != numVertices) ) {
// Set the component to be complete, i.e. the elements in
// the component will be [0:numVertices-1]
//
fnComponent.setCompleteData( numVertices );
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////////
MStatus apiMeshData::writeUVASCII( ostream &out )
{
int uvCount = fGeometry->uvcoords.uvcount();
int faceVertexCount = fGeometry->uvcoords.faceVertexIndex.length();
if ( uvCount > 0 ) {
out << "\n";
out << kWrapString;
out << kDblQteChar << kUVKeyword << kDblQteChar
<< kSpaceChar << uvCount << kSpaceChar << faceVertexCount;
int i;
float u, v;
for ( i = 0; i < uvCount; i ++ ) {
fGeometry->uvcoords.getUV( i, u, v );
out << kWrapString;
out << u << kSpaceChar;
out << v << kSpaceChar;
}
const MIntArray &fvl = fGeometry->uvcoords.faceVertexIndex;
for ( i = 0; i < faceVertexCount; i ++ ) {
out << kWrapString;
out << fvl[i] << kSpaceChar;
}
}
return MS::kSuccess;
}
MStatus apiMeshData::writeVerticesASCII( ostream& out )
{
MPoint vertex;
int vertexCount = fGeometry->vertices.length();
out << "\n";
out << kWrapString;
out << kDblQteChar << kVertexKeyword << kDblQteChar
<< kSpaceChar << vertexCount;
for ( int i=0; i<vertexCount; i++ ) {
vertex = fGeometry->vertices[i];
out << kWrapString;
out << vertex[0] << kSpaceChar;
out << vertex[1] << kSpaceChar;
out << vertex[2];
}
return MS::kSuccess;
}
MStatus apiMeshData::writeNormalsASCII( ostream& out )
{
MVector normal;
int normalCount = fGeometry->normals.length();
out << "\n";
out << kWrapString;
out << kDblQteChar << kNormalKeyword << kDblQteChar
<< kSpaceChar << normalCount;
for ( int i=0; i<normalCount; i++ ) {
normal = fGeometry->normals[i];
out << kWrapString;
out << normal[0] << kSpaceChar;
out << normal[1] << kSpaceChar;
out << normal[2];
}
return MS::kSuccess;
}
MStatus apiMeshData::writeFacesASCII( ostream& out )
{
int numFaces = fGeometry->face_counts.length();
int vid = 0;
for ( int f=0; f<numFaces; f++ )
{
int faceVertexCount = fGeometry->face_counts[f];
out << "\n";
out << kWrapString;
out << kDblQteChar << kFaceKeyword << kDblQteChar
<< kSpaceChar << faceVertexCount;
out << kWrapString;
for ( int v=0; v<faceVertexCount; v++ )
{
out << fGeometry->face_connects[vid] << kSpaceChar;
vid++;
}
}
return MS::kSuccess;
}
MStatus apiMeshData::readVerticesASCII( const MArgList& argList,
unsigned& index )
{
MStatus result;
MString geomStr;
MPoint vertex;
int vertexCount = 0;
result = argList.get( index, geomStr );
if ( result && (geomStr == kVertexKeyword) ) {
result = argList.get( ++index, vertexCount );
for ( int i=0; i<vertexCount; i++ )
{
if ( argList.get( ++index, vertex ) ) {
fGeometry->vertices.append( vertex );
}
else {
result = MS::kFailure;
}
}
}
return result;
}
MStatus apiMeshData::readNormalsASCII( const MArgList& argList,
unsigned& index )
{
MStatus result;
MString geomStr;
MPoint normal;
int normalCount = 0;
result = argList.get( index, geomStr );
if ( result && (geomStr == kNormalKeyword) ) {
result = argList.get( ++index, normalCount );
for ( int i=0; i<normalCount; i++ )
{
if ( argList.get( ++index, normal ) ) {
fGeometry->normals.append( normal );
}
else {
result = MS::kFailure;
}
}
}
return result;
}
MStatus apiMeshData::readUVASCII( const MArgList &argList,
unsigned &index )
{
MStatus result = MS::kSuccess;
MString uvStr;
double u, v;
int fvi;
int faceVertexListCount = 0;
int uvCount;
fGeometry->uvcoords.reset();
if ( argList.get(index,uvStr) && (uvStr == kUVKeyword) ) {
result = argList.get( ++index, uvCount );
if ( result ) {
result = argList.get( ++index, faceVertexListCount );
}
int i;
for ( i = 0; i < uvCount && result; i ++ ) {
if ( argList.get( ++index, u ) && argList.get( ++index, v ) ) {
fGeometry->uvcoords.append_uv( (float)u, (float)v );
} else {
result = MS::kFailure;
}
}
for ( i = 0; i < faceVertexListCount && result; i++ ) {
if ( argList.get( ++index, fvi ) ) {
fGeometry->uvcoords.faceVertexIndex.append( fvi );
} else {
result = MS::kFailure;
}
}
}
return result;
}
MStatus apiMeshData::readFacesASCII( const MArgList& argList,
unsigned& index )
{
MStatus result = MS::kSuccess;
MString geomStr;
int faceCount = 0;
int vid;
while( argList.get(index,geomStr) && (geomStr == kFaceKeyword) )
{
result = argList.get( ++index, faceCount );
fGeometry->face_counts.append( faceCount );
for ( int i=0; i<faceCount; i++ )
{
if ( argList.get( ++index, vid ) ) {
fGeometry->face_connects.append( vid );
}
else {
result = MS::kFailure;
}
}
index++;
}
fGeometry->faceCount = fGeometry->face_counts.length();
return result;
}