forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkSuperquadric.cxx
152 lines (124 loc) · 4.14 KB
/
vtkSuperquadric.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSuperquadric.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.
=========================================================================*/
/* vtkSuperQuadric originally written by Michael Halle,
Brigham and Women's Hospital, July 1998.
Based on "Rigid physically based superquadrics", A. H. Barr,
in "Graphics Gems III", David Kirk, ed., Academic Press, 1992.
*/
#include "vtkSuperquadric.h"
#include "vtkObjectFactory.h"
#include <math.h>
vtkStandardNewMacro(vtkSuperquadric);
// Construct with superquadric radius of 0.5, toroidal off, center at 0.0,
// scale (1,1,1), size 0.5, phi roundness 1.0, and theta roundness 0.0.
vtkSuperquadric::vtkSuperquadric()
{
this->Toroidal = 0;
this->Thickness = 0.3333;
this->PhiRoundness = 0.0;
this->SetPhiRoundness(1.0);
this->ThetaRoundness = 0.0;
this->SetThetaRoundness(1.0);
this->Center[0] = this->Center[1] = this->Center[2] = 0.0;
this->Scale[0] = this->Scale[1] = this->Scale[2] = 1.0;
this->Size = .5;
}
static const double MAX_FVAL = 1e12;
static double VTK_MIN_SUPERQUADRIC_ROUNDNESS = 1e-24;
void vtkSuperquadric::SetThetaRoundness(double e)
{
if(e < VTK_MIN_SUPERQUADRIC_ROUNDNESS)
{
e = VTK_MIN_SUPERQUADRIC_ROUNDNESS;
}
if (this->ThetaRoundness != e)
{
this->ThetaRoundness = e;
this->Modified();
}
}
void vtkSuperquadric::SetPhiRoundness(double e)
{
if(e < VTK_MIN_SUPERQUADRIC_ROUNDNESS)
{
e = VTK_MIN_SUPERQUADRIC_ROUNDNESS;
}
if (this->PhiRoundness != e)
{
this->PhiRoundness = e;
this->Modified();
}
}
// Evaluate Superquadric equation
double vtkSuperquadric::EvaluateFunction(double xyz[3])
{
double e = this->ThetaRoundness;
double n = this->PhiRoundness;
double p[3], s[3];
double val;
s[0] = this->Scale[0] * this->Size;
s[1] = this->Scale[1] * this->Size;
s[2] = this->Scale[2] * this->Size;
if(this->Toroidal) {
double tval;
double alpha;
alpha = (1.0 / this->Thickness);
s[0] /= (alpha + 1.0);
s[1] /= (alpha + 1.0);
s[2] /= (alpha + 1.0);
p[0] = (xyz[0] - this->Center[0]) / s[0];
p[1] = (xyz[1] - this->Center[1]) / s[1];
p[2] = (xyz[2] - this->Center[2]) / s[2];
tval = pow((pow(fabs(p[2]), 2.0/e) + pow(fabs(p[0]), 2.0/e)), e/2.0);
val = pow(fabs(tval - alpha), 2.0/n) + pow(fabs(p[1]), 2.0/n) - 1.0;
}
else { // Ellipsoidal
p[0] = (xyz[0] - this->Center[0]) / s[0];
p[1] = (xyz[1] - this->Center[1]) / s[1];
p[2] = (xyz[2] - this->Center[2]) / s[2];
val = pow((pow(fabs(p[2]), 2.0/e) + pow(fabs(p[0]), 2.0/e)), e/n) +
pow(fabs(p[1]),2.0/n) - 1.0;
}
if(val > MAX_FVAL){
val = MAX_FVAL;
}
else if(val < -MAX_FVAL){
val = -MAX_FVAL;
}
return val;
}
// Description
// Evaluate Superquadric function gradient.
void vtkSuperquadric::EvaluateGradient(double vtkNotUsed(xyz)[3], double g[3])
{
// bogus! lazy!
// if someone wants to figure these out, they are each the
// partial of x, then y, then z with respect to f as shown above.
// Careful for the fabs().
g[0] = g[1] = g[2] = 0.0;
}
void vtkSuperquadric::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Toroidal: " << (this->Toroidal ? "On\n" : "Off\n");
os << indent << "Size: " << this->Size << "\n";
os << indent << "Thickness: " << this->Thickness << "\n";
os << indent << "ThetaRoundness: " << this->ThetaRoundness << "\n";
os << indent << "PhiRoundness: " << this->PhiRoundness << "\n";
os << indent << "Center: ("
<< this->Center[0] << ", "
<< this->Center[1] << ", "
<< this->Center[2] << ")\n";
os << indent << "Scale: ("
<< this->Scale[0] << ", "
<< this->Scale[1] << ", "
<< this->Scale[2] << ")\n";
}