LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
arc.cpp
Go to the documentation of this file.
1 #include "arc.h"
2 
3 using namespace lc;
4 using namespace entity;
5 
6 Arc::Arc(const geo::Coordinate &center, double radius, double startAngle, double endAngle, bool isCCW,
7  const Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, const Block_CSPtr block) :
8  CADEntity(layer, metaInfo, block),
9  geo::Arc(center, radius, startAngle, endAngle, isCCW) {
10 }
11 
12 Arc::Arc(const geo::Arc &a, const Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, const Block_CSPtr block) :
13  CADEntity(layer, metaInfo, block),
14  geo::Arc(a) {
15 }
16 
17 Arc::Arc(const Arc_CSPtr other, bool sameID) : CADEntity(other, sameID),
18  geo::Arc(other->center(), other->radius(), other->startAngle(),
19  other->endAngle(), other->CCW()) {
20 }
21 
22 Arc::Arc(const builder::ArcBuilder& builder) :
23  CADEntity(builder),
24  geo::Arc(builder.center(), builder.radius(), builder.startAngle(), builder.endAngle(), builder.isCCW()) {
25 }
26 
27 std::vector<EntityCoordinate> Arc::snapPoints(const geo::Coordinate &coord, const SimpleSnapConstrain &constrain,
28  double minDistanceToSnap, int maxNumberOfSnapPoints) const {
29  std::vector<EntityCoordinate> points;
30  if (constrain.constrain() & SimpleSnapConstrain::LOGICAL) {
31  // Add center
32  lc::geo::Coordinate coord = center();
33  points.emplace_back(coord, 0);
34  points.emplace_back(startP(), 1);
35  points.emplace_back(endP(), 2);
36 
37  // Add 4 coordinates
38  // Top Point
39  if (geo::Arc::isAngleBetween(.5 * M_PI)) {
40  coord = center() + lc::geo::Coordinate(0., radius());
41  points.emplace_back(coord, 1);
42  }
43  // Right Point
44  if (geo::Arc::isAngleBetween(0)) {
45  coord = center() + lc::geo::Coordinate(radius(), 0.);
46  points.emplace_back(coord, 2);
47  }
48  // Left Point
50  coord = center() + lc::geo::Coordinate(-radius(), 0.);
51  points.emplace_back(coord, 3);
52  }
53  // Bottom Point
54  if (geo::Arc::isAngleBetween(-.5 * M_PI)) {
55  coord = center() + lc::geo::Coordinate(0., -radius());
56  points.emplace_back(coord, 4);
57  }
58  }
59 
60  // Any where on entity path
62  geo::Coordinate npoe = nearestPointOnPath(coord);
63  points.emplace_back(npoe, -1);
64  }
65 
66  // Any where on entity
67  if (constrain.constrain() & SimpleSnapConstrain::ON_ENTITY) {
68  geo::Coordinate npoe = nearestPointOnPath(coord);
69  const double a = (npoe - center()).angle();
70  if (isAngleBetween(a)) {
71  points.emplace_back(npoe, -1);
72  }
73  }
74 
75  // Cleanup array of snappoints
76  Snapable::snapPointsCleanup(points, coord, maxNumberOfSnapPoints, minDistanceToSnap);
77  return points;
78 }
79 
80 // TODO: Decide if a point like center should be returned by a function nearestPointOnPath
82  const geo::Coordinate pointOnPath = geo::Arc::nearestPointOnPath(coord);
83  /*
84  double vl1 = (center() - coord).magnitude();
85  double vl2 = (pointOnPath - coord).magnitude();
86  if (vl1 < vl2) {
87  return center();
88  } */
89 
90  return pointOnPath;
91 }
92 
93 CADEntity_CSPtr Arc::move(const geo::Coordinate &offset) const {
94  auto newArc = std::make_shared<Arc>(this->center() + offset, this->radius(), this->startAngle(), this->endAngle(),
95  this->CCW(), layer());
96  newArc->setID(this->id());
97  return newArc;
98 }
99 
100 CADEntity_CSPtr Arc::copy(const geo::Coordinate &offset) const {
101  auto newArc = std::make_shared<Arc>(this->center() + offset, this->radius(), this->startAngle(), this->endAngle(),
102  this->CCW(), layer());
103  return newArc;
104 }
105 
106 CADEntity_CSPtr Arc::rotate(const geo::Coordinate &rotation_center, const double rotation_angle) const {
107  auto newArc = std::make_shared<Arc>(this->center().rotate(rotation_center, rotation_angle),
108  this->radius(), this->startAngle() + rotation_angle,
109  this->endAngle() + rotation_angle, this->CCW(), layer());
110  newArc->setID(this->id());
111  return newArc;
112 }
113 
114 CADEntity_CSPtr Arc::scale(const geo::Coordinate &scale_center, const geo::Coordinate &scale_factor) const {
115  auto newArc = std::make_shared<Arc>(this->center().scale(scale_center, scale_factor),
116  this->radius() * fabs(scale_factor.x()),
117  this->startAngle(), this->endAngle(), this->CCW(), layer());
118  newArc->setID(this->id());
119  return newArc;
120 
121 }
122 
123 CADEntity_CSPtr Arc::mirror(const geo::Coordinate &axis1, const geo::Coordinate &axis2) const {
124  double a= (axis2- axis1).angle()*2;
125 
126  auto newArc = std::make_shared<Arc>(this->center().mirror(axis1,axis2),
127  this->radius(),
128  lc::Math::correctAngle(a - this->startAngle()),
129  lc::Math::correctAngle(a - this->endAngle()),
130  !this->CCW(), layer());
131  newArc->setID(this->id());
132  return newArc;
133 
134 }
135 
137  return geo::Arc::boundingBox();
138 }
139 
140 CADEntity_CSPtr Arc::modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, Block_CSPtr block) const {
141  auto newArc = std::make_shared<Arc>(this->center(), this->radius(), this->startAngle(), this->endAngle(),
142  this->CCW(), layer, metaInfo, block);
143  newArc->setID(this->id());
144  return newArc;
145 }
146 
147 
148 std::map<unsigned int, lc::geo::Coordinate> Arc::dragPoints() const {
149  std::map<unsigned int, lc::geo::Coordinate> dragPoints;
150 
151  dragPoints[0] = startP();
152  dragPoints[1] = endP();
153 
154  return dragPoints;
155 }
156 
157 
158 CADEntity_CSPtr Arc::setDragPoints(std::map<unsigned int, lc::geo::Coordinate> dragPoints) const {
159  try {
160  auto newEntity = std::make_shared<Arc>(geo::Arc::createArcBulge(dragPoints.at(0), dragPoints.at(1), bulge()), layer(), metaInfo());
161  newEntity->setID(id());
162  return newEntity;
163  }
164  catch(std::out_of_range& e) {
165  return shared_from_this();
166  }
167 }
Coordinate mirror(const Coordinate &axis1, const Coordinate &axis2) const
mirror a coordinate
double angle() const
Definition: geoarc.cpp:167
Coordinate startP() const
Definition: geoarc.cpp:130
Arc(const geo::Coordinate &center, double radius, double startAngle, double endAngle, bool CCW, const Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo=nullptr, const Block_CSPtr block=nullptr)
Arc, Arc constructor.
Definition: arc.cpp:6
Area boundingBox() const
Definition: geoarc.cpp:138
virtual CADEntity_CSPtr setDragPoints(std::map< unsigned int, lc::geo::Coordinate > dragPoints) const override
Return modified entity.
Definition: arc.cpp:158
static const uint16_t LOGICAL
Definition: snapconstrain.h:22
double x() const
Returns x of Coordinate.
Definition: geocoordinate.h:26
virtual CADEntity_CSPtr mirror(const geo::Coordinate &axis1, const geo::Coordinate &axis2) const override
Definition: arc.cpp:123
static const uint16_t ON_ENTITYPATH
Definition: snapconstrain.h:19
Layer_CSPtr layer() const
layer return the layer this entity is placed on
Definition: cadentity.cpp:29
virtual CADEntity_CSPtr copy(const geo::Coordinate &offset) const override
copy, copies line by an offset
Definition: arc.cpp:100
virtual CADEntity_CSPtr modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, Block_CSPtr block) const override
modify Return a new entity with the same ID bit with possible modified metainfo and/pr layer informat...
Definition: arc.cpp:140
Coordinate scale(const double &scale_factor) const
virtual CADEntity_CSPtr move(const geo::Coordinate &offset) const override
move, moves by an offset
Definition: arc.cpp:93
static Arc createArcBulge(const Coordinate &p1, const Coordinate &p2, const double bulge)
Definition: geoarc.cpp:51
MetaInfo_CSPtr metaInfo() const
Definition: cadentity.h:123
static const uint16_t ON_ENTITY
Definition: snapconstrain.h:18
bool CCW() const
Returns of the arc is in reversed direction.
Definition: geoarc.cpp:126
double endAngle() const
Returns the EndAngle.
Definition: geoarc.cpp:87
Definition: cadentity.h:12
Coordinate nearestPointOnPath(const Coordinate &coord) const
Definition: geoarc.cpp:95
Coordinate rotate(const Coordinate &angleVector) const
rotate around (0.,0.) with a given angle vector
static void snapPointsCleanup(std::vector< EntityCoordinate > &points, const geo::Coordinate &reference, const unsigned int maxNumberOfSnapPoints, const double minDistanceToSnap)
Definition: snapable.h:53
virtual CADEntity_CSPtr rotate(const geo::Coordinate &rotation_center, const double rotation_angle) const override
rotate, rotate operation
Definition: arc.cpp:106
virtual geo::Coordinate nearestPointOnPath(const geo::Coordinate &coord) const override
Find the nearest point on the path for this entity for the coordinate coord The path of a entity that...
Definition: arc.cpp:81
virtual const geo::Area boundingBox() const override
boundingBox of the entity
Definition: arc.cpp:136
Block_CSPtr block() const
Return the current entity block.
Definition: cadentity.cpp:33
#define M_PI
Definition: const.h:16
virtual CADEntity_CSPtr scale(const geo::Coordinate &scale_center, const geo::Coordinate &scale_factor) const override
scale, scales the entity
Definition: arc.cpp:114
static double correctAngle(double a)
correctAngle, Corrects angle to be in -PI to PI
Definition: lcmath.cpp:33
Coordinate endP() const
Definition: geoarc.cpp:134
const uint16_t constrain() const
Definition: snapconstrain.h:47
double radius() const
Returns the radius of Arc.
Definition: geoarc.cpp:79
virtual std::map< unsigned int, lc::geo::Coordinate > dragPoints() const override
Get all points of the entity that can be dragged.
Definition: arc.cpp:148
double bulge() const
Definition: geoarc.cpp:171
bool isAngleBetween(double angle) const
Definition: geoarc.cpp:163
double startAngle() const
Returns the startAngle.
Definition: geoarc.cpp:83
const Coordinate center() const
Returns center of Arc.
Definition: geoarc.cpp:91
virtual std::vector< EntityCoordinate > snapPoints(const geo::Coordinate &coord, const SimpleSnapConstrain &constrain, double minDistanceToSnap, int maxNumberOfSnapPoints) const override
Find a number of snap points the line has available This function returns a ordered list...
Definition: arc.cpp:27