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 prependVertex(const RVector& vertex, double bulge = 0.0, double w1 = 0.0, double w2 = 0.0);
84  void insertVertex(int index, const RVector& vertex, double bulgeBefore = 0.0, double bulgeAfter = 0.0);
85  void insertVertexAt(const RVector& point);
86  RVector insertVertexAtDistance(double dist);
87  void removeFirstVertex();
88  void removeLastVertex();
89  void removeVertex(int index);
90  void removeVerticesAfter(int index);
91  void removeVerticesBefore(int index);
92 
93  bool isEmpty() {
94  return countVertices()==0;
95  }
96 
97  void setVertices(const QList<RVector>& vertices);
98  QList<RVector> getVertices() const;
99  void setVertexAt(int i, const RVector& v);
100  void moveVertexAt(int i, const RVector& offset);
101  RVector getVertexAt(int i) const;
102  int getVertexIndex(const RVector& v, double tolerance=RS::PointTolerance) const;
103  RVector getLastVertex() const;
104  int countVertices() const;
105 
106  void setBulges(const QList<double>& b);
107  QList<double> getBulges() const;
108  double getBulgeAt(int i) const;
109  void setBulgeAt(int i, double b);
110  bool hasArcSegments() const;
111 
112  QList<double> getVertexAngles() const;
113  double getVertexAngle(int i, RS::Orientation orientation = RS::UnknownOrientation) const;
114 
115  void setGlobalWidth(double w);
116  void setStartWidthAt(int i, double w);
117  double getStartWidthAt(int i) const;
118  void setEndWidthAt(int i, double w);
119  double getEndWidthAt(int i) const;
120  bool hasWidths() const;
121  void setStartWidths(const QList<double>& sw);
122  QList<double> getStartWidths() const;
123  void setEndWidths(const QList<double>& ew);
124  QList<double> getEndWidths() const;
125 
126  void setClosed(bool on);
127  bool isClosed() const;
128  bool isGeometricallyClosed(double tolerance=RS::PointTolerance) const;
129  bool autoClose(double tolerance=RS::PointTolerance) {
130  return toLogicallyClosed(tolerance);
131  }
132  bool toLogicallyClosed(double tolerance=RS::PointTolerance);
133  bool toLogicallyOpen();
134 
135  QList<RVector> getSelfIntersectionPoints() const;
136 
137  RS::Orientation getOrientation(bool implicitelyClosed = false) const;
138  bool setOrientation(RS::Orientation orientation);
139 
140  RPolyline convertArcToLineSegments(int segments) const;
141  RPolyline convertArcToLineSegmentsLength(double segmentLength) const;
142 
143  bool contains(const RVector& point, bool borderIsInside=false, double tolerance=RS::PointTolerance) const;
144  bool containsShape(const RShape& shape) const;
145 
146  RVector getPointInside() const;
147 
148  virtual RVector getStartPoint() const;
149  virtual RVector getEndPoint() const;
150  virtual RVector getMiddlePoint() const;
151 
152  void moveStartPoint(const RVector& pos);
153  void moveEndPoint(const RVector& pos);
154 
155  void moveSegmentAt(int i, const RVector& offset);
156 
157  virtual double getDirection1() const;
158  virtual double getDirection2() const;
159 
160  virtual RS::Side getSideOfPoint(const RVector& point) const;
161 
162  virtual RBox getBoundingBox() const;
163 
164  double getArea() const;
165 
166  virtual double getLength() const;
167 
168  virtual double getDistanceFromStart(const RVector& p) const {
169  QList<double> res = getDistancesFromStart(p);
170  if (res.isEmpty()) {
171  return RMAXDOUBLE;
172  }
173  return res.first();
174  }
175  virtual QList<double> getDistancesFromStart(const RVector& p) const;
176  double getLengthTo(const RVector& p, bool limited = true) const;
177  double getSegmentsLength(int fromIndex, int toIndex) const;
178 
179  virtual QList<RVector> getEndPoints() const;
180  virtual QList<RVector> getMiddlePoints() const;
181  virtual QList<RVector> getCenterPoints() const;
182  virtual QList<RVector> getPointsWithDistanceToEnd(
183  double distance, int from = RS::FromAny) const;
184 
185  virtual double getAngleAt(double distance, RS::From from = RS::FromStart) const;
186 
187  virtual RVector getVectorTo(const RVector& point,
188  bool limited = true, double strictRange = RMAXDOUBLE) const;
189  virtual double getDistanceTo(const RVector& point, bool limited = true, double strictRange = RMAXDOUBLE) const;
190 
191  int getClosestSegment(const RVector& point) const;
192  int getClosestVertex(const RVector& point) const;
193 
194  virtual bool move(const RVector& offset);
195  virtual bool rotate(double rotation, const RVector& center = RDEFAULT_RVECTOR);
196  virtual bool scale(double scaleFactor, const RVector& center = RDEFAULT_RVECTOR);
197  virtual bool scale(const RVector& scaleFactors, const RVector& center = RDEFAULT_RVECTOR);
198  virtual bool mirror(const RLine& axis);
199  virtual bool reverse();
200  virtual bool stretch(const RPolyline& area, const RVector& offset);
201 
202  virtual QSharedPointer<RShape> getTransformed(const QTransform& transform) const;
203 
204  virtual RS::Ending getTrimEnd(const RVector& trimPoint, const RVector& clickPoint);
205  virtual bool trimStartPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false);
206  virtual bool trimEndPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false);
207  virtual bool trimStartPoint(double trimDist);
208  virtual bool trimEndPoint(double trimDist);
209 
210  virtual QList<QSharedPointer<RShape> > getExploded(int segments = RDEFAULT_MIN1) const;
211  QList<RPolyline> getOutline() const;
212  virtual bool isInterpolated() const {
213  return false;
214  }
215  int countSegments() const;
216  QSharedPointer<RShape> getSegmentAt(int i) const;
217  bool isArcSegmentAt(int i) const;
218  QSharedPointer<RShape> getLastSegment() const;
219  QSharedPointer<RShape> getFirstSegment() const;
220 
221  static bool isStraight(double bulge);
222 
223  RPainterPath toPainterPath() const;
224 
225  bool simplify(double tolerance = RS::PointTolerance);
226  QList<RVector> verifyTangency(double toleranceMin = RS::AngleTolerance, double toleranceMax = M_PI_4);
227 
228  void stripWidths();
229  void setMinimumWidth(double w);
230 
231  int getSegmentAtDist(double dist);
232  bool relocateStartPoint(const RVector& p);
233  bool relocateStartPoint(double dist);
234  bool convertToClosed();
235  bool convertToOpen();
236 
237  RPolyline modifyPolylineCorner(
238  const RShape& trimmedShape1, RS::Ending ending1, int segmentIndex1,
239  const RShape& trimmedShape2, RS::Ending ending2, int segmentIndex2,
240  const RShape* cornerShape = NULL) const;
241 
242  QList<RVector> getConvexVertices(bool convex = true) const;
243  QList<RVector> getConcaveVertices() const;
244 
245  QList<RPolyline> splitAtDiscontinuities(double tolerance) const;
246  QList<RPolyline> splitAtSegmentTypeChange() const;
247 
248  double getBaseAngle() const;
249  double getWidth() const;
250  bool setWidth(double v);
251  double getHeight() const;
252  bool setHeight(double v);
253 
254  QList<RPolyline> morph(const RPolyline& target, int steps, RS::Easing easing = RS::Linear, bool zLinear = true, double customFactor = RNANDOUBLE) const;
255 
256  static bool hasProxy() {
257  return polylineProxy!=NULL;
258  }
259 
264  if (polylineProxy!=NULL) {
265  delete polylineProxy;
266  }
267  polylineProxy = p;
268  }
269 
274  return polylineProxy;
275  }
276 
277 protected:
278  bool isLineSegment(int i) const;
279 
280  void print(QDebug dbg) const;
281 
282 protected:
287  QList<RVector> vertices;
288 
289  QList<double> bulges;
290 
291  QList<double> endWidths;
292  QList<double> startWidths;
293 
298  bool closed;
299 
300 private:
301  // TODO caching:
302  //QList<QSharedPointer<RShape> > subShapes;
304 };
305 
309 Q_DECLARE_METATYPE(QSharedPointer<RPolyline>)
310 Q_DECLARE_METATYPE(QSharedPointer<RPolyline>*)
311 
312 #endif
static const double AngleTolerance
Definition: RS.h:685
Represents a box e.g.
Definition: RBox.h:43
Ending
Entity ending.
Definition: RS.h:204
Definition: RS.h:571
virtual RVector getVectorTo(const RVector &point, bool limited=true, double strictRange=RMAXDOUBLE) const =0
#define M_PI_4
Definition: RMath.h:43
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
virtual QList< RVector > getEndPoints() const =0
virtual RVector getEndPoint() const
Definition: RShape.h:255
virtual QList< RVector > getVectorProperties() const
Definition: RShape.h:145
virtual QList< double > getDoubleProperties() const
Definition: RShape.h:147
virtual QList< bool > getBoolProperties() const
Definition: RShape.h:148
static RPolylineProxy * polylineProxy
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RPolyline.h:303
QList< double > endWidths
Definition: RPolyline.h:291
void mirror(void e, void axis)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:81
virtual bool reverse()
Definition: RShape.h:262
#define QCADCORE_EXPORT
Definition: core_global.h:10
void move(void e, void offset)
Moves the given entity or shape by the given offset.
Definition: simple_modify.js:109
virtual QList< RVector > getPointsWithDistanceToEnd(double distance, int from=RS::FromAny) const =0
static bool hasProxy()
Definition: RPolyline.h:256
virtual bool isDirected() const
Definition: RPolyline.h:64
virtual bool isInterpolated() const
Reimplement and return true if this shape relies on interpolation for geometrical operations such as ...
Definition: RPolyline.h:212
Definition: RS.h:593
virtual RShape::Type getShapeType() const
Definition: RPolyline.h:56
virtual double getLength() const =0
virtual RBox getBoundingBox() const =0
#define RNANDOUBLE
Definition: RMath.h:71
Start or end.
Definition: RS.h:255
QList< double > startWidths
Definition: RPolyline.h:292
virtual double getDirection1() const
Definition: RShape.h:240
QList< RVector > vertices
Getter function for this property: getVertices.
Definition: RPolyline.h:287
Side
Side used for side of a point relativ to an entity (right hand or left hand side) ...
Definition: RS.h:194
bool closed
Getter function for this property: isClosed.
Definition: RPolyline.h:298
virtual double getDirection2() const
Definition: RShape.h:243
virtual double getAngleAt(double distance, RS::From from=RS::FromStart) const
Definition: RShape.h:213
static const double PointTolerance
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RS.h:684
virtual bool stretch(const RBox &area, const RVector &offset)
Definition: RShape.cpp:1725
virtual RPolyline * clone() const
Definition: RPolyline.h:60
Q_DECLARE_METATYPE(RMath *)
virtual RVector getMiddlePoint() const
Definition: RShape.h:258
virtual QList< QSharedPointer< RShape > > getExploded(int segments=RDEFAULT_MIN1) const =0
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:25
bool isEmpty()
Definition: RPolyline.h:93
virtual QList< RVector > getMiddlePoints() const =0
Extended painter path with a z-level and a pen.
Definition: RPainterPath.h:46
virtual QList< double > getDistancesFromStart(const RVector &p) const
Definition: RShape.h:328
Type
Definition: RShape.h:54
void trimEndPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:596
#define RMAXDOUBLE
Definition: RMath.h:63
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:95
virtual double getDistanceTo(const RVector &point, bool limited=true, double strictRange=RMAXDOUBLE) const
Definition: RShape.cpp:47
Definition: RS.h:253
Definition: RShape.h:61
Low-level mathematical representation of a line.
Definition: RLine.h:41
void trimStartPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:864
virtual RS::Side getSideOfPoint(const RVector &point) const
Definition: RShape.h:247
#define RDEFAULT_MIN1
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RExplodable.h:30
virtual RS::Ending getTrimEnd(const RVector &trimPoint, const RVector &clickPoint)
Definition: RShape.h:308
Interface for explodable shape classes.
Definition: RExplodable.h:40
virtual QSharedPointer< RShape > getTransformed(const QTransform &transform) const =0
From
End used to specify from which end of a shape to measure a distance.
Definition: RS.h:252
Orientation
Definition: RS.h:570
Proxy for advanced polyline functionality.
Definition: RPolylineProxy.h:42
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
Interface for geometrical shape classes.
Definition: RShape.h:52
virtual QList< RVector > getCenterPoints() const =0
virtual void print(QDebug dbg) const
Definition: RShape.cpp:1740
virtual double getDistanceFromStart(const RVector &p) const
Definition: RPolyline.h:168
static const RVector invalid
invalid vector
Definition: RVector.h:322
Easing
Definition: RS.h:592
bool autoClose(double tolerance=RS::PointTolerance)
Definition: RPolyline.h:129
virtual RVector getStartPoint() const
Definition: RShape.h:252
virtual void setZ(double z)=0
QList< double > bulges
Definition: RPolyline.h:289
#define RDEFAULT_RVECTOR
Definition: RVector.h:37
static void setPolylineProxy(RPolylineProxy *p)
Definition: RPolyline.h:263
virtual QList< RVector > getSelfIntersectionPoints() const
Definition: RShape.h:232
static RPolylineProxy * getPolylineProxy()
Definition: RPolyline.h:273