forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkAttributesErrorMetric.cxx
272 lines (235 loc) · 8.45 KB
/
vtkAttributesErrorMetric.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAttributesErrorMetric.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 "vtkAttributesErrorMetric.h"
#include "vtkObjectFactory.h"
#include "vtkGenericAttribute.h"
#include "vtkGenericAttributeCollection.h"
#include "vtkGenericAdaptorCell.h"
#include "vtkGenericDataSet.h"
#include <assert.h>
vtkStandardNewMacro(vtkAttributesErrorMetric);
//-----------------------------------------------------------------------------
vtkAttributesErrorMetric::vtkAttributesErrorMetric()
{
this->AttributeTolerance = 0.1; // arbitrary
this->AbsoluteAttributeTolerance = 0.1; // arbitrary
this->SquareAbsoluteAttributeTolerance=this->AbsoluteAttributeTolerance*this->AbsoluteAttributeTolerance;
this->Range=0;
this->DefinedByAbsolute=1;
}
//-----------------------------------------------------------------------------
vtkAttributesErrorMetric::~vtkAttributesErrorMetric()
{
}
//-----------------------------------------------------------------------------
// Description:
// Set the absolute attribute accuracy to `value'. See
// GetAbsoluteAttributeTolerance() for details.
// \pre valid_range_value: value>0
void vtkAttributesErrorMetric::SetAbsoluteAttributeTolerance(double value)
{
assert("pre: valid_range_value" && value>0);
if(this->AbsoluteAttributeTolerance!=value || !this->DefinedByAbsolute)
{
this->AbsoluteAttributeTolerance=value;
this->SquareAbsoluteAttributeTolerance=this->AbsoluteAttributeTolerance*this->AbsoluteAttributeTolerance;
this->Range=0;
this->DefinedByAbsolute=1;
this->Modified();
}
}
//-----------------------------------------------------------------------------
// Description:
// Set the relative attribute accuracy to `value'. See
// GetAttributeTolerance() for details.
// \pre valid_range_value: value>0 && value<1
void vtkAttributesErrorMetric::SetAttributeTolerance(double value)
{
assert("pre: valid_range_value" && value>0 && value<1);
if(this->AttributeTolerance!=value || this->DefinedByAbsolute)
{
this->AttributeTolerance=value;
this->DefinedByAbsolute=0;
this->Modified();
}
}
//-----------------------------------------------------------------------------
int vtkAttributesErrorMetric::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;
double ae;
vtkGenericAttributeCollection *ac;
this->ComputeSquareAbsoluteAttributeTolerance();
const int ATTRIBUTE_OFFSET=6;
ac=this->DataSet->GetAttributes();
vtkGenericAttribute *a=ac->GetAttribute(ac->GetActiveAttribute());
if(this->GenericCell->IsAttributeLinear(a))
{
//don't need to do anything:
ae=0;
}
else
{
if(ac->GetActiveComponent()>=0)
{
int i=ac->GetAttributeIndex(ac->GetActiveAttribute())+ac->GetActiveComponent()+ATTRIBUTE_OFFSET;
double tmp=leftPoint[i]+alpha*(rightPoint[i]-leftPoint[i])-midPoint[i];
ae=tmp*tmp;
}
else // module of the vector
{
int i=ac->GetAttributeIndex(ac->GetActiveAttribute())+ATTRIBUTE_OFFSET;
int j=0;
int c=ac->GetNumberOfComponents();
double tmp;
#if 0
// If x and y are two vectors, we compute: ||x|-|y||
double interpolatedValueMod=0;
double midValueMod=0;
while(j<c)
{
tmp=leftPoint[i+j]+alpha*(rightPoint[i+j]-leftPoint[i+j]);
interpolatedValueMod+=tmp*tmp;
tmp=midPoint[i+j];
midValueMod+=tmp*tmp;
++j;
}
tmp=sqrt(midValueMod)-sqrt(interpolatedValueMod);
ae=tmp*tmp;
#else
// If x and y are two vectors, we compute: |x-y|
// We should compute ||x|-|y|| but |x-y| is usually enough
// and tends to produce less degenerated edges.
// Remind that: ||x|-|y||<=|x-y|
ae=0;
while(j<c)
{
tmp=leftPoint[i+j]+alpha*(rightPoint[i+j]-leftPoint[i+j])-midPoint[i+j];
ae+=tmp*tmp;
++j;
}
#endif
}
assert("check: positive_ae" && ae>=0);
}
if(this->SquareAbsoluteAttributeTolerance==0)
{
result=fabs(ae)>0.0001;
}
else
{
result=ae>this->SquareAbsoluteAttributeTolerance;
}
return result;
}
//-----------------------------------------------------------------------------
// Description:
// Return 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.
// \post positive_result: result>=0
double vtkAttributesErrorMetric::GetError(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);
double ae;
vtkGenericAttributeCollection *ac;
this->ComputeSquareAbsoluteAttributeTolerance();
const int ATTRIBUTE_OFFSET=6;
ac=this->DataSet->GetAttributes();
vtkGenericAttribute *a=ac->GetAttribute(ac->GetActiveAttribute());
if(this->GenericCell->IsAttributeLinear(a))
{
//don't need to do anything:
ae=0;
}
else
{
if(ac->GetActiveComponent()>=0) // one component
{
int i=ac->GetAttributeIndex(ac->GetActiveAttribute())+ac->GetActiveComponent()+ATTRIBUTE_OFFSET;
double tmp=leftPoint[i]+alpha*(rightPoint[i]-leftPoint[i])-midPoint[i];
ae=tmp*tmp;
}
else // module of the vector
{
// If x and y are two vectors, we compute: |x-y|
// We should compute ||x|-|y|| but |x-y| is usually enough
// and tends to produce less degenerated edges.
// Remind that: ||x|-|y||<=|x-y|
int i=ac->GetAttributeIndex(ac->GetActiveAttribute())+ATTRIBUTE_OFFSET;
int j=0;
int c=ac->GetNumberOfComponents();
double tmp;
ae=0;
while(j<c)
{
tmp=leftPoint[i+j]+alpha*(rightPoint[i+j]-leftPoint[i+j])-midPoint[i+j];
ae+=tmp*tmp;
++j;
}
}
}
double result;
if(this->Range!=0)
{
result=sqrt(ae)/this->Range;
}
else
{
result=0;
}
assert("post: positive_result" && result>=0);
return result;
}
//-----------------------------------------------------------------------------
void vtkAttributesErrorMetric::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "AttributeTolerance: " << this->AttributeTolerance << endl;
os << indent << "AbsoluteAttributeTolerance: " << this->AbsoluteAttributeTolerance << endl;
}
//-----------------------------------------------------------------------------
// Description:
// Compute the absolute attribute tolerance, only if the cached value is
// obsolete.
void vtkAttributesErrorMetric::ComputeSquareAbsoluteAttributeTolerance()
{
if(!this->DefinedByAbsolute)
{
if ( this->GetMTime() > this->SquareAbsoluteAttributeToleranceComputeTime )
{
vtkGenericAttributeCollection *ac=this->DataSet->GetAttributes();
vtkGenericAttribute *a=ac->GetAttribute(ac->GetActiveAttribute());
int i=ac->GetActiveComponent();
double r[2];
a->GetRange(i,r);
double tmp=(r[1]-r[0])*this->AttributeTolerance;
this->Range=r[1]-r[0];
this->SquareAbsoluteAttributeTolerance=tmp*tmp;
this->SquareAbsoluteAttributeToleranceComputeTime.Modified();
this->AbsoluteAttributeTolerance=sqrt(this->SquareAbsoluteAttributeTolerance);
}
}
}