Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JEllipse2D.hh
Go to the documentation of this file.
1#ifndef __JELLIPSE2D__
2#define __JELLIPSE2D__
3
4#include <istream>
5#include <ostream>
6#include <cmath>
7
8#include "JLang/JException.hh"
9#include "JLang/JManip.hh"
10
11#include "JMath/JConstants.hh"
12#include "JMath/JMatrix2S.hh"
13#include "JMath/JMatrix3S.hh"
14
17
18
19namespace JGEOMETRY2D {}
20namespace JPP { using namespace JGEOMETRY2D; }
21
22namespace JGEOMETRY2D {
23
25
26 /**
27 * Ellipse.
28 */
29 struct JEllipse2D :
30 public JPosition2D
31 {
32 static const int MINIMUM_NUMBER_OF_POINTS = 3;
33
34 /**
35 * Default constructor.
36 */
38 {}
39
40
41 /**
42 * Constructor.
43 *
44 * Khachiyan algorithm for Minimum Volume Enclosing Ellipse (MVEE).\n
45 * The template argument <tt>T</tt> refers to a data structure which should have the following member methods:
46 * - double getX(); // [a.u.]
47 * - double getY(); // [a.u.]
48 *
49 * \param __begin begin of data
50 * \param __end end of data
51 * \param Nmax maximum number of iterations
52 * \param precision precision
53 */
54 template<class T>
55 JEllipse2D(T __begin,
56 T __end,
57 const size_t Nmax = 1000,
58 const double precision = 1.0e-3)
59 {
60 using namespace std;
61 using namespace JPP;
62
63 const int N = distance(__begin, __end);
64
66 THROW(JValueOutOfRange, "Not enough points " << N << " < " << MINIMUM_NUMBER_OF_POINTS);
67 }
68
69 vector<double> U(N, 1.0 / (double) N); // initialize weights uniformly
70
72
73 // A = P^T x U x P, where P_i = (x_i,y_i,1)
74
75 JMatrix3S A;
76
77 for (T p = __begin; p != __end; ++p) {
78
79 const int i = distance(__begin, p);
80
81 A.a00 += p->getX() * U[i] * p->getX();
82 A.a01 += p->getX() * U[i] * p->getY();
83 A.a02 += p->getX() * U[i] * 1.0;
84
85 A.a11 += p->getY() * U[i] * p->getY();
86 A.a12 += p->getY() * U[i] * 1.0;
87
88 A.a22 += 1.0 * U[i] * 1.0;
89 }
90
91 A.a10 = A.a01;
92
93 A.a20 = A.a02;
94 A.a21 = A.a12;
95
96
97 // invert A to get ellipse matrix
98
99 try {
100 A.invert();
101 }
102 catch(exception& error) {
103 return;
104 }
105
106 // find point with maximum Mahalanobis distance
107
108 double Dmax = -1.0;
109 int index = 0;
110
111 for (T p = __begin; p != __end; ++p) {
112
113 const double D = (p->getX() * (A.a00 * p->getX() + A.a01 * p->getY() + A.a02 * 1.0) +
114 p->getY() * (A.a10 * p->getX() + A.a11 * p->getY() + A.a12 * 1.0) +
115 1.0 * (A.a20 * p->getX() + A.a21 * p->getY() + A.a22 * 1.0));
116
117 if (D > Dmax) {
118 Dmax = D;
119 index = distance(__begin, p);
120 }
121 }
122
123 double step = (Dmax - 3.0) / (3.0 * (Dmax - 1.0)); // 2D + 1 homogeneous coordinate
124
125 // update weights according step size
126
127 for (int i = 0; i < N; ++i) {
128 U[i] = (1.0 - step) * U[i];
129 }
130
131 U[index] += step;
132
133 // reached precision?
134
135 if (Dmax - 3.0 <= precision) {
136 break;
137 }
138 }
139
140 // center of ellipse
141
142 this->setPosition(JPosition2D(0.0, 0.0));
143
144 for (T p = __begin; p != __end; ++p) {
145
146 const int i = distance(__begin, p);
147
148 this->add(U[i] * JPosition2D(p->getX(), p->getY()));
149 }
150
151 // determine axes and rotation by eigendecomposition of covariance matrix
152
153 JMatrix2S B;
154
155 for (T p = __begin; p != __end; ++p) {
156
157 const int i = distance(__begin, p);
158
159 const double x = p->getX() - this->getX();
160 const double y = p->getY() - this->getY();
161
162 B.a00 += x * U[i] * x;
163 B.a01 += x * U[i] * y;
164 B.a11 += y * U[i] * y;
165 }
166
167 B.a10 = B.a01;
168
169 try {
170 B.invert();
171 }
172 catch(exception& error) {
173 return;
174 }
175
176 B *= 0.5;
177
178 const double trace = B.a00 + B.a11;
179 const double D = sqrt((B.a00 - B.a11) * (B.a00 - B.a11) + 4.0 * B.a01 * B.a01);
180
181 const double lambda_1 = (trace - D) / 2.0;
182 const double lambda_2 = (trace + D) / 2.0;
183
184 this->major = 1.0 / sqrt(lambda_1);
185 this->minor = 1.0 / sqrt(lambda_2);
186 this->angle = 0.5 * atan2(-2.0 * B.a01, B.a11 - B.a00);
187
188 // correct for precision
189
190 const JRotation2D R(this->angle);
191
192 double Fmax = 1.0;
193
194 for (T p = __begin; p != __end; ++p) {
195
196 JPosition2D pos(p->getX(), p->getY());
197
198 pos -= this->getPosition();
199
200 pos.rotate_back(R);
201
202 const double F = hypot(pos.getX() / this->major,
203 pos.getY() / this->minor);
204
205 if (F > Fmax) {
206 Fmax = F;
207 }
208 }
209
210 this->major *= Fmax;
211 this->minor *= Fmax;
212 }
213
214
215 /**
216 * Get major axis.
217 *
218 * \return major axis
219 */
220 double getMajorAxis() const
221 {
222 return major;
223 }
224
225
226 /**
227 * Get minor axis.
228 *
229 * \return minor axis
230 */
231 double getMinorAxis() const
232 {
233 return minor;
234 }
235
236
237 /**
238 * Get rotation angle.
239 *
240 * \return angle [rad]
241 */
242 double getAngle() const
243 {
244 return angle;
245 }
246
247
248 /**
249 * Get area.
250 *
251 * \return area
252 */
253 double getArea() const
254 {
255 using JMATH::PI;
256
257 return PI * this->getMajorAxis() * this->getMinorAxis();
258 }
259
260
261 /**
262 * Read ellipse from input.
263 *
264 * \param in input stream
265 * \param ellipse ellipse
266 * \return input stream
267 */
268 friend inline std::istream& operator>>(std::istream& in, JEllipse2D& ellipse)
269 {
270 in >> static_cast<JPosition2D&>(ellipse);
271
272 in >> ellipse.major
273 >> ellipse.minor
274 >> ellipse.angle;
275
276 return in;
277 }
278
279
280 /**
281 * Write ellipse to output.
282 *
283 * \param out output stream
284 * \param ellipse ellipse
285 * \return output stream
286 */
287 friend inline std::ostream& operator<<(std::ostream& out, const JEllipse2D& ellipse)
288 {
289 const JFormat format(out, getFormat<JEllipse2D>(JFormat_t(9, 5, std::ios::fixed | std::ios::showpos)));
290
291 out << static_cast<const JPosition2D&>(ellipse);
292
293 out << format << ellipse.getMajorAxis() << ' '
294 << format << ellipse.getMinorAxis() << ' '
295 << format << ellipse.getAngle();
296
297 return out;
298 }
299
301
302 protected:
303 double major = 0.0;
304 double minor = 0.0;
305 double angle = 0.0; //!< rotation angle in radians
306 };
307
308
309 /**
310 * Ellipse.
311 *
312 \f[
313 Ax^{2} + Bxy + Cy^{2} + Dx + Ey + F = 0
314 \f]
315 */
317 /**
318 * Constructor.
319 *
320 * \param A A
321 * \param B B
322 * \param C C
323 * \param D D
324 * \param E E
325 * \param F F
326 */
327 JEllipse2D_t(const double A,
328 const double B,
329 const double C,
330 const double D,
331 const double E,
332 const double F) :
333 A(A),
334 B(B),
335 C(C),
336 D(D),
337 E(E),
338 F(F)
339 {}
340
341
342 /**
343 * Constructor.
344 *
345 * \param ellipse ellipse
346 */
347 JEllipse2D_t(const JEllipse2D& ellipse)
348 {
349 const double st = sin(ellipse.getAngle());
350 const double ct = cos(ellipse.getAngle());
351
352 const double x = ellipse.getX();
353 const double y = ellipse.getY();
354
355 const double a = ellipse.getMajorAxis();
356 const double b = ellipse.getMinorAxis();
357
358 A = a*a * st*st + b*b * ct*ct;
359 B = 2.0 * (b*b - a*a) * st*ct;
360 C = a*a * ct*ct + b*b * st*st;
361 D = -2.0*A*x - B*y;
362 E = -B*x - 2.0*C*y;
363 F = A*x*x + B*x*y + C*y*y - a*a * b*b;
364 }
365
366
367 /**
368 * Evaluate ellipse equation for given point.
369 *
370 * \param p point
371 */
372 double operator()(const JVector2D& p) const
373 {
374 return (A * p.getX() * p.getX() +
375 B * p.getX() * p.getY() +
376 C * p.getY() * p.getY() +
377 D * p.getX() +
378 E * p.getY() +
379 F);
380 }
381
382
383 /**
384 * Check if a point is inside or on the boundary of the ellipse.
385 *
386 * \param p point
387 * \param precision precision
388 */
389 bool is_inside(const JVector2D& p, const double precision = 1.0e-8) const
390 {
391 return (*this)(p) <= precision;
392 }
393
394
395 /**
396 * Get area.
397 *
398 * \return area
399 */
400 double getArea() const
401 {
402 using JMATH::PI;
403
404 const double U = 4*A*C - B*B;
405
406 return PI * 2.0 * (A*E*E + C*D*D - B*D*E - U*F) / (U * sqrt(U));
407 }
408
409
410 /**
411 * Read ellipse from input.
412 *
413 * \param in input stream
414 * \param ellipse ellipse
415 * \return input stream
416 */
417 friend inline std::istream& operator>>(std::istream& in, JEllipse2D_t& ellipse)
418 {
419 return in >> ellipse.A >> ellipse.B >> ellipse.C >> ellipse.D >> ellipse.E >> ellipse.F;
420 }
421
422
423 /**
424 * Write ellipse to output.
425 *
426 * \param out output stream
427 * \param ellipse ellipse
428 * \return output stream
429 */
430 friend inline std::ostream& operator<<(std::ostream& out, const JEllipse2D_t& ellipse)
431 {
432 const JFormat format(out, getFormat<JEllipse2D_t>(JFormat_t(9, 5, std::ios::fixed | std::ios::showpos)));
433
434 return out << format << ellipse.A << ' '
435 << format << ellipse.B << ' '
436 << format << ellipse.C << ' '
437 << format << ellipse.D << ' '
438 << format << ellipse.E << ' '
439 << format << ellipse.F;
440 }
441
442
443 double A;
444 double B;
445 double C;
446 double D;
447 double E;
448 double F;
449 };
450}
451
452#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
I/O manipulators.
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
Mathematical constants.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Data structure for position in two dimensions.
const JPosition2D & getPosition() const
Get position.
JPosition2D & rotate_back(const JRotation2D &R)
Rotate back.
JPosition2D()
Default constructor.
void setPosition(const JVector2D &pos)
Set position.
Data structure for vector in two dimensions.
Definition JVector2D.hh:34
double getY() const
Get y position.
Definition JVector2D.hh:74
double getX() const
Get x position.
Definition JVector2D.hh:63
JVector2D & add(const JVector2D &vector)
Add vector.
Definition JVector2D.hh:100
Exception for accessing a value in a collection that is outside of its range.
2 x 2 symmetric matrix
Definition JMatrix2S.hh:28
3 x 3 symmetric matrix
Definition JMatrix3S.hh:31
Auxiliary classes and methods for 2D geometrical objects and operations.
Definition JAngle2D.hh:19
static const double PI
Mathematical constants.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Data structure for format specifications.
Definition JManip.hh:524
Auxiliary class to temporarily define format specifications.
Definition JManip.hh:636
friend std::ostream & operator<<(std::ostream &out, const JEllipse2D_t &ellipse)
Write ellipse to output.
JEllipse2D_t(const double A, const double B, const double C, const double D, const double E, const double F)
Constructor.
double operator()(const JVector2D &p) const
Evaluate ellipse equation for given point.
bool is_inside(const JVector2D &p, const double precision=1.0e-8) const
Check if a point is inside or on the boundary of the ellipse.
friend std::istream & operator>>(std::istream &in, JEllipse2D_t &ellipse)
Read ellipse from input.
JEllipse2D_t(const JEllipse2D &ellipse)
Constructor.
double getArea() const
Get area.
static const int MINIMUM_NUMBER_OF_POINTS
Definition JEllipse2D.hh:32
double getMajorAxis() const
Get major axis.
JEllipse2D(T __begin, T __end, const size_t Nmax=1000, const double precision=1.0e-3)
Constructor.
Definition JEllipse2D.hh:55
double angle
rotation angle in radians
friend std::ostream & operator<<(std::ostream &out, const JEllipse2D &ellipse)
Write ellipse to output.
JEllipse2D()
Default constructor.
Definition JEllipse2D.hh:37
double getMinorAxis() const
Get minor axis.
friend std::istream & operator>>(std::istream &in, JEllipse2D &ellipse)
Read ellipse from input.
double getAngle() const
Get rotation angle.
double getArea() const
Get area.