OpenLexocad  28.0
Vec.h
Go to the documentation of this file.
1 //
3 // Copyright(C) 2013-2016 OpenCascade [www.opencascade.org]
4 //
5 // This library is free software; you can redistribute it and / or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street,
18 // Fifth Floor, Boston, MA 02110 - 1301 USA
19 //
21 
22 #pragma once
23 #include <Geom/XYZ.h>
24 #include <QString>
25 
26 namespace Geom
27 {
28 class Ax1;
29 class Ax2;
30 class Trsf;
31 class Dir;
32 class Pnt;
33 class Vec;
34 
35 } // namespace Geom
36 
37 namespace Geom
38 {
40 class LX_GEOM_EXPORT Vec
41 {
42 public:
44  // //
45  // --------------------- BEGIN API --------------------- //
46  // //
47  // ATTENTION: DO NOT CHANGE ANY SIGNATURES IN THE API ! //
48  // //
50 
51  // Methods PUBLIC
52  //
54  Vec();
56  Vec(const Geom::Dir& V);
58  Vec(const Geom::XYZ& Coord);
60  Vec(const double Xv, const double Yv, const double Zv);
61 
64  Vec(const Geom::Pnt& P1, const Geom::Pnt& P2);
65 
67  Vec(const Vec& rhs);
68 
73  void setCoord(const int Index, const double Xi);
76  void setCoord(const double Xv, const double Yv, const double Zv);
78  void setX(const double X);
80  void setY(const double Y);
82  void setZ(const double Z);
84  void setXYZ(const Geom::XYZ& Coord);
85 
90  double coord(const int Index) const;
92  void coord(double& Xv, double& Yv, double& Zv) const;
94  double x() const;
96  double y() const;
98  double z() const;
101  const Geom::XYZ& xyz() const;
102 
103  float fx() const;
104  float fy() const;
105  float fz() const;
106 
107 
111  bool isEqual(const Vec& Other, const double LinearTolerance, const double AngularTolerance) const;
112 
116  bool isNormal(const Vec& Other, const double AngularTolerance) const;
117 
121  bool isOpposite(const Vec& Other, const double AngularTolerance) const;
122 
129  bool isParallel(const Vec& Other, const double AngularTolerance) const;
130 
136  double angle(const Vec& Other) const;
151  double angleWithRef(const Vec& Other, const Vec& VRef) const;
153  double magnitude() const;
155  double squareMagnitude() const;
156 
157  void add(const Vec& Other);
158  void operator+=(const Vec& Other) { add(Other); }
159 
161  Vec added(const Vec& Other) const;
162  Vec operator+(const Vec& Other) const { return added(Other); }
163 
164 
165  void subtract(const Vec& Right);
166  void operator-=(const Vec& Right) { subtract(Right); }
167 
169  Vec subtracted(const Vec& Right) const;
170  Vec operator-(const Vec& Right) const { return subtracted(Right); }
171 
172 
173  void multiply(const double Scalar);
174  void operator*=(const double Scalar) { multiply(Scalar); }
175 
177  Vec multiplied(const double Scalar) const;
178  Vec operator*(const double Scalar) const { return multiplied(Scalar); }
179 
180 
181  void divide(const double Scalar);
182  void operator/=(const double Scalar) { divide(Scalar); }
183 
185  Vec divided(const double Scalar) const;
186  Vec operator/(const double Scalar) const { return divided(Scalar); }
187 
188 
189  void cross(const Vec& Right);
190  void operator^=(const Vec& Right) { cross(Right); }
191 
193  Vec crossed(const Vec& Right) const;
194  Vec operator^(const Vec& Right) const { return crossed(Right); }
195 
196 
200  double crossMagnitude(const Vec& Right) const;
201 
206  double crossSquareMagnitude(const Vec& Right) const;
207 
208  void crossCross(const Vec& V1, const Vec& V2);
211  Vec crossCrossed(const Vec& V1, const Vec& V2) const;
213  double dot(const Vec& Other) const;
214  double operator*(const Vec& Other) const { return dot(Other); }
215 
219  double dotCross(const Vec& V1, const Vec& V2) const;
220 
221  void normalize();
225  Vec normalized() const;
226 
227  void reverse();
229  Vec reversed() const;
230  Vec operator-() const { return reversed(); }
231  Vec operator & (const Vec& rcVct) const;
232 
233 
236  void setLinearForm(const double A1, const Vec& V1, const double A2, const Vec& V2, const double A3, const Vec& V3, const Vec& V4);
237 
240  void setLinearForm(const double A1, const Vec& V1, const double A2, const Vec& V2, const double A3, const Vec& V3);
241 
244  void setLinearForm(const double A1, const Vec& V1, const double A2, const Vec& V2, const Vec& V3);
245 
248  void setLinearForm(const double A1, const Vec& V1, const double A2, const Vec& V2);
249 
251  void setLinearForm(const double A1, const Vec& V1, const Vec& V2);
252 
254  void setLinearForm(const Vec& V1, const Vec& V2);
255 
256 
257  void mirror(const Vec& V);
258 
259 
263  Vec mirrored(const Vec& V) const;
264 
265 
266  void mirror(const Geom::Ax1& A1);
267 
268 
272  Vec mirrored(const Geom::Ax1& A1) const;
273 
274 
275  void mirror(const Geom::Ax2& A2);
276 
277 
281  Vec mirrored(const Geom::Ax2& A2) const;
282 
283  void rotate(const Geom::Ax1& A1, const double Ang);
284 
287  Vec rotated(const Geom::Ax1& A1, const double Ang) const;
288 
289  void scale(const double S);
291  Vec scaled(const double S) const;
292 
293 
294  void transform(const Geom::Trsf& T);
296  Vec transformed(const Geom::Trsf& T) const;
298 
304  Geom::Vec& projectToLine(const Geom::Vec& rclPoint, const Geom::Vec& rclLine);
305 
310  Geom::Vec perpendicular(const Geom::Vec& rclBase, const Geom::Vec& rclDir) const;
311 
316  double distanceToPlane(const Geom::Vec& rclBase, const Geom::Vec& rclNorm) const;
318  double distanceToLine(const Geom::Vec& rclBase, const Geom::Vec& rclDirect) const;
324  Geom::Vec distanceToLineSegment(const Geom::Vec& rclP1, const Geom::Vec& rclP2) const;
325 
327  // //
328  // ---------------------- END API ---------------------- //
329  // //
331 
332  double& operator[](int i);
333 
334 private:
335  Geom::XYZ _coord;
336 };
337 
338 // global functions
339 
341 inline double distance(const Geom::Vec& v1, const Geom::Vec& v2)
342 {
343  double x = v1.x() - v2.x(), y = v1.y() - v2.y(), z = v1.z() - v2.z();
344  return static_cast<double>(sqrt((x * x) + (y * y) + (z * z)));
345 }
346 
348 inline double distanceP2(const Geom::Vec& v1, const Geom::Vec& v2)
349 {
350  double x = v1.x() - v2.x(), y = v1.y() - v2.y(), z = v1.z() - v2.z();
351  return x * x + y * y + z * z;
352 }
353 
354 LX_GEOM_EXPORT QString to_string(const Vec& vec);
355 } // namespace Geom
356 
357 inline Geom::Vec operator*(const double Scalar, const Geom::Vec& V)
358 {
359  return V.multiplied(Scalar);
360 }
Geom::Vec::setCoord
void setCoord(const double Xv, const double Yv, const double Zv)
Geom::Vec::setXYZ
void setXYZ(const Geom::XYZ &Coord)
Assigns the three coordinates of Coord to this vector.
Geom::Vec::transformed
Vec transformed(const Geom::Trsf &T) const
Transforms a vector with the transformation T.
Geom::Vec::normalized
Vec normalized() const
Geom::Vec::divided
Vec divided(const double Scalar) const
Divides a vector by a scalar //! computes the cross product between two vectors
Geom::Vec::multiplied
Vec multiplied(const double Scalar) const
Multiplies a vector by a scalar //! Divides a vector by a scalar
Geom::Vec::operator/
Vec operator/(const double Scalar) const
Definition: Vec.h:186
Geom::Vec::add
void add(const Vec &Other)
Geom::Vec::operator*
double operator*(const Vec &Other) const
Definition: Vec.h:214
Geom::Vec::operator[]
double & operator[](int i)
Geom::Vec::subtract
void subtract(const Vec &Right)
Geom::Vec::distanceToLine
double distanceToLine(const Geom::Vec &rclBase, const Geom::Vec &rclDirect) const
Computes the distance from this point to the line given by rclBase and rclDirect.
Geom::Vec::setCoord
void setCoord(const int Index, const double Xi)
XYZ.h
Geom::Vec::Vec
Vec(const Vec &rhs)
Copy constructor.
Geom::Vec::rotated
Vec rotated(const Geom::Ax1 &A1, const double Ang) const
Geom::Vec::isEqual
bool isEqual(const Vec &Other, const double LinearTolerance, const double AngularTolerance) const
Geom::Vec
Defines a non-persistent vector in 3D space.
Definition: Vec.h:41
Geom::Vec::Vec
Vec(const Geom::Dir &V)
Creates a unitary vector from a direction V.
Geom::Vec::Vec
Vec(const Geom::Pnt &P1, const Geom::Pnt &P2)
Geom::Vec::Vec
Vec(const double Xv, const double Yv, const double Zv)
Creates a point with its three Cartesian coordinates.
Geom::Vec::squareMagnitude
double squareMagnitude() const
Computes the square magnitude of this vector. //! Adds two vectors
Geom::Vec::operator^
Vec operator^(const Vec &Right) const
Definition: Vec.h:194
Geom::Vec::dotCross
double dotCross(const Vec &V1, const Vec &V2) const
Geom::Vec::subtracted
Vec subtracted(const Vec &Right) const
Subtracts two vectors //! Multiplies a vector by a scalar
Geom::Vec::mirrored
Vec mirrored(const Geom::Ax2 &A2) const
X
X
Definition: Globals.h:29
Geom::Vec::crossMagnitude
double crossMagnitude(const Vec &Right) const
Geom::Vec::crossCrossed
Vec crossCrossed(const Vec &V1, const Vec &V2) const
Geom::Ax1
Definition: Ax1.h:50
Geom::XYZ
Definition: XYZ.h:44
Geom::Vec::crossed
Vec crossed(const Vec &Right) const
computes the cross product between two vectors
Geom::Vec::x
double x() const
For this vector, returns its X coordinate.
Geom::Vec::magnitude
double magnitude() const
Computes the magnitude of this vector.
Geom::Vec::operator-=
void operator-=(const Vec &Right)
Definition: Vec.h:166
Geom::Vec::operator*
Vec operator*(const double Scalar) const
Definition: Vec.h:178
Geom::Vec::reversed
Vec reversed() const
Reverses the direction of a vector
Geom::Dir
Definition: Dir.h:45
Geom::Vec::angleWithRef
double angleWithRef(const Vec &Other, const Vec &VRef) const
Geom::Vec::cross
void cross(const Vec &Right)
Geom::Vec::setLinearForm
void setLinearForm(const double A1, const Vec &V1, const double A2, const Vec &V2, const double A3, const Vec &V3)
Geom::Vec::crossCross
void crossCross(const Vec &V1, const Vec &V2)
Geom::Vec::xyz
const Geom::XYZ & xyz() const
Geom::Vec::operator/=
void operator/=(const double Scalar)
Definition: Vec.h:182
Geom::Vec::isNormal
bool isNormal(const Vec &Other, const double AngularTolerance) const
Geom::Vec::mirror
void mirror(const Vec &V)
Geom::Vec::setLinearForm
void setLinearForm(const Vec &V1, const Vec &V2)
<me> is setted to the following linear form : V1 + V2
Geom::Pnt
Defines a non-persistent 3D Cartesian point.
Definition: Pnt.h:44
Geom::Ax2
Definition: Ax2.h:66
Geom::Vec::mirrored
Vec mirrored(const Geom::Ax1 &A1) const
Geom::Vec::coord
double coord(const int Index) const
Geom::Vec::operator-
Vec operator-(const Vec &Right) const
Definition: Vec.h:170
Geom::Vec::mirrored
Vec mirrored(const Vec &V) const
Geom::Vec::setZ
void setZ(const double Z)
Assigns the given value to the X coordinate of this vector.
Geom::Vec::perpendicular
Geom::Vec perpendicular(const Geom::Vec &rclBase, const Geom::Vec &rclDir) const
Z
Z
Definition: Globals.h:31
Geom::Vec::setLinearForm
void setLinearForm(const double A1, const Vec &V1, const Vec &V2)
<me> is setted to the following linear form : A1 * V1 + V2
Geom::Vec::fx
float fx() const
Geom::distance
double distance(const Geom::Vec &v1, const Geom::Vec &v2)
Returns the distance between two points.
Definition: Vec.h:341
Geom::Vec::operator^=
void operator^=(const Vec &Right)
Definition: Vec.h:190
Geom::Vec::crossSquareMagnitude
double crossSquareMagnitude(const Vec &Right) const
Geom::Vec::mirror
void mirror(const Geom::Ax2 &A2)
Geom::Vec::setY
void setY(const double Y)
Assigns the given value to the X coordinate of this vector.
Geom::Vec::operator+=
void operator+=(const Vec &Other)
Definition: Vec.h:158
Geom::Vec::divide
void divide(const double Scalar)
Geom::Vec::setX
void setX(const double X)
Assigns the given value to the X coordinate of this vector.
Y
Y
Definition: Globals.h:30
operator*
Geom::Vec operator*(const double Scalar, const Geom::Vec &V)
Definition: Vec.h:357
Geom::Vec::setLinearForm
void setLinearForm(const double A1, const Vec &V1, const double A2, const Vec &V2)
Geom::Vec::multiply
void multiply(const double Scalar)
Geom::Vec::distanceToLineSegment
Geom::Vec distanceToLineSegment(const Geom::Vec &rclP1, const Geom::Vec &rclP2) const
Geom::Vec::normalize
void normalize()
Geom::Vec::y
double y() const
For this vector, returns its Y coordinate.
Geom::Vec::z
double z() const
For this vector, returns its Z coordinate.
Geom::Vec::isOpposite
bool isOpposite(const Vec &Other, const double AngularTolerance) const
Geom::Vec::operator+
Vec operator+(const Vec &Other) const
Definition: Vec.h:162
Geom::Vec::dot
double dot(const Vec &Other) const
computes the scalar product
Geom::Vec::rotate
void rotate(const Geom::Ax1 &A1, const double Ang)
Geom::Vec::angle
double angle(const Vec &Other) const
Geom::Trsf
Definition: Trsf.h:58
Geom::to_string
LX_GEOM_EXPORT QString to_string(const Dir &dir)
Geom::Vec::fz
float fz() const
Geom::Vec::added
Vec added(const Vec &Other) const
Adds two vectors //! Subtracts two vectors
Geom::Vec::reverse
void reverse()
Geom::Vec::Vec
Vec(const Geom::XYZ &Coord)
Creates a vector with a triplet of coordinates.
Geom::Vec::setLinearForm
void setLinearForm(const double A1, const Vec &V1, const double A2, const Vec &V2, const Vec &V3)
Geom::Vec::setLinearForm
void setLinearForm(const double A1, const Vec &V1, const double A2, const Vec &V2, const double A3, const Vec &V3, const Vec &V4)
Geom::Vec::fy
float fy() const
Geom::Vec::mirror
void mirror(const Geom::Ax1 &A1)
Geom::Vec::transform
void transform(const Geom::Trsf &T)
Geom::Vec::scale
void scale(const double S)
Geom::Vec::operator-
Vec operator-() const
Definition: Vec.h:230
Geom::Vec::distanceToPlane
double distanceToPlane(const Geom::Vec &rclBase, const Geom::Vec &rclNorm) const
Geom::Vec::operator*=
void operator*=(const double Scalar)
Definition: Vec.h:174
Geom::Vec::isParallel
bool isParallel(const Vec &Other, const double AngularTolerance) const
Geom::Vec::Vec
Vec()
Creates an indefinite vector.
Geom::Vec::scaled
Vec scaled(const double S) const
Scales a vector. S is the scaling value. //! Transforms a vector with the transformation T.
Geom::Vec::projectToLine
Geom::Vec & projectToLine(const Geom::Vec &rclPoint, const Geom::Vec &rclLine)
Projects this point onto the line given by the base rclPoint and the direction rclLine.
Geom::Vec::coord
void coord(double &Xv, double &Yv, double &Zv) const
For this vector returns its three coordinates Xv, Yv, and Zvinline
Geom
Definition: PropertyContainer.h:33
Geom::distanceP2
double distanceP2(const Geom::Vec &v1, const Geom::Vec &v2)
Returns the squared distance between two points.
Definition: Vec.h:348