QCAD
Open Source 2D CAD
RShape.h
Go to the documentation of this file.
1 
20 #ifndef RSHAPE_H
21 #define RSHAPE_H
22 
23 #include "../core_global.h"
24 
25 #include <QList>
26 #include <QSharedPointer>
27 
28 #include "RVector.h"
29 #include "RMath.h"
30 
31 class RArc;
32 class RBox;
33 class RCircle;
34 class REllipse;
35 class RExplodable;
36 class RLine;
37 class RPolyline;
38 class RSpline;
39 class RTriangle;
40 
41 #ifndef RDEFAULT_TOLERANCE_1E_MIN4
42 #define RDEFAULT_TOLERANCE_1E_MIN4 1.0e-4
43 #endif
44 
53 public:
54  enum Type {
55  Unknown = -1,
56  Point = 0,
57  Line = 1,
58  Arc = 2,
59  Circle = 3,
60  Ellipse = 4,
61  Polyline = 5,
62  Spline = 6,
63  Triangle = 7,
64  XLine = 8,
65  Ray = 9
66  };
67 
68 public:
69  RShape() {
70  }
71 
72  virtual ~RShape() {
73  }
74 
75  virtual RShape::Type getShapeType() const {
76  return Unknown;
77  }
78 
79  static bool isPointShape(const RShape& s) {
80  return s.getShapeType()==RShape::Point;
81  }
82  static bool isLineShape(const RShape& s) {
83  return s.getShapeType()==RShape::Line;
84  }
85  static bool isArcShape(const RShape& s) {
86  return s.getShapeType()==RShape::Arc;
87  }
88  static bool isCircleShape(const RShape& s) {
89  return s.getShapeType()==RShape::Circle;
90  }
91  static bool isEllipseShape(const RShape& s) {
92  return s.getShapeType()==RShape::Ellipse;
93  }
94  static bool isFullEllipseShape(const RShape& s);
95  static bool isPolylineShape(const RShape& s) {
96  return s.getShapeType()==RShape::Polyline;
97  }
98  static bool isSplineShape(const RShape& s) {
99  return s.getShapeType()==RShape::Spline;
100  }
101  static bool isTriangleShape(const RShape& s) {
102  return s.getShapeType()==RShape::Triangle;
103  }
104  static bool isXLineShape(const RShape& s) {
105  return s.getShapeType()==RShape::XLine;
106  }
107  static bool isRayShape(const RShape& s) {
108  return s.getShapeType()==RShape::Ray;
109  }
110 
111  virtual RShape* clone() const = 0;
112 
118  virtual bool isInterpolated() const {
119  return false;
120  }
121 
122  virtual RVector getClosestPointOnShape(const RVector& p,
123  bool limited = true, double strictRange = RMAXDOUBLE) const;
124 
128  virtual RBox getBoundingBox() const = 0;
129 
133  virtual void to2D() = 0;
134 
138  virtual double getLength() const = 0;
139 
140  virtual bool equals(const RShape& other, double tolerance=RS::PointTolerance) const;
141  virtual QList<RVector> getVectorProperties() const { return QList<RVector>(); }
142  virtual QList<int> getIntProperties() const { return QList<int>(); }
143  virtual QList<double> getDoubleProperties() const { return QList<double>(); }
144  virtual QList<bool> getBoolProperties() const { return QList<bool>(); }
145 
156  virtual RVector getVectorTo(const RVector& point,
157  bool limited = true, double strictRange = RMAXDOUBLE) const = 0;
158 
159  virtual double getDistanceTo(const RVector& point, bool limited = true, double strictRange = RMAXDOUBLE) const;
160  virtual bool isOnShape(const RVector& point,
161  bool limited = true,
162  double tolerance = RDEFAULT_TOLERANCE_1E_MIN4) const;
163  virtual QList<RVector> filterOnShape(const QList<RVector>& pointList,
164  bool limited = true,
165  double tolerance = RDEFAULT_TOLERANCE_1E_MIN4) const;
166  virtual RVector getVectorFromEndpointTo(const RVector& point) const;
167 
171  virtual QList<RVector> getEndPoints() const = 0;
172 
176  virtual QList<RVector> getMiddlePoints() const = 0;
177 
181  virtual QList<RVector> getCenterPoints() const = 0;
182 
186  virtual QList<RVector> getPointsWithDistanceToEnd(
187  double distance, int from = RS::FromAny) const = 0;
188 
189  virtual RVector getPointWithDistanceToStart(double distance) {
190  QList<RVector> res = getPointsWithDistanceToEnd(distance, RS::FromStart);
191  if (res.isEmpty()) {
192  return RVector::invalid;
193  }
194  return res[0];
195  }
196 
197  virtual RVector getPointWithDistanceToEnd(double distance) {
198  QList<RVector> res = getPointsWithDistanceToEnd(distance, RS::FromEnd);
199  if (res.isEmpty()) {
200  return RVector::invalid;
201  }
202  return res[0];
203  }
204 
208  virtual double getAngleAt(double distance, RS::From from = RS::FromStart) const {
209  Q_UNUSED(distance)
210  Q_UNUSED(from)
211  return RNANDOUBLE;
212  }
213  virtual double getAngleAtPoint(const RVector& pos) const {
214  double d = getDistanceFromStart(pos);
215  return getAngleAt(d);
216  }
217 
218  virtual RVector getPointAtPercent(double p) const;
219 
220  virtual bool intersectsWith(const RShape& other,
221  bool limited = true) const;
222 
223  QList<RVector> getIntersectionPoints(const RShape& other,
224  bool limited = true, bool same = false, bool force = false) const;
225 
226  virtual QList<RVector> getSelfIntersectionPoints() const {
227  return QList<RVector>();
228  }
229 
230  virtual bool isDirected() const {
231  return false;
232  }
233 
234  virtual double getDirection1() const {
235  return RNANDOUBLE;
236  }
237  virtual double getDirection2() const {
238  return RNANDOUBLE;
239  }
240 
241  virtual RS::Side getSideOfPoint(const RVector& point) const {
242  Q_UNUSED(point)
243  return RS::NoSide;
244  }
245 
246  virtual RVector getStartPoint() const {
247  return RVector::invalid;
248  }
249  virtual RVector getEndPoint() const {
250  return RVector::invalid;
251  }
252  virtual RVector getMiddlePoint() const {
253  return RVector::invalid;
254  }
255 
256  virtual bool reverse() {
257  return false;
258  }
259 
266  virtual bool trimStartPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false) {
267  Q_UNUSED(trimPoint)
268  Q_UNUSED(clickPoint)
269  Q_UNUSED(extend)
270 
271  return false;
272  }
273 
274  virtual bool trimStartPoint(double trimDist) {
275  RVector p = getPointWithDistanceToStart(trimDist);
276  return trimStartPoint(p);
277  }
278 
285  virtual bool trimEndPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false) {
286  Q_UNUSED(trimPoint)
287  Q_UNUSED(clickPoint)
288  Q_UNUSED(extend)
289 
290  return false;
291  }
292 
293  virtual bool trimEndPoint(double trimDist) {
294  RVector p = getPointWithDistanceToStart(trimDist);
295  return trimEndPoint(p);
296  }
297 
302  virtual RS::Ending getTrimEnd(const RVector& trimPoint, const RVector& clickPoint) {
303  Q_UNUSED(trimPoint)
304  Q_UNUSED(clickPoint)
305 
306  return RS::EndingNone;
307  }
308 
313  virtual double getDistanceFromStart(const RVector& p) const {
314  Q_UNUSED(p)
315  return RMAXDOUBLE;
316  }
317 
322  virtual QList<double> getDistancesFromStart(const RVector& p) const {
323  return QList<double>() << getDistanceFromStart(p);
324  }
325 
330  static QList<RVector> getIntersectionPoints(const RShape& shape1,
331  const RShape& shape2, bool limited = true, bool same = false, bool force = false);
332 
333  virtual bool move(const RVector& offset)=0;
334  virtual bool rotate(double rotation, const RVector& center = RDEFAULT_RVECTOR)=0;
335  virtual bool scale(double scaleFactor, const RVector& center = RVector());
336  virtual bool scale(const RVector& scaleFactors, const RVector& center = RVector())=0;
337  virtual bool mirror(const RLine& axis)=0;
338  virtual bool flipHorizontal();
339  virtual bool flipVertical();
340  virtual bool stretch(const RBox& area, const RVector& offset);
341  virtual bool stretch(const RPolyline& area, const RVector& offset);
342 
343  virtual QSharedPointer<RShape> getTransformed(const QTransform& transform) const = 0;
344 
345  static bool order(QList<QList<QSharedPointer<RShape> > >& boundary);
346 
347  static QList<RVector> getIntersectionPointsLL(const RLine& line1,
348  const RLine& line2, bool limited = true) {
349  return getIntersectionPointsLL(line1, line2, limited, limited);
350  }
351  static QList<RVector> getIntersectionPointsLL(const RLine& line1,
352  const RLine& line2, bool limited1, bool limited2);
353  static QList<RVector> getIntersectionPointsLA(const RLine& line1,
354  const RArc& arc2, bool limited = true) {
355  return getIntersectionPointsLA(line1, arc2, limited, limited);
356  }
357  static QList<RVector> getIntersectionPointsLA(const RLine& line1,
358  const RArc& arc2, bool limited1, bool limited2);
359  static QList<RVector> getIntersectionPointsLC(const RLine& line1,
360  const RCircle& circle2, bool limited = true);
361  static QList<RVector> getIntersectionPointsLE(const RLine& line1,
362  const REllipse& ellipse2, bool limited = true) {
363  return getIntersectionPointsLE(line1, ellipse2, limited, limited);
364  }
365  static QList<RVector> getIntersectionPointsLE(const RLine& line1,
366  const REllipse& ellipse2, bool limited1, bool limited2);
367  static QList<RVector> getIntersectionPointsLT(const RLine& line1,
368  const RTriangle& triangle2, bool limited = true) {
369  return getIntersectionPointsLT(line1, triangle2, limited, limited);
370  }
371  static QList<RVector> getIntersectionPointsLT(const RLine& line1,
372  const RTriangle& triangle2, bool limited1, bool limited2);
373  static QList<RVector> getIntersectionPointsLS(const RLine& line1,
374  const RSpline& spline2, bool limited = true);
375  static QList<RVector> getIntersectionPointsLX(const RLine& line1,
376  const RExplodable& explodable2, bool limited = true);
377 
378  static QList<RVector> getIntersectionPointsAA(const RArc& arc1,
379  const RArc& arc2, bool limited = true);
380  static QList<RVector> getIntersectionPointsAC(const RArc& arc1,
381  const RCircle& circle2, bool limited = true);
382  static QList<RVector> getIntersectionPointsAE(const RArc& arc1,
383  const REllipse& ellipse2, bool limited = true);
384  static QList<RVector> getIntersectionPointsAT(const RArc& arc1,
385  const RTriangle& triangle2, bool limited = true);
386  static QList<RVector> getIntersectionPointsAS(const RArc& arc1,
387  const RSpline& spline2, bool limited = true);
388  static QList<RVector> getIntersectionPointsAX(const RArc& arc1,
389  const RExplodable& explodable2, bool limited = true);
390 
391  static QList<RVector> getIntersectionPointsCC(const RCircle& circle1,
392  const RCircle& circle2);
393  static QList<RVector> getIntersectionPointsCE(const RCircle& circle1,
394  const REllipse& ellipse2);
395  static QList<RVector> getIntersectionPointsCS(const RCircle& circle1,
396  const RSpline& spline2, bool limited = true);
397  static QList<RVector> getIntersectionPointsCX(const RCircle& circle1,
398  const RExplodable& explodable2, bool limited = true);
399 
400  static QList<RVector> getIntersectionPointsEE(const REllipse& ellipse1,
401  const REllipse& ellipse2);
402  static QList<RVector> getIntersectionPointsEE(const REllipse& ellipse1,
403  const REllipse& ellipse2, bool limited);
404  static QList<RVector> getIntersectionPointsES(const REllipse& ellipse1,
405  const RSpline& spline2, bool limited = true);
406  static QList<RVector> getIntersectionPointsEX(const REllipse& ellipse1,
407  const RExplodable& explodable2, bool limited = true);
408 
409  static QList<RVector> getIntersectionPointsSX(const RSpline& spline1,
410  const RExplodable& explodable2, bool limited);
411 
412  static QList<RVector> getIntersectionPointsSS(const RSpline& spline1,
413  const RSpline& spline2, bool limited = true, bool same = false);
414 
415  static QList<RVector> getIntersectionPointsXX(const RExplodable& explodable1,
416  const RExplodable& explodable2, bool limited = true, bool same = false);
417 
418  static const RExplodable* castToExplodable(const RShape* shape);
419 
420  virtual QList<QSharedPointer<RShape> > getOffsetShapes(double distance, int number, RS::Side side, const RVector& position = RVector::invalid);
421 
422  static QList<QSharedPointer<RShape> > getOffsetLines(const RShape& shape, double distance, int number, RS::Side side, const RVector& position = RVector::invalid);
423  static QList<QSharedPointer<RShape> > getOffsetArcs(const RShape& shape, double distance, int number, RS::Side side, const RVector& position = RVector::invalid);
424 
425  static QList<QSharedPointer<RShape> > getReversedShapeList(const QList<QSharedPointer<RShape> >& shapes);
426 
427  virtual QList<QSharedPointer<RShape> > splitAt(const QList<RVector>& points) const;
428 
429  static QList<QSharedPointer<RShape> > trim(
430  const RShape& trimShape, const RVector& trimClickPos,
431  const RShape& limitingShape, const RVector& limitingClickPos,
432  bool trimBoth, bool samePolyline);
433 
434  static int getErrorCode() {
435  return errorCode;
436  }
437 
438  void dump();
439 
443  friend QDebug operator<<(QDebug dbg, const RShape& s) {
444  s.print(dbg);
445  return dbg;
446  }
447 
448 private:
449  static double ellipse2tr(double x, double y, double AA, double BB,
450  double CC, double DD, double EE, double FF);
451 
452 private:
453  static double twopi;
454  static double epsTolerance;
455 
456 protected:
457  virtual void print(QDebug dbg) const;
458  static int errorCode;
459 };
460 
464 Q_DECLARE_METATYPE(QSharedPointer<RShape>)
465 Q_DECLARE_METATYPE(QSharedPointer<const RShape>)
466 Q_DECLARE_METATYPE(QSharedPointer<RShape>*)
467 Q_DECLARE_METATYPE(QList<QSharedPointer<RShape> >)
468 Q_DECLARE_METATYPE(QList<QSharedPointer<RShape> >*)
469 Q_DECLARE_METATYPE(QList<QSharedPointer<const RShape> >)
470 
471 #endif
virtual bool trimStartPoint(double trimDist)
Definition: RShape.h:274
Represents a box e.g.
Definition: RBox.h:43
Ending
Entity ending.
Definition: RS.h:152
Definition: RShape.h:59
Definition: RShape.h:58
Definition: RShape.h:57
Base class for all circle drawing tools.
Definition: Circle.js:17
Definition: RShape.h:62
virtual double getAngleAtPoint(const RVector &pos) const
Definition: RShape.h:213
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
virtual bool trimStartPoint(const RVector &trimPoint, const RVector &clickPoint=RVector::invalid, bool extend=false)
Definition: RShape.h:266
virtual QList< int > getIntProperties() const
Definition: RShape.h:142
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RSpline.h:55
static int errorCode
Definition: RShape.h:458
virtual RVector getEndPoint() const
Definition: RShape.h:249
virtual bool isInterpolated() const
Reimplement and return true if this shape relies on interpolation for geometrical operations such as ...
Definition: RShape.h:118
virtual QList< RVector > getVectorProperties() const
Definition: RShape.h:141
static QList< RVector > getIntersectionPointsLE(const RLine &line1, const REllipse &ellipse2, bool limited=true)
Definition: RShape.h:361
Low-level mathematical representation of a triangle.
Definition: RTriangle.h:46
virtual QList< double > getDoubleProperties() const
Definition: RShape.h:143
static QList< RVector > getIntersectionPointsLA(const RLine &line1, const RArc &arc2, bool limited=true)
Definition: RShape.h:353
Definition: RShape.h:56
virtual QList< bool > getBoolProperties() const
Definition: RShape.h:144
Low-level mathematical representation of an arc.
Definition: RArc.h:40
Definition: RShape.h:60
virtual bool trimEndPoint(double trimDist)
Definition: RShape.h:293
Definition: RS.h:202
Base class for all arc drawing tools.
Definition: Arc.js:17
static bool isPointShape(const RShape &s)
Definition: RShape.h:79
void mirror(void e, void axis)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:18
virtual bool reverse()
Definition: RShape.h:256
#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:85
virtual ~RShape()
Definition: RShape.h:72
Definition: RShape.h:64
static double epsTolerance
Definition: RShape.h:454
Definition: RShape.h:65
static bool isXLineShape(const RShape &s)
Definition: RShape.h:104
#define RNANDOUBLE
Definition: RMath.h:71
Start or end.
Definition: RS.h:203
virtual bool trimEndPoint(const RVector &trimPoint, const RVector &clickPoint=RVector::invalid, bool extend=false)
Definition: RShape.h:285
Base class for all ellipse drawing tools.
Definition: Ellipse.js:17
static double twopi
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RShape.h:453
void isFullEllipseShape(void obj)
Checks if the given object is a full ellipse shape.
Definition: library.js:778
virtual double getDirection1() const
Definition: RShape.h:234
Side
Side used for side of a point relativ to an entity (right hand or left hand side) ...
Definition: RS.h:142
virtual RVector getPointWithDistanceToEnd(double distance)
Definition: RShape.h:197
virtual RShape::Type getShapeType() const
Definition: RShape.h:75
void trim(void trimEntity, void trimClickPos, void limitingEntity, void limitingClickPos, void trimBoth)
Trims the given entity / entities or shape(s).
Definition: simple_modify.js:57
virtual double getDirection2() const
Definition: RShape.h:237
virtual double getAngleAt(double distance, RS::From from=RS::FromStart) const
Definition: RShape.h:208
static const double PointTolerance
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RS.h:576
Q_DECLARE_METATYPE(RMath *)
virtual RVector getMiddlePoint() const
Definition: RShape.h:252
Base class for all line drawing tools.
Definition: Line.js:17
Low-level mathematical representation of an ellipse or ellipse arc.
Definition: REllipse.h:42
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
virtual QList< double > getDistancesFromStart(const RVector &p) const
Definition: RShape.h:322
Type
Definition: RShape.h:54
void trimEndPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:819
#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:71
Base class for all spline drawing tools.
Definition: Spline.js:17
static bool isArcShape(const RShape &s)
Definition: RShape.h:85
static bool isEllipseShape(const RShape &s)
Definition: RShape.h:91
#define RDEFAULT_TOLERANCE_1E_MIN4
Definition: RShape.h:42
void getIntersectionPoints(void e1, void e2, void limited)
Returns intersection points between the two given entities or shapes.
Definition: simple_info.js:12
Definition: RS.h:201
Definition: RShape.h:61
Low-level mathematical representation of a line.
Definition: RLine.h:41
friend QDebug operator<<(QDebug dbg, const RShape &s)
Definition: RShape.h:443
static int getErrorCode()
Definition: RShape.h:434
void trimStartPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:147
virtual RS::Side getSideOfPoint(const RVector &point) const
Definition: RShape.h:241
virtual RS::Ending getTrimEnd(const RVector &trimPoint, const RVector &clickPoint)
Definition: RShape.h:302
static bool isPolylineShape(const RShape &s)
Definition: RShape.h:95
Interface for explodable shape classes.
Definition: RExplodable.h:40
virtual RVector getPointWithDistanceToStart(double distance)
Definition: RShape.h:189
Definition: RShape.h:63
static QList< RVector > getIntersectionPointsLL(const RLine &line1, const RLine &line2, bool limited=true)
Definition: RShape.h:347
static bool isLineShape(const RShape &s)
Definition: RShape.h:82
Low-level mathematical representation of a circle.
Definition: RCircle.h:39
Neither.
Definition: RS.h:155
From
End used to specify from which end of a shape to measure a distance.
Definition: RS.h:200
static QList< RVector > getIntersectionPointsLT(const RLine &line1, const RTriangle &triangle2, bool limited=true)
Definition: RShape.h:367
static bool isRayShape(const RShape &s)
Definition: RShape.h:107
RShape()
Definition: RShape.h:69
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
Base class for all polyline drawing tools.
Definition: Polyline.js:18
static bool isSplineShape(const RShape &s)
Definition: RShape.h:98
Interface for geometrical shape classes.
Definition: RShape.h:52
virtual void print(QDebug dbg) const
Definition: RShape.cpp:1719
Definition: RS.h:143
static const RVector invalid
invalid vector
Definition: RVector.h:317
virtual RVector getStartPoint() const
Definition: RShape.h:246
static bool isTriangleShape(const RShape &s)
Definition: RShape.h:101
Base class for all point drawing tools.
Definition: Point.js:17
static bool isCircleShape(const RShape &s)
Definition: RShape.h:88
#define RDEFAULT_RVECTOR
Definition: RVector.h:37
virtual QList< RVector > getSelfIntersectionPoints() const
Definition: RShape.h:226
virtual bool isDirected() const
Definition: RShape.h:230
virtual double getDistanceFromStart(const RVector &p) const
Definition: RShape.h:313