Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JGEOMETRY2D::JEllipse2D Struct Reference

Ellipse. More...

#include <JEllipse2D.hh>

Inheritance diagram for JGEOMETRY2D::JEllipse2D:
JGEOMETRY2D::JPosition2D JGEOMETRY2D::JVector2D JMATH::JMath< JFirst_t, JSecond_t >

Public Member Functions

 JEllipse2D ()
 Default constructor.
 
template<class T >
 JEllipse2D (T __begin, T __end, const size_t Nmax=1000, const double precision=1.0e-3)
 Constructor.
 
double getMajorAxis () const
 Get major axis.
 
double getMinorAxis () const
 Get minor axis.
 
double getAngle () const
 Get rotation angle.
 
double getArea () const
 Get area.
 
const JPosition2D & getPosition () const
 Get position.
 
JPosition2D & getPosition ()
 Get position.
 
void setPosition (const JVector2D &pos)
 Set position.
 
 operator JAngle2D () const
 Type conversion operator.
 
 operator JVersor2D () const
 Type conversion operator.
 
JPosition2D & rotate (const JRotation2D &R)
 Rotate.
 
JPosition2D & rotate_back (const JRotation2D &R)
 Rotate back.
 
double getDot (const JAngle2D &angle) const
 Get dot product.
 
double getDot (const JVersor2D &versor) const
 Get dot product.
 
double getDot (const JVector2D &point) const
 Get dot product.
 
double getPerpDot (const JAngle2D &angle) const
 Get perpendicular dot product.
 
double getPerpDot (const JVersor2D &dir) const
 Get perpendicular dot product.
 
double getPerpDot (const JVector2D &point) const
 Get perpendicular dot product.
 
JVector2D & transform (const JMatrix2D &T)
 Transform.
 
double getX () const
 Get x position.
 
double getY () const
 Get y position.
 
JVector2D & negate ()
 Negate vector.
 
JVector2D & add (const JVector2D &vector)
 Add vector.
 
JVector2D & sub (const JVector2D &vector)
 Subtract vector.
 
JVector2D & mul (const double factor)
 Scale vector.
 
JFirst_t & mul (const JSecond_t &object)
 Multiply with object.
 
JVector2D & div (const double factor)
 Scale vector.
 
bool equals (const JVector2D &vector, const double precision=std::numeric_limits< double >::min()) const
 Check equality.
 
double getLengthSquared () const
 Get length squared.
 
double getLength () const
 Get length.
 
double getDistanceSquared (const JVector2D &point) const
 Get squared of distance to point.
 
double getDistance (const JVector2D &point) const
 Get distance to point.
 

Public Attributes

size_t numberOfIterations
 

Static Public Attributes

static const int MINIMUM_NUMBER_OF_POINTS = 3
 

Protected Attributes

double major = 0.0
 
double minor = 0.0
 
double angle = 0.0
 rotation angle in radians
 
double __x
 
double __y
 

Friends

std::istream & operator>> (std::istream &in, JEllipse2D &ellipse)
 Read ellipse from input.
 
std::ostream & operator<< (std::ostream &out, const JEllipse2D &ellipse)
 Write ellipse to output.
 

Detailed Description

Ellipse.

Definition at line 29 of file JEllipse2D.hh.

Constructor & Destructor Documentation

◆ JEllipse2D() [1/2]

JGEOMETRY2D::JEllipse2D::JEllipse2D ( )
inline

Default constructor.

Definition at line 37 of file JEllipse2D.hh.

38 {}

◆ JEllipse2D() [2/2]

template<class T >
JGEOMETRY2D::JEllipse2D::JEllipse2D ( T __begin,
T __end,
const size_t Nmax = 1000,
const double precision = 1.0e-3 )
inline

Constructor.

Khachiyan algorithm for Minimum Volume Enclosing Ellipse (MVEE).
The template argument T refers to a data structure which should have the following member methods:

Parameters
__beginbegin of data
__endend of data
Nmaxmaximum number of iterations
precisionprecision

Definition at line 55 of file JEllipse2D.hh.

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 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
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()
Default constructor.
void setPosition(const JVector2D &pos)
Set position.
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
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static const int MINIMUM_NUMBER_OF_POINTS
Definition JEllipse2D.hh:32
double angle
rotation angle in radians

Member Function Documentation

◆ getMajorAxis()

double JGEOMETRY2D::JEllipse2D::getMajorAxis ( ) const
inline

Get major axis.

Returns
major axis

Definition at line 220 of file JEllipse2D.hh.

221 {
222 return major;
223 }

◆ getMinorAxis()

double JGEOMETRY2D::JEllipse2D::getMinorAxis ( ) const
inline

Get minor axis.

Returns
minor axis

Definition at line 231 of file JEllipse2D.hh.

232 {
233 return minor;
234 }

◆ getAngle()

double JGEOMETRY2D::JEllipse2D::getAngle ( ) const
inline

Get rotation angle.

Returns
angle [rad]

Definition at line 242 of file JEllipse2D.hh.

243 {
244 return angle;
245 }

◆ getArea()

double JGEOMETRY2D::JEllipse2D::getArea ( ) const
inline

Get area.

Returns
area

Definition at line 253 of file JEllipse2D.hh.

254 {
255 using JMATH::PI;
256
257 return PI * this->getMajorAxis() * this->getMinorAxis();
258 }
static const double PI
Mathematical constants.
double getMajorAxis() const
Get major axis.
double getMinorAxis() const
Get minor axis.

◆ getPosition() [1/2]

const JPosition2D & JGEOMETRY2D::JPosition2D::getPosition ( ) const
inlineinherited

Get position.

Returns
position

Definition at line 98 of file JPosition2D.hh.

99 {
100 return static_cast<const JPosition2D&>(*this);
101 }

◆ getPosition() [2/2]

JPosition2D & JGEOMETRY2D::JPosition2D::getPosition ( )
inlineinherited

Get position.

Returns
position

Definition at line 109 of file JPosition2D.hh.

110 {
111 return static_cast<JPosition2D&>(*this);
112 }

◆ setPosition()

void JGEOMETRY2D::JPosition2D::setPosition ( const JVector2D & pos)
inlineinherited

Set position.

Parameters
posposition

Definition at line 120 of file JPosition2D.hh.

121 {
122 static_cast<JVector2D&>(*this) = pos;
123 }
JVector2D()
Default constructor.
Definition JVector2D.hh:39

◆ operator JAngle2D()

JGEOMETRY2D::JPosition2D::operator JAngle2D ( ) const
inlineinherited

Type conversion operator.

Returns
angle

Definition at line 131 of file JPosition2D.hh.

132 {
133 return JAngle2D(getX(), getY());
134 }

◆ operator JVersor2D()

JGEOMETRY2D::JPosition2D::operator JVersor2D ( ) const
inlineinherited

Type conversion operator.

Returns
direction

Definition at line 142 of file JPosition2D.hh.

143 {
144 return JVersor2D(getX(), getY());
145 }

◆ rotate()

JPosition2D & JGEOMETRY2D::JPosition2D::rotate ( const JRotation2D & R)
inlineinherited

Rotate.

Parameters
Rrotation matrix
Returns
this position

Definition at line 154 of file JPosition2D.hh.

155 {
156 R.rotate(__x, __y);
157
158 return *this;
159 }

◆ rotate_back()

JPosition2D & JGEOMETRY2D::JPosition2D::rotate_back ( const JRotation2D & R)
inlineinherited

Rotate back.

Parameters
Rrotation matrix
Returns
this position

Definition at line 168 of file JPosition2D.hh.

169 {
170 R.rotate_back(__x, __y);
171
172 return *this;
173 }

◆ getDot() [1/3]

double JGEOMETRY2D::JPosition2D::getDot ( const JAngle2D & angle) const
inlineinherited

Get dot product.

Parameters
angleangle
Returns
dot product

Definition at line 182 of file JPosition2D.hh.

183 {
184 return
185 getX() * angle.getDX() +
186 getY() * angle.getDY();
187 }

◆ getDot() [2/3]

double JGEOMETRY2D::JPosition2D::getDot ( const JVersor2D & versor) const
inlineinherited

Get dot product.

Parameters
versorversor
Returns
dot product

Definition at line 196 of file JPosition2D.hh.

197 {
198 return
199 getX() * versor.getDX() +
200 getY() * versor.getDY();
201 }

◆ getDot() [3/3]

double JGEOMETRY2D::JVector2D::getDot ( const JVector2D & point) const
inlineinherited

Get dot product.

Parameters
pointvector
Returns
dot product

Definition at line 235 of file JVector2D.hh.

236 {
237 return
238 getX() * point.getX() +
239 getY() * point.getY();
240 }

◆ getPerpDot() [1/3]

double JGEOMETRY2D::JPosition2D::getPerpDot ( const JAngle2D & angle) const
inlineinherited

Get perpendicular dot product.

Parameters
angleangle
Returns
perpendicular dot product

Definition at line 210 of file JPosition2D.hh.

211 {
212 return
213 getX() * angle.getDY() -
214 getY() * angle.getDX();
215 }

◆ getPerpDot() [2/3]

double JGEOMETRY2D::JPosition2D::getPerpDot ( const JVersor2D & dir) const
inlineinherited

Get perpendicular dot product.

Parameters
dirdirection
Returns
perpendicular dot product

Definition at line 224 of file JPosition2D.hh.

225 {
226 return
227 getX() * dir.getDY() -
228 getY() * dir.getDX();
229 }

◆ getPerpDot() [3/3]

double JGEOMETRY2D::JVector2D::getPerpDot ( const JVector2D & point) const
inlineinherited

Get perpendicular dot product.

Parameters
pointvector
Returns
perpendicular dot product

Definition at line 249 of file JVector2D.hh.

250 {
251 return
252 getX() * point.getY() -
253 getY() * point.getX();
254 }

◆ transform()

JVector2D & JGEOMETRY2D::JVector2D::transform ( const JMatrix2D & T)
inlineinherited

Transform.

Parameters
Tmatrix
Returns
this vector

Definition at line 160 of file JVector2D.hh.

161 {
162 T.transform(__x, __y);
163
164 return *this;
165 }

◆ getX()

double JGEOMETRY2D::JVector2D::getX ( ) const
inlineinherited

Get x position.

Returns
x position

Definition at line 63 of file JVector2D.hh.

64 {
65 return __x;
66 }

◆ getY()

double JGEOMETRY2D::JVector2D::getY ( ) const
inlineinherited

Get y position.

Returns
y position

Definition at line 74 of file JVector2D.hh.

75 {
76 return __y;
77 }

◆ negate()

JVector2D & JGEOMETRY2D::JVector2D::negate ( )
inlineinherited

Negate vector.

Returns
this vector

Definition at line 85 of file JVector2D.hh.

86 {
87 __x = -__x;
88 __y = -__y;
89
90 return *this;
91 }

◆ add()

JVector2D & JGEOMETRY2D::JVector2D::add ( const JVector2D & vector)
inlineinherited

Add vector.

Parameters
vectorvector
Returns
this vector

Definition at line 100 of file JVector2D.hh.

101 {
102 __x += vector.getX();
103 __y += vector.getY();
104
105 return *this;
106 }

◆ sub()

JVector2D & JGEOMETRY2D::JVector2D::sub ( const JVector2D & vector)
inlineinherited

Subtract vector.

Parameters
vectorvector
Returns
this vector

Definition at line 115 of file JVector2D.hh.

116 {
117 __x -= vector.getX();
118 __y -= vector.getY();
119
120 return *this;
121 }

◆ mul() [1/2]

JVector2D & JGEOMETRY2D::JVector2D::mul ( const double factor)
inlineinherited

Scale vector.

Parameters
factormultiplication factor
Returns
this vector

Definition at line 130 of file JVector2D.hh.

131 {
132 __x *= factor;
133 __y *= factor;
134
135 return *this;
136 }

◆ mul() [2/2]

template<class JFirst_t , class JSecond_t >
JFirst_t & JMATH::JMath< JFirst_t, JSecond_t >::mul ( const JSecond_t & object)
inlineinherited

Multiply with object.

Parameters
objectobject
Returns
result object

Definition at line 354 of file JMath.hh.

355 {
356 return static_cast<JFirst_t&>(*this) = JFirst_t().mul(static_cast<const JFirst_t&>(*this), object);
357 }

◆ div()

JVector2D & JGEOMETRY2D::JVector2D::div ( const double factor)
inlineinherited

Scale vector.

Parameters
factordivision factor
Returns
this vector

Definition at line 145 of file JVector2D.hh.

146 {
147 __x /= factor;
148 __y /= factor;
149
150 return *this;
151 }

◆ equals()

bool JGEOMETRY2D::JVector2D::equals ( const JVector2D & vector,
const double precision = std::numeric_limits<double>::min() ) const
inlineinherited

Check equality.

Parameters
vectorvector
precisionprecision
Returns
true if vectors are equal; else false

Definition at line 175 of file JVector2D.hh.

177 {
178 return (fabs(getX() - vector.getX()) <= precision &&
179 fabs(getY() - vector.getY()) <= precision);
180 }

◆ getLengthSquared()

double JGEOMETRY2D::JVector2D::getLengthSquared ( ) const
inlineinherited

Get length squared.

Returns
square of length

Definition at line 188 of file JVector2D.hh.

189 {
190 return getX()*getX() + getY()*getY();
191 }

◆ getLength()

double JGEOMETRY2D::JVector2D::getLength ( ) const
inlineinherited

Get length.

Returns
length

Definition at line 199 of file JVector2D.hh.

200 {
201 return sqrt(getLengthSquared());
202 }
double getLengthSquared() const
Get length squared.
Definition JVector2D.hh:188

◆ getDistanceSquared()

double JGEOMETRY2D::JVector2D::getDistanceSquared ( const JVector2D & point) const
inlineinherited

Get squared of distance to point.

Parameters
pointpoint
Returns
square of distance

Definition at line 211 of file JVector2D.hh.

212 {
213 return JVector2D(point).sub(*this).getLengthSquared();
214 }

◆ getDistance()

double JGEOMETRY2D::JVector2D::getDistance ( const JVector2D & point) const
inlineinherited

Get distance to point.

Parameters
pointpoint
Returns
distance

Definition at line 223 of file JVector2D.hh.

224 {
225 return sqrt(getDistanceSquared(point));
226 }
double getDistanceSquared(const JVector2D &point) const
Get squared of distance to point.
Definition JVector2D.hh:211

Friends And Related Symbol Documentation

◆ operator>>

std::istream & operator>> ( std::istream & in,
JEllipse2D & ellipse )
friend

Read ellipse from input.

Parameters
ininput stream
ellipseellipse
Returns
input stream

Definition at line 268 of file JEllipse2D.hh.

269 {
270 in >> static_cast<JPosition2D&>(ellipse);
271
272 in >> ellipse.major
273 >> ellipse.minor
274 >> ellipse.angle;
275
276 return in;
277 }

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const JEllipse2D & ellipse )
friend

Write ellipse to output.

Parameters
outoutput stream
ellipseellipse
Returns
output stream

Definition at line 287 of file JEllipse2D.hh.

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 }
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
Data structure for format specifications.
Definition JManip.hh:524
Auxiliary class to temporarily define format specifications.
Definition JManip.hh:636

Member Data Documentation

◆ MINIMUM_NUMBER_OF_POINTS

const int JGEOMETRY2D::JEllipse2D::MINIMUM_NUMBER_OF_POINTS = 3
static

Definition at line 32 of file JEllipse2D.hh.

◆ numberOfIterations

size_t JGEOMETRY2D::JEllipse2D::numberOfIterations

Definition at line 300 of file JEllipse2D.hh.

◆ major

double JGEOMETRY2D::JEllipse2D::major = 0.0
protected

Definition at line 303 of file JEllipse2D.hh.

◆ minor

double JGEOMETRY2D::JEllipse2D::minor = 0.0
protected

Definition at line 304 of file JEllipse2D.hh.

◆ angle

double JGEOMETRY2D::JEllipse2D::angle = 0.0
protected

rotation angle in radians

Definition at line 305 of file JEllipse2D.hh.

◆ __x

double JGEOMETRY2D::JVector2D::__x
protectedinherited

Definition at line 257 of file JVector2D.hh.

◆ __y

double JGEOMETRY2D::JVector2D::__y
protectedinherited

Definition at line 258 of file JVector2D.hh.


The documentation for this struct was generated from the following file: