forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkGenericCellTessellator.cxx
242 lines (210 loc) · 8.43 KB
/
vtkGenericCellTessellator.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGenericCellTessellator.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 "vtkGenericCellTessellator.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkIdList.h"
#include "vtkGenericAdaptorCell.h"
#include "vtkPointData.h"
#include "vtkDoubleArray.h"
#include "vtkMergePoints.h"
#include "vtkCellArray.h"
#include "vtkCollection.h"
#include "vtkGenericSubdivisionErrorMetric.h"
#include "vtkGenericAttribute.h"
#include "vtkGenericAttributeCollection.h"
#include "vtkGenericCellIterator.h"
#include <assert.h>
#include "vtkMath.h"
vtkCxxSetObjectMacro(vtkGenericCellTessellator, ErrorMetrics, vtkCollection);
//-----------------------------------------------------------------------------
// Create the tessellator helper with a default of 0.25 for threshold
vtkGenericCellTessellator::vtkGenericCellTessellator()
{
this->ErrorMetrics = vtkCollection::New();
this->MaxErrorsCapacity = 0;
this->MaxErrors = 0;
this->Measurement = 0;
}
//-----------------------------------------------------------------------------
vtkGenericCellTessellator::~vtkGenericCellTessellator()
{
this->SetErrorMetrics( 0 );
if(this->MaxErrors)
{
delete[] this->MaxErrors;
}
}
//-----------------------------------------------------------------------------
void vtkGenericCellTessellator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Measurement: " << this->Measurement << endl;
os << indent << "ErrorMetrics: " << this->ErrorMetrics << endl;
/* this->MaxErrorsCapacity */
/* this->MaxErrors */
}
//-----------------------------------------------------------------------------
// Description:
// Does the edge need to be subdivided according to at least one error
// metric? The edge is defined by its `leftPoint' and its `rightPoint'.
// `leftPoint', `midPoint' and `rightPoint' have to be initialized before
// calling RequiresEdgeSubdivision().
// Their format is global coordinates, parametric coordinates and
// point centered attributes: xyx rst abc de...
// `alpha' is the normalized abscissa of the midpoint along the edge.
// (close to 0 means close to the left point, close to 1 means close to the
// right point)
// \pre leftPoint_exists: leftPoint!=0
// \pre midPoint_exists: midPoint!=0
// \pre rightPoint_exists: rightPoint!=0
// \pre clamped_alpha: alpha>0 && alpha<1
// \pre valid_size: sizeof(leftPoint)=sizeof(midPoint)=sizeof(rightPoint)
// =GetAttributeCollection()->GetNumberOfPointCenteredComponents()+6
int vtkGenericCellTessellator::RequiresEdgeSubdivision(double *leftPoint,
double *midPoint,
double *rightPoint,
double alpha)
{
assert("pre: leftPoint_exists" && leftPoint!=0);
assert("pre: midPoint_exists" && midPoint!=0);
assert("pre: rightPoint_exists" && rightPoint!=0);
assert("pre: clamped_alpha" && alpha>0 && alpha<1);
int result = 0;
this->ErrorMetrics->InitTraversal();
vtkGenericSubdivisionErrorMetric *e =
static_cast<vtkGenericSubdivisionErrorMetric *>(this->ErrorMetrics->GetNextItemAsObject());
// Once we found at least one error metric that need subdivision,
// the subdivision has to be done and there is no need to check for other
// error metrics.
while(!result && e != 0 )
{
result = e->RequiresEdgeSubdivision(leftPoint,midPoint,rightPoint,alpha);
e = static_cast<vtkGenericSubdivisionErrorMetric *>
(this->ErrorMetrics->GetNextItemAsObject());
}
return result;
}
//-----------------------------------------------------------------------------
// Description:
// Update the max error of each error metric according to the error at the
// mid-point. The type of error depends on the state
// of the concrete error metric. For instance, it can return an absolute
// or relative error metric.
// See RequiresEdgeSubdivision() for a description of the arguments.
// \pre leftPoint_exists: leftPoint!=0
// \pre midPoint_exists: midPoint!=0
// \pre rightPoint_exists: rightPoint!=0
// \pre clamped_alpha: alpha>0 && alpha<1
// \pre valid_size: sizeof(leftPoint)=sizeof(midPoint)=sizeof(rightPoint)
// =GetAttributeCollection()->GetNumberOfPointCenteredComponents()+6
void vtkGenericCellTessellator::UpdateMaxError(double *leftPoint,
double *midPoint,
double *rightPoint,
double alpha)
{
assert("pre: leftPoint_exists" && leftPoint!=0);
assert("pre: midPoint_exists" && midPoint!=0);
assert("pre: rightPoint_exists" && rightPoint!=0);
assert("pre: clamped_alpha" && alpha>0 && alpha<1);
this->ErrorMetrics->InitTraversal();
vtkGenericSubdivisionErrorMetric *e =
static_cast<vtkGenericSubdivisionErrorMetric *>(this->ErrorMetrics->GetNextItemAsObject());
// Once we found at least one error metric that need subdivision,
// the subdivision has to be done and there is no need to check for other
// error metrics.
for(int i = 0; e!=0; ++i)
{
double error = e->GetError(leftPoint,midPoint,rightPoint,alpha);
assert("check: positive_error" && error>=0);
if(error > this->MaxErrors[i])
{
this->MaxErrors[i] = error;
}
e = static_cast<vtkGenericSubdivisionErrorMetric *>
(this->ErrorMetrics->GetNextItemAsObject());
}
}
//-----------------------------------------------------------------------------
// Description:
// Init the error metric with the dataset. Should be called in each filter
// before any tessellation of any cell.
void vtkGenericCellTessellator::InitErrorMetrics(vtkGenericDataSet *ds)
{
this->Initialize(ds);
this->ErrorMetrics->InitTraversal();
vtkGenericSubdivisionErrorMetric *e =
static_cast<vtkGenericSubdivisionErrorMetric *>(this->ErrorMetrics->GetNextItemAsObject());
while(e!=0)
{
e->SetDataSet(ds);
e = static_cast<vtkGenericSubdivisionErrorMetric *>
(this->ErrorMetrics->GetNextItemAsObject());
}
if(this->Measurement)
{
this->ResetMaxErrors();
}
}
//-----------------------------------------------------------------------------
// Description:
// Reset the maximal error of each error metric. The purpose of the maximal
// error is to measure the quality of a fixed subdivision.
void vtkGenericCellTessellator::ResetMaxErrors()
{
int c = this->ErrorMetrics->GetNumberOfItems();
// Allocate the array.
if(c>this->MaxErrorsCapacity)
{
this->MaxErrorsCapacity = c;
if(this->MaxErrors)
{
delete[] this->MaxErrors;
}
this->MaxErrors = new double[this->MaxErrorsCapacity];
}
for(int i = 0; i<c; ++i)
{
this->MaxErrors[i] = 0;
}
}
//-----------------------------------------------------------------------------
// Description:
// Get the maximum error measured after the fixed subdivision.
// \pre errors_exists: errors!=0
// \pre valid_size: sizeof(errors)==GetErrorMetrics()->GetNumberOfItems()
void vtkGenericCellTessellator::GetMaxErrors(double *errors)
{
assert("pre: errors_exists" && errors!=0);
int c = this->ErrorMetrics->GetNumberOfItems();
for(int i = 0; i<c; ++i)
{
errors[i] = this->MaxErrors[i];
}
}
//-----------------------------------------------------------------------------
// Description:
// Send the current cell to error metrics. Should be called at the beginning
// of the implementation of Tessellate(), Triangulate()
// or TessellateFace()
// \pre cell_exists: cell!=0
void vtkGenericCellTessellator::SetGenericCell(vtkGenericAdaptorCell *cell)
{
assert("pre: cell_exists" && cell!=0);
this->ErrorMetrics->InitTraversal();
vtkGenericSubdivisionErrorMetric *e=static_cast<vtkGenericSubdivisionErrorMetric *>(this->ErrorMetrics->GetNextItemAsObject());
while(e!=0)
{
e->SetGenericCell(cell);
e = static_cast<vtkGenericSubdivisionErrorMetric *>
(this->ErrorMetrics->GetNextItemAsObject());
}
}