forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkSphere.cxx
345 lines (319 loc) · 11.9 KB
/
vtkSphere.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSphere.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 "vtkSphere.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkSphere);
//----------------------------------------------------------------------------
// Construct sphere with center at (0,0,0) and radius=0.5.
vtkSphere::vtkSphere()
{
this->Radius = 0.5;
this->Center[0] = 0.0;
this->Center[1] = 0.0;
this->Center[2] = 0.0;
}
//----------------------------------------------------------------------------
// Evaluate sphere equation ((x-x0)^2 + (y-y0)^2 + (z-z0)^2) - R^2.
double vtkSphere::EvaluateFunction(double x[3])
{
return ( ((x[0] - this->Center[0]) * (x[0] - this->Center[0]) +
(x[1] - this->Center[1]) * (x[1] - this->Center[1]) +
(x[2] - this->Center[2]) * (x[2] - this->Center[2])) -
this->Radius*this->Radius );
}
//----------------------------------------------------------------------------
// Evaluate sphere gradient.
void vtkSphere::EvaluateGradient(double x[3], double n[3])
{
n[0] = 2.0 * (x[0] - this->Center[0]);
n[1] = 2.0 * (x[1] - this->Center[1]);
n[2] = 2.0 * (x[2] - this->Center[2]);
}
// The following methods are used to compute bounding spheres.
//
#define VTK_ASSIGN_POINT(_x,_y) {_x[0]=_y[0];_x[1]=_y[1];_x[2]=_y[2];}
//----------------------------------------------------------------------------
// Inspired by Graphics Gems Vol. I ("An Efficient Bounding Sphere" by Jack Ritter).
// The algorithm works in two parts: first an initial estimate of the largest sphere;
// second an adjustment to the sphere to make sure that it includes all the points.
// Typically this returns a bounding sphere that is ~5% larger than the minimal
// bounding sphere.
template <class T>
void vtkSphereComputeBoundingSphere(T *pts, vtkIdType numPts, T sphere[4],
vtkIdType hints[2])
{
sphere[0] = sphere[1] = sphere[2] = sphere[3] = 0.0;
if ( numPts < 1 )
{
return;
}
vtkIdType i;
T *p, d1[3], d2[3];
if ( hints )
{
p = pts + 3*hints[0];
VTK_ASSIGN_POINT(d1,p);
p = pts + 3*hints[1];
VTK_ASSIGN_POINT(d2,p);
}
else //no hints provided, compute an initial guess
{
T xMin[3], xMax[3], yMin[3], yMax[3], zMin[3], zMax[3];
xMin[0] = xMin[1] = xMin[2] = VTK_LARGE_FLOAT;
yMin[0] = yMin[1] = yMin[2] = VTK_LARGE_FLOAT;
zMin[0] = zMin[1] = zMin[2] = VTK_LARGE_FLOAT;
xMax[0] = xMax[1] = xMax[2] = -VTK_LARGE_FLOAT;
yMax[0] = yMax[1] = yMax[2] = -VTK_LARGE_FLOAT;
zMax[0] = zMax[1] = zMax[2] = -VTK_LARGE_FLOAT;
// First part: Estimate the points furthest apart to define the largest sphere.
// Find the points that span the greatest distance on the x-y-z axes. Use these
// two points to define a sphere centered between the two points.
for (p=pts, i=0; i<numPts; ++i, p+=3)
{
if (p[0] < xMin[0] ) VTK_ASSIGN_POINT(xMin,p);
if (p[0] > xMax[0] ) VTK_ASSIGN_POINT(xMax,p);
if (p[1] < yMin[1] ) VTK_ASSIGN_POINT(yMin,p);
if (p[1] > yMax[1] ) VTK_ASSIGN_POINT(yMax,p);
if (p[2] < zMin[2] ) VTK_ASSIGN_POINT(zMin,p);
if (p[2] > zMax[2] ) VTK_ASSIGN_POINT(zMax,p);
}
T xSpan = (xMax[0]-xMin[0])*(xMax[0]-xMin[0]) + (xMax[1]-xMin[1])*(xMax[1]-xMin[1]) +
(xMax[2]-xMin[2])*(xMax[2]-xMin[2]);
T ySpan = (yMax[0]-yMin[0])*(yMax[0]-yMin[0]) + (yMax[1]-yMin[1])*(yMax[1]-yMin[1]) +
(yMax[2]-yMin[2])*(yMax[2]-yMin[2]);
T zSpan = (zMax[0]-zMin[0])*(zMax[0]-zMin[0]) + (zMax[1]-zMin[1])*(zMax[1]-zMin[1]) +
(zMax[2]-zMin[2])*(zMax[2]-zMin[2]);
if ( xSpan > ySpan )
{
if ( xSpan > zSpan )
{
VTK_ASSIGN_POINT(d1,xMin);
VTK_ASSIGN_POINT(d2,xMax);
}
else
{
VTK_ASSIGN_POINT(d1,zMin);
VTK_ASSIGN_POINT(d2,zMax);
}
}
else //ySpan > xSpan
{
if ( ySpan > zSpan )
{
VTK_ASSIGN_POINT(d1,yMin);
VTK_ASSIGN_POINT(d2,yMax);
}
else
{
VTK_ASSIGN_POINT(d1,zMin);
VTK_ASSIGN_POINT(d2,zMax);
}
}
}//no hints provided
// Compute intial estimated sphere
sphere[0] = (d1[0]+d2[0]) / 2.0;
sphere[1] = (d1[1]+d2[1]) / 2.0;
sphere[2] = (d1[2]+d2[2]) / 2.0;
T r2 = vtkMath::Distance2BetweenPoints(d1,d2)/4.0;
sphere[3] = sqrt(r2);
// Second part: Make a pass over the points to make sure that they fit inside the sphere.
// If not, adjust the sphere to fit the point.
T dist, dist2, delta;
for (p=pts, i=0; i<numPts; ++i, p+=3)
{
dist2 = vtkMath::Distance2BetweenPoints(p,sphere);
if ( dist2 > r2 )
{
dist = sqrt(dist2);
sphere[3] = (sphere[3] + dist) / 2.0;
r2 = sphere[3]*sphere[3];
delta = dist - sphere[3];
sphere[0] = (sphere[3]*sphere[0] + delta*p[0]) / dist;
sphere[1] = (sphere[3]*sphere[1] + delta*p[1]) / dist;
sphere[2] = (sphere[3]*sphere[2] + delta*p[2]) / dist;
}
}
}
#undef VTK_ASSIGN_POINT
#define VTK_ASSIGN_SPHERE(_x,_y) {_x[0]=_y[0];_x[1]=_y[1];_x[2]=_y[2];_x[3]=_y[3];}
// An approximation to the bounding sphere of a set of spheres. The algorithm
// creates an iniitial approximation from two spheres that are expected to be
// the farthest apart (taking into accout their radius). A second pass may
// grow the bounding sphere if the remaining spheres are not contained within
// it. The hints[2] array indicates two spheres that are expected to be the
// farthest apart.
//----------------------------------------------------------------------------
template <class T>
void vtkSphereComputeBoundingSphere(T **spheres, vtkIdType numSpheres, T sphere[4],
vtkIdType hints[2])
{
if ( numSpheres < 1 )
{
sphere[0] = sphere[1] = sphere[2] = sphere[3] = 0.0;
return;
}
else if ( numSpheres == 1 )
{
VTK_ASSIGN_SPHERE(sphere,spheres[0]);
return;
}
// Okay two or more spheres
vtkIdType i, j;
T *s, s1[4], s2[4];
if ( hints )
{
s = spheres[hints[0]];
VTK_ASSIGN_SPHERE(s1,s);
s = spheres[hints[1]];
VTK_ASSIGN_SPHERE(s2,s);
}
else //no hints provided, compute an initial guess
{
T xMin[4], xMax[4], yMin[4], yMax[4], zMin[4], zMax[4];
xMin[0] = xMin[1] = xMin[2] = xMin[3] = VTK_LARGE_FLOAT;
yMin[0] = yMin[1] = yMin[2] = yMin[3] = VTK_LARGE_FLOAT;
zMin[0] = zMin[1] = zMin[2] = zMin[3] = VTK_LARGE_FLOAT;
xMax[0] = xMax[1] = xMax[2] = xMax[3] = -VTK_LARGE_FLOAT;
yMax[0] = yMax[1] = yMax[2] = yMax[3] = -VTK_LARGE_FLOAT;
zMax[0] = zMax[1] = zMax[2] = zMax[3] = -VTK_LARGE_FLOAT;
// First part: Estimate the points furthest apart to define the largest sphere.
// Find the points that span the greatest distance on the x-y-z axes. Use these
// two points to define a sphere centered between the two points.
for (i=0; i<numSpheres; ++i)
{
s = spheres[i];
if ((s[0]-s[3]) < xMin[0] ) VTK_ASSIGN_SPHERE(xMin,s);
if ((s[0]+s[3]) > xMax[0] ) VTK_ASSIGN_SPHERE(xMax,s);
if ((s[1]-s[3]) < yMin[1] ) VTK_ASSIGN_SPHERE(yMin,s);
if ((s[1]+s[3]) > yMax[1] ) VTK_ASSIGN_SPHERE(yMax,s);
if ((s[2]-s[3]) < zMin[2] ) VTK_ASSIGN_SPHERE(zMin,s);
if ((s[2]+s[3]) > zMax[2] ) VTK_ASSIGN_SPHERE(zMax,s);
}
T xSpan = (xMax[0]+xMax[3]-xMin[0]-xMin[3])*(xMax[0]+xMax[3]-xMin[0]-xMin[3]) +
(xMax[1]+xMax[3]-xMin[1]-xMin[3])*(xMax[1]+xMax[3]-xMin[1]-xMin[3]) +
(xMax[2]+xMax[3]-xMin[2]-xMin[3])*(xMax[2]+xMax[3]-xMin[2]-xMin[3]);
T ySpan = (yMax[0]+yMax[3]-yMin[0]-yMin[3])*(yMax[0]+yMax[3]-yMin[0]-yMin[3]) +
(yMax[1]+yMax[3]-yMin[1]-yMin[3])*(yMax[1]+yMax[3]-yMin[1]-yMin[3]) +
(yMax[2]+yMax[3]-yMin[2]-yMin[3])*(yMax[2]+yMax[3]-yMin[2]-yMin[3]);
T zSpan = (zMax[0]+zMax[3]-zMin[0]-zMin[3])*(zMax[0]+zMax[3]-zMin[0]-zMin[3]) +
(zMax[1]+zMax[3]-zMin[1]-zMin[3])*(zMax[1]+zMax[3]-zMin[1]-zMin[3]) +
(zMax[2]+zMax[3]-zMin[2]-zMin[3])*(zMax[2]+zMax[3]-zMin[2]-zMin[3]);
if ( xSpan > ySpan )
{
if ( xSpan > zSpan )
{
VTK_ASSIGN_SPHERE(s1,xMin);
VTK_ASSIGN_SPHERE(s2,xMax);
}
else
{
VTK_ASSIGN_SPHERE(s1,zMin);
VTK_ASSIGN_SPHERE(s2,zMax);
}
}
else //ySpan > xSpan
{
if ( ySpan > zSpan )
{
VTK_ASSIGN_SPHERE(s1,yMin);
VTK_ASSIGN_SPHERE(s2,yMax);
}
else
{
VTK_ASSIGN_SPHERE(s1,zMin);
VTK_ASSIGN_SPHERE(s2,zMax);
}
}
}//no hints provided
// Compute intial estimated sphere, take into account the radius of each sphere
T tmp, v[3], r2 = vtkMath::Distance2BetweenPoints(s1,s2)/4.0;
sphere[3] = sqrt(r2);
T t1 = -s1[3]/(2.0*sphere[3]);
T t2 = 1.0 + s2[3]/(2.0*sphere[3]);
for (i=0; i<3; ++i)
{
v[i] = s2[i] - s1[i];
tmp = s1[i] + t1*v[i];
s2[i] = s1[i] + t2*v[i];
s1[i] = tmp;
sphere[i] = (s1[i]+s2[i]) / 2.0;
}
r2 = vtkMath::Distance2BetweenPoints(s1,s2)/4.0;
sphere[3] = sqrt(r2);
// Second part: Make a pass over the points to make sure that they fit inside the sphere.
// If not, adjust the sphere to fit the point.
T dist, dist2, fac, sR2;
for (i=0; i<numSpheres; ++i)
{
s = spheres[i];
sR2 = s[3]*s[3];
dist2 = vtkMath::Distance2BetweenPoints(s,sphere);
if ( sR2 > dist2 ) //approximation to avoid square roots if possible
{
fac = 2.0*sR2;
}
else
{
fac = 2.0*dist2;
}
if ( (dist2 + fac + sR2) > r2 ) //approximate test
{
dist = sqrt(dist2);
if ( ((dist+s[3])*(dist+s[3])) > r2 ) //more accurate test
{
for (j=0; j<3; ++j)
{
v[j] = s[j] - sphere[j];
s1[j] = sphere[j] - (sphere[3]/dist)*v[j];
s2[j] = sphere[j] + (1.0+s[3]/dist)*v[j];
sphere[j] = (s1[j]+s2[j]) / 2.0;
}
r2 = vtkMath::Distance2BetweenPoints(s1,s2)/4.0;
sphere[3] = sqrt(r2);
}
}
}
}
#undef VTK_ASSIGN_SPHERE
// Type specific wrappers for the templated functions below
//----------------------------------------------------------------------------
void vtkSphere::ComputeBoundingSphere(float *pts, vtkIdType numPts, float sphere[4],
vtkIdType hints[2])
{
vtkSphereComputeBoundingSphere(pts,numPts,sphere,hints);
}
//----------------------------------------------------------------------------
void vtkSphere::ComputeBoundingSphere(double *pts, vtkIdType numPts, double sphere[4],
vtkIdType hints[2])
{
vtkSphereComputeBoundingSphere(pts,numPts,sphere,hints);
}
//----------------------------------------------------------------------------
void vtkSphere::ComputeBoundingSphere(float **spheres, vtkIdType numSpheres, float sphere[4],
vtkIdType hints[2])
{
vtkSphereComputeBoundingSphere(spheres,numSpheres,sphere,hints);
}
//----------------------------------------------------------------------------
void vtkSphere::ComputeBoundingSphere(double **spheres, vtkIdType numSpheres, double sphere[4],
vtkIdType hints[2])
{
vtkSphereComputeBoundingSphere(spheres,numSpheres,sphere,hints);
}
//----------------------------------------------------------------------------
void vtkSphere::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Radius: " << this->Radius << "\n";
os << indent << "Center: (" << this->Center[0] << ", "
<< this->Center[1] << ", " << this->Center[2] << ")\n";
}