forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkDecimatePolylineFilter.cxx
281 lines (241 loc) · 8.48 KB
/
vtkDecimatePolylineFilter.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDecimatePolylineFilter.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 "vtkDecimatePolylineFilter.h"
#include "vtkDoubleArray.h"
#include "vtkLine.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkLine.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkPriorityQueue.h"
#include <map>
vtkStandardNewMacro(vtkDecimatePolylineFilter);
// Representation of a polyline as a doubly linked list of vertices.
class vtkDecimatePolylineFilter::Polyline
{
public:
struct Vertex
{
unsigned index;
vtkIdType id;
Vertex* prev;
Vertex* next;
bool removable;
};
Polyline(vtkIdType* vertexOrdering, vtkIdType size)
{
this->Size = size;
Vertices = new Vertex[size];
for (vtkIdType idx=0;idx<size;idx++)
{
Vertices[idx].index = idx;
Vertices[idx].id = vertexOrdering[idx];
Vertices[idx].prev = (idx > 0 ? &Vertices[idx-1] : NULL);
Vertices[idx].next = (idx < size-1 ? &Vertices[idx+1] : NULL);
Vertices[idx].removable = true;
}
Vertices[0].removable = Vertices[size-1].removable = false;
}
~Polyline()
{
if (Vertices)
{
delete [] Vertices;
Vertices = NULL;
}
}
void Remove(vtkIdType vertexIdx)
{
this->Size--;
(*(this->Vertices[vertexIdx].prev)).next = this->Vertices[vertexIdx].next;
(*(this->Vertices[vertexIdx].next)).prev = this->Vertices[vertexIdx].prev;
}
vtkIdType Size;
Vertex* Vertices;
};
//---------------------------------------------------------------------
// Create object with specified reduction of 90%.
vtkDecimatePolylineFilter::vtkDecimatePolylineFilter()
{
this->TargetReduction = 0.90;
this->PriorityQueue = vtkSmartPointer< vtkPriorityQueue >::New();
this->OutputPointsPrecision = vtkAlgorithm::DEFAULT_PRECISION;
}
//---------------------------------------------------------------------
vtkDecimatePolylineFilter::~vtkDecimatePolylineFilter()
{
}
//---------------------------------------------------------------------
double
vtkDecimatePolylineFilter::ComputeError( vtkPolyData* input,
Polyline* polyline,
vtkIdType idx )
{
vtkPoints * inputPoints = input->GetPoints();
double x1[3], x[3], x2[3];
inputPoints->GetPoint( polyline->Vertices[idx].prev->id, x1 );
inputPoints->GetPoint( polyline->Vertices[idx].id, x );
inputPoints->GetPoint( polyline->Vertices[idx].next->id, x2 );
if ( vtkMath::Distance2BetweenPoints( x1, x2 ) == 0.0 )
{
return 0.0;
}
else
{
return vtkLine::DistanceToLine( x, x1, x2 );
}
}
//---------------------------------------------------------------------
// Reduce the number of points in a set of polylines
//
int vtkDecimatePolylineFilter::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()));
vtkCellArray *inputLines = input->GetLines();
vtkPoints * inputPoints = input->GetPoints();
vtkDebugMacro("Decimating polylines");
if (!inputLines || !inputPoints)
{
return 1;
}
vtkIdType numLines = inputLines->GetNumberOfCells();
vtkIdType numPts = inputPoints->GetNumberOfPoints();
if ( numLines < 1 || numPts < 1 )
{
return 1;
}
// Allocate memory and prepare for data processing
vtkPoints *newPts = vtkPoints::New();
// Set the desired precision for the points in the output.
if(this->OutputPointsPrecision == vtkAlgorithm::DEFAULT_PRECISION)
{
newPts->SetDataType(inputPoints->GetDataType());
}
else if(this->OutputPointsPrecision == vtkAlgorithm::SINGLE_PRECISION)
{
newPts->SetDataType(VTK_FLOAT);
}
else if(this->OutputPointsPrecision == vtkAlgorithm::DOUBLE_PRECISION)
{
newPts->SetDataType(VTK_DOUBLE);
}
vtkCellArray *newLines = vtkCellArray::New();
newLines->Allocate(numLines,2);
vtkPointData *inPD = input->GetPointData();
vtkPointData *outPD = output->GetPointData();
vtkCellData *inCD = input->GetCellData();
vtkCellData *outCD = output->GetCellData();
outPD->CopyAllocate(inPD);
outCD->CopyAllocate(inCD);
vtkIdType *linePtr = inputLines->GetPointer();
vtkIdType firstVertexIndex = 0;
vtkIdType polylineSize = 0;
// Decimate each polyline (represented as a single cell) in series
for ( vtkIdType lineId=0;lineId<numLines;lineId++,
firstVertexIndex+=polylineSize)
{
polylineSize = linePtr[firstVertexIndex + lineId];
// construct a polyline as a doubly linked list
vtkDecimatePolylineFilter::Polyline* polyline =
new vtkDecimatePolylineFilter::Polyline(&(linePtr[firstVertexIndex +
lineId + 1]),
polylineSize);
double error;
for (vtkIdType vertexIdx=0;vertexIdx<polyline->Size;++vertexIdx)
{
// only vertices that are removable have associated error values
if (polyline->Vertices[vertexIdx].removable)
{
error = this->ComputeError(input,polyline,vertexIdx);
this->PriorityQueue->Insert(error,vertexIdx);
}
}
// Now process structures,
// deleting vertices until the decimation target is met.
vtkIdType currentNumPts = polylineSize;
while ( 1.0 - ( static_cast<double>( currentNumPts ) / static_cast<double>(
polylineSize ) )
< this->TargetReduction &&
currentNumPts > 2 )
{
--currentNumPts;
vtkIdType poppedIdx = this->PriorityQueue->Pop();
polyline->Remove(poppedIdx);
vtkIdType prevIdx = polyline->Vertices[poppedIdx].prev->index;
vtkIdType nextIdx = polyline->Vertices[poppedIdx].next->index;
// again, only vertices that are removable have associated error values
if (polyline->Vertices[poppedIdx].prev->removable)
{
error = this->ComputeError(input,polyline,prevIdx);
this->PriorityQueue->DeleteId(prevIdx);
this->PriorityQueue->Insert(error,prevIdx);
}
if (polyline->Vertices[poppedIdx].next->removable)
{
error = this->ComputeError(input,polyline,nextIdx);
this->PriorityQueue->DeleteId(nextIdx);
this->PriorityQueue->Insert(error,nextIdx);
}
}
// What's left over is now spit out as a new polyline
vtkIdType newId = newLines->InsertNextCell(currentNumPts);
outCD->CopyData(inCD,firstVertexIndex,newId);
std::map<vtkIdType,vtkIdType> pointIdMap;
std::map<vtkIdType,vtkIdType>::iterator it;
Polyline::Vertex* vertex = &(polyline->Vertices[0]);
while (vertex != NULL)
{
// points that are repeated within a single polyline are represented by
// only one point instance
it = pointIdMap.find(vertex->id);
if (it == pointIdMap.end())
{
newId = newPts->InsertNextPoint( inputPoints->GetPoint( vertex->id ) );
newLines->InsertCellPoint( newId );
outPD->CopyData( inPD, vertex->id, newId );
}
else
newLines->InsertCellPoint( it->second );
vertex = vertex->next;
}
delete polyline;
}
// Create output and clean up
output->SetPoints( newPts );
output->SetLines( newLines );
newLines->Delete();
newPts->Delete();
return 1;
}
//---------------------------------------------------------------------
void vtkDecimatePolylineFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Target Reduction: " << this->TargetReduction << "\n";
os << indent << "Output Points Precision: " << this->OutputPointsPrecision
<< "\n";
}