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 rotate3D(const RLine& axis, double rotation);
127  RVector rotate3D(const QQuaternion& quaternion);
128 
132  static void rotateList(QList<RVector>& list, double rotation);
133 
137  static void rotateList(QList<RVector>& list, double rotation, const RVector& center);
138 
139  RVector scale(double factor, const RVector& center=nullVector);
140  RVector scale(const RVector& factors, const RVector& center=nullVector);
141 
145  static void scaleList(QList<RVector>& list, double factor, const RVector& center=nullVector);
146 
150  static void scaleList(QList<RVector>& list, const RVector& factors, const RVector& center=nullVector);
151 
152  RVector mirror(const RLine& axis);
153  RVector mirror(const RVector& axis1, const RVector& axis2);
154  RVector flipHorizontal();
155  RVector flipVertical();
156  RVector stretch(const RPolyline& area, const RVector& offset);
157  RVector isoProject(RS::IsoProjectionType type);
158  RVector axonometricProjection(RS::IsoProjectionType type);
159  RVector obliqueProjection(RS::IsoProjectionType type);
160  RVector transform(const RMatrix& m);
161  RVector transform2DM(const RMatrix& m);
162  RVector transform2D(const QTransform& t);
163 
164  RVector getTransformed(const RMatrix& m) const;
165  RVector getTransformed2DM(const RMatrix& m) const;
166  RVector getTransformed2D(const QTransform& t) const;
167 
168  RVector getDividedComponents(const RVector& v) const;
169  RVector getMultipliedComponents(const RVector& v) const;
170 
171  RVector getClosest(const QList<RVector>& list) const;
172  RVector getClosest2D(const QList<RVector>& list) const;
173  double getClosestDistance(const QList<RVector>& list, int counts);
174  int getClosestIndex(const QList<RVector>& list, bool ignoreZ = false) const;
175  int getClosestIndex2D(const QList<RVector>& list) const {
176  return getClosestIndex(list, true);
177  }
178 
179  RVector operator +(const RVector& v) const;
180  RVector operator -(const RVector& v) const;
181  RVector operator *(double s) const;
182  RVector operator /(double s) const;
183  RVector operator -() const;
184  RVector getNegated() const;
185 
186  double dot(const RVector& other) {
187  return RVector::getDotProduct(*this, other);
188  }
189 
190  RVector normalize();
191  RVector getNormalized() const;
192 
193  void operator +=(const RVector& v);
194  void operator -=(const RVector& v);
195  void operator *=(double s);
196  void operator /=(double s);
197 
198  bool operator ==(const RVector& v) const;
199  bool operator !=(const RVector& v) const {
200  return !operator==(v);
201  }
202 
203  RVector getFloor() const;
204  RVector getCeil() const;
205 
206  bool lteXY(const RVector& v) const {
207  return RVector::lessThanEqualXY(*this, v);
208  }
209  bool gteXY(const RVector& v) const {
210  return RVector::greaterThanEqualXY(*this, v);
211  }
212 
213  static bool containsFuzzy(const QList<RVector>& vectors, const RVector& v, double tol = RS::PointTolerance);
214 
215  static RVector getMinimum(const QList<RVector>& vectors);
216  static RVector getMaximum(const QList<RVector>& vectors);
217 
218  static RVector getMinimumX(const QList<RVector>& vectors);
219  static RVector getMaximumX(const QList<RVector>& vectors);
220  static RVector getMinimumY(const QList<RVector>& vectors);
221  static RVector getMaximumY(const QList<RVector>& vectors);
222 
223  static RVector getMinimum(const RVector& v1, const RVector& v2);
224  static RVector getMaximum(const RVector& v1, const RVector& v2);
225 
226  static RVector getAverage(const RVector& v1, const RVector& v2);
227  static RVector getAverage(const QList<RVector>& vectors);
228 
229  static QList<RVector> getUnion(const QList<RVector>& vectorsA, const QList<RVector>& vectorsB, double tol = RS::PointTolerance);
230  static QList<RVector> getUnique(const QList<RVector>& vectors, double tol = RS::PointTolerance);
231 
232  static QList<double> getXList(const QList<RVector>& vectors);
233  static QList<double> getYList(const QList<RVector>& vectors);
234  static QList<double> getZList(const QList<RVector>& vectors);
235 
236  static RVector getCrossProduct(const RVector& v1, const RVector& v2);
237  static double getDotProduct(const RVector& v1, const RVector& v2);
238  static RVector createPolar(double radius, double angle) {
239  RVector ret;
240  ret.setPolar(radius, angle);
241  return ret;
242  }
243 
244  static bool lessThanX(const RVector& v1, const RVector& v2) {
245  return v1.x < v2.x;
246  }
247 
248  static bool greaterThanX(const RVector& v1, const RVector& v2) {
249  return v1.x > v2.x;
250  }
251 
252  static bool lessThanY(const RVector& v1, const RVector& v2) {
253  return v1.y < v2.y;
254  }
255 
256  static bool greaterThanY(const RVector& v1, const RVector& v2) {
257  return v1.y > v2.y;
258  }
259 
260  static bool lessThanEqualXY(const RVector& v1, const RVector& v2) {
261  return v1.x <= v2.x && v2.y <= v2.y;
262  }
263 
264  static bool greaterThanEqualXY(const RVector& v1, const RVector& v2) {
265  return v1.x >= v2.x && v2.y >= v2.y;
266  }
267 
268 
269  static QList<RVector> getSortedByDistance(const QList<RVector>& list, const RVector& v);
271  public:
272  static bool lessThan(const RVector& v1, const RVector& v2);
273  static RVector v;
274  };
275 
276  static QList<RVector> getSortedLeftRightTopBottom(const QList<RVector>& list);
278  public:
279  static bool lessThan(const RVector& v1, const RVector& v2);
280  };
281 
282  static QList<RVector> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
284  public:
285  static bool lessThan(const RVector& v1, const RVector& v2);
286  static RVector center;
287  static double angle;
288  };
289 
290 public:
295  double x;
296 
301  double y;
302 
307  double z;
308 
312  bool valid;
313 
317  static const RVector invalid;
318 
322  static const RVector nullVector;
323 
327  static const RVector nanVector;
328 };
329 
330 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
331 
332 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
333 
337 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
338 
342 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
343 
344 
345 Q_DECLARE_METATYPE(QList<RVector>)
347 typedef QMultiMap<int, RVector> _RMapIntVector;
348 Q_DECLARE_METATYPE(_RMapIntVector*)
352 
353 #endif
static double getDotProduct(const RVector &v1, const RVector &v2)
Scalarproduct (dot product).
Definition: RVector.cpp:704
Represents a box e.g.
Definition: RBox.h:43
static bool lessThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:252
static bool lessThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:260
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
bool lteXY(const RVector &v) const
Definition: RVector.h:206
RVector()
Constructor for a valid null vector (0/0/0).
Definition: RVector.h:51
bool gteXY(const RVector &v) const
Definition: RVector.h:209
void mirror(void e, void axis)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:18
#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
static RVector createPolar(double radius, double angle)
Definition: RVector.h:238
double z
Getter function for this property: getZ.
Definition: RVector.h:307
double y
Getter function for this property: getY.
Definition: RVector.h:301
static bool lessThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:244
static bool greaterThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:248
QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector &v)
Stream operator for QDebug.
Definition: RVector.cpp:1195
Definition: RVector.h:270
int getClosestIndex2D(const QList< RVector > &list) const
Definition: RVector.h:175
Definition: RVector.h:283
static const double PointTolerance
Copyright (c) 2011-2017 by Andrew Mustun.
Definition: RS.h:576
bool valid
Getter function for this property: isValid.
Definition: RVector.h:312
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:109
QCADCORE_EXPORT QDataStream & operator>>(QDataStream &stream, RVector &vector)
Deserializes the given RColor object.
Definition: RVector.cpp:1215
IsoProjectionType
Projection type for isometric projections.
Definition: RS.h:210
double dot(const RVector &other)
Definition: RVector.h:186
static const RVector nullVector
null vector
Definition: RVector.h:322
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
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:264
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:295
static bool greaterThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:256
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
static double angle
Definition: RVector.h:287
static RVector center
Definition: RVector.h:286
static RVector v
Definition: RVector.h:273
QMultiMap< int, RVector > _RMapIntVector
Definition: RVector.h:347
static const RVector nanVector
NaN vector.
Definition: RVector.h:327
static const RVector invalid
invalid vector
Definition: RVector.h:317
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:1188