forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkTriangularTCoords.cxx
199 lines (165 loc) · 5.34 KB
/
vtkTriangularTCoords.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTriangularTCoords.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 "vtkTriangularTCoords.h"
#include "vtkCellArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include <cmath>
vtkStandardNewMacro(vtkTriangularTCoords);
int vtkTriangularTCoords::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
// get the info objects
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkPolyData* input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType tmp;
int j;
vtkPoints* inPts;
vtkPointData* pd;
vtkCellArray *inPolys, *inStrips;
vtkIdType numNewPts, numNewPolys, polyAllocSize;
vtkFloatArray* newTCoords;
vtkIdType newId, numCells, cellId;
const vtkIdType* pts = nullptr;
vtkIdType newIds[3];
vtkIdType npts = 0;
int errorLogging = 1;
vtkPoints* newPoints;
vtkCellArray* newPolys;
double p1[3], p2[3], p3[3];
double tCoords[6];
vtkPointData* pointData = output->GetPointData();
// Initialize
//
vtkDebugMacro(<< "Generating triangular texture coordinates");
inPts = input->GetPoints();
pd = input->GetPointData();
inPolys = input->GetPolys();
inStrips = input->GetStrips();
// Count the number of new points and other primitives that
// need to be created.
//
numNewPts = input->GetNumberOfVerts();
numNewPolys = 0;
polyAllocSize = 0;
for (inPolys->InitTraversal(); inPolys->GetNextCell(npts, pts);)
{
numNewPts += npts;
numNewPolys++;
polyAllocSize += npts + 1;
}
for (inStrips->InitTraversal(); inStrips->GetNextCell(npts, pts);)
{
numNewPts += (npts - 2) * 3;
polyAllocSize += (npts - 2) * 4;
}
numCells = inPolys->GetNumberOfCells() + inStrips->GetNumberOfCells();
// Allocate texture data
//
newTCoords = vtkFloatArray::New();
newTCoords->SetNumberOfComponents(2);
newTCoords->Allocate(2 * numNewPts);
// Allocate
//
newPoints = vtkPoints::New();
newPoints->Allocate(numNewPts);
newPolys = vtkCellArray::New();
newPolys->AllocateEstimate(polyAllocSize, 1);
pointData->CopyTCoordsOff();
pointData->CopyAllocate(pd);
// Texture coordinates are the same for each triangle
//
tCoords[0] = 0.0;
tCoords[1] = 0.0;
tCoords[2] = 1.0;
tCoords[3] = 0.0;
tCoords[4] = 0.5;
tCoords[5] = sqrt(3.0) / 2.0;
int abort = 0;
vtkIdType progressInterval = numCells / 20 + 1;
for (cellId = 0, inPolys->InitTraversal(); inPolys->GetNextCell(npts, pts) && !abort; cellId++)
{
if (!(cellId % progressInterval))
{
this->UpdateProgress((double)cellId / numCells);
abort = this->GetAbortExecute();
}
if (npts != 3)
{
if (errorLogging)
vtkWarningMacro(<< "No texture coordinates for this cell, it is not a triangle");
errorLogging = 0;
continue;
}
newPolys->InsertNextCell(npts);
for (j = 0; j < npts; j++)
{
inPts->GetPoint(pts[j], p1);
newId = newPoints->InsertNextPoint(p1);
newPolys->InsertCellPoint(newId);
pointData->CopyData(pd, pts[j], newId);
newTCoords->InsertNextTuple(&tCoords[2 * j]);
}
}
// Triangle strips
//
for (inStrips->InitTraversal(); inStrips->GetNextCell(npts, pts) && !abort; cellId++)
{
if (!(cellId % progressInterval))
{
this->UpdateProgress((double)cellId / numCells);
abort = this->GetAbortExecute();
}
for (j = 0; j < (npts - 2); j++)
{
inPts->GetPoint(pts[j], p1);
inPts->GetPoint(pts[j + 1], p2);
inPts->GetPoint(pts[j + 2], p3);
newIds[0] = newPoints->InsertNextPoint(p1);
pointData->CopyData(pd, pts[j], newIds[0]);
newTCoords->InsertNextTuple(&tCoords[0]);
newIds[1] = newPoints->InsertNextPoint(p2);
pointData->CopyData(pd, pts[j + 1], newIds[1]);
newTCoords->InsertNextTuple(&tCoords[2]);
newIds[2] = newPoints->InsertNextPoint(p3);
pointData->CopyData(pd, pts[j + 2], newIds[2]);
newTCoords->InsertNextTuple(&tCoords[4]);
// flip orientation for odd tris
if (j % 2)
{
tmp = newIds[0];
newIds[0] = newIds[2];
newIds[2] = tmp;
}
newPolys->InsertNextCell(3, newIds);
}
}
// Update self and release memory
//
output->SetPoints(newPoints);
newPoints->Delete();
output->SetPolys(newPolys);
newPolys->Delete();
output->GetPointData()->SetTCoords(newTCoords);
newTCoords->Delete();
return 1;
}
void vtkTriangularTCoords::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}