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, bool trueScale);
158  RVector transform(const RMatrix& m);
159  RVector transform2DM(const RMatrix& m);
160  RVector transform2D(const QTransform& t);
161 
162  RVector getTransformed(const RMatrix& m) const;
163  RVector getTransformed2DM(const RMatrix& m) const;
164  RVector getTransformed2D(const QTransform& t) const;
165 
166  RVector getDividedComponents(const RVector& v) const;
167  RVector getMultipliedComponents(const RVector& v) const;
168 
169  RVector getClosest(const QList<RVector>& list) const;
170  RVector getClosest2D(const QList<RVector>& list) const;
171  double getClosestDistance(const QList<RVector>& list, int counts);
172  int getClosestIndex(const QList<RVector>& list, bool ignoreZ = false) const;
173  int getClosestIndex2D(const QList<RVector>& list) const {
174  return getClosestIndex(list, true);
175  }
176 
177  RVector operator +(const RVector& v) const;
178  RVector operator -(const RVector& v) const;
179  RVector operator *(double s) const;
180  RVector operator /(double s) const;
181  RVector operator -() const;
182  RVector getNegated() const;
183 
184  double dot(const RVector& other) {
185  return RVector::getDotProduct(*this, other);
186  }
187 
188  RVector normalize();
189  RVector getNormalized() const;
190 
191  void operator +=(const RVector& v);
192  void operator -=(const RVector& v);
193  void operator *=(double s);
194  void operator /=(double s);
195 
196  bool operator ==(const RVector& v) const;
197  bool operator !=(const RVector& v) const {
198  return !operator==(v);
199  }
200 
201  RVector getFloor() const;
202  RVector getCeil() const;
203 
204  bool lteXY(const RVector& v) const {
205  return RVector::lessThanEqualXY(*this, v);
206  }
207  bool gteXY(const RVector& v) const {
208  return RVector::greaterThanEqualXY(*this, v);
209  }
210 
211  static bool containsFuzzy(const QList<RVector>& vectors, const RVector& v, double tol = RS::PointTolerance);
212 
213  static RVector getMinimum(const QList<RVector>& vectors);
214  static RVector getMaximum(const QList<RVector>& vectors);
215 
216  static RVector getMinimumX(const QList<RVector>& vectors);
217  static RVector getMaximumX(const QList<RVector>& vectors);
218  static RVector getMinimumY(const QList<RVector>& vectors);
219  static RVector getMaximumY(const QList<RVector>& vectors);
220 
221  static RVector getMinimum(const RVector& v1, const RVector& v2);
222  static RVector getMaximum(const RVector& v1, const RVector& v2);
223 
224  static RVector getAverage(const RVector& v1, const RVector& v2);
225  static RVector getAverage(const QList<RVector>& vectors);
226 
227  static QList<RVector> getUnion(const QList<RVector>& vectorsA, const QList<RVector>& vectorsB, double tol = RS::PointTolerance);
228 
229  static QList<double> getXList(const QList<RVector>& vectors);
230  static QList<double> getYList(const QList<RVector>& vectors);
231  static QList<double> getZList(const QList<RVector>& vectors);
232 
233  static RVector getCrossProduct(const RVector& v1, const RVector& v2);
234  static double getDotProduct(const RVector& v1, const RVector& v2);
235  static RVector createPolar(double radius, double angle) {
236  RVector ret;
237  ret.setPolar(radius, angle);
238  return ret;
239  }
240 
241  static bool lessThanX(const RVector& v1, const RVector& v2) {
242  return v1.x < v2.x;
243  }
244 
245  static bool greaterThanX(const RVector& v1, const RVector& v2) {
246  return v1.x > v2.x;
247  }
248 
249  static bool lessThanY(const RVector& v1, const RVector& v2) {
250  return v1.y < v2.y;
251  }
252 
253  static bool greaterThanY(const RVector& v1, const RVector& v2) {
254  return v1.y > v2.y;
255  }
256 
257  static bool lessThanEqualXY(const RVector& v1, const RVector& v2) {
258  return v1.x <= v2.x && v2.y <= v2.y;
259  }
260 
261  static bool greaterThanEqualXY(const RVector& v1, const RVector& v2) {
262  return v1.x >= v2.x && v2.y >= v2.y;
263  }
264 
265 
266  static QList<RVector> getSortedByDistance(const QList<RVector>& list, const RVector& v);
268  public:
269  static bool lessThan(const RVector& v1, const RVector& v2);
270  static RVector v;
271  };
272 
273  static QList<RVector> getSortedLeftRightTopBottom(const QList<RVector>& list);
275  public:
276  static bool lessThan(const RVector& v1, const RVector& v2);
277  };
278 
279  static QList<RVector> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
281  public:
282  static bool lessThan(const RVector& v1, const RVector& v2);
283  static RVector center;
284  static double angle;
285  };
286 
287 public:
292  double x;
293 
298  double y;
299 
304  double z;
305 
309  bool valid;
310 
314  static const RVector invalid;
315 
319  static const RVector nullVector;
320 
324  static const RVector nanVector;
325 };
326 
327 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
328 
329 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
330 
334 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
335 
339 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
340 
341 
342 Q_DECLARE_METATYPE(QList<RVector>)
344 typedef QMultiMap<int, RVector> _RMapIntVector;
345 Q_DECLARE_METATYPE(_RMapIntVector*)
349 
350 #endif
static double getDotProduct(const RVector &v1, const RVector &v2)
Scalarproduct (dot product).
Definition: RVector.cpp:558
Represents a box e.g.
Definition: RBox.h:43
static bool lessThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:249
static bool lessThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:257
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
bool lteXY(const RVector &v) const
Definition: RVector.h:204
RVector()
Constructor for a valid vector (0/0/0).
Definition: RVector.h:51
bool gteXY(const RVector &v) const
Definition: RVector.h:207
void mirror(void e, void axis)
Mirrors the given entity or shape at the given axis.
Definition: simple_modify.js:67
#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:235
double z
Getter function for this property: getZ.
Definition: RVector.h:304
double y
Getter function for this property: getY.
Definition: RVector.h:298
static bool lessThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:241
static bool greaterThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:245
QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector &v)
Stream operator for QDebug.
Definition: RVector.cpp:1039
Definition: RVector.h:267
int getClosestIndex2D(const QList< RVector > &list) const
Definition: RVector.h:173
Definition: RVector.h:280
static const double PointTolerance
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RS.h:554
bool valid
Getter function for this property: isValid.
Definition: RVector.h:309
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:81
QCADCORE_EXPORT QDataStream & operator>>(QDataStream &stream, RVector &vector)
Deserializes the given RColor object.
Definition: RVector.cpp:1059
IsoProjectionType
Projection type for isometric projections.
Definition: RS.h:210
double dot(const RVector &other)
Definition: RVector.h:184
static const RVector nullVector
null vector
Definition: RVector.h:319
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:106
static bool greaterThanEqualXY(const RVector &v1, const RVector &v2)
Definition: RVector.h:261
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:292
static bool greaterThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:253
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
static double angle
Definition: RVector.h:284
static RVector center
Definition: RVector.h:283
static RVector v
Definition: RVector.h:270
QMultiMap< int, RVector > _RMapIntVector
Definition: RVector.h:344
static const RVector nanVector
NaN vector.
Definition: RVector.h:324
static const RVector invalid
invalid vector
Definition: RVector.h:314
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:1032