-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
vtkPLYReader.cxx
662 lines (609 loc) · 22.8 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*=========================================================================
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 "vtkDataArray.h"
#include "vtkFloatArray.h"
#include "vtkIncrementalOctreePointLocator.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMathUtilities.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPLY.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolygon.h"
#include "vtkSmartPointer.h"
#include "vtkStringArray.h"
#include "vtkUnsignedCharArray.h"
#include <vtksys/SystemTools.hxx>
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <vector>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkPLYReader);
namespace
{
/**
* Create an extra point in 'data' with the same coordinates and data as
* the point at cellPointIndex inside cell. This is to avoid texture artifacts
* when you have one point with two different texture values (so the latter
* value override the first. This results in a texture discontinuity which results
* in artifacts).
*/
vtkIdType duplicateCellPoint(vtkPolyData* data, vtkCell* cell, int cellPointIndex)
{
// get the old point id
vtkIdList* pointIds = cell->GetPointIds();
vtkIdType pointId = pointIds->GetId(cellPointIndex);
// duplicate that point and all associated data
vtkPoints* points = data->GetPoints();
double* point = data->GetPoint(pointId);
vtkIdType newPointId = points->InsertNextPoint(point);
for (int i = 0; i < data->GetPointData()->GetNumberOfArrays(); ++i)
{
vtkDataArray* a = data->GetPointData()->GetArray(i);
a->InsertTuple(newPointId, a->GetTuple(pointId));
}
// make cell use the new point
pointIds->SetId(cellPointIndex, newPointId);
return newPointId;
}
/**
* Set a newPointId at cellPointIndex inside cell.
*/
void setCellPoint(vtkCell* cell, int cellPointIndex, vtkIdType newPointId)
{
// get the old point id
vtkIdList* pointIds = cell->GetPointIds();
// make cell use the new point
pointIds->SetId(cellPointIndex, newPointId);
}
/**
* Compare two points for equality
*/
bool FuzzyEqual(double* f, double* s, double t)
{
return vtkMathUtilities::FuzzyCompare(f[0], s[0], t) &&
vtkMathUtilities::FuzzyCompare(f[1], s[1], t) && vtkMathUtilities::FuzzyCompare(f[2], s[2], t);
}
}
// Construct object with merging set to true.
vtkPLYReader::vtkPLYReader()
{
this->Comments = vtkStringArray::New();
this->ReadFromInputString = false;
this->FaceTextureTolerance = 0.000001;
this->DuplicatePointsForFaceTexture = true;
}
vtkPLYReader::~vtkPLYReader()
{
this->Comments->Delete();
this->Comments = nullptr;
}
namespace
{ // required so we don't violate ODR
typedef struct _plyVertex
{
float x[3]; // the usual 3-space position of a vertex
float tex[2];
float normal[3];
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} plyVertex;
typedef struct _plyFace
{
unsigned char intensity; // optional face attributes
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
unsigned char nverts; // number of vertex indices in list
int* verts; // vertex index list
unsigned char ntexcoord; // number of texcoord in list
float* texcoord; // texcoord list
} plyFace;
}
int vtkPLYReader::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
{
// get the info object
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// get the output
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 },
{ "u", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex, tex)), 0, 0, 0, 0 },
{ "v", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex, tex) + sizeof(float)), 0, 0,
0, 0 },
{ "nx", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex, normal)), 0, 0, 0, 0 },
{ "ny", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex, normal) + sizeof(float)), 0,
0, 0, 0 },
{ "nz", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyVertex, normal) + 2 * 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 },
{ "alpha", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyVertex, alpha)), 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 },
{ "alpha", PLY_UCHAR, PLY_UCHAR, static_cast<int>(offsetof(plyFace, alpha)), 0, 0, 0, 0 },
{ "texcoord", PLY_FLOAT, PLY_FLOAT, static_cast<int>(offsetof(plyFace, texcoord)), 1, PLY_UCHAR,
PLY_UCHAR, static_cast<int>(offsetof(plyFace, ntexcoord)) },
};
// open a PLY file for reading
PlyFile* ply;
int nelems, numElems, nprops;
char **elist, *elemName;
if (this->ReadFromInputStream)
{
if (!(ply = vtkPLY::ply_read(this->Stream, &nelems, &elist)))
{
vtkWarningMacro(<< "Could not open PLY file");
return 0;
}
}
else if (this->ReadFromInputString)
{
if (!(ply = vtkPLY::ply_open_for_reading_from_string(this->InputString, &nelems, &elist)))
{
vtkWarningMacro(<< "Could not open PLY file");
return 0;
}
}
else
{
if (!(ply = vtkPLY::ply_open_for_reading(this->FileName, &nelems, &elist)))
{
vtkWarningMacro(<< "Could not open PLY file");
return 0;
}
}
int numberOfComments = 0;
char** comments = vtkPLY::ply_get_comments(ply, &numberOfComments);
this->Comments->Reset();
for (int i = 0; i < numberOfComments; i++)
{
this->Comments->InsertNextValue(comments[i]);
}
// Check to make sure that we can read geometry
PlyElement* elem;
int index;
if ((elem = vtkPLY::find_element(ply, "vertex")) == nullptr ||
vtkPLY::find_property(elem, "x", &index) == nullptr ||
vtkPLY::find_property(elem, "y", &index) == nullptr ||
vtkPLY::find_property(elem, "z", &index) == nullptr)
{
vtkErrorMacro(<< "Cannot read geometry");
vtkPLY::ply_close(ply);
}
// Check for optional attribute data. We can handle intensity; and the
// triplet red, green, blue.
bool intensityAvailable = false;
vtkSmartPointer<vtkUnsignedCharArray> intensity = nullptr;
if ((elem = vtkPLY::find_element(ply, "face")) != nullptr &&
vtkPLY::find_property(elem, "intensity", &index) != nullptr)
{
intensity = vtkSmartPointer<vtkUnsignedCharArray>::New();
intensity->SetName("intensity");
intensityAvailable = true;
output->GetCellData()->AddArray(intensity);
output->GetCellData()->SetActiveScalars("intensity");
}
bool rgbCellsAvailable = false;
bool rgbCellsHaveAlpha = false;
vtkSmartPointer<vtkUnsignedCharArray> rgbCells = nullptr;
if ((elem = vtkPLY::find_element(ply, "face")) != nullptr &&
vtkPLY::find_property(elem, "red", &index) != nullptr &&
vtkPLY::find_property(elem, "green", &index) != nullptr &&
vtkPLY::find_property(elem, "blue", &index) != nullptr)
{
rgbCellsAvailable = true;
rgbCells = vtkSmartPointer<vtkUnsignedCharArray>::New();
if (vtkPLY::find_property(elem, "alpha", &index) != nullptr)
{
rgbCells->SetName("RGBA");
rgbCells->SetNumberOfComponents(4);
rgbCellsHaveAlpha = true;
}
else
{
rgbCells->SetName("RGB");
rgbCells->SetNumberOfComponents(3);
}
output->GetCellData()->AddArray(rgbCells);
output->GetCellData()->SetActiveScalars("RGB");
}
bool rgbPointsAvailable = false;
bool rgbPointsHaveAlpha = false;
vtkSmartPointer<vtkUnsignedCharArray> rgbPoints = nullptr;
if ((elem = vtkPLY::find_element(ply, "vertex")) != nullptr)
{
if (vtkPLY::find_property(elem, "red", &index) != nullptr &&
vtkPLY::find_property(elem, "green", &index) != nullptr &&
vtkPLY::find_property(elem, "blue", &index) != nullptr)
{
rgbPointsAvailable = true;
}
else if (vtkPLY::find_property(elem, "diffuse_red", &index) != nullptr &&
vtkPLY::find_property(elem, "diffuse_green", &index) != nullptr &&
vtkPLY::find_property(elem, "diffuse_blue", &index) != nullptr)
{
rgbPointsAvailable = true;
vertProps[8].name = "diffuse_red";
vertProps[9].name = "diffuse_green";
vertProps[10].name = "diffuse_blue";
}
if (rgbPointsAvailable)
{
rgbPoints = vtkSmartPointer<vtkUnsignedCharArray>::New();
if (vtkPLY::find_property(elem, "alpha", &index) != nullptr)
{
rgbPoints->SetName("RGBA");
rgbPoints->SetNumberOfComponents(4);
rgbPointsHaveAlpha = true;
}
else
{
rgbPoints->SetName("RGB");
rgbPoints->SetNumberOfComponents(3);
}
output->GetPointData()->SetScalars(rgbPoints);
}
}
bool normalPointsAvailable = false;
vtkSmartPointer<vtkFloatArray> normals = nullptr;
if ((elem = vtkPLY::find_element(ply, "vertex")) != nullptr &&
vtkPLY::find_property(elem, "nx", &index) != nullptr &&
vtkPLY::find_property(elem, "ny", &index) != nullptr &&
vtkPLY::find_property(elem, "nz", &index) != nullptr)
{
normals = vtkSmartPointer<vtkFloatArray>::New();
normalPointsAvailable = true;
normals->SetName("Normals");
normals->SetNumberOfComponents(3);
output->GetPointData()->SetNormals(normals);
}
bool texCoordsPointsAvailable = false;
vtkSmartPointer<vtkFloatArray> texCoordsPoints = nullptr;
if ((elem = vtkPLY::find_element(ply, "vertex")) != nullptr)
{
if (vtkPLY::find_property(elem, "u", &index) != nullptr &&
vtkPLY::find_property(elem, "v", &index) != nullptr)
{
texCoordsPointsAvailable = true;
}
else if (vtkPLY::find_property(elem, "texture_u", &index) != nullptr &&
vtkPLY::find_property(elem, "texture_v", &index) != nullptr)
{
texCoordsPointsAvailable = true;
vertProps[3].name = "texture_u";
vertProps[4].name = "texture_v";
}
if (texCoordsPointsAvailable)
{
texCoordsPoints = vtkSmartPointer<vtkFloatArray>::New();
texCoordsPoints->SetName("TCoords");
texCoordsPoints->SetNumberOfComponents(2);
output->GetPointData()->SetTCoords(texCoordsPoints);
}
}
bool texCoordsFaceAvailable = false;
if ((elem = vtkPLY::find_element(ply, "face")) != nullptr && !texCoordsPointsAvailable)
{
if (vtkPLY::find_property(elem, "texcoord", &index) != nullptr)
{
texCoordsFaceAvailable = true;
texCoordsPoints = vtkSmartPointer<vtkFloatArray>::New();
texCoordsPoints->SetName("TCoords");
texCoordsPoints->SetNumberOfComponents(2);
output->GetPointData()->SetTCoords(texCoordsPoints);
}
}
// Okay, now we can grab the data
int numPts = 0, numPolys = 0;
for (int 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 (texCoordsPointsAvailable)
{
vtkPLY::ply_get_property(ply, elemName, &vertProps[3]);
vtkPLY::ply_get_property(ply, elemName, &vertProps[4]);
texCoordsPoints->SetNumberOfTuples(numPts);
}
if (normalPointsAvailable)
{
vtkPLY::ply_get_property(ply, elemName, &vertProps[5]);
vtkPLY::ply_get_property(ply, elemName, &vertProps[6]);
vtkPLY::ply_get_property(ply, elemName, &vertProps[7]);
normals->SetNumberOfTuples(numPts);
}
if (rgbPointsAvailable)
{
vtkPLY::ply_get_property(ply, elemName, &vertProps[8]);
vtkPLY::ply_get_property(ply, elemName, &vertProps[9]);
vtkPLY::ply_get_property(ply, elemName, &vertProps[10]);
if (rgbPointsHaveAlpha)
{
vtkPLY::ply_get_property(ply, elemName, &vertProps[11]);
}
rgbPoints->SetNumberOfTuples(numPts);
}
plyVertex vertex;
for (int j = 0; j < numPts; j++)
{
vtkPLY::ply_get_element(ply, (void*)&vertex);
pts->SetPoint(j, vertex.x);
if (texCoordsPointsAvailable)
{
texCoordsPoints->SetTuple2(j, vertex.tex[0], vertex.tex[1]);
}
if (normalPointsAvailable)
{
normals->SetTuple3(j, vertex.normal[0], vertex.normal[1], vertex.normal[2]);
}
if (rgbPointsAvailable)
{
if (rgbPointsHaveAlpha)
{
rgbPoints->SetTuple4(j, vertex.red, vertex.green, vertex.blue, vertex.alpha);
}
else
{
rgbPoints->SetTuple3(j, vertex.red, vertex.green, vertex.blue);
}
}
}
output->SetPoints(pts);
pts->Delete();
} // if vertex
else if (elemName && !strcmp("face", elemName))
{
// texture coordinates
vtkNew<vtkPoints> texCoords;
// We store a list of pointIds (that have the same texture coordinates)
// at the texture index returned by texLocator
std::vector<std::vector<vtkIdType>> pointIds;
pointIds.resize(output->GetNumberOfPoints());
// Used to detect different texture values at a vertex.
vtkNew<vtkIncrementalOctreePointLocator> texLocator;
texLocator->SetTolerance(this->FaceTextureTolerance);
double bounds[] = { 0.0, 1.0, 0.0, 1.0, 0.0, 0.0 };
texLocator->InitPointInsertion(texCoords, bounds);
// Create a polygonal array
numPolys = numElems;
vtkSmartPointer<vtkCellArray> polys = vtkSmartPointer<vtkCellArray>::New();
polys->AllocateEstimate(numPolys, 3);
plyFace face;
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]);
intensity->SetNumberOfComponents(1);
intensity->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]);
if (rgbCellsHaveAlpha)
{
vtkPLY::ply_get_property(ply, elemName, &faceProps[5]);
}
rgbCells->SetNumberOfTuples(numPolys);
}
if (texCoordsFaceAvailable)
{
vtkPLY::ply_get_property(ply, elemName, &faceProps[6]);
texCoordsPoints->SetNumberOfTuples(numPts);
if (this->DuplicatePointsForFaceTexture)
{
// initialize texture coordinates with invalid value
for (int j = 0; j < numPts; ++j)
{
texCoordsPoints->SetTuple2(j, -1, -1);
}
}
}
// grab all the face elements
vtkNew<vtkPolygon> cell;
for (int j = 0; j < numPolys; j++)
{
// grab and element from the file
vtkPLY::ply_get_element(ply, (void*)&face);
for (int k = 0; k < face.nverts; k++)
{
vtkVerts[k] = face.verts[k];
}
free(face.verts); // allocated in vtkPLY::ascii/binary_get_element
cell->Initialize(face.nverts, vtkVerts, output->GetPoints());
if (intensityAvailable)
{
intensity->SetValue(j, face.intensity);
}
if (rgbCellsAvailable)
{
if (rgbCellsHaveAlpha)
{
rgbCells->SetValue(4 * j, face.red);
rgbCells->SetValue(4 * j + 1, face.green);
rgbCells->SetValue(4 * j + 2, face.blue);
rgbCells->SetValue(4 * j + 3, face.alpha);
}
else
{
rgbCells->SetValue(3 * j, face.red);
rgbCells->SetValue(3 * j + 1, face.green);
rgbCells->SetValue(3 * j + 2, face.blue);
}
}
if (texCoordsFaceAvailable)
{
// Test to know if there is a texcoord for every vertex
if (face.nverts == (face.ntexcoord / 2))
{
if (this->DuplicatePointsForFaceTexture)
{
for (int k = 0; k < face.nverts; k++)
{
// new texture stored at the current face
float newTex[] = { face.texcoord[k * 2], face.texcoord[k * 2 + 1] };
// texture stored at vtkVerts[k] point
float currentTex[2];
texCoordsPoints->GetTypedTuple(vtkVerts[k], currentTex);
double newTex3[] = { newTex[0], newTex[1], 0 };
if (currentTex[0] == -1.0)
{
// newly seen texture coordinates for vertex
texCoordsPoints->SetTuple2(vtkVerts[k], newTex[0], newTex[1]);
vtkIdType ti;
texLocator->InsertUniquePoint(newTex3, ti);
pointIds.resize(std::max(ti + 1, static_cast<vtkIdType>(pointIds.size())));
pointIds[ti].push_back(vtkVerts[k]);
}
else
{
if (!vtkMathUtilities::FuzzyCompare(
currentTex[0], newTex[0], this->FaceTextureTolerance) ||
!vtkMathUtilities::FuzzyCompare(
currentTex[1], newTex[1], this->FaceTextureTolerance))
{
// different texture coordinate
// than stored at point vtkVerts[k]
vtkIdType ti;
int inserted = texLocator->InsertUniquePoint(newTex3, ti);
if (inserted)
{
// newly seen texture coordinate for vertex
// which already has some texture coordinates.
vtkIdType dp = duplicateCellPoint(output, cell, k);
texCoordsPoints->SetTuple2(dp, newTex[0], newTex[1]);
pointIds.resize(std::max(ti + 1, static_cast<vtkIdType>(pointIds.size())));
pointIds[ti].push_back(dp);
}
else
{
size_t sameTexIndex = 0;
double first[3];
output->GetPoint(vtkVerts[k], first);
for (; sameTexIndex < pointIds[ti].size(); ++sameTexIndex)
{
double second[3];
output->GetPoint(pointIds[ti][sameTexIndex], second);
if (FuzzyEqual(first, second, this->FaceTextureTolerance))
{
break;
}
}
if (sameTexIndex == pointIds[ti].size())
{
// newly seen point for this texture coordinate
vtkIdType dp = duplicateCellPoint(output, cell, k);
texCoordsPoints->SetTuple2(dp, newTex[0], newTex[1]);
pointIds[ti].push_back(dp);
}
// texture coordinate already seen before, use the vertex
// associated with these texture coordinates
vtkIdType vi = pointIds[ti][sameTexIndex];
setCellPoint(cell, k, vi);
}
}
// same texture coordinate, nothing to do.
}
}
}
else
{
// if we don't want point duplication we only need to set
// the texture coordinates
for (int k = 0; k < face.nverts; k++)
{
// new texture stored at the current face
float newTex[] = { face.texcoord[k * 2], face.texcoord[k * 2 + 1] };
texCoordsPoints->SetTuple2(vtkVerts[k], newTex[0], newTex[1]);
}
}
}
else
{
vtkWarningMacro(<< "Number of texture coordinates " << face.ntexcoord
<< " different than number of points " << face.nverts);
}
free(face.texcoord);
}
polys->InsertNextCell(cell);
}
output->SetPolys(polys);
}
free(elist[i]); // allocated by ply_open_for_reading
elist[i] = nullptr;
} // 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 = vtksys::SystemTools::Fopen(filename, "rb");
if (!fd)
return 0;
char line[4] = {};
const char* result = fgets(line, sizeof(line), fd);
fclose(fd);
return (result && strncmp(result, "ply", 3) == 0);
}
void vtkPLYReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Comments:\n";
indent = indent.GetNextIndent();
for (int i = 0; i < this->Comments->GetNumberOfValues(); ++i)
{
os << indent << this->Comments->GetValue(i) << "\n";
}
}
VTK_ABI_NAMESPACE_END