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

#include <geoarc.h>

Inheritance diagram for lc::geo::Arc:
Collaboration diagram for lc::geo::Arc:

Public Member Functions

 Arc (const Coordinate &center, double radius, double startAngle, double endAngle)
 
 Arc (const Coordinate &center, double radius, double startAngle, double endAngle, bool CCW)
 
 Arc (const Arc &c)
 
 Arc (Arc &&c) noexcept
 
Arcoperator= (Arc &&c) noexcept
 
const Coordinate center () const
 Returns center of Arc. More...
 
double radius () const
 Returns the radius of Arc. More...
 
double startAngle () const
 Returns the startAngle. More...
 
double endAngle () const
 Returns the EndAngle. More...
 
double length () const
 
Coordinate startP () const
 
Coordinate endP () const
 
Area boundingBox () const
 
bool CCW () const
 Returns of the arc is in reversed direction. More...
 
Coordinate nearestPointOnPath (const Coordinate &coord) const
 
Coordinate nearestPointOnEntity (const Coordinate &coord) const
 
const maths::Equation equation () const
 
bool isAngleBetween (double angle) const
 
virtual void accept (GeoEntityVisitor &v) const override
 
double angle () const
 
double bulge () const
 
- Public Member Functions inherited from lc::geo::Base
virtual ~Base ()=default
 
- Public Member Functions inherited from lc::Visitable
virtual ~Visitable ()=default
 

Static Public Member Functions

static Arc createArc3P (const Coordinate &p1, const Coordinate &p2, const Coordinate &p3)
 
static Arc createArcBulge (const Coordinate &p1, const Coordinate &p2, const double bulge)
 

Private Attributes

Coordinate _center
 Coordinate center of Arc. More...
 
double _radius
 Double _Radius of Arc. More...
 
double _startAngle
 Double startAngle of Arc. More...
 
double _endAngle
 Double endAngle of Arc. More...
 
bool _CCW
 

Friends

std::ostream & operator<< (std::ostream &os, const Arc &a)
 

Detailed Description

Definition at line 13 of file geoarc.h.

Constructor & Destructor Documentation

Arc::Arc ( const Coordinate center,
double  radius,
double  startAngle,
double  endAngle 
)

Create a new Arc center center location radius radius of the arc startAngle, will be corrected to 0..2*PI endAngle, will be corrected to 0..2*PI

Arc will always be setup such that startAngle will increase or decrease to endAngle So it's perfectly possible to have a startAngle > endAngle

Definition at line 8 of file geoarc.cpp.

10  /*
11  if (startAngle<0.0 || startAngle>PI2 || startAngle<endAngle) {
12  throw "Invalid start angle";
13  }
14  if (endAngle<0.0 || endAngle> PI2) {
15  throw "Invalid end angle";
16  }
17  if (radius<0.0) {
18  throw "Invalid radius";
19  }*/
20 
21 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
double endAngle() const
Returns the EndAngle.
Definition: geoarc.cpp:87
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
bool _CCW
Definition: geoarc.h:144
static double correctAngle(double a)
correctAngle, Corrects angle to be in -PI to PI
Definition: lcmath.cpp:33
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
double startAngle() const
Returns the startAngle.
Definition: geoarc.cpp:83
Arc::Arc ( const Coordinate center,
double  radius,
double  startAngle,
double  endAngle,
bool  CCW 
)

Create a new Arc center center location radius radius of the arc startAngle, will be corrected to 0..2*PI endAngle, will be corrected to 0..2*PI CCW, setup arc to be drawn ClockWise or counter clockwise

Definition at line 23 of file geoarc.cpp.

25 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
double endAngle() const
Returns the EndAngle.
Definition: geoarc.cpp:87
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
bool _CCW
Definition: geoarc.h:144
static double correctAngle(double a)
correctAngle, Corrects angle to be in -PI to PI
Definition: lcmath.cpp:33
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
double startAngle() const
Returns the startAngle.
Definition: geoarc.cpp:83
lc::geo::Arc::Arc ( const Arc c)
inline

Definition at line 40 of file geoarc.h.

40 : _center(c._center), _radius(c._radius), _startAngle(c._startAngle), _endAngle(c._endAngle), _CCW(c._CCW) {}
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
bool _CCW
Definition: geoarc.h:144
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
lc::geo::Arc::Arc ( Arc &&  c)
inlinenoexcept

Definition at line 41 of file geoarc.h.

41  {
42  std::swap(_center, c._center);
43  std::swap(_radius, c._radius);
44  std::swap(_startAngle, c._startAngle);
45  std::swap(_endAngle, c._endAngle);
46  std::swap(_CCW, c._CCW);
47  }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
bool _CCW
Definition: geoarc.h:144
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141

Member Function Documentation

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

Implements lc::Visitable.

Reimplemented in lc::entity::Arc.

Definition at line 120 of file geoarc.h.

120 { v.visit(*this); }
double Arc::angle ( ) const

Returns the angle between start and end points

Returns
double angle

Definition at line 167 of file geoarc.cpp.

167  {
168  return Math::correctAngle(std::abs(_endAngle - _startAngle));
169 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
static double correctAngle(double a)
correctAngle, Corrects angle to be in -PI to PI
Definition: lcmath.cpp:33
Area Arc::boundingBox ( ) const

Tight bounding box for the area

Definition at line 138 of file geoarc.cpp.

138  {
139  geo::Area area(startP(), endP());
140 
141  const double startAngle = _startAngle;
142  const double endAngle = _endAngle;
143  const double p0 = 0.0 * M_PI;
144  const double p1 = 0.5 * M_PI;
145  const double p2 = 1.0 * M_PI;
146  const double p3 = 1.5 * M_PI;
147 
148  if ((Math::isAngleBetween(p0, startAngle, endAngle, _CCW))) {
149  area = area.merge(Coordinate(_center.x() + _radius, _center.y()));
150  }
151  if ((Math::isAngleBetween(p1, startAngle, endAngle, _CCW))) {
152  area = area.merge(Coordinate(_center.x(), _center.y() + _radius));
153  }
154  if ((Math::isAngleBetween(p2, startAngle, endAngle, _CCW))) {
155  area = area.merge(Coordinate(_center.x() - _radius, _center.y()));
156  }
157  if ((Math::isAngleBetween(p3, startAngle, endAngle, _CCW))) {
158  area = area.merge(Coordinate(_center.x(), _center.y() - _radius));
159  }
160  return area;
161 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
Coordinate startP() const
Definition: geoarc.cpp:130
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
double endAngle() const
Returns the EndAngle.
Definition: geoarc.cpp:87
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
bool _CCW
Definition: geoarc.h:144
#define M_PI
Definition: const.h:16
static bool isAngleBetween(double a, double start, double end, bool CCW)
isAngleBetween, checks if angle is between
Definition: lcmath.cpp:21
Coordinate endP() const
Definition: geoarc.cpp:134
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
double startAngle() const
Returns the startAngle.
Definition: geoarc.cpp:83
double Arc::bulge ( ) const

Returns arc bulge

Returns
double bulge

Definition at line 171 of file geoarc.cpp.

171  {
172  double bulge = std::tan(angle()/4.0);
173 
174  if(!CCW()) {
175  bulge *= -1;
176  }
177 
178  return bulge;
179 }
double angle() const
Definition: geoarc.cpp:167
bool CCW() const
Returns of the arc is in reversed direction.
Definition: geoarc.cpp:126
double bulge() const
Definition: geoarc.cpp:171
bool Arc::CCW ( ) const

Returns of the arc is in reversed direction.

Returns
Double reversed.

Definition at line 126 of file geoarc.cpp.

126  {
127  return _CCW;
128 }
bool _CCW
Definition: geoarc.h:144
const Coordinate Arc::center ( ) const

Returns center of Arc.

Returns
geo::Coordinate Center

Definition at line 91 of file geoarc.cpp.

91  {
92  return _center;
93 }
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
Arc Arc::createArc3P ( const Coordinate p1,
const Coordinate p2,
const Coordinate p3 
)
static

Definition at line 27 of file geoarc.cpp.

27  {
28 
29  geo::Coordinate vra = p2 - p1;
30  geo::Coordinate vrb = p3 - p1;
31  double ra2 = vra.squared() * 0.5;
32  double rb2 = vrb.squared() * 0.5;
33  double crossp = vra.x() * vrb.y() - vra.y() * vrb.x();
34  if (std::abs(crossp) <= 0.0) {
35  // "Cannot create a arc with radius 0.0.");
36  // TODO add exception handling of some sort
37 // throw;
38  return Arc(geo::Coordinate(0., 0.), 0., 0., 0.);
39  }
40  crossp = 1. / crossp;
41 
42  auto center = geo::Coordinate((ra2 * vrb.y() - rb2 * vra.y()) * crossp, (rb2 * vra.x() - ra2 * vrb.x()) * crossp);
43  auto radius = center.magnitude();
44  center = center + p1;
45  auto angle1 = center.angleTo(p1);
46  auto angle2 = center.angleTo(p3);
47 
48  return Arc(center, radius, angle1, angle2, false);
49 }
double squared() const
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
double angleTo(const Coordinate &v) const
Returns angle To the coordinate.
Definition: geocoordinate.h:77
double y() const
Returns y of Coordinate.
Definition: geocoordinate.h:34
double magnitude() const
Arc(const Coordinate &center, double radius, double startAngle, double endAngle)
Definition: geoarc.cpp:8
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
const Coordinate center() const
Returns center of Arc.
Definition: geoarc.cpp:91
Arc Arc::createArcBulge ( const Coordinate p1,
const Coordinate p2,
const double  bulge 
)
static

Definition at line 51 of file geoarc.cpp.

51  {
52  auto isCCW = bulge>0.;
53  auto delta = atan(bulge) * 4.0;
54 
55  auto middle = p1.mid(p2);
56  auto dist = p1.distanceTo(p2)/2.0;
57 
58  auto radius = std::abs(dist / std::sin(delta/2.0));
59 
60  auto wu = std::abs(std::pow(radius, 2.0) - std::pow(dist, 2.0));
61  auto h = std::sqrt(wu);
62  auto angle = p1.angleTo(p2);
63 
64  if (isCCW) {
65  angle-=M_PI/2.0;
66  } else {
67  angle+=M_PI/2.0;
68  }
69 
70  if (std::abs(delta)<M_PI) {
71  h*=-1.0;
72  }
73 
74  auto center = geo::Coordinate(angle) * h + middle;
75 
76  return Arc(center, radius, center.angleTo(p1), center.angleTo(p2), isCCW);
77 }
double angle() const
Definition: geoarc.cpp:167
Coordinate mid(const Coordinate &other) const
double angleTo(const Coordinate &v) const
Returns angle To the coordinate.
Definition: geocoordinate.h:77
Arc(const Coordinate &center, double radius, double startAngle, double endAngle)
Definition: geoarc.cpp:8
#define M_PI
Definition: const.h:16
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
double distanceTo(const geo::Coordinate &c) const
double bulge() const
Definition: geoarc.cpp:171
const Coordinate center() const
Returns center of Arc.
Definition: geoarc.cpp:91
double Arc::endAngle ( ) const

Returns the EndAngle.

Returns
Double EndAngle.

Definition at line 87 of file geoarc.cpp.

87  {
88  return _endAngle;
89 }
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
Coordinate Arc::endP ( ) const

Coordinate of the end point

Definition at line 134 of file geoarc.cpp.

134  {
135  return _center + Coordinate(_endAngle) * _radius;
136 }
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
const maths::Equation lc::geo::Arc::equation ( ) const
inline

Definition at line 112 of file geoarc.h.

112  {
113  return maths::Equation(1., 0.,1., 0., 0., -_radius* _radius).move(_center);
114  }
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
bool Arc::isAngleBetween ( double  angle) const

Definition at line 163 of file geoarc.cpp.

163  {
165 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double angle() const
Definition: geoarc.cpp:167
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
bool _CCW
Definition: geoarc.h:144
static bool isAngleBetween(double a, double start, double end, bool CCW)
isAngleBetween, checks if angle is between
Definition: lcmath.cpp:21
double Arc::length ( ) const

Calculates arc length This assumes that startAngle and endAngle are between -M_PI and M_PI and the difference is not bigger then 2.*M_PI TODO: Should we make this more reliable, for example to have angle is 4*MPI it's still correctly calculated?

Definition at line 118 of file geoarc.cpp.

118  {
119  if (_startAngle > _endAngle) {
120  return std::abs((2. * M_PI + endAngle() - startAngle()) * radius());
121  } else {
122  return std::abs((endAngle() - startAngle()) * radius());
123  }
124 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
double endAngle() const
Returns the EndAngle.
Definition: geoarc.cpp:87
#define M_PI
Definition: const.h:16
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
double startAngle() const
Returns the startAngle.
Definition: geoarc.cpp:83
Coordinate Arc::nearestPointOnEntity ( const Coordinate coord) const

Definition at line 98 of file geoarc.cpp.

98  {
99  const auto angle = (coord - center()).angle();
100 
101  // if the angle is between start and stop then calculate the nearest point
102  // on it's entity
103  if (isAngleBetween(angle)) {
104  return center() + angle * radius();
105  }
106 
107  // Find out if angle is closer to start or end and return the appropriate coordinate
108  const auto ad1 = std::abs(angle - _startAngle);
109  const auto ad2 = std::abs(angle - _endAngle);
110 
111  if (ad1<=ad2) {
112  return startP();
113  } else {
114  return endP();
115  }
116 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double angle() const
Definition: geoarc.cpp:167
Coordinate startP() const
Definition: geoarc.cpp:130
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
Coordinate endP() const
Definition: geoarc.cpp:134
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
bool isAngleBetween(double angle) const
Definition: geoarc.cpp:163
const Coordinate center() const
Returns center of Arc.
Definition: geoarc.cpp:91
Coordinate Arc::nearestPointOnPath ( const Coordinate coord) const

Definition at line 95 of file geoarc.cpp.

95  {
96  return center() + Coordinate((coord - center()).angle()) * radius();
97 }
double angle() const
Definition: geoarc.cpp:167
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
const Coordinate center() const
Returns center of Arc.
Definition: geoarc.cpp:91
Arc& lc::geo::Arc::operator= ( Arc &&  c)
inlinenoexcept

Definition at line 49 of file geoarc.h.

49  {
50  std::swap(_center, c._center);
51  std::swap(_radius, c._radius);
52  std::swap(_startAngle, c._startAngle);
53  std::swap(_endAngle, c._endAngle);
54  std::swap(_CCW, c._CCW);
55  return *this;
56  }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
double _endAngle
Double endAngle of Arc.
Definition: geoarc.h:143
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
bool _CCW
Definition: geoarc.h:144
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
double Arc::radius ( ) const

Returns the radius of Arc.

Returns
Double Radius.

Definition at line 79 of file geoarc.cpp.

79  {
80  return _radius;
81 }
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141
double Arc::startAngle ( ) const

Returns the startAngle.

Returns
Double startAngle.

Definition at line 83 of file geoarc.cpp.

83  {
84  return _startAngle;
85 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
Coordinate Arc::startP ( ) const

Coordinate of the start point

Definition at line 130 of file geoarc.cpp.

130  {
132 }
double _startAngle
Double startAngle of Arc.
Definition: geoarc.h:142
Coordinate _center
Coordinate center of Arc.
Definition: geoarc.h:140
double _radius
Double _Radius of Arc.
Definition: geoarc.h:141

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const Arc a 
)
friend

Definition at line 134 of file geoarc.h.

134  {
135  os << "Arc(center=" << a._center << " radius=" << a._radius << " startAngle=" << a._startAngle << " endAngle=" << a._endAngle << " ccw=" << a._CCW << ")";
136  return os;
137  }

Member Data Documentation

bool lc::geo::Arc::_CCW
private

Definition at line 144 of file geoarc.h.

Coordinate lc::geo::Arc::_center
private

Coordinate center of Arc.

Definition at line 140 of file geoarc.h.

double lc::geo::Arc::_endAngle
private

Double endAngle of Arc.

Definition at line 143 of file geoarc.h.

double lc::geo::Arc::_radius
private

Double _Radius of Arc.

Definition at line 141 of file geoarc.h.

double lc::geo::Arc::_startAngle
private

Double startAngle of Arc.

Definition at line 142 of file geoarc.h.


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