-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathcone.c
252 lines (230 loc) · 5.86 KB
/
cone.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
// Cone
// Changelog 03.08.17
#include "shapes.h"
/*!
\class Cone shapes.h
\brief This class implements a truncated cone primitive.
*/
/*!
\brief Create a cone skeletal element.
This function assumes that the first radius ra is greater than rb.
\param a, b End vertices of the cone.
\param ra, rb Radii at the end vertices of the cone.
*/
Cone::Cone(const Vector& a,const Vector& b,const double& ra,const double& rb):Axis(a,b)
{
Cone::ra=ra;
Cone::rb=rb;
Cone::rrb=rb*rb;
Cone::rra=ra*ra;
// Compute the length of side of cone, i.e. its slant height
Cone::conelength=sqrt((rb-ra)*(rb-ra)+length*length);
// Line segment
Cone::side=Vector(rb-ra,length,0.0);
Cone::side/=conelength;
}
/*!
\brief Compute the distance between a point in space and a cone.
*/
double Cone::R(const Vector& p) const
{
// Compute revolution coordinates
Vector n=p-a; // Cost: 3 +
double y=n*axis; // Cost: 3 * 2 +
double yy=y*y; // Cost: 1 *
// Squared radial distance to axis: postpone square root evaluation only when needed
double xx=n*n-yy; // Cost: 3 * 3 +
// Overall cost: 8 + 7 *
double e=0.0;
// Distance to large cap
if (y<0.0) // Cost: 1 ?
{
// Disk : distance to plane cap
if (xx<rra) // Cost: 1 ?
{
e=yy;
}
// Distance to plane circle
else
{
double x=sqrt(xx)-ra;
e=x*x+yy; // Cost: 1 + 1 *
} // Cost in worst case: 10 + 8 * 1 sqrt() 3 ?
}
// Small cylinder test (optimization)
else if (xx<rrb) // Cost: 1 ?
{
if (y>length) // Cost: 1 ?
{
e=y-length; // Cost: 1 -
e*=e; // Cost: 1 *
} // Total cost in worst case : 9 + 8 * 3 ?
// Inside cone
//else
//{
// e=0.0;
//}
}
else
{
// Evaluate radial distance to axis
double x=sqrt(xx); // Cost: 1 sqrt()
// Change frame, so that point is now on large cap
x-=ra; // Cost: 1 -
// Distance to large cap
if (y<0.0) // Cost: 1 ?
{
// Disk : distance to plane cap
if (x<0.0) // Cost: 1 ?
{
e=yy;
}
// Distance to plane circle
else
{
e=x*x+yy; // Cost: 1 + 1 *
} // Cost in worst case: 10 + 8 * 1 sqrt() 3 ?
}
else
{
// Compute coordinates in the new rotated frame
// Postpone some computation that may not be needed in the following case
double ry=x*side[0]+y*side[1]; // Cost : 1 + 2 *
if (ry<0.0) // Cost: 1 ?
{
e=x*x+yy; // Cost: 1+ 1 *
}
else
{
double rx=x*side[1]-y*side[0]; // Cost : 1 + 2 *
if (ry>conelength) // Cost: 1 ?
{
ry-=conelength; // Cost: 1 +
e=rx*rx+ry*ry; // Cost: 1 + 2 *
}
else
{
if (rx>0.0) // Cost: 1 ?
{
e=rx*rx; // Cost: 1 *
}
//else
//{
// e=0.0;
//}
}
} // Cost in worst case: 14 + 14 * 1 sqrt() 4 ?
}
}
return e;
}
/*!
\brief Computes the pre-processing equations.
\param o, d Ray origin and direction (which should be normalized).
*/
void Cone::Set(const Vector& o,const Vector& d)
{
Vector pa=a-o;
double dx=d*axis;
double pax=pa*axis;
double dpa=d*pa;
quadric[2]=1.0-dx*dx;
quadric[1]=2.0*(dx*pax-dpa);
quadric[0]=pa*pa-pax*pax;
linear[1]=dx;
linear[0]=-pax;
}
/*!
\brief Compute the distance between a points on a line and a cone.
The member function Cone::Set() should be called for pre-processing.
\param t Parameter of the point on the line.
*/
double Cone::R(const double& t) const
{
double y=linear[1]*t+linear[0];
double xx=(quadric[2]*t+quadric[1])*t+quadric[0];
double yy=y*y; // Cost: 1 *
// Overall cost: 8 + 7 *
double e=0.0;
// Distance to large cap
if (y<0.0) // Cost: 1 ?
{
// Disk : distance to plane cap
if (xx<rra) // Cost: 1 ?
{
e=yy;
}
// Distance to plane circle
else
{
double x=sqrt(xx)-ra;
e=x*x+yy; // Cost: 1 + 1 *
} // Cost in worst case: 10 + 8 * 1 sqrt() 3 ?
}
// Small cylinder test (optimization)
else if (xx<rrb) // Cost: 1 ?
{
if (y>length) // Cost: 1 ?
{
e=y-length; // Cost: 1 -
e*=e; // Cost: 1 *
} // Total cost in worst case : 9 + 8 * 3 ?
// Inside cone
//else
//{
// e=0.0;
//}
}
else
{
// Evaluate radial distance to axis
double x=sqrt(xx); // Cost: 1 sqrt()
// Change frame, so that point is now on large cap
x-=ra; // Cost: 1 -
// Distance to large cap
if (y<0.0) // Cost: 1 ?
{
// Disk : distance to plane cap
if (x<0.0) // Cost: 1 ?
{
e=yy;
}
// Distance to plane circle
else
{
e=x*x+yy; // Cost: 1 + 1 *
} // Cost in worst case: 10 + 8 * 1 sqrt() 3 ?
}
else
{
// Compute coordinates in the new rotated frame
// Postpone some computation that may not be needed in the following case
double ry=x*side[0]+y*side[1]; // Cost : 1 + 2 *
if (ry<0.0) // Cost: 1 ?
{
e=x*x+yy; // Cost: 1+ 1 *
}
else
{
double rx=x*side[1]-y*side[0]; // Cost : 1 + 2 *
if (ry>conelength) // Cost: 1 ?
{
ry-=conelength; // Cost: 1 +
e=rx*rx+ry*ry; // Cost: 1 + 2 *
}
else
{
if (rx>0.0) // Cost: 1 ?
{
e=rx*rx; // Cost: 1 *
}
//else
//{
// e=0.0;
//}
}
} // Cost in worst case: 14 + 14 * 1 sqrt() 4 ?
}
}
return e;
}