forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkPLYReader.cxx
305 lines (264 loc) · 9.56 KB
/
vtkPLYReader.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPLYReader.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 "vtkPLYReader.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPLY.h"
#include "vtkPolyData.h"
#include <ctype.h>
#include <stddef.h>
vtkStandardNewMacro(vtkPLYReader);
// Construct object with merging set to true.
vtkPLYReader::vtkPLYReader()
{
this->FileName = NULL;
this->SetNumberOfInputPorts(0);
}
vtkPLYReader::~vtkPLYReader()
{
if (this->FileName)
{
delete [] this->FileName;
}
}
typedef struct _plyVertex {
float x[3]; // the usual 3-space position of a vertex
unsigned char red;
unsigned char green;
unsigned char blue;
} plyVertex;
typedef struct _plyFace {
unsigned char intensity; // optional face attributes
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char nverts; // number of vertex indices in list
int *verts; // vertex index list
} plyFace;
int vtkPLYReader::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the ouptut
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
PlyProperty vertProps[] = {
{"x", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex,x)),
0, 0, 0, 0},
{"y", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex,x)+sizeof(float)),
0, 0, 0, 0},
{"z", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex,x)+sizeof(float)+sizeof(float)),
0, 0, 0, 0},
{"red", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyVertex,red)), 0, 0, 0, 0},
{"green", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyVertex,green)), 0, 0, 0, 0},
{"blue", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyVertex,blue)), 0, 0, 0, 0},
};
PlyProperty faceProps[] = {
{"vertex_indices", PLY_INT, PLY_INT,
static_cast<int>(offsetof(plyFace,verts)),
1, PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyFace,nverts))},
{"intensity", PLY_UCHAR, PLY_UCHAR,
static_cast<int>(offsetof(plyFace,intensity)), 0, 0, 0, 0},
{"red", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyFace,red)), 0, 0, 0, 0},
{"green", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyFace,green)), 0, 0, 0, 0},
{"blue", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyFace,blue)), 0, 0, 0, 0},
};
int i, j, k;
int numPts=0, numPolys=0;
if (!this->FileName)
{
vtkErrorMacro(<<"A File Name must be specified.");
return 0;
}
// open a PLY file for reading
PlyFile *ply;
int nelems, fileType, numElems, nprops;
char **elist, *elemName;
float version;
if ( !(ply = vtkPLY::ply_open_for_reading(this->FileName, &nelems, &elist,
&fileType, &version)) )
{
vtkErrorMacro(<<"Could not open PLY file");
return 0;
}
// Check to make sure that we can read geometry
PlyElement *elem;
int index;
if ( (elem = vtkPLY::find_element (ply, "vertex")) == NULL ||
vtkPLY::find_property (elem, "x", &index) == NULL ||
vtkPLY::find_property (elem, "y", &index) == NULL ||
vtkPLY::find_property (elem, "z", &index) == NULL ||
(elem = vtkPLY::find_element (ply, "face")) == NULL ||
vtkPLY::find_property (elem, "vertex_indices", &index) == NULL )
{
vtkErrorMacro(<<"Cannot read geometry");
vtkPLY::ply_close (ply);
}
// Check for optional attribute data. We can handle intensity; and the
// triplet red, green, blue.
unsigned char intensityAvailable=false, RGBCellsAvailable=false, RGBPointsAvailable=false;
vtkUnsignedCharArray *intensity=NULL;
vtkUnsignedCharArray *RGBCells=NULL;
vtkUnsignedCharArray *RGBPoints=NULL;
if ( (elem = vtkPLY::find_element (ply, "face")) != NULL &&
vtkPLY::find_property (elem, "intensity", &index) != NULL )
{
intensity = vtkUnsignedCharArray::New();
intensity->SetName("intensity");
intensityAvailable = true;
output->GetCellData()->AddArray(intensity);
output->GetCellData()->SetActiveScalars("intensity");
intensity->Delete();
}
if ( (elem = vtkPLY::find_element (ply, "face")) != NULL &&
vtkPLY::find_property (elem, "red", &index) != NULL &&
vtkPLY::find_property (elem, "green", &index) != NULL &&
vtkPLY::find_property (elem, "blue", &index) != NULL )
{
RGBCells = vtkUnsignedCharArray::New();
RGBCells->SetName("RGB");
RGBCellsAvailable = true;
output->GetCellData()->AddArray(RGBCells);
output->GetCellData()->SetActiveScalars("RGB");
RGBCells->Delete();
}
if ( (elem = vtkPLY::find_element (ply, "vertex")) != NULL &&
vtkPLY::find_property (elem, "red", &index) != NULL &&
vtkPLY::find_property (elem, "green", &index) != NULL &&
vtkPLY::find_property (elem, "blue", &index) != NULL )
{
RGBPoints = vtkUnsignedCharArray::New();
RGBPointsAvailable = true;
RGBPoints->SetName("RGB");
output->GetPointData()->SetScalars(RGBPoints);
RGBPoints->Delete();
}
// Okay, now we can grab the data
for (i = 0; i < nelems; i++)
{
//get the description of the first element */
elemName = elist[i];
vtkPLY::ply_get_element_description (ply, elemName, &numElems, &nprops);
// if we're on vertex elements, read them in
if ( elemName && !strcmp ("vertex", elemName) )
{
// Create a list of points
numPts = numElems;
vtkPoints *pts = vtkPoints::New();
pts->SetDataTypeToFloat();
pts->SetNumberOfPoints(numPts);
// Setup to read the PLY elements
vtkPLY::ply_get_property (ply, elemName, &vertProps[0]);
vtkPLY::ply_get_property (ply, elemName, &vertProps[1]);
vtkPLY::ply_get_property (ply, elemName, &vertProps[2]);
if ( RGBPointsAvailable )
{
vtkPLY::ply_get_property (ply, elemName, &vertProps[3]);
vtkPLY::ply_get_property (ply, elemName, &vertProps[4]);
vtkPLY::ply_get_property (ply, elemName, &vertProps[5]);
RGBPoints->SetNumberOfComponents(3);
RGBPoints->SetNumberOfTuples(numPts);
}
plyVertex vertex;
for (j=0; j < numPts; j++)
{
vtkPLY::ply_get_element (ply, (void *) &vertex);
pts->SetPoint (j, vertex.x);
if ( RGBPointsAvailable )
{
RGBPoints->SetTuple3(j,vertex.red,vertex.green,vertex.blue);
}
}
output->SetPoints(pts);
pts->Delete();
}//if vertex
else if ( elemName && !strcmp ("face", elemName) )
{
// Create a polygonal array
numPolys = numElems;
vtkCellArray *polys = vtkCellArray::New();
polys->Allocate(polys->EstimateSize(numPolys,3),numPolys/2);
plyFace face;
int verts[256];
vtkIdType vtkVerts[256];
// Get the face properties
vtkPLY::ply_get_property (ply, elemName, &faceProps[0]);
if ( intensityAvailable )
{
vtkPLY::ply_get_property (ply, elemName, &faceProps[1]);
RGBCells->SetNumberOfComponents(1);
RGBCells->SetNumberOfTuples(numPolys);
}
if ( RGBCellsAvailable )
{
vtkPLY::ply_get_property (ply, elemName, &faceProps[2]);
vtkPLY::ply_get_property (ply, elemName, &faceProps[3]);
vtkPLY::ply_get_property (ply, elemName, &faceProps[4]);
RGBCells->SetNumberOfComponents(3);
RGBCells->SetNumberOfTuples(numPolys);
}
// grab all the face elements
for (j=0; j < numPolys; j++)
{
//grab and element from the file
face.verts = verts;
vtkPLY::ply_get_element (ply, (void *) &face);
for (k=0; k<face.nverts; k++)
{
vtkVerts[k] = face.verts[k];
}
polys->InsertNextCell(face.nverts,vtkVerts);
if ( intensityAvailable )
{
intensity->SetValue(j,face.intensity);
}
if ( RGBCellsAvailable )
{
RGBCells->SetValue(3*j,face.red);
RGBCells->SetValue(3*j+1,face.green);
RGBCells->SetValue(3*j+2,face.blue);
}
}
output->SetPolys(polys);
polys->Delete();
}//if face
free(elist[i]); //allocated by ply_open_for_reading
}//for all elements of the PLY file
free(elist); //allocated by ply_open_for_reading
vtkDebugMacro( <<"Read: " << numPts << " points, "
<< numPolys << " polygons");
// close the PLY file
vtkPLY::ply_close (ply);
return 1;
}
int vtkPLYReader::CanReadFile(const char *filename)
{
FILE *fd = fopen(filename, "rb");
if (!fd) return 0;
char line[16];
fgets(line, 16, fd);
fclose(fd);
return (strncmp(line, "ply", 3) == 0);
}
void vtkPLYReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "File Name: "
<< (this->FileName ? this->FileName : "(none)") << "\n";
}