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 
87  bool isInside(const RBox& b) const;
88 
89  bool equalsFuzzy(const RVector& v, double tol = RS::PointTolerance) const;
90  double getDistanceTo(const RVector& v) const;
91  double getDistanceTo2d(const RVector& v) const;
92  void setAngle(double a);
93  double getAngle() const;
94  double getAngleToPlaneXY() const;
95  double getAngleTo(const RVector& v) const;
96  void setMagnitude2d(double m);
97  double getMagnitude() const;
98  double getSquaredMagnitude() const;
99  double getMagnitude2d() const;
100  RVector getLerp(const RVector& v, double t) const;
101  RVector getUnitVector() const;
102  void setX(double x);
103  double getX();
104  void setY(double y);
105  double getY();
106  void setZ(double z);
107  double getZ();
108 
109  bool isInWindow(const RVector& firstCorner, const RVector& secondCorner);
110 
111  RVector move(const RVector& offset);
112 
116  static void moveList(QList<RVector>& list, const RVector& offset);
117 
118  RVector rotate(double rotation);
119  RVector rotate(double rotation, const RVector& center);
120  RVector rotate3d(const RLine& axis, double rotation);
121  RVector rotate3d(const QQuaternion& quaternion);
122 
126  static void rotateList(QList<RVector>& list, double rotation);
127 
131  static void rotateList(QList<RVector>& list, double rotation, const RVector& center);
132 
133  RVector scale(double factor, const RVector& center=nullVector);
134  RVector scale(const RVector& factors, const RVector& center=nullVector);
135 
139  static void scaleList(QList<RVector>& list, double factor, const RVector& center=nullVector);
140 
144  static void scaleList(QList<RVector>& list, const RVector& factors, const RVector& center=nullVector);
145 
146  RVector mirror(const RLine& axis);
147  RVector mirror(const RVector& axis1, const RVector& axis2);
148  RVector flipHorizontal();
149  RVector flipVertical();
150  RVector stretch(const RPolyline& area, const RVector& offset);
151  RVector isoProject(RS::IsoProjectionType type, bool trueScale);
152  RVector transform(const RMatrix& m);
153  RVector transform2dM(const RMatrix& m);
154  RVector transform2d(const QTransform& t);
155 
156  RVector getTransformed(const RMatrix& m) const;
157  RVector getTransformed2dM(const RMatrix& m) const;
158  RVector getTransformed2d(const QTransform& t) const;
159 
160  RVector getDividedComponents(const RVector& v) const;
161  RVector getMultipliedComponents(const RVector& v) const;
162 
163  RVector getClosest(const QList<RVector>& list) const;
164  double getClosestDistance(const QList<RVector>& list, int counts);
165  int getClosestIndex(const QList<RVector>& list) const;
166 
167  RVector operator +(const RVector& v) const;
168  RVector operator -(const RVector& v) const;
169  RVector operator *(double s) const;
170  RVector operator /(double s) const;
171  RVector operator -() const;
172  RVector getNegated() const;
173 
174  RVector normalize();
175  RVector getNormalized() const;
176 
177  void operator +=(const RVector& v);
178  void operator -=(const RVector& v);
179  void operator *=(double s);
180  void operator /=(double s);
181 
182  bool operator ==(const RVector& v) const;
183  bool operator !=(const RVector& v) const {
184  return !operator==(v);
185  }
186 
187  RVector getFloor() const;
188  RVector getCeil() const;
189 
190  static RVector getMinimum(const QList<RVector>& vectors);
191  static RVector getMaximum(const QList<RVector>& vectors);
192 
193  static RVector getMinimumX(const QList<RVector>& vectors);
194  static RVector getMaximumX(const QList<RVector>& vectors);
195  static RVector getMinimumY(const QList<RVector>& vectors);
196  static RVector getMaximumY(const QList<RVector>& vectors);
197 
198  static RVector getMinimum(const RVector& v1, const RVector& v2);
199  static RVector getMaximum(const RVector& v1, const RVector& v2);
200 
201  static RVector getAverage(const RVector& v1, const RVector& v2);
202 
203  static QList<double> getXList(const QList<RVector>& vectors);
204  static QList<double> getYList(const QList<RVector>& vectors);
205  static QList<double> getZList(const QList<RVector>& vectors);
206 
207  static RVector getCrossProduct(const RVector& v1, const RVector& v2);
208  static double getDotProduct(const RVector& v1, const RVector& v2);
209  static RVector createPolar(double radius, double angle) {
210  RVector ret;
211  ret.setPolar(radius, angle);
212  return ret;
213  }
214 
215  static bool lessThanX(const RVector& v1, const RVector& v2) {
216  return v1.x < v2.x;
217  }
218 
219  static bool greaterThanX(const RVector& v1, const RVector& v2) {
220  return v1.x > v2.x;
221  }
222 
223  static bool lessThanY(const RVector& v1, const RVector& v2) {
224  return v1.y < v2.y;
225  }
226 
227  static bool greaterThanY(const RVector& v1, const RVector& v2) {
228  return v1.y > v2.y;
229  }
230 
231  static QList<RVector> getSortedByDistance(const QList<RVector>& list, const RVector& v);
233  public:
234  static bool lessThan(const RVector& v1, const RVector& v2);
235  static RVector v;
236  };
237 
238  static QList<RVector> getSortedLeftRightTopBottom(const QList<RVector>& list);
240  public:
241  static bool lessThan(const RVector& v1, const RVector& v2);
242  };
243 
244  static QList<RVector> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
246  public:
247  static bool lessThan(const RVector& v1, const RVector& v2);
248  static RVector center;
249  static double angle;
250  };
251 
252 public:
257  double x;
258 
263  double y;
264 
269  double z;
270 
274  bool valid;
275 
279  static const RVector invalid;
280 
284  static const RVector nullVector;
285 };
286 
287 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
288 
289 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
290 
294 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
295 
299 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
300 
301 
302 Q_DECLARE_METATYPE(QList<RVector>)
304 typedef QMultiMap<int, RVector> _RMapIntVector;
305 Q_DECLARE_METATYPE(_RMapIntVector*)
309 
310 #endif
Represents a box e.g.
Definition: RBox.h:43
static bool lessThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:223
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
RVector()
Constructor for a valid vector (0/0/0).
Definition: RVector.h:51
#define QCADCORE_EXPORT
Definition: core_global.h:10
static RVector createPolar(double radius, double angle)
Definition: RVector.h:209
double z
Getter function for this property: getZ.
Definition: RVector.h:269
double y
Getter function for this property: getY.
Definition: RVector.h:263
static bool lessThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:215
static bool greaterThanX(const RVector &v1, const RVector &v2)
Definition: RVector.h:219
QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector &v)
Stream operator for QDebug.
Definition: RVector.cpp:955
Definition: RVector.h:232
Definition: RVector.h:245
static const double PointTolerance
Copyright (c) 2011-2015 by Andrew Mustun.
Definition: RS.h:524
bool valid
Getter function for this property: isValid.
Definition: RVector.h:274
Q_DECLARE_METATYPE(RMath *)
QCADCORE_EXPORT QDataStream & operator>>(QDataStream &stream, RVector &vector)
Deserializes the given RColor object.
Definition: RVector.cpp:980
IsoProjectionType
Projection type for isometric projections.
Definition: RS.h:194
static const RVector nullVector
null vector
Definition: RVector.h:284
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:80
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:257
static bool greaterThanY(const RVector &v1, const RVector &v2)
Definition: RVector.h:227
Low-level mathematical representation of an open polyline or closed polyline (= polygon).
Definition: RPolyline.h:49
static double angle
Definition: RVector.h:249
static RVector center
Definition: RVector.h:248
static RVector v
Definition: RVector.h:235
QMultiMap< int, RVector > _RMapIntVector
Definition: RVector.h:304
static const RVector invalid
invalid vector
Definition: RVector.h:279
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:948