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 
73  inline void set(double vx, double vy, double vz = 0.0) {
74  x = vx;
75  y = vy;
76  z = vz;
77  valid = true;
78  }
79  void setPolar(double radius, double angle);
80 
81  RVector get2D() const {
82  return RVector(x,y);
83  }
84 
85  bool isValid() const;
86  bool isSane() const;
87 
88  bool isInside(const RBox& b) const;
89 
90  bool equalsFuzzy(const RVector& v, double tol = RS::PointTolerance) const;
91  double getDistanceTo(const RVector& v) const;
92  double getDistanceTo2d(const RVector& v) const;
93  void setAngle(double a);
94  double getAngle() const;
95  double getAngleToPlaneXY() const;
96  double getAngleTo(const RVector& v) const;
97  void setMagnitude2d(double m);
98  double getMagnitude() const;
99  double getSquaredMagnitude() const;
100  double getMagnitude2d() const;
101  RVector getLerp(const RVector& v, double t) const;
102  RVector getUnitVector() const;
103  void setX(double x);
104  double getX();
105  void setY(double y);
106  double getY();
107  void setZ(double z);
108  double getZ();
109 
110  bool isInWindow(const RVector& firstCorner, const RVector& secondCorner);
111 
112  RVector move(const RVector& offset);
113 
117  static void moveList(QList<RVector>& list, const RVector& offset);
118 
119  RVector rotate(double rotation);
120  RVector rotate(double rotation, const RVector& center);
121  RVector rotate3d(const RLine& axis, double rotation);
122  RVector rotate3d(const QQuaternion& quaternion);
123 
127  static void rotateList(QList<RVector>& list, double rotation);
128 
132  static void rotateList(QList<RVector>& list, double rotation, const RVector& center);
133 
134  RVector scale(double factor, const RVector& center=nullVector);
135  RVector scale(const RVector& factors, const RVector& center=nullVector);
136 
140  static void scaleList(QList<RVector>& list, double factor, const RVector& center=nullVector);
141 
145  static void scaleList(QList<RVector>& list, const RVector& factors, const RVector& center=nullVector);
146 
147  RVector mirror(const RLine& axis);
148  RVector mirror(const RVector& axis1, const RVector& axis2);
149  RVector flipHorizontal();
150  RVector flipVertical();
151  RVector stretch(const RPolyline& area, const RVector& offset);
152  RVector isoProject(RS::IsoProjectionType type, bool trueScale);
153  RVector transform(const RMatrix& m);
154  RVector transform2dM(const RMatrix& m);
155  RVector transform2d(const QTransform& t);
156 
157  RVector getTransformed(const RMatrix& m) const;
158  RVector getTransformed2dM(const RMatrix& m) const;
159  RVector getTransformed2d(const QTransform& t) const;
160 
161  RVector getDividedComponents(const RVector& v) const;
162  RVector getMultipliedComponents(const RVector& v) const;
163 
164  RVector getClosest(const QList<RVector>& list) const;
165  RVector getClosest2d(const QList<RVector>& list) const;
166  double getClosestDistance(const QList<RVector>& list, int counts);
167  int getClosestIndex(const QList<RVector>& list, bool ignoreZ = false) const;
168  int getClosestIndex2d(const QList<RVector>& list) const {
169  return getClosestIndex(list, true);
170  }
171 
172  RVector operator +(const RVector& v) const;
173  RVector operator -(const RVector& v) const;
174  RVector operator *(double s) const;
175  RVector operator /(double s) const;
176  RVector operator -() const;
177  RVector getNegated() const;
178 
179  double dot(const RVector& other) {
180  return RVector::getDotProduct(*this, other);
181  }
182 
183  RVector normalize();
184  RVector getNormalized() const;
185 
186  void operator +=(const RVector& v);
187  void operator -=(const RVector& v);
188  void operator *=(double s);
189  void operator /=(double s);
190 
191  bool operator ==(const RVector& v) const;
192  bool operator !=(const RVector& v) const {
193  return !operator==(v);
194  }
195 
196  RVector getFloor() const;
197  RVector getCeil() const;
198 
199  bool lteXY(const RVector& v) const {
200  return RVector::lessThanEqualXY(*this, v);
201  }
202  bool gteXY(const RVector& v) const {
203  return RVector::greaterThanEqualXY(*this, v);
204  }
205 
206  static bool containsFuzzy(const QList<RVector>& vectors, const RVector& v, double tol = RS::PointTolerance);
207 
208  static RVector getMinimum(const QList<RVector>& vectors);
209  static RVector getMaximum(const QList<RVector>& vectors);
210 
211  static RVector getMinimumX(const QList<RVector>& vectors);
212  static RVector getMaximumX(const QList<RVector>& vectors);
213  static RVector getMinimumY(const QList<RVector>& vectors);
214  static RVector getMaximumY(const QList<RVector>& vectors);
215 
216  static RVector getMinimum(const RVector& v1, const RVector& v2);
217  static RVector getMaximum(const RVector& v1, const RVector& v2);
218 
219  static RVector getAverage(const RVector& v1, const RVector& v2);
220  static RVector getAverage(const QList<RVector>& vectors);
221 
222  static QList<double> getXList(const QList<RVector>& vectors);
223  static QList<double> getYList(const QList<RVector>& vectors);
224  static QList<double> getZList(const QList<RVector>& vectors);
225 
226  static RVector getCrossProduct(const RVector& v1, const RVector& v2);
227  static double getDotProduct(const RVector& v1, const RVector& v2);
228  static RVector createPolar(double radius, double angle) {
229  RVector ret;
230  ret.setPolar(radius, angle);
231  return ret;
232  }
233 
234  static bool lessThanX(const RVector& v1, const RVector& v2) {
235  return v1.x < v2.x;
236  }
237 
238  static bool greaterThanX(const RVector& v1, const RVector& v2) {
239  return v1.x > v2.x;
240  }
241 
242  static bool lessThanY(const RVector& v1, const RVector& v2) {
243  return v1.y < v2.y;
244  }
245 
246  static bool greaterThanY(const RVector& v1, const RVector& v2) {
247  return v1.y > v2.y;
248  }
249 
250  static bool lessThanEqualXY(const RVector& v1, const RVector& v2) {
251  return v1.x <= v2.x && v2.y <= v2.y;
252  }
253 
254  static bool greaterThanEqualXY(const RVector& v1, const RVector& v2) {
255  return v1.x >= v2.x && v2.y >= v2.y;
256  }
257 
258 
259  static QList<RVector> getSortedByDistance(const QList<RVector>& list, const RVector& v);
261  public:
262  static bool lessThan(const RVector& v1, const RVector& v2);
263  static RVector v;
264  };
265 
266  static QList<RVector> getSortedLeftRightTopBottom(const QList<RVector>& list);
268  public:
269  static bool lessThan(const RVector& v1, const RVector& v2);
270  };
271 
272  static QList<RVector> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
274  public:
275  static bool lessThan(const RVector& v1, const RVector& v2);
276  static RVector center;
277  static double angle;
278  };
279 
280 public:
285  double x;
286 
291  double y;
292 
297  double z;
298 
302  bool valid;
303 
307  static const RVector invalid;
308 
312  static const RVector nullVector;
313 };
314 
315 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
316 
317 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
318 
322 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
323 
327 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
328 
329 
330 Q_DECLARE_METATYPE(QList<RVector>)
332 typedef QMultiMap<int, RVector> _RMapIntVector;
333 Q_DECLARE_METATYPE(_RMapIntVector*)
337 
338 #endif
static double getDotProduct(const RVector &v1, const RVector &v2)
Scalarproduct (dot product).
Definition: RVector.cpp:523
Represents a box e.g.
Definition: RBox.h:43
static bool lessThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:242
static bool lessThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:250
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
bool lteXY(const RVector &v) const
Definition: RVector.h:199
RVector()
Constructor for a valid vector (0/0/0).
Definition: RVector.h:51
bool gteXY(const RVector &v) const
Definition: RVector.h:202
void mirror(void e, void p1, void p2, void p3, void p4)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:41
#define QCADCORE_EXPORT
Definition: core_global.h:10
void rotate(void e, void angle, void p1, void p2)
Rotates the given entity or shape by the given angle around the given center.
Definition: simple_modify.js:54
static RVector createPolar(double radius, double angle)
Definition: RVector.h:228
double z
Getter function for this property: getZ.
Definition: RVector.h:297
void move(void e, void p1, void p2)
Moves the given entity or shape by the given offset.
Definition: simple_modify.js:14
double y
Getter function for this property: getY.
Definition: RVector.h:291
static bool lessThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:234
static bool greaterThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:238
QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector &v)
Stream operator for QDebug.
Definition: RVector.cpp:991
Definition: RVector.h:260
Definition: RVector.h:273
static const double PointTolerance
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RS.h:551
bool valid
Getter function for this property: isValid.
Definition: RVector.h:302
int getClosestIndex2d(const QList< RVector > &list) const
Definition: RVector.h:168
Q_DECLARE_METATYPE(RMath *)
QCADCORE_EXPORT QDataStream & operator>>(QDataStream &stream, RVector &vector)
Deserializes the given RColor object.
Definition: RVector.cpp:1011
IsoProjectionType
Projection type for isometric projections.
Definition: RS.h:208
double dot(const RVector &other)
Definition: RVector.h:179
static const RVector nullVector
null vector
Definition: RVector.h:312
void scale(void e, void factor, void p1, void p2)
Scales the given entity or shape by the given factor with the given focus point.
Definition: simple_modify.js:27
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:84
static bool greaterThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:254
RVector get2D() const
Definition: RVector.h:81
Low-level mathematical representation of a line.
Definition: RLine.h:42
double x
Getter function for this property: getX.
Definition: RVector.h:285
static bool greaterThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:246
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:50
static double angle
Definition: RVector.h:277
static RVector center
Definition: RVector.h:276
static RVector v
Definition: RVector.h:263
QMultiMap< int, RVector > _RMapIntVector
Definition: RVector.h:332
static const RVector invalid
invalid vector
Definition: RVector.h:307
void set(double vx, double vy, double vz=0.0)
Sets new values for the vector and makes the vector valid.
Definition: RVector.h:73
QCADCORE_EXPORT RVector operator*(double s, const RVector &v)
Multiplication operator.
Definition: RVector.cpp:984