LibreCAD
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
spline.cpp
Go to the documentation of this file.
1 #include "cad/primitive/spline.h"
2 #include <algorithm>
3 #include "cad/geometry/geoarea.h"
4 
5 using namespace lc;
6 using namespace entity;
7 
9  const std::vector<geo::Coordinate>& controlPoints,
10  const std::vector<double>& knotPoints,
11  const std::vector<geo::Coordinate>& fitPoints,
12  int degree, bool closed, double fitTolerance,
13  double stanx, double stany, double stanz,
14  double etanx, double etany, double etanz,
15  double nx, double ny, double nz,
16  enum Spline::splineflag flags,
17  const Layer_CSPtr layer,
18  const MetaInfo_CSPtr metaInfo,
19  const Block_CSPtr block) :
20  CADEntity(layer, metaInfo, block),
21  geo::Spline(controlPoints,
22  knotPoints,
23  fitPoints,
24  degree,
25  closed,
26  fitTolerance,
27  stanx, stany, stanz,
28  etanx, etany, etanz,
29  nx, ny, nz,
30  flags
31  ) {
33 }
34 
35 Spline::Spline(const Spline_CSPtr other, bool sameID) :
36  CADEntity(other, sameID),
37  geo::Spline(
38  other->controlPoints(),
39  other->knotPoints(),
40  other->fitPoints(),
41  other->degree(),
42  other->closed(),
43  other->fitTolerance(),
44  other->startTanX(), other->startTanY(), other->startTanZ(),
45  other->endTanX(), other->endTanY(), other->endTanZ(),
46  other->nX(), other->nY(), other->nZ(),
47  other->flags()
48  ),
49  _boundingBox(other->boundingBox()) {
50 }
51 
52 std::vector<EntityCoordinate> Spline::snapPoints(const geo::Coordinate& coord, const SimpleSnapConstrain & constrain, double minDistanceToSnap, int maxNumberOfSnapPoints) const {
53  /* TODO implement
54  * fix compiler warning
55  */
56  return std::vector<EntityCoordinate>();
57 }
58 
60  /* TODO implement
61  * fix compiler warning
62  */
63  return geo::Coordinate();
64 }
65 
66 CADEntity_CSPtr Spline::move(const geo::Coordinate& offset) const {
67  std::vector<geo::Coordinate> control_pts;
68 
69  for (auto point : this->controlPoints()) {
70  control_pts.push_back(point + offset);
71  }
72 
73  auto newSpline = std::make_shared<Spline>(control_pts, knotPoints(), fitPoints(), degree(), closed(), fitTolerance(), startTanX(), startTanY(), startTanZ(), endTanX(), endTanY(), endTanZ(), nX(), nY(), nZ(), flags(), layer(), metaInfo());
74  newSpline->setID(this->id());
75  return newSpline;
76 }
77 
78 CADEntity_CSPtr Spline::copy(const geo::Coordinate& offset) const {
79  std::vector<geo::Coordinate> control_pts;
80 
81  for (auto point : this->controlPoints()) {
82  control_pts.push_back(point + offset);
83  }
84 
85  auto newSpline = std::make_shared<Spline>(control_pts, knotPoints(), fitPoints(), degree(), closed(), fitTolerance(), startTanX(), startTanY(), startTanZ(), endTanX(), endTanY(), endTanZ(), nX(), nY(), nZ(), flags(), layer(), metaInfo());
86  return newSpline;
87 }
88 
89 CADEntity_CSPtr Spline::rotate(const geo::Coordinate& rotation_center, const double rotation_angle) const {
90  std::vector<geo::Coordinate> control_pts;
91 
92  for (auto point : this->controlPoints()) {
93  control_pts.push_back(point.rotate(rotation_center, rotation_angle));
94  }
95 
96  auto normal = geo::Coordinate(nX(), nY(), nZ()).rotate(rotation_angle);
97 
98  auto newSpline = std::make_shared<Spline>(control_pts, knotPoints(), fitPoints(), degree(), closed(), fitTolerance(), startTanX(), startTanY(), startTanZ(), endTanX(), endTanY(), endTanZ(), normal.x(), normal.y(), normal.z(), flags(), layer(), metaInfo());
99  newSpline->setID(this->id());
100  return newSpline;
101 }
102 
103 CADEntity_CSPtr Spline::scale(const geo::Coordinate& scale_center, const geo::Coordinate& scale_factor) const {
104  std::vector<geo::Coordinate> control_pts;
105 
106  for (auto point : this->controlPoints()) {
107  control_pts.push_back(point.scale(scale_center, scale_factor));
108  }
109 
110  auto newSpline = std::make_shared<Spline>(control_pts, knotPoints(), fitPoints(), degree(), closed(), fitTolerance(), startTanX(), startTanY(), startTanZ(), endTanX(), endTanY(), endTanZ(), nX(), nY(), nZ(), flags(), layer(), metaInfo());
111  newSpline->setID(this->id());
112  return newSpline;
113 }
114 
115 CADEntity_CSPtr Spline::mirror(const geo::Coordinate& axis1, const geo::Coordinate& axis2) const {
116  std::vector<geo::Coordinate> control_pts;
117 
118  for (auto point : this->controlPoints()) {
119  control_pts.push_back(point.mirror(axis1, axis2));
120  }
121 
122  auto newSpline = std::make_shared<Spline>(control_pts, knotPoints(), fitPoints(), degree(), closed(), fitTolerance(), startTanX(), startTanY(), startTanZ(), endTanX(), endTanY(), endTanZ(), nX(), nY(), nZ(), flags(), layer(), metaInfo());
123  newSpline->setID(this->id());
124  return newSpline;
125 }
126 
128  return this->_boundingBox;
129 }
130 
131 CADEntity_CSPtr Spline::modify(Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo, Block_CSPtr block) const {
132  auto newSpline = std::make_shared<Spline>(
133  controlPoints(),
134  knotPoints(),
135  fitPoints(),
136  degree(),
137  closed(),
138  fitTolerance(),
139  startTanX(), startTanY(), startTanZ(),
140  endTanX(), endTanY(), endTanZ(),
141  nX(), nY(), nZ(), flags(),
142  layer,
143  metaInfo,
144  block
145  );
146  newSpline->setID(id());
147 
148  return newSpline;
149 }
150 
152  //TODO: better bounding box generation
153  _boundingBox = geo::Area(this->controlPoints()[0], this->controlPoints()[0]);
154 
155  for(auto cp : this->controlPoints()) {
157  }
158 }
159 
160 
161 std::map<unsigned int, lc::geo::Coordinate> Spline::dragPoints() const {
162  std::map<unsigned int, lc::geo::Coordinate> dragpoints;
163 
164  unsigned int i = 0;
165 
166  for(auto point : fitPoints()) {
167  dragpoints[i] = point;
168  i++;
169  }
170 
171  for(auto point : controlPoints()) {
172  dragpoints[i] = point;
173  i++;
174  }
175 
176  return dragpoints;
177 }
178 
179 
180 CADEntity_CSPtr Spline::setDragPoints(std::map<unsigned int, lc::geo::Coordinate> dragPoints) const {
181  try {
182  std::vector<lc::geo::Coordinate> fitPoints;
183  std::vector<lc::geo::Coordinate> controlPoints;
184 
185  unsigned int i = 0;
186 
187  for(unsigned int j = 0; j < this->fitPoints().size(); j++) {
188  fitPoints.push_back(dragPoints.at(i));
189  i++;
190  }
191 
192  for(unsigned int j = 0; j < this->controlPoints().size(); j++) {
193  controlPoints.push_back(dragPoints.at(i));
194  i++;
195  }
196 
197  auto newEntity = std::make_shared<Spline>(controlPoints,
198  knotPoints(),
199  fitPoints,
200  degree(),
201  closed(),
202  fitTolerance(),
203  startTanX(), startTanY(), startTanZ(),
204  endTanX(), endTanY(), endTanZ(),
205  nX(), nY(), nZ(),
206  flags(),
207  layer(),
208  metaInfo());
209 
210  newEntity->setID(id());
211 
212  return newEntity;
213  }
214  catch(const std::out_of_range& e) {
215  return shared_from_this();
216  }
217 }
splineflag flags() const
Definition: geospline.cpp:61
double startTanY() const
Definition: geospline.cpp:31
double startTanZ() const
Definition: geospline.cpp:35
virtual CADEntity_CSPtr mirror(const geo::Coordinate &axis1, const geo::Coordinate &axis2) const override
Definition: spline.cpp:115
virtual CADEntity_CSPtr move(const geo::Coordinate &offset) const override
move, moves by an offset
Definition: spline.cpp:66
short degree() const
degree, Returns degree of spline
Definition: geospline.cpp:14
double fitTolerance() const
Returns the fit point tolerance of the spline.
Definition: geospline.cpp:64
double endTanX() const
Definition: geospline.cpp:40
The Spline class.
Definition: spline.h:27
Layer_CSPtr layer() const
layer return the layer this entity is placed on
Definition: cadentity.cpp:29
virtual std::map< unsigned int, lc::geo::Coordinate > dragPoints() const override
Get all points of the entity that can be dragged.
Definition: spline.cpp:161
double nX() const
Definition: geospline.cpp:52
bool closed() const
closed, returns if spline is closed or not
Definition: geospline.cpp:10
Area merge(const Area &other) const
merge two area's and expand if required to largest containing area
Definition: geoarea.h:156
MetaInfo_CSPtr metaInfo() const
Definition: cadentity.h:123
Definition: cadentity.h:12
double endTanY() const
Definition: geospline.cpp:44
void calculateBoundingBox()
Definition: spline.cpp:151
double nZ() const
Definition: geospline.cpp:58
Coordinate rotate(const Coordinate &angleVector) const
rotate around (0.,0.) with a given angle vector
virtual CADEntity_CSPtr copy(const geo::Coordinate &offset) const override
copy, copies line by an offset
Definition: spline.cpp:78
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: spline.cpp:59
Block_CSPtr block() const
Return the current entity block.
Definition: cadentity.cpp:33
double nY() const
Definition: geospline.cpp:55
const std::vector< double > & knotPoints() const
Return a vector of knotpoints.
Definition: geospline.cpp:18
double endTanZ() const
Definition: geospline.cpp:48
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: spline.cpp:52
const std::vector< Coordinate > & fitPoints() const
Return a vector of fitpoints.
Definition: geospline.cpp:22
virtual CADEntity_CSPtr rotate(const geo::Coordinate &rotation_center, const double rotation_angle) const override
rotate, rotate operation
Definition: spline.cpp:89
geo::Area _boundingBox
Definition: spline.h:115
Spline(const std::vector< geo::Coordinate > &controlPoints, const std::vector< double > &knotPoints, const std::vector< geo::Coordinate > &fitPoints, int degree, bool closed, double fitTolerance, double stanx, double stany, double stanz, double etanx, double etany, double etanz, double nx, double ny, double nz, splineflag flags, const Layer_CSPtr layer, const MetaInfo_CSPtr metaInfo=nullptr, const Block_CSPtr block=nullptr)
Spline, Constructor with MetaTypes.
Definition: spline.cpp:8
const std::vector< Coordinate > & controlPoints() const
control_points, Returns Control points of spline
Definition: geospline.cpp:6
virtual CADEntity_CSPtr setDragPoints(std::map< unsigned int, lc::geo::Coordinate > dragPoints) const override
Return modified entity.
Definition: spline.cpp:180
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: spline.cpp:131
virtual const geo::Area boundingBox() const override
boundingBox of the entity
Definition: spline.cpp:127
double startTanX() const
Definition: geospline.cpp:27
virtual CADEntity_CSPtr scale(const geo::Coordinate &scale_center, const geo::Coordinate &scale_factor) const override
scale, scales the entity
Definition: spline.cpp:103