-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathellipse.cpp
More file actions
280 lines (231 loc) · 6.97 KB
/
ellipse.cpp
File metadata and controls
280 lines (231 loc) · 6.97 KB
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
#include "ellipse.hpp"
#include "distance_to_ellipse.hpp"
using std::ostream;
ellipse_st::ellipse_st( double bb, double ee, double phiphi, xypoint cc ) :
b(bb), e(ee), phi(phiphi), c(cc) {
epenalty = 0.0;
if ( e>1.0 ) {
//std::cerr<<"Error eccentricity e="<<e<<" too large, capping at e=1"<<std::endl;
epenalty = (e-1.0)*(e-1.0)*100;
e=1.0;
}
if ( e<0.0 ) {
//std::cerr<<"Error eccentricity e="<<e<<" negative, capping at e=0"<<std::endl;
epenalty = e*e*100;
e=0.0;
}
if ( bb<0.0 ) bb = fabs( bb );
}
// parametric function radius of ellipse
// at theta == angle from long axis in radians
double ellipse_st::r_of_theta( double theta ) const {
double checkfirst = 1 - e*e*cos(theta)*cos(theta);
if ( checkfirst < 0. ) return 0.;
return b / sqrt( checkfirst );
}
// get x, y coordinates on ellipse at
// theta == angle from long axis in radians
xypoint ellipse_st::xy( double theta ) const{
double r = r_of_theta( theta );
xypoint p = c;
p.x += r*cos(theta) * cos( phi ) - r*sin(theta) * sin( phi );
p.y += r*cos(theta) * sin( phi ) + r*sin(theta) * cos( phi );
return p;
}
// find smallest distance from arbitrary point (px, py) to
// a point on the ellipse.
double ellipse_st::dmin( xypoint p ) const{
// slow method: scan points around ellipse
if (1){ // delete if(0) after testing get_distance
double curdmin = 999e99;
for ( double theta=0; theta<2*pi; theta+=2*pi/1000 ){
xypoint curxy = xy( theta );
double cur_d = sqrt( (curxy.x-p.x)*(curxy.x-p.x) +(curxy.y-p.y)*(curxy.y-p.y) );
if ( cur_d < curdmin ) curdmin = cur_d;
}
return curdmin;
} else {
cv::Point q;
cv::Point center( c.x, c.y );
cv::Point point( p.x, p.y );
return get_distance( center, get_a(), b, point, q, phi );
}
}
// find smallest distance-squared from arbitrary point (px, py) to
// a point on the ellipse.
double ellipse_st::dmin2( xypoint p ) const{
// slow method: scan points around ellipse
if (1){
double curdmin = 999e99;
for ( double theta=0; theta<2*pi; theta+=2*pi/1000 ){
xypoint curxy = xy( theta );
double cur_d2 = (curxy.x-p.x)*(curxy.x-p.x) +(curxy.y-p.y)*(curxy.y-p.y) ;
if ( cur_d2 < curdmin ) curdmin = cur_d2;
}
return curdmin;
} else {
double d = dmin( p );
return d*d;
}
}
// faster method of finding smallest-squared distnce from point p
// to a point on the ellipse. From:
// https://www.geometrictools.com/Documentation/DistancePointEllipseEllipsoid.pdf
double ellipse_st::fast_dmin2(xypoint p) const{
double dmin2;
bool xflip;
bool yflip;
//work in coordinates of ellipse
xypoint pprime((p.x-c.x)*cos(phi)+(p.y-c.y)*sin(phi),
-1*(p.x-c.x)*sin(phi)+(p.y-c.y)*cos(phi));
//do calculation as if point is in the first quadrant of the ellipse
if(pprime.x < 0){
pprime.x *= -1;
xflip = true;
}
if (pprime.y < 0){
pprime.y *= -1;
yflip = true;
}
double a = b/sqrt(1-e*e); //long axis of ellipse
double tbar = Findtbar(a, pprime); //find roots of F
double xmin = a*a*pprime.x/(tbar+a*a); //use tbar to get coordinates
double ymin = b*b*pprime.y/(tbar+b*b); //of closest point in ellipse
//flip back to initial quadrant
if (xflip)
xmin *= -1;
if (yflip)
ymin *= -1;
//calculate minimum distance squared
dmin2 = (pprime.x-xmin)*(pprime.x-xmin) + (pprime.y-ymin)*(pprime.y-ymin);
return dmin2;
}
// function for which roots are found that give point on ellipse
// with minimum distance to point p
double ellipse_st::Findtbar(double a, xypoint pprime) const{
double t0 = -1*b*b+b*pprime.y; //staring left most point of biseciton
//starting left most point of bisection
double t1 = -1*b*b+sqrt(a*a*pprime.x*pprime.x + b*b*pprime.y*pprime.y);
double f = F(a, pprime, t0, t1);
//int count =0;
//do the bisection method to find the roots of F.
// 0.01 can be reduced for more accurate roots
while ( sqrt(f*f) > 0.01 ){
//count++;
//if(count<20){
// std::cout<<"t1 = "<<t1<<"\tt0 = "<<t0<<std::endl;
// std::cout<<"F = "<<f<<std::endl;
//}
if(f > 0)
t0 =(t1+t0)/2;
if(f < 0)
t1 =(t1+t0)/2;
f = F(a, pprime, t0, t1);
}
double tbar = (t1+t0)/2;
return tbar;
}
double ellipse_st::F(double a, xypoint pprime, double t0, double t1) const{
double tmp = (t1+t0)/2;
return (a*pprime.x/(tmp+a*a))*(a*pprime.x/(tmp+a*a))
+ (b*pprime.y/(tmp+b*b))*(b*pprime.y/(tmp+b*b)) -1;
}
double ellipse_st::GetRoot(double r0, double z0, double z1, double g) const{
double n0 = r0*z0;
double s0 = z1-1;
double s1 = (g<0 ? 0: sqrt(n0*n0 + z1*z1) -1);
double s = 0;
while (sqrt(g*g)>0.001){
s = (s0+s1)/2;
if(s==s0 || s==s1){break;}
double ratio0 = n0/(s+r0);
double ratio1 = z1/(s+1);
g = ratio0*ratio0 + ratio1*ratio1 -1;
if(g>0){
s0 = s;
}else if(g<0){
s1=s;
}else{
break;
}
}
return s;
}
double ellipse_st::DistancePointEllipse2(xypoint p) const{
//bool xflip=false;
//bool yflip=false;
//work in coordinates of ellipse
xypoint pprime((p.x-c.x)*cos(phi)+(p.y-c.y)*sin(phi),
-1*(p.x-c.x)*sin(phi)+(p.y-c.y)*cos(phi));
//do calculation as if point is in the first quadrant of the ellipse
//if(pprime.x < 0){
// pprime.x *= -1;
// xflip = true;
// }
//if (pprime.y < 0){
// pprime.y *= -1;
// yflip = true;
// }
double x0, x1;
double dmin2=0;
double a = b/sqrt(1-e*e); //long axis of ellipse
if(pprime.y>0){
if (pprime.x > 0){
double z0 = pprime.x/a;
double z1 = pprime.y/b;
double g = z0*z0 + z1+z1 -1;
if(g!=0){
double r0 = (a/b)*(a/b);
double sbar = GetRoot(r0, z0, z1, g);
x0 = r0*pprime.x/(sbar+r0);
x1 = pprime.y/(sbar+1);
} else{
x0 = pprime.x;
x1 = pprime.y;
dmin2 = 0;
}
} else{
x0 = 0;
x1 = b;
dmin2 = (pprime.y-b)*(pprime.y-b);
}
}else{
double number0 = a*pprime.x;
double denom0 = a*a - b*b;
if(number0<denom0){
double xde0 = number0/denom0;
x0 = a*xde0;
x1 = b*sqrt(1-xde0*xde0);
dmin2 = (x0 - pprime.x)*(x0-pprime.x) + (x1-pprime.y)*(x1*pprime.y);
} else{
x0 = a;
x1 = 0;
dmin2 = (pprime.x-a)*(pprime.x-a);
}
}
return dmin2;
}
// find closest point on ellipse that is smallest distance from
// arbitrary point (px, py) to a point on the ellipse.
xypoint ellipse_st::closest_point( xypoint p ) const{
// slow method: scan points around ellipse
double curdmin = 999e99;
xypoint xymin = c;
for ( double theta=0; theta<2*pi; theta+=2*pi/1000 ){
xypoint curxy = xy( theta );
double cur_d = sqrt( (curxy.x-p.x)*(curxy.x-p.x) +(curxy.y-p.y)*(curxy.y-p.y) );
if ( cur_d < curdmin ){
curdmin = cur_d;
xymin = xy( theta );
}
}
return xymin;
}
ostream& operator<<( ostream& os, const ellipse_st & e ){
os << "center (x,y) = (" << e.c.x <<", " << e.c.y << ") "
<< " short radius b = " << e.b
<< " eccentricity = " << e.e
<< " phi = " << e.phi
<<std::endl;
return os;
}