QCAD
Open Source 2D CAD
RPolyline.h
Go to the documentation of this file.
1 
20 #ifndef RPOLYLINE_H
21 #define RPOLYLINE_H
22 
23 #include "../core_global.h"
24 
25 #include <QSharedPointer>
26 
27 #include "RExplodable.h"
28 #include "RPainterPath.h"
29 #include "RPolylineProxy.h"
30 #include "RShape.h"
31 #include "RVector.h"
32 
33 class RBox;
34 
35 #ifndef RDEFAULT_MIN1
36 #define RDEFAULT_MIN1 -1
37 #endif
38 
50 public:
51  RPolyline();
52  RPolyline(const QList<RVector>& vertices, bool closed);
53  RPolyline(const QList<QSharedPointer<RShape> >& segments);
54  virtual ~RPolyline();
55 
56  virtual RShape::Type getShapeType() const {
57  return Polyline;
58  }
59 
60  virtual RPolyline* clone() const {
61  return new RPolyline(*this);
62  }
63 
64  virtual bool isDirected() const {
65  return true;
66  }
67 
68  virtual void setZ(double z);
69  bool isFlat() const;
70 
71  virtual QList<RVector> getVectorProperties() const;
72  virtual QList<double> getDoubleProperties() const;
73  virtual QList<bool> getBoolProperties() const;
74 
75  void clear();
76  void normalize(double tolerance = RS::PointTolerance);
77 
78  bool prependShape(const RShape& shape);
79  bool appendShape(const RShape& shape, bool prepend = false);
80  bool appendShapeAuto(const RShape& shape);
81 
82  void appendVertex(const RVector& vertex, double bulge = 0.0, double w1 = 0.0, double w2 = 0.0);
83  void appendVertex(double x, double y, double bulge = 0.0, double w1 = 0.0, double w2 = 0.0);
84  void prependVertex(const RVector& vertex, double bulge = 0.0, double w1 = 0.0, double w2 = 0.0);
85  void insertVertex(int index, const RVector& vertex, double bulgeBefore = 0.0, double bulgeAfter = 0.0);
86  void insertVertexAt(const RVector& point);
87  RVector insertVertexAtDistance(double dist);
88  void removeFirstVertex();
89  void removeLastVertex();
90  void removeVertex(int index);
91  void removeVerticesAfter(int index);
92  void removeVerticesBefore(int index);
93 
94  bool isEmpty() const {
95  return countVertices()==0;
96  }
97 
98  void setVertices(const QList<RVector>& vertices);
99  QList<RVector> getVertices() const;
100  void setVertexAt(int i, const RVector& v);
101  void moveVertexAt(int i, const RVector& offset);
102  RVector getVertexAt(int i) const;
103  int getVertexIndex(const RVector& v, double tolerance=RS::PointTolerance) const;
104  RVector getLastVertex() const;
105  int countVertices() const;
106 
107  void setBulges(const QList<double>& b);
108  QList<double> getBulges() const;
109  double getBulgeAt(int i) const;
110  void setBulgeAt(int i, double b);
111  bool hasArcSegments() const;
112 
113  QList<double> getVertexAngles() const;
114  double getVertexAngle(int i, RS::Orientation orientation = RS::UnknownOrientation) const;
115 
116  void setGlobalWidth(double w);
117  void setStartWidthAt(int i, double w);
118  double getStartWidthAt(int i) const;
119  void setEndWidthAt(int i, double w);
120  double getEndWidthAt(int i) const;
121  bool hasWidths() const;
122  void setStartWidths(const QList<double>& sw);
123  QList<double> getStartWidths() const;
124  void setEndWidths(const QList<double>& ew);
125  QList<double> getEndWidths() const;
126 
127  void setClosed(bool on);
128  bool isClosed() const;
129  bool isGeometricallyClosed(double tolerance=RS::PointTolerance) const;
130  bool autoClose(double tolerance=RS::PointTolerance) {
131  return toLogicallyClosed(tolerance);
132  }
133  bool toLogicallyClosed(double tolerance=RS::PointTolerance);
134  bool toLogicallyOpen();
135 
136  QList<RVector> getSelfIntersectionPoints() const;
137 
138  RS::Orientation getOrientation(bool implicitelyClosed = false) const;
139  bool setOrientation(RS::Orientation orientation);
140 
141  RPolyline convertArcToLineSegments(int segments) const;
142  RPolyline convertArcToLineSegmentsLength(double segmentLength) const;
143 
144  bool contains(const RVector& point, bool borderIsInside=false, double tolerance=RS::PointTolerance) const;
145  bool containsShape(const RShape& shape) const;
146 
147  RVector getPointInside() const;
148 
149  virtual RVector getStartPoint() const;
150  virtual RVector getEndPoint() const;
151  virtual RVector getMiddlePoint() const;
152 
153  void moveStartPoint(const RVector& pos);
154  void moveEndPoint(const RVector& pos);
155 
156  void moveSegmentAt(int i, const RVector& offset);
157 
158  virtual double getDirection1() const;
159  virtual double getDirection2() const;
160 
161  virtual RS::Side getSideOfPoint(const RVector& point) const;
162 
163  virtual RBox getBoundingBox() const;
164 
165  double getArea() const;
166 
167  virtual double getLength() const;
168 
169  virtual double getDistanceFromStart(const RVector& p) const {
170  QList<double> res = getDistancesFromStart(p);
171  if (res.isEmpty()) {
172  return RMAXDOUBLE;
173  }
174  return res.first();
175  }
176  virtual QList<double> getDistancesFromStart(const RVector& p) const;
177  double getLengthTo(const RVector& p, bool limited = true) const;
178  double getSegmentsLength(int fromIndex, int toIndex) const;
179 
180  virtual QList<RVector> getEndPoints() const;
181  virtual QList<RVector> getMiddlePoints() const;
182  virtual QList<RVector> getCenterPoints() const;
183  virtual RVector getPointAtPercent(double p) const;
184  virtual QList<RVector> getPointsWithDistanceToEnd(
185  double distance, int from = RS::FromAny) const;
186  virtual QList<RVector> getPointCloud(double segmentLength) const;
187 
188  virtual double getAngleAt(double distance, RS::From from = RS::FromStart) const;
189 
190  virtual RVector getVectorTo(const RVector& point,
191  bool limited = true, double strictRange = RMAXDOUBLE) const;
192  virtual double getDistanceTo(const RVector& point, bool limited = true, double strictRange = RMAXDOUBLE) const;
193 
194  int getClosestSegment(const RVector& point) const;
195  int getClosestVertex(const RVector& point) const;
196 
197  virtual bool move(const RVector& offset);
198  virtual bool rotate(double rotation, const RVector& center = RDEFAULT_RVECTOR);
199  virtual bool scale(double scaleFactor, const RVector& center = RDEFAULT_RVECTOR);
200  virtual bool scale(const RVector& scaleFactors, const RVector& center = RDEFAULT_RVECTOR);
201  virtual bool mirror(const RLine& axis);
202  virtual bool reverse();
203  virtual RPolyline getReversed() const;
204  virtual bool stretch(const RPolyline& area, const RVector& offset);
205 
206  virtual QSharedPointer<RShape> getTransformed(const QTransform& transform) const;
207 
208  virtual RS::Ending getTrimEnd(const RVector& trimPoint, const RVector& clickPoint);
209  virtual bool trimStartPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false);
210  virtual bool trimEndPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false);
211  virtual bool trimStartPoint(double trimDist);
212  virtual bool trimEndPoint(double trimDist);
213 
214  virtual QList<QSharedPointer<RShape> > getExploded(int segments = RDEFAULT_MIN1) const;
215  QList<RPolyline> getOutline() const;
216  QList<QPair<RPolyline, RPolyline> > getLeftRightOutline() const;
217  virtual bool isInterpolated() const {
218  return false;
219  }
220  int countSegments() const;
221  QSharedPointer<RShape> getSegmentAt(int i) const;
222  bool isArcSegmentAt(int i) const;
223  QSharedPointer<RShape> getLastSegment() const;
224  QSharedPointer<RShape> getFirstSegment() const;
225 
226  static bool isStraight(double bulge);
227 
228  RPainterPath toPainterPath(bool addOriginalShapes = false) const;
229 
230  bool simplify(double tolerance = RS::PointTolerance);
231  QList<RVector> verifyTangency(double toleranceMin = RS::AngleTolerance, double toleranceMax = M_PI_4);
232 
233  void stripWidths();
234  void setMinimumWidth(double w);
235 
236  int getSegmentAtDist(double dist);
237  bool relocateStartPoint(const RVector& p);
238  bool relocateStartPoint(double dist);
239  bool convertToClosed();
240  bool convertToOpen();
241 
242  RPolyline modifyPolylineCorner(
243  const RShape& trimmedShape1, RS::Ending ending1, int segmentIndex1,
244  const RShape& trimmedShape2, RS::Ending ending2, int segmentIndex2,
245  const RShape* cornerShape = NULL) const;
246 
247  bool isConcave() const;
248  QList<RVector> getConvexVertices(bool convex = true) const;
249  QList<RVector> getConcaveVertices() const;
250 
251  QList<RPolyline> splitAtDiscontinuities(double tolerance) const;
252  QList<RPolyline> splitAtSegmentTypeChange() const;
253 
254  double getBaseAngle() const;
255  double getWidth() const;
256  bool setWidth(double v);
257  double getHeight() const;
258  bool setHeight(double v);
259 
260  QList<RPolyline> morph(const RPolyline& target, int steps, RS::Easing easing = RS::Linear, bool zLinear = true, double customFactor = RNANDOUBLE) const;
261  RPolyline roundAllCorners(double radius) const;
262  RPolyline getPolygonHull(double angle, double tolerance, bool inner = false) const;
263 
264  static bool hasProxy() {
265  return polylineProxy!=NULL;
266  }
267 
272  if (polylineProxy!=NULL) {
273  delete polylineProxy;
274  }
275  polylineProxy = p;
276  }
277 
282  return polylineProxy;
283  }
284 
285 protected:
286  bool isLineSegment(int i) const;
287 
288  void print(QDebug dbg) const;
289 
290 protected:
295  QList<RVector> vertices;
296 
297  QList<double> bulges;
298 
299  QList<double> endWidths;
300  QList<double> startWidths;
301 
306  bool closed;
307 
308 private:
309  // TODO caching:
310  //QList<QSharedPointer<RShape> > subShapes;
312 };
313 
317 Q_DECLARE_METATYPE(QSharedPointer<RPolyline>)
318 Q_DECLARE_METATYPE(QSharedPointer<RPolyline>*)
319 
320 #endif
RPolyline::polylineProxy
static RPolylineProxy * polylineProxy
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RPolyline.h:311
M_PI_4
#define M_PI_4
Definition: RMath.h:43
RPolyline::isDirected
virtual bool isDirected() const
Definition: RPolyline.h:64
RShape::getSelfIntersectionPoints
virtual QList< RVector > getSelfIntersectionPoints() const
Definition: RShape.h:263
RShape::getTransformed
virtual QSharedPointer< RShape > getTransformed(const QTransform &transform) const =0
RShape::getLength
virtual double getLength() const =0
RShape::stretch
virtual bool stretch(const RBox &area, const RVector &offset)
Definition: RShape.cpp:1740
RPolyline::clone
virtual RPolyline * clone() const
Definition: RPolyline.h:60
RPolyline::endWidths
QList< double > endWidths
Definition: RPolyline.h:299
RS::AngleTolerance
static const double AngleTolerance
Definition: RS.h:700
rotate
void rotate(void e, void angle, void center)
Rotates the given entity or shape by the given angle around the given center.
Definition: simple_modify.js:109
RShape::getMiddlePoints
virtual QList< RVector > getMiddlePoints() const =0
RPolyline::hasProxy
static bool hasProxy()
Definition: RPolyline.h:264
RS::Side
Side
Side used for side of a point relative to an entity (right hand or left hand side)
Definition: RS.h:205
RS::PointTolerance
static const double PointTolerance
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RS.h:699
RVector
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
RS::Ending
Ending
Entity ending.
Definition: RS.h:215
RPolyline::vertices
QList< RVector > vertices
Getter function for this property: getVertices Setter function for this property: setVertices
Definition: RPolyline.h:295
RS::FromStart
@ FromStart
Definition: RS.h:264
RPolylineProxy
Proxy for advanced polyline functionality.
Definition: RPolylineProxy.h:42
RShape::getVectorProperties
virtual QList< RVector > getVectorProperties() const
Definition: RShape.h:165
i
int i
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: autostart.js:49
RPolyline::isEmpty
bool isEmpty() const
Definition: RPolyline.h:94
RS::UnknownOrientation
@ UnknownOrientation
Definition: RS.h:583
RShape::getCenterPoints
virtual QList< RVector > getCenterPoints() const =0
RS::Linear
@ Linear
Definition: RS.h:605
RLine
Low-level mathematical representation of a line.
Definition: RLine.h:41
RShape::getTrimEnd
virtual RS::Ending getTrimEnd(const RVector &trimPoint, const RVector &clickPoint)
Definition: RShape.h:339
RShape::Type
Type
Definition: RShape.h:74
RPolyline::isInterpolated
virtual bool isInterpolated() const
Reimplement and return true if this shape relies on interpolation for geometrical operations such as ...
Definition: RPolyline.h:217
RShape
Interface for geometrical shape classes.
Definition: RShape.h:72
RShape.h
RDEFAULT_RVECTOR
#define RDEFAULT_RVECTOR
Definition: RVector.h:37
RShape::getVectorTo
virtual RVector getVectorTo(const RVector &point, bool limited=true, double strictRange=RMAXDOUBLE) const =0
RPolyline::startWidths
QList< double > startWidths
Definition: RPolyline.h:300
RVector.h
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(RMath *)
RShape::getDirection1
virtual double getDirection1() const
Definition: RShape.h:271
RPolyline::closed
bool closed
Getter function for this property: isClosed Setter function for this property: setClosed
Definition: RPolyline.h:306
RShape::getDoubleProperties
virtual QList< double > getDoubleProperties() const
Definition: RShape.h:167
RNANDOUBLE
#define RNANDOUBLE
Definition: RMath.h:71
RShape::getBoolProperties
virtual QList< bool > getBoolProperties() const
Definition: RShape.h:168
RDEFAULT_MIN1
#define RDEFAULT_MIN1
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RExplodable.h:30
mirror
void mirror(void e, void axis)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:42
RShape::getPointCloud
virtual QList< RVector > getPointCloud(double segmentLength) const =0
RShape::getPointAtPercent
virtual RVector getPointAtPercent(double p) const
Definition: RShape.cpp:182
RPainterPath
Extended painter path with a z-level and a pen.
Definition: RPainterPath.h:46
RShape::getBoundingBox
virtual RBox getBoundingBox() const =0
RS::Easing
Easing
Definition: RS.h:604
RShape::getSideOfPoint
virtual RS::Side getSideOfPoint(const RVector &point) const
Definition: RShape.h:278
RExplodable::getExploded
virtual QList< QSharedPointer< RShape > > getExploded(int segments=RDEFAULT_MIN1) const =0
RShape::getEndPoints
virtual QList< RVector > getEndPoints() const =0
RMAXDOUBLE
#define RMAXDOUBLE
Definition: RMath.h:63
trimEndPoint
void trimEndPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:690
RPolyline::getShapeType
virtual RShape::Type getShapeType() const
Definition: RPolyline.h:56
RShape::getStartPoint
virtual RVector getStartPoint() const
Definition: RShape.h:283
RShape::Polyline
@ Polyline
Definition: RShape.h:81
RS::Orientation
Orientation
Definition: RS.h:582
RShape::getPointsWithDistanceToEnd
virtual QList< RVector > getPointsWithDistanceToEnd(double distance, int from=RS::FromAny) const =0
RShape::setZ
virtual void setZ(double z)=0
RS::FromAny
@ FromAny
Start or end.
Definition: RS.h:266
RShape::getDirection2
virtual double getDirection2() const
Definition: RShape.h:274
RShape::print
virtual void print(QDebug dbg) const
Definition: RShape.cpp:1759
RPolyline::autoClose
bool autoClose(double tolerance=RS::PointTolerance)
Definition: RPolyline.h:130
RPolylineProxy.h
RShape::getMiddlePoint
virtual RVector getMiddlePoint() const
Definition: RShape.h:289
RShape::getDistancesFromStart
virtual QList< double > getDistancesFromStart(const RVector &p) const
Definition: RShape.h:359
RPolyline::getPolylineProxy
static RPolylineProxy * getPolylineProxy()
Definition: RPolyline.h:281
RShape::reverse
virtual bool reverse()
Definition: RShape.h:293
RBox
Represents a box e.g.
Definition: RBox.h:43
RShape::roundAllCorners
static QList< QSharedPointer< RShape > > roundAllCorners(const QList< QSharedPointer< RShape > > &shapes, double radius)
Round every corner of the list of given (connected) shapes with the given radius.
Definition: RShape.cpp:2328
move
void move(void e, void offset)
Moves the given entity or shape by the given offset.
Definition: simple_modify.js:25
RPolyline::bulges
QList< double > bulges
Definition: RPolyline.h:297
RVector::invalid
static const RVector invalid
invalid vector
Definition: RVector.h:322
RExplodable.h
RS::From
From
End used to specify from which end of a shape to measure a distance.
Definition: RS.h:263
RPainterPath.h
RExplodable
Interface for explodable shape classes.
Definition: RExplodable.h:40
RPolyline::getDistanceFromStart
virtual double getDistanceFromStart(const RVector &p) const
Definition: RPolyline.h:169
RShape::getAngleAt
virtual double getAngleAt(double distance, RS::From from=RS::FromStart) const
Definition: RShape.h:244
RShape::getEndPoint
virtual RVector getEndPoint() const
Definition: RShape.h:286
trimStartPoint
void trimStartPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:206
RPolyline
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
QCADCORE_EXPORT
#define QCADCORE_EXPORT
Definition: core_global.h:10
scale
void scale(void e, void factor, void focusPoint)
Scales the given entity or shape by the given factor with the given focus point.
Definition: simple_modify.js:76
RShape::getDistanceTo
virtual double getDistanceTo(const RVector &point, bool limited=true, double strictRange=RMAXDOUBLE) const
Definition: RShape.cpp:48
RPolyline::setPolylineProxy
static void setPolylineProxy(RPolylineProxy *p)
Definition: RPolyline.h:271