QCAD
Open Source 2D CAD
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  bool isValid() const;
82 
83  bool isInside(const RBox& b) const;
84 
85  bool equalsFuzzy(const RVector& v, double tol = RS::PointTolerance) const;
86  double getDistanceTo(const RVector& v) const;
87  double getDistanceTo2d(const RVector& v) const;
88  void setAngle(double a);
89  double getAngle() const;
90  double getAngleToPlaneXY() const;
91  double getAngleTo(const RVector& v) const;
92  void setMagnitude2d(double m);
93  double getMagnitude() const;
94  double getSquaredMagnitude() const;
95  double getMagnitude2d() const;
96  RVector getLerp(const RVector& v, double t) const;
97  RVector getUnitVector() const;
98  void setX(double x);
99  double getX();
100  void setY(double y);
101  double getY();
102  void setZ(double z);
103  double getZ();
104 
105  bool isInWindow(const RVector& firstCorner, const RVector& secondCorner);
106 
107  RVector move(const RVector& offset);
108 
112  static void moveList(QList<RVector>& list, const RVector& offset);
113 
114  RVector rotate(double rotation);
115  RVector rotate(double rotation, const RVector& center);
116  RVector rotate3d(const RLine& axis, double rotation);
117  RVector rotate3d(const QQuaternion& quaternion);
118 
122  static void rotateList(QList<RVector>& list, double rotation);
123 
127  static void rotateList(QList<RVector>& list, double rotation, const RVector& center);
128 
129  RVector scale(double factor, const RVector& center=nullVector);
130  RVector scale(const RVector& factors, const RVector& center=nullVector);
131 
135  static void scaleList(QList<RVector>& list, double factor, const RVector& center=nullVector);
136 
140  static void scaleList(QList<RVector>& list, const RVector& factors, const RVector& center=nullVector);
141 
142  RVector mirror(const RLine& axis);
143  RVector mirror(const RVector& axis1, const RVector& axis2);
144  RVector flipHorizontal();
145  RVector flipVertical();
146  RVector stretch(const RPolyline& area, const RVector& offset);
147  RVector isoProject(RS::IsoProjectionType type, bool trueScale);
148  RVector transform(const RMatrix& m);
149  RVector transform2dM(const RMatrix& m);
150  RVector transform2d(const QTransform& t);
151 
152  RVector getTransformed(const RMatrix& m) const;
153  RVector getTransformed2dM(const RMatrix& m) const;
154  RVector getTransformed2d(const QTransform& t) const;
155 
156  RVector getDividedComponents(const RVector& v) const;
157  RVector getMultipliedComponents(const RVector& v) const;
158 
159  RVector getClosest(const QList<RVector>& list) const;
160  double getClosestDistance(const QList<RVector>& list, int counts);
161  int getClosestIndex(const QList<RVector>& list) const;
162 
163  RVector operator +(const RVector& v) const;
164  RVector operator -(const RVector& v) const;
165  RVector operator *(double s) const;
166  RVector operator /(double s) const;
167  RVector operator -() const;
168  RVector getNegated() const;
169 
170  RVector normalize();
171  RVector getNormalized() const;
172 
173  void operator +=(const RVector& v);
174  void operator -=(const RVector& v);
175  void operator *=(double s);
176  void operator /=(double s);
177 
178  bool operator ==(const RVector& v) const;
179  bool operator !=(const RVector& v) const {
180  return !operator==(v);
181  }
182 
183  RVector getFloor() const;
184  RVector getCeil() const;
185 
186  static RVector getMinimum(const QList<RVector>& vectors);
187  static RVector getMaximum(const QList<RVector>& vectors);
188 
189  static RVector getMinimumX(const QList<RVector>& vectors);
190  static RVector getMaximumX(const QList<RVector>& vectors);
191  static RVector getMinimumY(const QList<RVector>& vectors);
192  static RVector getMaximumY(const QList<RVector>& vectors);
193 
194  static RVector getMinimum(const RVector& v1, const RVector& v2);
195  static RVector getMaximum(const RVector& v1, const RVector& v2);
196 
197  static RVector getAverage(const RVector& v1, const RVector& v2);
198 
199  static QList<double> getXList(const QList<RVector>& vectors);
200  static QList<double> getYList(const QList<RVector>& vectors);
201  static QList<double> getZList(const QList<RVector>& vectors);
202 
203  static RVector getCrossProduct(const RVector& v1, const RVector& v2);
204  static double getDotProduct(const RVector& v1, const RVector& v2);
205  static RVector createPolar(double radius, double angle) {
206  RVector ret;
207  ret.setPolar(radius, angle);
208  return ret;
209  }
210 
211  static bool lessThanX(const RVector& v1, const RVector& v2) {
212  return v1.x < v2.x;
213  }
214 
215  static bool greaterThanX(const RVector& v1, const RVector& v2) {
216  return v1.x > v2.x;
217  }
218 
219  static bool lessThanY(const RVector& v1, const RVector& v2) {
220  return v1.y < v2.y;
221  }
222 
223  static bool greaterThanY(const RVector& v1, const RVector& v2) {
224  return v1.y > v2.y;
225  }
226 
227  static QList<RVector> getSortedByDistance(const QList<RVector>& list, const RVector& v);
229  public:
230  static bool lessThan(const RVector& v1, const RVector& v2);
231  static RVector v;
232  };
233 
234  static QList<RVector> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
236  public:
237  static bool lessThan(const RVector& v1, const RVector& v2);
238  static RVector center;
239  static double angle;
240  };
241 
242 public:
247  double x;
248 
253  double y;
254 
259  double z;
260 
264  bool valid;
265 
269  static const RVector invalid;
270 
274  static const RVector nullVector;
275 };
276 
277 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
278 
279 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
280 
284 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
285 
289 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
290 
291 
292 Q_DECLARE_METATYPE(QList<RVector>)
294 typedef QMultiMap<int, RVector> _RMapIntVector;
295 Q_DECLARE_METATYPE(_RMapIntVector*)
299 
300 #endif