QCAD
Open Source 2D CAD
RVector.h
Go to the documentation of this file.
1 
20 #ifndef RVECTOR_H
21 #define RVECTOR_H
22 
23 #include "../core_global.h"
24 
25 #include <QDebug>
26 #include <QList>
27 #include <QMetaType>
28 #include <QTransform>
29 
30 #include "RGlobal.h"
31 
32 class RMatrix;
33 class RBox;
34 class RLine;
35 class RPolyline;
36 
37 #define RDEFAULT_RVECTOR RVector()
38 
47 public:
51  inline RVector() :
52  x(0.0), y(0.0), z(0.0), valid(true) {
53  }
54 
61  // inline explicit RVector(bool v) :
62  // x(0.0), y(0.0), z(0.0), valid(v) {
63  // }
64 
68  RVector(double vx, double vy, double vz = 0.0, bool valid_in = true);
69 
70  RVector(const QList<double>& tuples);
71 
75  inline void set(double vx, double vy, double vz = 0.0) {
76  x = vx;
77  y = vy;
78  z = vz;
79  valid = true;
80  }
81  void setPolar(double radius, double angle);
82 
83  RVector get2D() const {
84  return RVector(x,y);
85  }
86 
87  bool isValid() const;
88  bool isZero() const;
89  bool isSane() const;
90  bool isNaN() const;
91 
92  bool isInside(const RBox& b) const;
93 
94  bool equalsFuzzy(const RVector& v, double tol = RS::PointTolerance) const;
95  bool equalsFuzzy2D(const RVector& v, double tol = RS::PointTolerance) const;
96  double getDistanceTo(const RVector& v) const;
97  double getDistanceTo2D(const RVector& v) const;
98  void setAngle(double a);
99  double getAngle() const;
100  double getAngleToPlaneXY() const;
101  double getAngleTo(const RVector& v) const;
102  void setMagnitude2D(double m);
103  double getMagnitude() const;
104  double getSquaredMagnitude() const;
105  double getMagnitude2D() const;
106  RVector getLerp(const RVector& v, double t) const;
107  RVector getUnitVector() const;
108  void setX(double x);
109  double getX();
110  void setY(double y);
111  double getY();
112  void setZ(double z);
113  double getZ();
114 
115  bool isInWindow(const RVector& firstCorner, const RVector& secondCorner);
116 
117  RVector move(const RVector& offset);
118 
122  static void moveList(QList<RVector>& list, const RVector& offset);
123 
124  RVector rotate(double rotation);
125  RVector rotate(double rotation, const RVector& center);
126  RVector getRotated(double rotation, const RVector& center) const;
127  RVector rotate3D(const RLine& axis, double rotation);
128  RVector rotate3D(const QQuaternion& quaternion);
129 
133  static void rotateList(QList<RVector>& list, double rotation);
134 
138  static void rotateList(QList<RVector>& list, double rotation, const RVector& center);
139 
140  RVector scale(double factor, const RVector& center=nullVector);
141  RVector scale(const RVector& factors, const RVector& center=nullVector);
142  RVector getScaled(const RVector& factors, const RVector& center) const;
143 
147  static void scaleList(QList<RVector>& list, double factor, const RVector& center=nullVector);
148 
152  static void scaleList(QList<RVector>& list, const RVector& factors, const RVector& center=nullVector);
153 
154  RVector mirror(const RLine& axis);
155  RVector getMirrored(const RLine& axis) const;
156  RVector mirror(const RVector& axis1, const RVector& axis2);
157  RVector flipHorizontal();
158  RVector flipVertical();
159  RVector stretch(const RPolyline& area, const RVector& offset);
160  RVector isoProject(RS::IsoProjectionType type);
161  RVector axonometricProjection(RS::IsoProjectionType type);
162  RVector obliqueProjection(RS::IsoProjectionType type);
163  RVector transform(const RMatrix& m);
164  RVector transform2DM(const RMatrix& m);
165  RVector transform2D(const QTransform& t);
166 
167  RVector getTransformed(const RMatrix& m) const;
168  RVector getTransformed2DM(const RMatrix& m) const;
169  RVector getTransformed2D(const QTransform& t) const;
170 
171  RVector getDividedComponents(const RVector& v) const;
172  RVector getMultipliedComponents(const RVector& v) const;
173 
174  RVector getClosest(const QList<RVector>& list) const;
175  RVector getClosest2D(const QList<RVector>& list) const;
176  double getClosestDistance(const QList<RVector>& list, int counts);
177  int getClosestIndex(const QList<RVector>& list, bool ignoreZ = false) const;
178  int getClosestIndex2D(const QList<RVector>& list) const {
179  return getClosestIndex(list, true);
180  }
181 
182  RVector operator +(const RVector& v) const;
183  RVector operator -(const RVector& v) const;
184  RVector operator *(double s) const;
185  RVector operator /(double s) const;
186  RVector operator -() const;
187  RVector getNegated() const;
188  RVector getAbsolute() const;
189 
190  double dot(const RVector& other) const {
191  return RVector::getDotProduct(*this, other);
192  }
193 
194  RVector normalize();
195  RVector getNormalized() const;
196 
197  void operator +=(const RVector& v);
198  void operator -=(const RVector& v);
199  void operator *=(double s);
200  void operator /=(double s);
201 
202  bool operator ==(const RVector& v) const;
203  bool operator !=(const RVector& v) const {
204  return !operator==(v);
205  }
206 
207  RVector getFloor() const;
208  RVector getCeil() const;
209 
210  bool lteXY(const RVector& v) const {
211  return RVector::lessThanEqualXY(*this, v);
212  }
213  bool gteXY(const RVector& v) const {
214  return RVector::greaterThanEqualXY(*this, v);
215  }
216 
217  static bool containsFuzzy(const QList<RVector>& vectors, const RVector& v, double tol = RS::PointTolerance);
218  static int findFirstFuzzy(const QList<RVector>& vectors, const RVector& v, double tol = RS::PointTolerance);
219 
220  static RVector getMinimum(const QList<RVector>& vectors);
221  static RVector getMaximum(const QList<RVector>& vectors);
222 
223  static RVector getMinimumX(const QList<RVector>& vectors);
224  static RVector getMaximumX(const QList<RVector>& vectors);
225  static RVector getMinimumY(const QList<RVector>& vectors);
226  static RVector getMaximumY(const QList<RVector>& vectors);
227 
228  static RVector getMinimum(const RVector& v1, const RVector& v2);
229  static RVector getMaximum(const RVector& v1, const RVector& v2);
230 
231  static RVector getAverage(const RVector& v1, const RVector& v2);
232  static RVector getAverage(const QList<RVector>& vectors);
233 
234  static QList<RVector> getUnion(const QList<RVector>& vectorsA, const QList<RVector>& vectorsB, double tol = RS::PointTolerance);
235  static QList<RVector> getUnique(const QList<RVector>& vectors, double tol = RS::PointTolerance);
236 
237  static QList<double> getXList(const QList<RVector>& vectors);
238  static QList<double> getYList(const QList<RVector>& vectors);
239  static QList<double> getZList(const QList<RVector>& vectors);
240 
241  static RVector getCrossProduct(const RVector& v1, const RVector& v2);
242  static double getDotProduct(const RVector& v1, const RVector& v2);
243  static RVector createPolar(double radius, double angle) {
244  RVector ret;
245  ret.setPolar(radius, angle);
246  return ret;
247  }
248 
249  static bool lessThanX(const RVector& v1, const RVector& v2) {
250  return v1.x < v2.x;
251  }
252 
253  static bool greaterThanX(const RVector& v1, const RVector& v2) {
254  return v1.x > v2.x;
255  }
256 
257  static bool lessThanY(const RVector& v1, const RVector& v2) {
258  return v1.y < v2.y;
259  }
260 
261  static bool greaterThanY(const RVector& v1, const RVector& v2) {
262  return v1.y > v2.y;
263  }
264 
265  static bool lessThanEqualXY(const RVector& v1, const RVector& v2) {
266  return v1.x <= v2.x && v2.y <= v2.y;
267  }
268 
269  static bool greaterThanEqualXY(const RVector& v1, const RVector& v2) {
270  return v1.x >= v2.x && v2.y >= v2.y;
271  }
272 
273 
274  static QList<RVector> getSortedByDistance(const QList<RVector>& list, const RVector& v);
276  public:
277  static bool lessThan(const RVector& v1, const RVector& v2);
278  static RVector v;
279  };
280 
281  static QList<RVector> getSortedLeftRightTopBottom(const QList<RVector>& list);
283  public:
284  static bool lessThan(const RVector& v1, const RVector& v2);
285  };
286 
287  static QList<RVector> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
289  public:
290  static bool lessThan(const RVector& v1, const RVector& v2);
291  static RVector center;
292  static double angle;
293  };
294 
295 public:
300  double x;
301 
306  double y;
307 
312  double z;
313 
317  bool valid;
318 
322  static const RVector invalid;
323 
327  static const RVector nullVector;
328 
332  static const RVector nanVector;
333 };
334 
335 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
336 
337 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
338 
342 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
343 
347 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
348 
349 
350 Q_DECLARE_METATYPE(QList<RVector>)
352 typedef QMultiMap<int, RVector> _RMapIntVector;
353 Q_DECLARE_METATYPE(_RMapIntVector*)
357 
358 #endif
static double getDotProduct(const RVector &v1, const RVector &v2)
Scalarproduct (dot product).
Definition: RVector.cpp:726
Represents a box e.g.
Definition: RBox.h:43
static bool lessThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:257
static bool lessThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:265
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
bool lteXY(const RVector &v) const
Definition: RVector.h:210
double dot(const RVector &other) const
Definition: RVector.h:190
RVector()
Constructor for a valid null vector (0/0/0).
Definition: RVector.h:51
bool gteXY(const RVector &v) const
Definition: RVector.h:213
void mirror(void e, void axis)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:81
#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
static RVector createPolar(double radius, double angle)
Definition: RVector.h:243
double z
Getter function for this property: getZ.
Definition: RVector.h:312
double y
Getter function for this property: getY.
Definition: RVector.h:306
static bool lessThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:249
static bool greaterThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:253
QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector &v)
Stream operator for QDebug.
Definition: RVector.cpp:1221
Definition: RVector.h:275
int getClosestIndex2D(const QList< RVector > &list) const
Definition: RVector.h:178
Definition: RVector.h:288
static const double PointTolerance
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RS.h:684
bool valid
Getter function for this property: isValid.
Definition: RVector.h:317
Q_DECLARE_METATYPE(RMath *)
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
QCADCORE_EXPORT QDataStream & operator>>(QDataStream &stream, RVector &vector)
Deserializes the given RColor object.
Definition: RVector.cpp:1241
IsoProjectionType
Projection type for isometric projections.
Definition: RS.h:262
static const RVector nullVector
null vector
Definition: RVector.h:327
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
Simple matrix class.
Definition: RMatrix.h:36
void setPolar(double radius, double angle)
Sets a new position for the vector in polar coordinates.
Definition: RVector.cpp:107
static bool greaterThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:269
RVector get2D() const
Definition: RVector.h:83
Low-level mathematical representation of a line.
Definition: RLine.h:41
double x
Getter function for this property: getX.
Definition: RVector.h:300
static bool greaterThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:261
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
static double angle
Definition: RVector.h:292
static RVector center
Definition: RVector.h:291
static RVector v
Definition: RVector.h:278
QMultiMap< int, RVector > _RMapIntVector
Definition: RVector.h:352
static const RVector nanVector
NaN vector.
Definition: RVector.h:332
static const RVector invalid
invalid vector
Definition: RVector.h:322
void set(double vx, double vy, double vz=0.0)
Sets new values for the vector and makes the vector valid.
Definition: RVector.h:75
QCADCORE_EXPORT RVector operator*(double s, const RVector &v)
Multiplication operator.
Definition: RVector.cpp:1214