LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
line.cpp
Go to the documentation of this file.
1 #include "line.h"
2 
3 #include <algorithm>
4 #include "cad/geometry/geoarea.h"
5 
6 using namespace lc;
7 using namespace entity;
8 
10  const geo::Coordinate& end,
11  const Layer_CSPtr layer,
12  const MetaInfo_CSPtr metaInfo,
13  const Block_CSPtr block) :
14  CADEntity(layer, metaInfo, block),
15  geo::Vector(start, end) {
16 }
17 
18 Line::Line(const geo::Vector& vector,
19  const Layer_CSPtr layer,
20  const MetaInfo_CSPtr metaInfo,
21  const Block_CSPtr block) :
22  CADEntity(layer, metaInfo, block),
23  geo::Vector(vector) {
24 }
25 
26 Line::Line(const Line_CSPtr other, bool sameID) : CADEntity(other, sameID), geo::Vector(other->start(), other->end()) {
27 }
28 
30  CADEntity(builder),
31  geo::Vector(builder.start(), builder.end()) {
32 }
33 
34 std::vector<EntityCoordinate> Line::snapPoints(const geo::Coordinate& coord, const SimpleSnapConstrain & constrain, double minDistanceToSnap, int maxNumberOfSnapPoints) const {
35  std::vector<EntityCoordinate> points;
36 
37  if (constrain.constrain() & SimpleSnapConstrain::LOGICAL) {
38  points.emplace_back(start(), 0);
39  points.emplace_back(end(), 1);
40  points.emplace_back(end().mid(start()), 1);
41  }
42 
43 
44  const geo::Coordinate npoe = nearestPointOnPath(coord);
46  points.emplace_back(npoe, 2);
47  }
48 
49  if (constrain.constrain() & SimpleSnapConstrain::ON_ENTITY) {
50  if (this->nearestPointOnEntity(coord).distanceTo(coord)<minDistanceToSnap) {
51  points.emplace_back(npoe, 3);
52  }
53  }
54 
55  // Cleanup array of snappoints
56  Snapable::snapPointsCleanup(points, coord, maxNumberOfSnapPoints, minDistanceToSnap);
57  return points;
58 }
59 
61  return geo::Vector::nearestPointOnPath(coord);
62 }
63 
64 CADEntity_CSPtr Line::move(const geo::Coordinate& offset) const {
65  auto newLine = std::make_shared<Line>(this->start() + offset,
66  this->end() + offset,
67  layer(),
68  metaInfo(),
69  block()
70  );
71  newLine->setID(this->id());
72  return newLine;
73 }
74 
75 CADEntity_CSPtr Line::copy(const geo::Coordinate& offset) const {
76  auto newLine = std::make_shared<Line>(this->start() + offset,
77  this->end() + offset,
78  layer(),
79  metaInfo(),
80  block());
81  return newLine;
82 }
83 
84 CADEntity_CSPtr Line::rotate(const geo::Coordinate& rotation_center, const double rotation_angle) const {
85  auto newLine = std::make_shared<Line>(this->start().rotate(rotation_center, rotation_angle),
86  this->end().rotate(rotation_center, rotation_angle),
87  layer(),
88  metaInfo(),
89  block());
90  newLine->setID(this->id());
91  return newLine;
92 }
93 
94 CADEntity_CSPtr Line::scale(const geo::Coordinate& scale_center, const geo::Coordinate& scale_factor) const {
95  auto newLine = std::make_shared<Line>(this->start().scale(scale_center, scale_factor),
96  this->end().scale(scale_center, scale_factor),
97  layer(),
98  metaInfo(),
99  block());
100  newLine->setID(this->id());
101  return newLine;
102 }
103 
104 CADEntity_CSPtr Line::mirror(const geo::Coordinate& axis1,
105  const geo::Coordinate& axis2) const {
106  auto newLine = std::make_shared<Line>(this->start().mirror(axis1, axis2),
107  this->end().mirror(axis1, axis2),
108  layer(),
109  metaInfo(),
110  block());
111  newLine->setID(this->id());
112  return newLine;
113 }
114 
116  return geo::Area(start(), end());
117 }
118 
119 CADEntity_CSPtr Line::modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, Block_CSPtr block) const {
120  auto newEntity = std::make_shared<Line>(
121  this->start(),
122  this->end(),
123  layer,
124  metaInfo,
125  block
126  );
127  newEntity->setID(this->id());
128 
129  return newEntity;
130 }
131 
132 std::map<unsigned int, lc::geo::Coordinate> Line::dragPoints() const {
133  std::map<unsigned int, lc::geo::Coordinate> points;
134 
135  points[0] = start();
136  points[1] = end();
137 
138  return points;
139 }
140 
141 CADEntity_CSPtr Line::setDragPoints(std::map<unsigned int, lc::geo::Coordinate> dragPoints) const {
142  try {
143  auto newEntity = std::make_shared<Line>(dragPoints.at(0),
144  dragPoints.at(1),
145  layer(),
146  metaInfo(),
147  block()
148  );
149  newEntity->setID(this->id());
150  return newEntity;
151  }
152  catch(const std::out_of_range& e) {
153  //A point was not in the map, don't change the entity
154  return shared_from_this();
155  }
156 }
virtual CADEntity_CSPtr scale(const geo::Coordinate &scale_center, const geo::Coordinate &scale_factor) const override
scale, scales the entity
Definition: line.cpp:94
Coordinate mirror(const Coordinate &axis1, const Coordinate &axis2) const
mirror a coordinate
const Coordinate end() const
Definition: geovector.h:23
virtual CADEntity_CSPtr copy(const geo::Coordinate &offset) const override
copy, copies line by an offset
Definition: line.cpp:75
static const uint16_t LOGICAL
Definition: snapconstrain.h:22
const Coordinate nearestPointOnEntity(const Coordinate &coord) const
Definition: geovector.h:49
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
Coordinate scale(const double &scale_factor) const
virtual CADEntity_CSPtr rotate(const geo::Coordinate &rotation_center, const double rotation_angle) const override
rotate, rotate operation
Definition: line.cpp:84
MetaInfo_CSPtr metaInfo() const
Definition: cadentity.h:123
static const uint16_t ON_ENTITY
Definition: snapconstrain.h:18
Definition: cadentity.h:12
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 const geo::Area boundingBox() const override
boundingBox of the entity
Definition: line.cpp:115
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: line.cpp:60
virtual std::map< unsigned int, lc::geo::Coordinate > dragPoints() const override
Get all points of the entity that can be dragged.
Definition: line.cpp:132
virtual CADEntity_CSPtr move(const geo::Coordinate &offset) const override
move, moves by an offset
Definition: line.cpp:64
const Coordinate nearestPointOnPath(const Coordinate &coord) const
Definition: geovector.h:39
Block_CSPtr block() const
Return the current entity block.
Definition: cadentity.cpp:33
virtual CADEntity_CSPtr setDragPoints(std::map< unsigned int, lc::geo::Coordinate > dragPoints) const override
Return modified entity.
Definition: line.cpp:141
virtual CADEntity_CSPtr mirror(const geo::Coordinate &axis1, const geo::Coordinate &axis2) const override
Definition: line.cpp:104
const uint16_t constrain() const
Definition: snapconstrain.h:47
const Coordinate start() const
Definition: geovector.h:20
double distanceTo(const geo::Coordinate &c) const
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: line.cpp:34
Line(const geo::Coordinate &start, const geo::Coordinate &end, const Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo=nullptr, const Block_CSPtr block=nullptr)
Construct a new line.
Definition: line.cpp:9
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: line.cpp:119