forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkBYUReader.cxx
393 lines (344 loc) · 9.66 KB
/
vtkBYUReader.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkBYUReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkBYUReader.h"
#include "vtkCell.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
vtkStandardNewMacro(vtkBYUReader);
vtkBYUReader::vtkBYUReader()
{
this->GeometryFileName = NULL;
this->DisplacementFileName = NULL;
this->ScalarFileName = NULL;
this->TextureFileName = NULL;
this->ReadDisplacement = 1;
this->ReadScalar = 1;
this->ReadTexture = 1;
this->PartNumber = 0;
this->SetNumberOfInputPorts(0);
}
vtkBYUReader::~vtkBYUReader()
{
if ( this->GeometryFileName )
{
delete [] this->GeometryFileName;
}
if ( this->DisplacementFileName )
{
delete [] this->DisplacementFileName;
}
if ( this->ScalarFileName )
{
delete [] this->ScalarFileName;
}
if ( this->TextureFileName )
{
delete [] this->TextureFileName;
}
}
int vtkBYUReader::CanReadFile(const char *filename)
{
int result;
FILE *fp = fopen(filename, "r");
if (fp == NULL) return 0;
int numParts, numPts, numPolys, numEdges;
result = fscanf(fp, "%d %d %d %d", &numParts, &numPts, &numPolys, &numEdges);
if ((result < 4) || (numParts < 1) || (numPts < 1) || (numPolys < 1))
{
fclose(fp);
return 0;
}
for (int part = 0; part < numParts; part++)
{
int partStart, partEnd;
result = fscanf(fp, "%d %d", &partStart, &partEnd);
if ( (result < 2)
|| (partStart < 1) || (partStart > numPolys)
|| (partEnd < 1) || (partEnd > numPolys)
|| (partStart >= partEnd) )
{
fclose(fp);
return 0;
}
}
fclose(fp);
return 1;
}
int vtkBYUReader::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
FILE *geomFp;
int numPts;
if (this->GeometryFileName == NULL || this->GeometryFileName == '\0')
{
vtkErrorMacro(<< "No GeometryFileName specified!");
return 0;
}
if ((geomFp = fopen(this->GeometryFileName, "r")) == NULL)
{
vtkErrorMacro(<< "Geometry file: " << this->GeometryFileName << " not found");
return 0;
}
else
{
this->ReadGeometryFile(geomFp,numPts,outInfo);
fclose(geomFp);
}
this->ReadDisplacementFile(numPts, outInfo);
this->ReadScalarFile(numPts, outInfo);
this->ReadTextureFile(numPts, outInfo);
this->UpdateProgress(1.0);
return 1;
}
void vtkBYUReader::ReadGeometryFile(FILE *geomFile, int &numPts,
vtkInformation *outInfo)
{
int numParts, numPolys, numEdges;
int partStart, partEnd;
int i;
vtkPoints *newPts;
vtkCellArray *newPolys;
float x[3];
vtkIdList *pts;
int polyId, pt;
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
pts = vtkIdList::New();
pts->Allocate(VTK_CELL_SIZE);
//
// Read header (not using fixed format! - potential problem in some files.)
//
fscanf (geomFile, "%d %d %d %d", &numParts, &numPts, &numPolys, &numEdges);
if ( this->PartNumber > numParts )
{
vtkWarningMacro(<<"Specified part number > number of parts");
this->PartNumber = 0;
}
if ( this->PartNumber > 0 ) // read just part specified
{
vtkDebugMacro(<<"Reading part number: " << this->PartNumber);
for (i=0; i < (this->PartNumber-1); i++)
{
fscanf (geomFile, "%*d %*d");
}
fscanf (geomFile, "%d %d", &partStart, &partEnd);
for (i=this->PartNumber; i < numParts; i++)
{
fscanf (geomFile, "%*d %*d");
}
}
else // read all parts
{
vtkDebugMacro(<<"Reading all parts.");
for (i=0; i < numParts; i++)
{
fscanf (geomFile, "%*d %*d");
}
partStart = 1;
partEnd = VTK_LARGE_INTEGER;
}
if ( numParts < 1 || numPts < 1 || numPolys < 1 )
{
vtkErrorMacro(<<"Bad MOVIE.BYU file");
pts->Delete();
return;
}
//
// Allocate data objects
//
newPts = vtkPoints::New();
newPts->Allocate(numPts);
newPolys = vtkCellArray::New();
newPolys->Allocate(numPolys+numEdges);
//
// Read data
//
// read point coordinates
for (i=0; i<numPts; i++)
{
fscanf(geomFile, "%e %e %e", x, x+1, x+2);
newPts->InsertPoint(i,x);
}
this->UpdateProgress(0.333);
// read poly data. Have to fix 1-offset. Only reading part number specified.
for ( polyId=1; polyId <= numPolys; polyId++ )
{
// read this polygon
for ( pts->Reset(); fscanf(geomFile, "%d", &pt) && pt > 0; )
{
pts->InsertNextId(pt-1);//convert to vtk 0-offset
}
pts->InsertNextId(-(pt+1));
// Insert polygon (if in selected part)
if ( partStart <= polyId && polyId <= partEnd )
{
newPolys->InsertNextCell(pts);
}
}
this->UpdateProgress(0.6667);
vtkDebugMacro(<<"Reading:" << numPts << " points, "
<< numPolys << " polygons.");
output->SetPoints(newPts);
newPts->Delete();
output->SetPolys(newPolys);
newPolys->Delete();
pts->Delete();
}
void vtkBYUReader::ReadDisplacementFile(int numPts, vtkInformation *outInfo)
{
FILE *dispFp;
int i;
float v[3];
vtkFloatArray *newVectors;
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if ( this->ReadDisplacement && this->DisplacementFileName )
{
if ( !(dispFp = fopen(this->DisplacementFileName, "r")) )
{
vtkErrorMacro (<<"Couldn't open displacement file");
return;
}
}
else
{
return;
}
//
// Allocate and read data
//
newVectors = vtkFloatArray::New();
newVectors->SetNumberOfComponents(3);
newVectors->SetNumberOfTuples(numPts);
for (i=0; i<numPts; i++)
{
fscanf(dispFp, "%e %e %e", v, v+1, v+2);
newVectors->SetTuple(i,v);
}
fclose(dispFp);
vtkDebugMacro(<<"Read " << numPts << " displacements");
output->GetPointData()->SetVectors(newVectors);
newVectors->Delete();
}
void vtkBYUReader::ReadScalarFile(int numPts, vtkInformation *outInfo)
{
FILE *scalarFp;
int i;
float s;
vtkFloatArray *newScalars;
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if ( this->ReadScalar && this->ScalarFileName )
{
if ( !(scalarFp = fopen(this->ScalarFileName, "r")) )
{
vtkErrorMacro (<<"Couldn't open scalar file");
return;
}
}
else
{
return;
}
//
// Allocate and read data
//
newScalars = vtkFloatArray::New();
newScalars->SetNumberOfTuples(numPts);
for (i=0; i<numPts; i++)
{
fscanf(scalarFp, "%e", &s);
newScalars->SetTuple(i,&s);
}
fclose(scalarFp);
vtkDebugMacro(<<"Read " << numPts << " scalars");
output->GetPointData()->SetScalars(newScalars);
newScalars->Delete();
}
void vtkBYUReader::ReadTextureFile(int numPts, vtkInformation *outInfo)
{
FILE *textureFp;
int i;
float t[2];
vtkFloatArray *newTCoords;
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
if ( this->ReadTexture && this->TextureFileName )
{
if ( !(textureFp = fopen(this->TextureFileName, "r")) )
{
vtkErrorMacro (<<"Couldn't open texture file");
return;
}
}
else
{
return;
}
//
// Allocate and read data
//
newTCoords = vtkFloatArray::New();
newTCoords->SetNumberOfComponents(3);
newTCoords->SetNumberOfTuples(numPts);
for (i=0; i<numPts; i++)
{
fscanf(textureFp, "%e %e", t, t+1);
newTCoords->SetTuple(i,t);
}
fclose(textureFp);
vtkDebugMacro(<<"Read " << numPts << " texture coordinates");
output->GetPointData()->SetTCoords(newTCoords);
newTCoords->Delete();
}
//----------------------------------------------------------------------------
// This source does not know how to generate pieces yet.
int vtkBYUReader::ComputeDivisionExtents(vtkDataObject *vtkNotUsed(output),
int idx, int numDivisions)
{
if (idx == 0 && numDivisions == 1)
{
// I will give you the whole thing
return 1;
}
else
{
// I have nothing to give you for this piece.
return 0;
}
}
void vtkBYUReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Geometry File Name: "
<< (this->GeometryFileName ? this->GeometryFileName : "(none)") << "\n";
os << indent << "Read Displacement: " << (this->ReadDisplacement ? "On\n" : "Off\n");
os << indent << "Displacement File Name: "
<< (this->DisplacementFileName ? this->DisplacementFileName : "(none)") << "\n";
os << indent << "Part Number: " << this->PartNumber << "\n";
os << indent << "Read Scalar: " << (this->ReadScalar ? "On\n" : "Off\n");
os << indent << "Scalar File Name: "
<< (this->ScalarFileName ? this->ScalarFileName : "(none)") << "\n";
os << indent << "Read Texture: " << (this->ReadTexture ? "On\n" : "Off\n");
os << indent << "Texture File Name: "
<< (this->TextureFileName ? this->TextureFileName : "(none)") << "\n";
}