LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lc::geo::Ellipse Class Reference

#include <geoellipse.h>

Inheritance diagram for lc::geo::Ellipse:
Collaboration diagram for lc::geo::Ellipse:

Public Member Functions

 Ellipse (const Coordinate &center, const Coordinate &majorP, double minorRadius, double startAngle, double endAngle, bool reversed=false)
 
const Coordinate center () const
 center, Returns Center point of Ellipse More...
 
const Coordinate majorP () const
 majorP, Returns major point of the ellipse, relative to center More...
 
double minorRadius () const
 minorRadius, Returns the minor radius of ellipse More...
 
double startAngle () const
 startAngle, Returns Start elliptic!! angle of ellipse More...
 
double endAngle () const
 endAngle, Return the end elliptic!! angle of ellipse More...
 
Coordinate getPoint (const double &angle) const
 getPoint, return a point on ellipse with given elliptic angle More...
 
Coordinate startPoint () const
 startPoint, start point of ellipse More...
 
Coordinate endPoint () const
 endPoint, end point of ellipse More...
 
std::vector< CoordinatefindPotentialNearestPoints (const Coordinate &coord) const
 findPotentialNearestPoints More...
 
Coordinate nearestPointOnPath (const Coordinate &coord) const
 nearestPointOnPath, (ignore if it arc) More...
 
Coordinate nearestPointOnEntity (const Coordinate &coord) const
 nearestPointOnEntity, ( not ignore arc) More...
 
bool isArc () const
 isArc More...
 
bool isReversed () const
 
double ratio () const
 ratio of major radius to minor radius More...
 
double majorRadius () const
 Major Radius. More...
 
double getAngle () const
 getAngle of MajorP More...
 
double getEllipseAngle (const Coordinate &coord) const
 getEllipseAngle More...
 
bool isAngleBetween (double angle) const
 
Ellipse georotate (const Coordinate &center, const double rotation_angle) const
 rotate an ellipse at a center by an angle More...
 
Ellipse geoscale (const Coordinate &center, const Coordinate &factor) const
 scale an ellipse at some center by some factor More...
 
const maths::Equation equation () const
 Returns the quadratic equation. More...
 
virtual void accept (GeoEntityVisitor &v) const override
 
- Public Member Functions inherited from lc::geo::Base
virtual ~Base ()=default
 
- Public Member Functions inherited from lc::Visitable
virtual ~Visitable ()=default
 

Private Attributes

const Coordinate _center
 
const Coordinate _majorP
 
const double _minorRadius
 
const double _startAngle
 
const double _endAngle
 
const bool _isReversed
 

Friends

std::ostream & operator<< (std::ostream &os, const Ellipse &e)
 

Detailed Description

Definition at line 14 of file geoellipse.h.

Constructor & Destructor Documentation

Ellipse::Ellipse ( const Coordinate center,
const Coordinate majorP,
double  minorRadius,
double  startAngle,
double  endAngle,
bool  reversed = false 
)
Parameters
center
majorPrelative to center
minorRadius
startAngle
endAngle
reversed

Definition at line 10 of file geoellipse.cpp.

10  :
11  _center(center),
12  _majorP(majorP),
16  _isReversed(reversed) {
17 
18 }
double startAngle() const
startAngle, Returns Start elliptic!! angle of ellipse
Definition: geoellipse.cpp:32
const double _startAngle
Definition: geoellipse.h:175
const double _minorRadius
Definition: geoellipse.h:174
const Coordinate _majorP
Definition: geoellipse.h:173
const double _endAngle
Definition: geoellipse.h:176
const Coordinate _center
Definition: geoellipse.h:172
const bool _isReversed
Definition: geoellipse.h:177
double endAngle() const
endAngle, Return the end elliptic!! angle of ellipse
Definition: geoellipse.cpp:189
double minorRadius() const
minorRadius, Returns the minor radius of ellipse
Definition: geoellipse.cpp:28

Member Function Documentation

virtual void lc::geo::Ellipse::accept ( GeoEntityVisitor v) const
inlineoverridevirtual

Implements lc::Visitable.

Reimplemented in lc::entity::Ellipse.

Definition at line 164 of file geoellipse.h.

164 { v.visit(*this); }
const Coordinate Ellipse::center ( ) const

center, Returns Center point of Ellipse

Returns
geo::Coordinate center

Definition at line 20 of file geoellipse.cpp.

20  {
21  return _center;
22 }
const Coordinate _center
Definition: geoellipse.h:172
double Ellipse::endAngle ( ) const

endAngle, Return the end elliptic!! angle of ellipse

Returns
double endangle

Definition at line 189 of file geoellipse.cpp.

189  {
190  return _endAngle;
191 }
const double _endAngle
Definition: geoellipse.h:176
Coordinate Ellipse::endPoint ( ) const

endPoint, end point of ellipse

Returns
for elliptic arc, return the endPoint, otherwise return end of majorP

Definition at line 202 of file geoellipse.cpp.

202  {
203  return getPoint(endAngle());
204 }
Coordinate getPoint(const double &angle) const
getPoint, return a point on ellipse with given elliptic angle
Definition: geoellipse.cpp:193
double endAngle() const
endAngle, Return the end elliptic!! angle of ellipse
Definition: geoellipse.cpp:189
const maths::Equation lc::geo::Ellipse::equation ( ) const
inline

Returns the quadratic equation.

Returns
Equation

Definition at line 153 of file geoellipse.h.

153  {
154  auto ce0 = _majorP.squared();
155  auto ce2 = this->ratio() * this->ratio() * ce0;
156 
157  if (ce0 < LCTOLERANCE * LCTOLERANCE || ce2 < LCTOLERANCE * LCTOLERANCE) {
158  return maths::Equation();
159  }
160 
161  return maths::Equation(1. / ce0, 0., 1. / ce2, 0., 0., -1.).rotate(getAngle()).move(_center);
162  }
double squared() const
const Coordinate _majorP
Definition: geoellipse.h:173
double ratio() const
ratio of major radius to minor radius
Definition: geoellipse.cpp:210
const Coordinate _center
Definition: geoellipse.h:172
#define LCTOLERANCE
Definition: const.h:6
double getAngle() const
getAngle of MajorP
Definition: geoellipse.cpp:40
std::vector< Coordinate > Ellipse::findPotentialNearestPoints ( const Coordinate coord) const

findPotentialNearestPoints

Parameters
coord,thepoint of which we search
Returns

Definition at line 88 of file geoellipse.cpp.

88  {
89  std::vector<Coordinate> pnp;
90  pnp.push_back(this->startPoint());
91  pnp.push_back(this->endPoint());
92 
93  double ellipseAngle = this->getAngle();
94 
95  Coordinate trcoord = coord-_center;
96  trcoord = trcoord.rotate(Coordinate(0, 0), -ellipseAngle);
97 
98  double b = _minorRadius;
99  double a = _majorP.magnitude();
100 
101  double x=trcoord.x(),y=trcoord.y();
102 
103  double twoa2b2=2*(a*a-b*b);
104  double twoax=2*a*x;
105  double twoby=2*b*y;
106  double a0=twoa2b2*twoa2b2;
107  std::vector<double> ce(4,0.);
108  std::vector<double> roots(0,0.);
109 
110  if(a0 > LCTOLERANCE) { // a != b , ellipse
111  ce[0]=-2.*twoax/twoa2b2;
112  ce[1]= (twoax*twoax+twoby*twoby)/a0-1.;
113  ce[2]= - ce[0];
114  ce[3]= -twoax*twoax/a0;
115  //std::cout<<"1::find cosine, variable c, solve(c^4 +("<<ce[0]<<")*c^3+("<<ce[1]<<")*c^2+("<<ce[2]<<")*c+("<<ce[3]<<")=0,c)\n";
116  roots=Math::quarticSolver(ce);
117  } else {//a=b, quadratic equation for circle
118  a0=twoby/twoax;
119  roots.push_back(sqrt(1./(1.+a0*a0)));
120  roots.push_back(-roots[0]);
121  }
122 
123  if(roots.size() == 0) {
124  //this should not happen
125  std::cout<<"(a= "<<a<<" b= "<<b<<" x= "<<x<<" y= "<<y<<" )\n";
126  std::cout<<"finding minimum for ("<<x<<"-"<<a<<"*cos(t))^2+("<<y<<"-"<<b<<"*sin(t))^2\n";
127  std::cout<<"2::find cosine, variable c, solve(c^4 +("<<ce[0]<<")*c^3+("<<ce[1]<<")*c^2+("<<ce[2]<<")*c+("<<ce[3]<<")=0,c)\n";
128  std::cout<<ce[0]<<' '<<ce[1]<<' '<<ce[2]<<' '<<ce[3]<<std::endl;
129  std::cerr<<"lcmath::geoellipse::findPotentialNearestPoints() finds no root from quartic, this should not happen\n";
130  return pnp;
131  }
132 
133 
134  for(size_t i=0; i<roots.size(); i++) {
135  double const s=twoby*roots[i]/(twoax-twoa2b2*roots[i]); //sine
136  double const d2=twoa2b2+(twoax-2.*roots[i]*twoa2b2)*roots[i]+twoby*s;
137  if (d2<0) continue; // fartherest
138  Coordinate t(a*roots[i], b*s);
139  //double d=(t-trcoord).magnitude();
140  //double angle = atan2(roots[i],s);
141  // std::cout<<i<<" Find Point: cos= "<<roots[i]<<" sin= "<<s<<" angle= "<<angle<<" ds2= "<<d<<" d="<<d2<<std::endl;
142  t = t.rotate(ellipseAngle);
143  t = t + _center;
144  pnp.push_back(t);
145  }
146  return pnp;
147 }
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
Coordinate startPoint() const
startPoint, start point of ellipse
Definition: geoellipse.cpp:199
const double _minorRadius
Definition: geoellipse.h:174
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
double magnitude() const
const Coordinate _majorP
Definition: geoellipse.h:173
Coordinate rotate(const Coordinate &angleVector) const
rotate around (0.,0.) with a given angle vector
const Coordinate _center
Definition: geoellipse.h:172
#define LCTOLERANCE
Definition: const.h:6
double getAngle() const
getAngle of MajorP
Definition: geoellipse.cpp:40
Coordinate endPoint() const
endPoint, end point of ellipse
Definition: geoellipse.cpp:202
static std::vector< double > quarticSolver(const std::vector< double > &ce)
Definition: lcmath.cpp:127
Ellipse Ellipse::georotate ( const Coordinate center,
const double  rotation_angle 
) const

rotate an ellipse at a center by an angle

Parameters
centerof rotation
rotation_angle
Returns
Rotated Ellipse

Definition at line 82 of file geoellipse.cpp.

82  {
83  return Ellipse(center().rotate(rotation_center, rotation_angle),
84  majorP().rotate(rotation_center, rotation_angle),
86 }
double startAngle() const
startAngle, Returns Start elliptic!! angle of ellipse
Definition: geoellipse.cpp:32
Ellipse(const Coordinate &center, const Coordinate &majorP, double minorRadius, double startAngle, double endAngle, bool reversed=false)
Definition: geoellipse.cpp:10
double endAngle() const
endAngle, Return the end elliptic!! angle of ellipse
Definition: geoellipse.cpp:189
const Coordinate center() const
center, Returns Center point of Ellipse
Definition: geoellipse.cpp:20
double minorRadius() const
minorRadius, Returns the minor radius of ellipse
Definition: geoellipse.cpp:28
const Coordinate majorP() const
majorP, Returns major point of the ellipse, relative to center
Definition: geoellipse.cpp:24
Ellipse Ellipse::geoscale ( const Coordinate center,
const Coordinate factor 
) const

scale an ellipse at some center by some factor

Parameters
centerof scaling
factorof scaling
Returns
scaled ellipse

Definition at line 44 of file geoellipse.cpp.

44  {
45  geo::Coordinate vp1(this->majorP());
46  double a(vp1.magnitude());
47  geo::Coordinate vp2(vp1.x() * 1. / a, vp1.y() * 1. / a);
50 
51  if (isArc()) {
52  startPoint = this->startPoint();
53  endPoint = this->endPoint();
54  }
55 
56  double ct = vp2.x();
57  double ct2 = ct * ct; // cos^2 angle
58  double st = vp2.y();
59  double st2 = 1.0 - ct2; // sin^2 angle
60  double kx2 = factor.x() * factor.x();
61  double ky2 = factor.y() * factor.y();
62  double b = (this->minorRadius() / this->majorP().magnitude()) * a;
63  double cA = 0.5 * a * a * (kx2 * ct2 + ky2 * st2);
64  double cB = 0.5 * b * b * (kx2 * st2 + ky2 * ct2);
65  double cC = a * b * ct * st * (ky2 - kx2);
66  geo::Coordinate vp(cA - cB, cC);
67  geo::Coordinate vp3(a, b);
68  geo::Coordinate vp4(vp3.scale(geo::Coordinate(vp.angle() * 0.5)));
69  geo::Coordinate vp5(vp4.rotate(geo::Coordinate(ct, st)));
70  geo::Coordinate vp6(vp5.scale(factor));
71  double z = cA + cB;
72  double x = vp.magnitude();
73  double ratio = std::sqrt((z - x) / (z + x));
74  double minor_ = vp6.magnitude() * ratio;
75 
76  return Ellipse(this->center().scale(center, factor), vp6,
77  minor_,
78  isArc() ? this->getEllipseAngle(startPoint) : 0.,
79  isArc() ? this->getEllipseAngle(endPoint) : 2.*M_PI);
80 }
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
Coordinate startPoint() const
startPoint, start point of ellipse
Definition: geoellipse.cpp:199
double getEllipseAngle(const Coordinate &coord) const
getEllipseAngle
Definition: geoellipse.cpp:218
bool isArc() const
isArc
Definition: geoellipse.cpp:214
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
Ellipse(const Coordinate &center, const Coordinate &majorP, double minorRadius, double startAngle, double endAngle, bool reversed=false)
Definition: geoellipse.cpp:10
double magnitude() const
double ratio() const
ratio of major radius to minor radius
Definition: geoellipse.cpp:210
#define M_PI
Definition: const.h:16
Coordinate endPoint() const
endPoint, end point of ellipse
Definition: geoellipse.cpp:202
const Coordinate center() const
center, Returns Center point of Ellipse
Definition: geoellipse.cpp:20
double minorRadius() const
minorRadius, Returns the minor radius of ellipse
Definition: geoellipse.cpp:28
const Coordinate majorP() const
majorP, Returns major point of the ellipse, relative to center
Definition: geoellipse.cpp:24
double Ellipse::getAngle ( ) const

getAngle of MajorP

Returns
double majorP angle

Definition at line 40 of file geoellipse.cpp.

40  {
41  return _majorP.angle();
42 }
const Coordinate _majorP
Definition: geoellipse.h:173
double angle() const
double Ellipse::getEllipseAngle ( const Coordinate coord) const

getEllipseAngle

Parameters
coord,apoint on ellipse, not actually required to be on ellipse
Returns
elliptic angle to the position of coord

Definition at line 218 of file geoellipse.cpp.

218  {
219  Coordinate offset = coord - _center;
220  Coordinate point = offset.rotate(-_majorP.angle());
221  return atan2(point.y() * _majorP.magnitude(), point.x() * _minorRadius);
222 }
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
const double _minorRadius
Definition: geoellipse.h:174
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
double magnitude() const
const Coordinate _majorP
Definition: geoellipse.h:173
Coordinate rotate(const Coordinate &angleVector) const
rotate around (0.,0.) with a given angle vector
const Coordinate _center
Definition: geoellipse.h:172
double angle() const
Coordinate Ellipse::getPoint ( const double &  angle) const

getPoint, return a point on ellipse with given elliptic angle

Parameters
angle,elliptic!!angle in radians
Returns
coordinates of a point on ellipse with given elliptic angle

Definition at line 193 of file geoellipse.cpp.

193  {
194  const double a = majorP().magnitude();
195  const double b = minorRadius();
196  return _center + Coordinate(a * cos(angle), b * sin(angle)).rotate(Coordinate(0., 0.), majorP().angle());
197 }
double magnitude() const
Coordinate rotate(const Coordinate &angleVector) const
rotate around (0.,0.) with a given angle vector
const Coordinate _center
Definition: geoellipse.h:172
double minorRadius() const
minorRadius, Returns the minor radius of ellipse
Definition: geoellipse.cpp:28
const Coordinate majorP() const
majorP, Returns major point of the ellipse, relative to center
Definition: geoellipse.cpp:24
bool lc::geo::Ellipse::isAngleBetween ( double  angle) const
inline

Definition at line 127 of file geoellipse.h.

127  {
128  if (!isArc())
129  return true;
131  }
const double _startAngle
Definition: geoellipse.h:175
bool isArc() const
isArc
Definition: geoellipse.cpp:214
const double _endAngle
Definition: geoellipse.h:176
const bool _isReversed
Definition: geoellipse.h:177
static bool isAngleBetween(double a, double start, double end, bool CCW)
isAngleBetween, checks if angle is between
Definition: lcmath.cpp:21
bool Ellipse::isArc ( ) const

isArc

Returns
true if entity is an elliptic arc

Definition at line 214 of file geoellipse.cpp.

214  {
216 }
static double getAngleDifferenceShort(double a1, double a2, bool CCW)
getAngleDifference, Angle difference between 2 angles
Definition: lcmath.cpp:41
#define LCARCTOLERANCE
Definition: const.h:7
const double _startAngle
Definition: geoellipse.h:175
const double _endAngle
Definition: geoellipse.h:176
const bool _isReversed
Definition: geoellipse.h:177
bool Ellipse::isReversed ( ) const
Return values
trueif the arc is reversed (clockwise),
falseotherwise

Definition at line 206 of file geoellipse.cpp.

206  {
207  return _isReversed;
208 }
const bool _isReversed
Definition: geoellipse.h:177
const Coordinate Ellipse::majorP ( ) const

majorP, Returns major point of the ellipse, relative to center

Returns
geo::Coordinate majorP

Definition at line 24 of file geoellipse.cpp.

24  {
25  return _majorP;
26 }
const Coordinate _majorP
Definition: geoellipse.h:173
double Ellipse::majorRadius ( ) const

Major Radius.

Returns
double Major radius

Definition at line 36 of file geoellipse.cpp.

36  {
37  return _majorP.magnitude();
38 }
double magnitude() const
const Coordinate _majorP
Definition: geoellipse.h:173
double Ellipse::minorRadius ( ) const

minorRadius, Returns the minor radius of ellipse

Returns
double minor radius

Definition at line 28 of file geoellipse.cpp.

28  {
29  return _minorRadius;
30 }
const double _minorRadius
Definition: geoellipse.h:174
Coordinate Ellipse::nearestPointOnEntity ( const Coordinate coord) const

nearestPointOnEntity, ( not ignore arc)

Parameters
coord,thepoint of which we search
Returns
nearest point on the ellipse Entity

Definition at line 167 of file geoellipse.cpp.

167  {
168  Coordinate res;
169  double minDist = DBL_MAX;
170  std::vector<Coordinate> potencialPoinst = this->findPotentialNearestPoints(coord);
171 
172 
173  for (auto verifiedPoint: potencialPoinst)
174  {
175  if (this->isAngleBetween(verifiedPoint.angle()))
176  {
177  double d = verifiedPoint.distanceTo(coord);
178  if (d<minDist)
179  {
180  minDist = verifiedPoint.distanceTo(coord);
181  res = verifiedPoint;
182  }
183  }
184  };
185 
186  return res;
187 }
std::vector< Coordinate > findPotentialNearestPoints(const Coordinate &coord) const
findPotentialNearestPoints
Definition: geoellipse.cpp:88
bool isAngleBetween(double angle) const
Definition: geoellipse.h:127
Coordinate Ellipse::nearestPointOnPath ( const Coordinate coord) const

nearestPointOnPath, (ignore if it arc)

Parameters
coord,thepoint of which we search
Returns
nearest point on the ellipse path

Definition at line 149 of file geoellipse.cpp.

149  {
150  Coordinate res;
151  auto minDist = DBL_MAX;
152  std::vector<Coordinate> potencialPoinst = this->findPotentialNearestPoints(coord);
153 
154  for (auto& verifiedPoint: potencialPoinst)
155  {
156  double d = verifiedPoint.distanceTo(coord);
157  if (d<minDist)
158  {
159  minDist = verifiedPoint.distanceTo(coord);
160  res = verifiedPoint;
161  }
162  };
163 
164  return res;
165 }
std::vector< Coordinate > findPotentialNearestPoints(const Coordinate &coord) const
findPotentialNearestPoints
Definition: geoellipse.cpp:88
double Ellipse::ratio ( ) const

ratio of major radius to minor radius

Returns
double ratio

Definition at line 210 of file geoellipse.cpp.

210  {
211  return _majorP.magnitude() / _minorRadius;
212 }
const double _minorRadius
Definition: geoellipse.h:174
double magnitude() const
const Coordinate _majorP
Definition: geoellipse.h:173
double Ellipse::startAngle ( ) const

startAngle, Returns Start elliptic!! angle of ellipse

Returns
double startangle

Definition at line 32 of file geoellipse.cpp.

32  {
33  return _startAngle;
34 }
const double _startAngle
Definition: geoellipse.h:175
Coordinate Ellipse::startPoint ( ) const

startPoint, start point of ellipse

Returns
for elliptic arc, return the startPoint, otherwise return end of majorP

Definition at line 199 of file geoellipse.cpp.

199  {
200  return getPoint(startAngle());
201 }
double startAngle() const
startAngle, Returns Start elliptic!! angle of ellipse
Definition: geoellipse.cpp:32
Coordinate getPoint(const double &angle) const
getPoint, return a point on ellipse with given elliptic angle
Definition: geoellipse.cpp:193

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const Ellipse e 
)
friend

Definition at line 167 of file geoellipse.h.

167  {
168  os << "Ellipse(center=" << e._center << " majorP=" << e._majorP << " minorRadius=" << e._minorRadius << " startAngle=" << e._startAngle << " endAngle=" << e._endAngle << " isReversed=" << e._isReversed << ")";
169  return os;
170  }

Member Data Documentation

const Coordinate lc::geo::Ellipse::_center
private

Definition at line 172 of file geoellipse.h.

const double lc::geo::Ellipse::_endAngle
private

Definition at line 176 of file geoellipse.h.

const bool lc::geo::Ellipse::_isReversed
private

Definition at line 177 of file geoellipse.h.

const Coordinate lc::geo::Ellipse::_majorP
private

Definition at line 173 of file geoellipse.h.

const double lc::geo::Ellipse::_minorRadius
private

Definition at line 174 of file geoellipse.h.

const double lc::geo::Ellipse::_startAngle
private

Definition at line 175 of file geoellipse.h.


The documentation for this class was generated from the following files: