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  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> getSortedByAngle(const QList<RVector>& list, const RVector& center, double angle);
240  public:
241  static bool lessThan(const RVector& v1, const RVector& v2);
242  static RVector center;
243  static double angle;
244  };
245 
246 public:
251  double x;
252 
257  double y;
258 
263  double z;
264 
268  bool valid;
269 
273  static const RVector invalid;
274 
278  static const RVector nullVector;
279 };
280 
281 QCADCORE_EXPORT RVector operator*(double s, const RVector& v);
282 
283 QCADCORE_EXPORT QDebug operator<<(QDebug dbg, const RVector& v);
284 
288 QCADCORE_EXPORT QDataStream& operator<<(QDataStream& stream, const RVector& vector);
289 
293 QCADCORE_EXPORT QDataStream& operator>>(QDataStream& stream, RVector& vector);
294 
295 
296 Q_DECLARE_METATYPE(QList<RVector>)
298 typedef QMultiMap<int, RVector> _RMapIntVector;
299 Q_DECLARE_METATYPE(_RMapIntVector*)
303 
304 #endif