-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathspherecone.c
324 lines (290 loc) · 7.79 KB
/
spherecone.c
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
// Cone
// Changelog 03.08.17
#include "shapes.h"
/*!
\class SphereCone shapes.h
\brief This class implements a rounded cone primitive.
*/
/*!
\brief Create a rounded cone skeletal element.
This function assumes that the first radius ra is greater than rb.
\param a, b Vertices of the cone-sphere.
\param ra, rb Radii at the end vertices of the cone-sphere.
*/
SphereCone::SphereCone(const Vector& a,const Vector& b,const double& ra,const double& rb):Axis(a,b)
{
SphereCone::ra=ra;
SphereCone::rb=rb;
SphereCone::rrb=rb*rb;
SphereCone::rra=ra*ra;
double rab=ra-rb;
double s=sqrt(length*length-rab*rab);
SphereCone::ha=ra*rab/length;
SphereCone::hb=rb*rab/length;
SphereCone::hra=ra*s/length;
SphereCone::hrb=rb*s/length;
SphereCone::side=Vector(-ha/ra,hra/ra,0.0);
conelength=length*hra/ra;
}
/*!
\brief Computes the distance between a point in space and the cone.
The overall computational cost in the worst case is 12 <B>+</B>,
11 <B>*</B>, 2 <B>sqrt()</B> and 5 <B>?</B>.
Timings on a 2.4MHz PIV report 23.380 seconds for 100.000.000 queries
without sphere and cylinder optimizations. Turning both SPHERE and CYLINDER
optimizations reduce computational time to 13.020 seconds. Turning SPHERE
optimization only yields 13.110 seconds.
*/
#define SPHERE
#define CYLINDER
double SphereCone::R(const Vector& p) const
{
Vector n=p-a; // Cost: 3 +
double y=n*axis; // Cost: 3 * 2 +
double nn=n*n; // Cost: 3 * 2 +
double e=0.0;
#ifdef SPHERE
// Speed-up large sphere check (could be disabled)
if (y<ha) // Cost: 1 ?
{
// Sphere check
if (nn>rra) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=ra; // Cost: 1 +
e*=e; // Cost: 1 *
} // Overall: 2 ? 7 * 8 + 1 sqrt()
//else
//{
// e=0.0;
//}
}
else
#endif
{
// Compute squared distance to axis
double yy=y*y; // Cost: 1 *
double xx=nn-yy; // Cost: 1 +
#ifdef CYLINDER
// Speed-up cylinder check (could be disabled)
if (xx<rrb) // Cost: 1 ?
{
// Check if on the left part of the cone-sphere
if (y>length+hb) // Cost: 1 + 1 ?
{
y-=length; // Cost: 1 +
yy=y*y; // Cost: 1 *
nn=xx+yy; // Cost: 1 +
// Sphere check
if (nn>rrb) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=rb; // Cost: 1 +
e*=e; // Cost: 1 *
} // Overall: 4 ? 9 * 12 + 1 sqrt()
//else
//{
// e=0.0;
//}
}
// Otherwise, inside cylinder part of the cone
//else
//{
// e=0.0;
//}
}
else
#endif
// The last complex cases need a different coordinate system
{
double x=sqrt(xx); // Cost: 1 sqrt()
// Rotate
double ry=x*side[0]+y*side[1]; // Cost: 2 * 1+
// Sphere check
if (ry<0.0) // Cost: 1 ?
{
if (nn>rra) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=ra; // Cost: 1 +
e*=e; // Cost: 1 *
} // Overall: 4 ? 10 * 10 + 2 sqrt()
//else
//{
// e=0.0;
//}
}
// Other sphere check
else if (ry>conelength) // Cost: 1 ?
{
//cout<<"Small sphere"<<endl;
// Compute the squared distance to the other end vertex
y-=length; // Cost: 1 +
yy=y*y; // Cost: 1 *
nn=xx+yy; // Cost: 1 +
// Sphere check
if (nn>rrb) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=rb; // Cost: 1 +
e*=e; // Cost: 1 *
}
//else
//{
// e=0.0;
//} // Cost: 5 ? 11 * 12 + 2 sqrt()
}
else
{
double rx=x*side[1]-y*side[0]; // Cost: 2 * 1 +
if (rx>ra) // Cost: 1 ?
{
rx-=ra; // Cost: 1 +
e=rx*rx; // Cost: 1 *
}
// Otherwise, inside cone
//else
//{
// e=0.0;
//} // Cost: 5 ? 12 * 11 + 1 sqrt()
}
}
}
return e;
}
/*!
\brief Computes the pre-processing equations.
\param o, d Ray origin and direction (which should be normalized).
*/
void SphereCone::Set(const Vector& o,const Vector& d)
{
Vector pa=a-o;
double dx=d*axis;
double pax=pa*axis;
double dpa=d*pa;
// Quadric stores the squared distance to vertex a
quadric[2]=1.0;
quadric[1]=-2.0*dpa;
quadric[0]=pa*pa;
linear[1]=dx;
linear[0]=-pax;
}
/*!
\brief Compute the distance between a points on a line and a cone-sphere.
The member function SphereCone::Set() should be called for pre-processing.
\param t Parameter of the point on the line.
*/
double SphereCone::R(const double& t) const
{
double y=linear[1]*t+linear[0];
double nn=(quadric[2]*t+quadric[1])*t+quadric[0];
double e=0.0;
#ifdef SPHERE
// Speed-up large sphere check (could be disabled)
if (y<ha) // Cost: 1 ?
{
// Sphere check
if (nn>rra) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=ra; // Cost: 1 +
e*=e; // Cost: 1 *
} // Overall: 2 ? 7 * 8 + 1 sqrt()
//else
//{
// e=0.0;
//}
}
else
#endif
{
// Compute squared distance to axis
double yy=y*y; // Cost: 1 *
double xx=nn-yy; // Cost: 1 +
#ifdef CYLINDER
// Speed-up cylinder check (could be disabled)
if (xx<rrb) // Cost: 1 ?
{
// Check if on the left part of the cone-sphere
if (y>length+hb) // Cost: 1 + 1 ?
{
y-=length; // Cost: 1 +
yy=y*y; // Cost: 1 *
nn=xx+yy; // Cost: 1 +
// Sphere check
if (nn>rrb) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=rb; // Cost: 1 +
e*=e; // Cost: 1 *
} // Overall: 4 ? 9 * 12 + 1 sqrt()
//else
//{
// e=0.0;
//}
}
// Otherwise, inside cylinder part of the cone
//else
//{
// e=0.0;
//}
}
else
#endif
// The last complex cases need a different coordinate system
{
double x=sqrt(xx); // Cost: 1 sqrt()
// Rotate
double ry=x*side[0]+y*side[1]; // Cost: 2 * 1+
// Sphere check
if (ry<0.0) // Cost: 1 ?
{
if (nn>rra) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=ra; // Cost: 1 +
e*=e; // Cost: 1 *
} // Overall: 4 ? 10 * 10 + 2 sqrt()
//else
//{
// e=0.0;
//}
}
// Other sphere check
else if (ry>conelength) // Cost: 1 ?
{
//cout<<"Small sphere"<<endl;
// Compute the squared distance to the other end vertex
y-=length; // Cost: 1 +
yy=y*y; // Cost: 1 *
nn=xx+yy; // Cost: 1 +
// Sphere check
if (nn>rrb) // Cost: 1 ?
{
e=sqrt(nn); // Cost: 1 sqrt()
e-=rb; // Cost: 1 +
e*=e; // Cost: 1 *
}
//else
//{
// e=0.0;
//} // Cost: 5 ? 11 * 12 + 2 sqrt()
}
else
{
double rx=x*side[1]-y*side[0]; // Cost: 2 * 1 +
if (rx>ra) // Cost: 1 ?
{
rx-=ra; // Cost: 1 +
e=rx*rx; // Cost: 1 *
}
// Otherwise, inside cone
//else
//{
// e=0.0;
//} // Cost: 5 ? 12 * 11 + 1 sqrt()
}
}
}
return e;
}