VMTK
Loading...
Searching...
No Matches
vtkvmtkMath Class Reference

Provides static utility mathematical functions to avoid repeating code across classes. More...

#include <vtkvmtkMath.h>

Inheritance diagram for vtkvmtkMath:
[legend]
Collaboration diagram for vtkvmtkMath:
[legend]

Public Types

typedef vtkObject Superclass

Public Member Functions

virtual int IsA (const char *type)
vtkvmtkMath * NewInstance () const

Static Public Member Functions

static int IsTypeOf (const char *type)
static vtkvmtkMath * SafeDownCast (vtkObjectBase *o)
static vtkvmtkMath * New ()
static double Cotangent (double point0[3], double point1[3], double point2[3])
static double TriangleArea (double point0[3], double point1[3], double point2[3])
static int IsAngleObtuse (double point0[3], double point1[3], double point2[3])
static int IsTriangleObtuse (double point0[3], double point1[3], double point2[3])
static double VoronoiSectorArea (double point0[3], double point1[3], double point2[3])
static double TriangleGradient (double point0[3], double point1[3], double point2[3], double scalar0, double scalar1, double scalar2, double gradient[3])
static void TwoSphereIntersection (double center0[3], double radius0, double center1[3], double radius1, double origin[3], double normal[3])
static double AngleBetweenNormals (double normal0[3], double normal1[3])
static double EvaluateSphereFunction (double center[3], double radius, double point[3])

Protected Member Functions

virtual vtkObjectBase * NewInstanceInternal () const
 vtkvmtkMath ()
 ~vtkvmtkMath ()

Static Protected Member Functions

static double GetTolerance (float cookie)
static double GetTolerance (double cookie)
static double GetLarge (float cookie)
static double GetLarge (double cookie)

Detailed Description

Provides static utility mathematical functions to avoid repeating code across classes.

All arguments expect doubles.

Definition at line 37 of file vtkvmtkMath.h.

Member Typedef Documentation

◆ Superclass

typedef vtkObject vtkvmtkMath::Superclass

Definition at line 41 of file vtkvmtkMath.h.

Constructor & Destructor Documentation

◆ vtkvmtkMath()

vtkvmtkMath::vtkvmtkMath ( )
inlineprotected

Definition at line 114 of file vtkvmtkMath.h.

114{};

◆ ~vtkvmtkMath()

vtkvmtkMath::~vtkvmtkMath ( )
inlineprotected

Definition at line 115 of file vtkvmtkMath.h.

115{};

Member Function Documentation

◆ IsTypeOf()

int vtkvmtkMath::IsTypeOf ( const char * type)
static

◆ IsA()

virtual int vtkvmtkMath::IsA ( const char * type)
virtual

◆ SafeDownCast()

vtkvmtkMath * vtkvmtkMath::SafeDownCast ( vtkObjectBase * o)
static

◆ NewInstanceInternal()

virtual vtkObjectBase * vtkvmtkMath::NewInstanceInternal ( ) const
protectedvirtual

◆ NewInstance()

vtkvmtkMath * vtkvmtkMath::NewInstance ( ) const

◆ New()

vtkvmtkMath * vtkvmtkMath::New ( )
static

◆ Cotangent()

double vtkvmtkMath::Cotangent ( double point0[3],
double point1[3],
double point2[3] )
static

Compute the cotangent of the angle at point1 subtended by the segments point1-point0 and point1-point2 (i.e. the angle at the vertex shared by the two segments). Degenerate (zero-area) configurations return either 0.0 or a large sentinel value (see VTK_VMTK_LARGE_DOUBLE), depending on whether the segments are also aligned (dot product near zero).

Definition at line 28 of file vtkvmtkMath.cxx.

29 {
30 double norm0Squared, norm1Squared, dot, crossNorm, cotangent;
31
32 norm0Squared = (point0[0]-point1[0])*(point0[0]-point1[0]) + (point0[1]-point1[1])*(point0[1]-point1[1]) + (point0[2]-point1[2])*(point0[2]-point1[2]);
33
34 norm1Squared = (point2[0]-point1[0])*(point2[0]-point1[0]) + (point2[1]-point1[1])*(point2[1]-point1[1]) + (point2[2]-point1[2])*(point2[2]-point1[2]);
35
36 dot = (point0[0]-point1[0])*(point2[0]-point1[0]) + (point0[1]-point1[1])*(point2[1]-point1[1]) + (point0[2]-point1[2])*(point2[2]-point1[2]);
37
38 crossNorm = sqrt(norm0Squared * norm1Squared - dot*dot);
39
40 cotangent = 0.0;
41
42 if (crossNorm < GetTolerance(crossNorm))
43 {
44 if (fabs(dot) < GetTolerance(dot))
45 cotangent = 0.0;
46 else
47 cotangent = GetLarge(cotangent);
48 }
49 else if (fabs(dot) < GetTolerance(dot))
50 {
51 cotangent = 0.0;
52 }
53 else
54 {
55 cotangent = dot / crossNorm;
56 }
57
58 return cotangent;
59 }
static double GetLarge(float cookie)
static double GetTolerance(float cookie)

◆ TriangleArea()

double vtkvmtkMath::TriangleArea ( double point0[3],
double point1[3],
double point2[3] )
static

Compute the area of the triangle formed by point0, point1, point2, via the norm of the cross product of two of its edge vectors.

Definition at line 61 of file vtkvmtkMath.cxx.

62 {
63 double norm0Squared, norm1Squared, dot, crossNorm;
64
65 norm0Squared = (point0[0]-point1[0])*(point0[0]-point1[0]) + (point0[1]-point1[1])*(point0[1]-point1[1]) + (point0[2]-point1[2])*(point0[2]-point1[2]);
66 norm1Squared = (point2[0]-point1[0])*(point2[0]-point1[0]) + (point2[1]-point1[1])*(point2[1]-point1[1]) + (point2[2]-point1[2])*(point2[2]-point1[2]);
67 dot = (point0[0]-point1[0])*(point2[0]-point1[0]) + (point0[1]-point1[1])*(point2[1]-point1[1]) + (point0[2]-point1[2])*(point2[2]-point1[2]);
68
69 crossNorm = sqrt(norm0Squared * norm1Squared - dot*dot);
70
71 if (crossNorm < GetTolerance(crossNorm))
72 crossNorm = 0.0;
73
74 return 0.5 * crossNorm;
75 }

◆ IsAngleObtuse()

int vtkvmtkMath::IsAngleObtuse ( double point0[3],
double point1[3],
double point2[3] )
static

Return 1 if the angle at point1 subtended by point0 and point2 is obtuse (the dot product of the two edge vectors from point1 is negative, within tolerance), 0 otherwise.

Definition at line 77 of file vtkvmtkMath.cxx.

78 {
79 if ((point0[0]-point1[0])*(point2[0]-point1[0]) + (point0[1]-point1[1])*(point2[1]-point1[1]) + (point0[2]-point1[2])*(point2[2]-point1[2])<GetTolerance(point0[0]))
80 return 1;
81
82 return 0;
83 }

◆ IsTriangleObtuse()

int vtkvmtkMath::IsTriangleObtuse ( double point0[3],
double point1[3],
double point2[3] )
static

Classify the triangle (point0, point1, point2) according to which, if any, of its angles is obtuse. Returns VTK_VMTK_OBTUSE_IN_POINT if the angle at point1 is obtuse, VTK_VMTK_OBTUSE_NOT_IN_POINT if the angle at point0 or point2 is obtuse instead, or VTK_VMTK_NON_OBTUSE if the triangle is non-obtuse. Used to select between the Voronoi and simple barycentric/mixed area formulas when computing per-vertex mesh areas (e.g. in Voronoi/mixed-area curvature or stencil weighting schemes).

Definition at line 85 of file vtkvmtkMath.cxx.

86 {
87 if (IsAngleObtuse(point0,point1,point2))
89 else if (IsAngleObtuse(point2,point0,point1) || IsAngleObtuse(point1,point2,point0))
91
93 }
static int IsAngleObtuse(double point0[3], double point1[3], double point2[3])
#define VTK_VMTK_OBTUSE_NOT_IN_POINT
Definition vtkvmtkMath.h:35
#define VTK_VMTK_NON_OBTUSE
Definition vtkvmtkMath.h:33
#define VTK_VMTK_OBTUSE_IN_POINT
Definition vtkvmtkMath.h:34

◆ VoronoiSectorArea()

double vtkvmtkMath::VoronoiSectorArea ( double point0[3],
double point1[3],
double point2[3] )
static

Compute the contribution of point1 to the (non-obtuse) Voronoi region area within the triangle (point0, point1, point2), using the cotangent-weighted formula 1/8 * (|point0-point1|^2 * cot(angle at point2) + |point2-point1|^2 * cot(angle at point0)) (Meyer et al.). Only meaningful when the triangle is non-obtuse; callers typically dispatch on IsTriangleObtuse() first and fall back to a simpler area split for obtuse triangles.

Definition at line 95 of file vtkvmtkMath.cxx.

96 {
97 double cotAlpha, cotBeta, norm0Squared, norm1Squared;
98 double sectorArea;
99
100 cotAlpha = Cotangent(point2,point0,point1);
101 cotBeta = Cotangent(point1,point2,point0);
102
103 norm0Squared = (point0[0]-point1[0])*(point0[0]-point1[0]) + (point0[1]-point1[1])*(point0[1]-point1[1]) + (point0[2]-point1[2])*(point0[2]-point1[2]);
104 norm1Squared = (point2[0]-point1[0])*(point2[0]-point1[0]) + (point2[1]-point1[1])*(point2[1]-point1[1]) + (point2[2]-point1[2])*(point2[2]-point1[2]);
105
106 sectorArea = 1.0 / 8.0 * (norm0Squared * cotBeta + norm1Squared * cotAlpha);
107
108 if (sectorArea < GetTolerance(sectorArea))
109 sectorArea = 0.0;
110
111 return sectorArea;
112 }
static double Cotangent(double point0[3], double point1[3], double point2[3])

◆ TriangleGradient()

double vtkvmtkMath::TriangleGradient ( double point0[3],
double point1[3],
double point2[3],
double scalar0,
double scalar1,
double scalar2,
double gradient[3] )
static

Compute the gradient, over the triangle (point0, point1, point2), of a scalar field linearly interpolated from the per-vertex values scalar0, scalar1, scalar2. The gradient vector (constant over the triangle) is stored in gradient and its magnitude is returned. If the triangle is degenerate (zero area), returns 0.0 when the scalar values are all equal, or a large sentinel value otherwise.

Definition at line 114 of file vtkvmtkMath.cxx.

115{
116 double vector0[3], vector1[3], vector2[3];
117 double gradientNormalVector[3], gradientDirection[3], gradientNorm;
118 double triangleNormal[3];
119 double area;
120
121 area = TriangleArea(point0,point1,point2);
122
123 if (area<GetTolerance(area))
124 {
125 gradientNormalVector[0] = 0.0;
126 gradientNormalVector[1] = 0.0;
127 gradientNormalVector[2] = 0.0;
128 if ((fabs(scalar0-scalar1)>GetTolerance(scalar0))||(fabs(scalar1-scalar2)>GetTolerance(scalar1))||(fabs(scalar2-scalar0)>GetTolerance(scalar2)))
129 return GetLarge(scalar0);
130 else
131 return 0.0;
132 }
133
134 vector0[0] = point2[0] - point1[0];
135 vector0[1] = point2[1] - point1[1];
136 vector0[2] = point2[2] - point1[2];
137 vector1[0] = point0[0] - point2[0];
138 vector1[1] = point0[1] - point2[1];
139 vector1[2] = point0[2] - point2[2];
140 vector2[0] = point1[0] - point0[0];
141 vector2[1] = point1[1] - point0[1];
142 vector2[2] = point1[2] - point0[2];
143
144 vtkMath::Cross(vector0,vector1,triangleNormal);
145 vtkMath::Normalize(triangleNormal);
146
147 gradientNormalVector[0] = vector0[0]*scalar0 + vector1[0]*scalar1 + vector2[0]*scalar2;
148 gradientNormalVector[1] = vector0[1]*scalar0 + vector1[1]*scalar1 + vector2[1]*scalar2;
149 gradientNormalVector[2] = vector0[2]*scalar0 + vector1[2]*scalar1 + vector2[2]*scalar2;
150 gradientNormalVector[0] /= 2.0*area;
151 gradientNormalVector[1] /= 2.0*area;
152 gradientNormalVector[2] /= 2.0*area;
153
154 gradientNorm = vtkMath::Norm(gradientNormalVector);
155
156 vtkMath::Cross(triangleNormal,gradientNormalVector,gradientDirection);
157 vtkMath::Normalize(gradientDirection);
158
159 gradient[0] = gradientNorm * gradientDirection[0];
160 gradient[1] = gradientNorm * gradientDirection[1];
161 gradient[2] = gradientNorm * gradientDirection[2];
162
163 return gradientNorm;
164}
static double TriangleArea(double point0[3], double point1[3], double point2[3])

◆ TwoSphereIntersection()

void vtkvmtkMath::TwoSphereIntersection ( double center0[3],
double radius0,
double center1[3],
double radius1,
double origin[3],
double normal[3] )
static

Compute the center (origin) and axis (normal) of the circle formed by the intersection of two spheres (center0, radius0) and (center1, radius1); origin lies on the line joining the two centers. If the two centers coincide, origin is set to center1 and normal to the zero vector.

Definition at line 166 of file vtkvmtkMath.cxx.

167{
168 double distance, displacement, squaredRadiusDifference, displacementRatio;
169
170 normal[0] = center1[0] - center0[0];
171 normal[1] = center1[1] - center0[1];
172 normal[2] = center1[2] - center0[2];
173
174 distance = vtkMath::Normalize(normal);
175
176 if (distance<GetTolerance(distance))
177 {
178 origin[0] = center1[0];
179 origin[1] = center1[1];
180 origin[2] = center1[2];
181 normal[0] = normal[1] = normal[2] = 0.0;
182 return;
183 }
184
185 squaredRadiusDifference = radius0*radius0 - radius1*radius1;
186 displacementRatio = squaredRadiusDifference/distance;
187
188 if (distance*distance - fabs(squaredRadiusDifference) > GetTolerance(distance))
189 {
190 displacement = 0.5 * (displacementRatio + distance);
191 }
192 else
193 {
194 if (squaredRadiusDifference > GetTolerance(distance))
195 {
196 displacement = 0.5 * (displacementRatio - distance);
197 }
198 else
199 {
200 displacement = 0.5 * (displacementRatio + distance);
201 }
202 }
203
204 origin[0] = center0[0] + displacement * normal[0];
205 origin[1] = center0[1] + displacement * normal[1];
206 origin[2] = center0[2] + displacement * normal[2];
207}

◆ AngleBetweenNormals()

double vtkvmtkMath::AngleBetweenNormals ( double normal0[3],
double normal1[3] )
static

Compute the angle, in radians, between two (not necessarily unit-length) normal vectors, using the numerically stable formula 2*atan2(|normal0-normal1|, |normal0+normal1|) (equivalent to acos of the normalized dot product, but well-conditioned for angles near 0 or pi).

Definition at line 209 of file vtkvmtkMath.cxx.

210{
211 double sum[3], difference[3];
212 double sumNorm, differenceNorm;
213
214 sum[0] = normal0[0] + normal1[0];
215 sum[1] = normal0[1] + normal1[1];
216 sum[2] = normal0[2] + normal1[2];
217 sumNorm = vtkMath::Norm(sum);
218
219 difference[0] = normal0[0] - normal1[0];
220 difference[1] = normal0[1] - normal1[1];
221 difference[2] = normal0[2] - normal1[2];
222 differenceNorm = vtkMath::Norm(difference);
223
224 return 2.0 * atan2(differenceNorm,sumNorm);
225}

◆ EvaluateSphereFunction()

double vtkvmtkMath::EvaluateSphereFunction ( double center[3],
double radius,
double point[3] )
static

Evaluate the implicit sphere function of center/radius at point: the squared distance from point to center minus radius squared. Negative inside the sphere, positive outside, zero on the sphere.

Definition at line 227 of file vtkvmtkMath.cxx.

228{
229 return (center[0]-point[0]) * (center[0]-point[0]) + (center[1]-point[1]) * (center[1]-point[1]) + (center[2]-point[2]) * (center[2]-point[2]) - radius * radius;
230}

◆ GetTolerance() [1/2]

double vtkvmtkMath::GetTolerance ( float cookie)
inlinestaticprotected

Definition at line 117 of file vtkvmtkMath.h.

118 {
119 return VTK_VMTK_FLOAT_TOL;
120 }
#define VTK_VMTK_FLOAT_TOL

◆ GetTolerance() [2/2]

double vtkvmtkMath::GetTolerance ( double cookie)
inlinestaticprotected

Definition at line 122 of file vtkvmtkMath.h.

123 {
124 return VTK_VMTK_DOUBLE_TOL;
125 }
#define VTK_VMTK_DOUBLE_TOL

◆ GetLarge() [1/2]

double vtkvmtkMath::GetLarge ( float cookie)
inlinestaticprotected

Definition at line 127 of file vtkvmtkMath.h.

128 {
129 return VTK_VMTK_LARGE_FLOAT;
130 }
#define VTK_VMTK_LARGE_FLOAT

◆ GetLarge() [2/2]

double vtkvmtkMath::GetLarge ( double cookie)
inlinestaticprotected

Definition at line 132 of file vtkvmtkMath.h.

133 {
134 return VTK_VMTK_LARGE_DOUBLE;
135 }
#define VTK_VMTK_LARGE_DOUBLE

The documentation for this class was generated from the following files: