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 getType() const {
76  return Unknown;
77  }
78 
79  static bool isPointShape(const RShape& s) {
80  return s.getType()==RShape::Point;
81  }
82  static bool isLineShape(const RShape& s) {
83  return s.getType()==RShape::Line;
84  }
85  static bool isArcShape(const RShape& s) {
86  return s.getType()==RShape::Arc;
87  }
88  static bool isCircleShape(const RShape& s) {
89  return s.getType()==RShape::Circle;
90  }
91  static bool isEllipseShape(const RShape& s) {
92  return s.getType()==RShape::Ellipse;
93  }
94  static bool isFullEllipseShape(const RShape& s);
95  static bool isPolylineShape(const RShape& s) {
96  return s.getType()==RShape::Polyline;
97  }
98  static bool isSplineShape(const RShape& s) {
99  return s.getType()==RShape::Spline;
100  }
101  static bool isTriangleShape(const RShape& s) {
102  return s.getType()==RShape::Triangle;
103  }
104  static bool isXLineShape(const RShape& s) {
105  return s.getType()==RShape::XLine;
106  }
107  static bool isRayShape(const RShape& s) {
108  return s.getType()==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 
214  virtual RVector getPointAtPercent(double p) const;
215 
216  virtual bool intersectsWith(const RShape& other,
217  bool limited = true) const;
218 
219  QList<RVector> getIntersectionPoints(const RShape& other,
220  bool limited = true, bool same = false, bool force = false) const;
221 
222  virtual QList<RVector> getSelfIntersectionPoints() const {
223  return QList<RVector>();
224  }
225 
226  virtual bool isDirected() const {
227  return false;
228  }
229 
230  virtual double getDirection1() const {
231  return RNANDOUBLE;
232  }
233  virtual double getDirection2() const {
234  return RNANDOUBLE;
235  }
236 
237  virtual RS::Side getSideOfPoint(const RVector& point) const {
238  Q_UNUSED(point)
239  return RS::NoSide;
240  }
241 
242  virtual RVector getStartPoint() const {
243  return RVector::invalid;
244  }
245  virtual RVector getEndPoint() const {
246  return RVector::invalid;
247  }
248  virtual RVector getMiddlePoint() const {
249  return RVector::invalid;
250  }
251 
252  virtual bool reverse() {
253  return false;
254  }
255 
262  virtual bool trimStartPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false) {
263  Q_UNUSED(trimPoint)
264  Q_UNUSED(clickPoint)
265  Q_UNUSED(extend)
266 
267  return false;
268  }
269 
270  virtual bool trimStartPoint(double trimDist) {
271  RVector p = getPointWithDistanceToStart(trimDist);
272  return trimStartPoint(p);
273  }
274 
281  virtual bool trimEndPoint(const RVector& trimPoint, const RVector& clickPoint = RVector::invalid, bool extend = false) {
282  Q_UNUSED(trimPoint)
283  Q_UNUSED(clickPoint)
284  Q_UNUSED(extend)
285 
286  return false;
287  }
288 
289  virtual bool trimEndPoint(double trimDist) {
290  RVector p = getPointWithDistanceToStart(trimDist);
291  return trimEndPoint(p);
292  }
293 
298  virtual RS::Ending getTrimEnd(const RVector& trimPoint, const RVector& clickPoint) {
299  Q_UNUSED(trimPoint)
300  Q_UNUSED(clickPoint)
301 
302  return RS::EndingNone;
303  }
304 
309  virtual double getDistanceFromStart(const RVector& p) const {
310  Q_UNUSED(p)
311  return RMAXDOUBLE;
312  }
313 
318  virtual QList<double> getDistancesFromStart(const RVector& p) const {
319  return QList<double>() << getDistanceFromStart(p);
320  }
321 
326  static QList<RVector> getIntersectionPoints(const RShape& shape1,
327  const RShape& shape2, bool limited = true, bool same = false, bool force = false);
328 
329  virtual bool move(const RVector& offset)=0;
330  virtual bool rotate(double rotation, const RVector& center = RDEFAULT_RVECTOR)=0;
331  virtual bool scale(double scaleFactor, const RVector& center = RVector());
332  virtual bool scale(const RVector& scaleFactors, const RVector& center = RVector())=0;
333  virtual bool mirror(const RLine& axis)=0;
334  virtual bool flipHorizontal();
335  virtual bool flipVertical();
336  virtual bool stretch(const RBox& area, const RVector& offset);
337  virtual bool stretch(const RPolyline& area, const RVector& offset);
338 
339  virtual QSharedPointer<RShape> getTransformed(const QTransform& transform) const = 0;
340 
341  static bool order(QList<QList<QSharedPointer<RShape> > >& boundary);
342 
343  static QList<RVector> getIntersectionPointsLL(const RLine& line1,
344  const RLine& line2, bool limited = true) {
345  return getIntersectionPointsLL(line1, line2, limited, limited);
346  }
347  static QList<RVector> getIntersectionPointsLL(const RLine& line1,
348  const RLine& line2, bool limited1, bool limited2);
349  static QList<RVector> getIntersectionPointsLA(const RLine& line1,
350  const RArc& arc2, bool limited = true) {
351  return getIntersectionPointsLA(line1, arc2, limited, limited);
352  }
353  static QList<RVector> getIntersectionPointsLA(const RLine& line1,
354  const RArc& arc2, bool limited1, bool limited2);
355  static QList<RVector> getIntersectionPointsLC(const RLine& line1,
356  const RCircle& circle2, bool limited = true);
357  static QList<RVector> getIntersectionPointsLE(const RLine& line1,
358  const REllipse& ellipse2, bool limited = true) {
359  return getIntersectionPointsLE(line1, ellipse2, limited, limited);
360  }
361  static QList<RVector> getIntersectionPointsLE(const RLine& line1,
362  const REllipse& ellipse2, bool limited1, bool limited2);
363  static QList<RVector> getIntersectionPointsLT(const RLine& line1,
364  const RTriangle& triangle2, bool limited = true) {
365  return getIntersectionPointsLT(line1, triangle2, limited, limited);
366  }
367  static QList<RVector> getIntersectionPointsLT(const RLine& line1,
368  const RTriangle& triangle2, bool limited1, bool limited2);
369  static QList<RVector> getIntersectionPointsLS(const RLine& line1,
370  const RSpline& spline2, bool limited = true);
371  static QList<RVector> getIntersectionPointsLX(const RLine& line1,
372  const RExplodable& explodable2, bool limited = true);
373 
374  static QList<RVector> getIntersectionPointsAA(const RArc& arc1,
375  const RArc& arc2, bool limited = true);
376  static QList<RVector> getIntersectionPointsAC(const RArc& arc1,
377  const RCircle& circle2, bool limited = true);
378  static QList<RVector> getIntersectionPointsAE(const RArc& arc1,
379  const REllipse& ellipse2, bool limited = true);
380  static QList<RVector> getIntersectionPointsAT(const RArc& arc1,
381  const RTriangle& triangle2, bool limited = true);
382  static QList<RVector> getIntersectionPointsAS(const RArc& arc1,
383  const RSpline& spline2, bool limited = true);
384  static QList<RVector> getIntersectionPointsAX(const RArc& arc1,
385  const RExplodable& explodable2, bool limited = true);
386 
387  static QList<RVector> getIntersectionPointsCC(const RCircle& circle1,
388  const RCircle& circle2);
389  static QList<RVector> getIntersectionPointsCE(const RCircle& circle1,
390  const REllipse& ellipse2);
391  static QList<RVector> getIntersectionPointsCS(const RCircle& circle1,
392  const RSpline& spline2, bool limited = true);
393  static QList<RVector> getIntersectionPointsCX(const RCircle& circle1,
394  const RExplodable& explodable2, bool limited = true);
395 
396  static QList<RVector> getIntersectionPointsEE(const REllipse& ellipse1,
397  const REllipse& ellipse2);
398  static QList<RVector> getIntersectionPointsEE(const REllipse& ellipse1,
399  const REllipse& ellipse2, bool limited);
400  static QList<RVector> getIntersectionPointsES(const REllipse& ellipse1,
401  const RSpline& spline2, bool limited = true);
402  static QList<RVector> getIntersectionPointsEX(const REllipse& ellipse1,
403  const RExplodable& explodable2, bool limited = true);
404 
405  static QList<RVector> getIntersectionPointsSX(const RSpline& spline1,
406  const RExplodable& explodable2, bool limited);
407 
408  static QList<RVector> getIntersectionPointsSS(const RSpline& spline1,
409  const RSpline& spline2, bool limited = true, bool same = false);
410 
411  static QList<RVector> getIntersectionPointsXX(const RExplodable& explodable1,
412  const RExplodable& explodable2, bool limited = true, bool same = false);
413 
414  static const RExplodable* castToExplodable(const RShape* shape);
415 
416  virtual QList<QSharedPointer<RShape> > getOffsetShapes(double distance, int number, RS::Side side, const RVector& position = RVector::invalid);
417 
418  static QList<QSharedPointer<RShape> > getOffsetLines(const RShape& shape, double distance, int number, RS::Side side, const RVector& position = RVector::invalid);
419  static QList<QSharedPointer<RShape> > getOffsetArcs(const RShape& shape, double distance, int number, RS::Side side, const RVector& position = RVector::invalid);
420 
421  static QList<QSharedPointer<RShape> > getReversedShapeList(const QList<QSharedPointer<RShape> >& shapes);
422 
423  virtual QList<QSharedPointer<RShape> > splitAt(const QList<RVector>& points) const;
424 
425  static QList<QSharedPointer<RShape> > trim(
426  const RShape& trimShape, const RVector& trimClickPos,
427  const RShape& limitingShape, const RVector& limitingClickPos,
428  bool trimBoth, bool samePolyline);
429 
430  static int getErrorCode() {
431  return errorCode;
432  }
433 
434  void dump();
435 
439  friend QDebug operator<<(QDebug dbg, const RShape& s) {
440  s.print(dbg);
441  return dbg;
442  }
443 
444 private:
445  static double ellipse2tr(double x, double y, double AA, double BB,
446  double CC, double DD, double EE, double FF);
447 
448 private:
449  static double twopi;
450  static double epsTolerance;
451 
452 protected:
453  virtual void print(QDebug dbg) const;
454  static int errorCode;
455 };
456 
460 Q_DECLARE_METATYPE(QSharedPointer<RShape>)
461 Q_DECLARE_METATYPE(QSharedPointer<const RShape>)
462 Q_DECLARE_METATYPE(QSharedPointer<RShape>*)
463 Q_DECLARE_METATYPE(QList<QSharedPointer<RShape> >)
464 Q_DECLARE_METATYPE(QList<QSharedPointer<RShape> >*)
465 Q_DECLARE_METATYPE(QList<QSharedPointer<const RShape> >)
466 
467 #endif
virtual bool trimStartPoint(double trimDist)
Definition: RShape.h:270
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
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:262
virtual QList< int > getIntProperties() const
Definition: RShape.h:142
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RSpline.h:55
static int errorCode
Definition: RShape.h:454
virtual RVector getEndPoint() const
Definition: RShape.h:245
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:357
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:349
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:289
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:67
virtual bool reverse()
Definition: RShape.h:252
#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 ~RShape()
Definition: RShape.h:72
Definition: RShape.h:64
static double epsTolerance
Definition: RShape.h:450
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:281
Base class for all ellipse drawing tools.
Definition: Ellipse.js:17
static double twopi
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RShape.h:449
void isFullEllipseShape(void obj)
Checks if the given object is a full ellipse shape.
Definition: library.js:888
virtual double getDirection1() const
Definition: RShape.h:230
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
void trim(void trimEntity, void trimClickPos, void limitingEntity, void limitingClickPos, void trimBoth)
Trims the given entity / entities or shape(s).
Definition: simple_modify.js:40
virtual double getDirection2() const
Definition: RShape.h:233
virtual double getAngleAt(double distance, RS::From from=RS::FromStart) const
Definition: RShape.h:208
static const double PointTolerance
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RS.h:554
Q_DECLARE_METATYPE(RMath *)
virtual RVector getMiddlePoint() const
Definition: RShape.h:248
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:81
virtual QList< double > getDistancesFromStart(const RVector &p) const
Definition: RShape.h:318
Type
Definition: RShape.h:54
void trimEndPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:712
#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
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)
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:439
static int getErrorCode()
Definition: RShape.h:430
void trimStartPoint(void shape, void trimPoint, void clickPoint)
Definition: library.js:770
virtual RS::Side getSideOfPoint(const RVector &point) const
Definition: RShape.h:237
virtual RS::Ending getTrimEnd(const RVector &trimPoint, const RVector &clickPoint)
Definition: RShape.h:298
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:343
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:363
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
virtual RShape::Type getType() const
Definition: RShape.h:75
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:314
virtual RVector getStartPoint() const
Definition: RShape.h:242
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:222
virtual bool isDirected() const
Definition: RShape.h:226
virtual double getDistanceFromStart(const RVector &p) const
Definition: RShape.h:309