OpenLexocad  27.1
Vector3D.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2005 Imetric 3D GmbH *
3  * *
4  * This file is part of the FreeCAD CAx development system. *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Library General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Library General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public *
17  * License along with this library; see the file COPYING.LIB. If not, *
18  * write to the Free Software Foundation, Inc., 59 Temple Place, *
19  * Suite 330, Boston, MA 02111-1307, USA *
20  * *
21  ***************************************************************************/
22 
23 
24 #ifndef BASE_VECTOR3D_H
25 #define BASE_VECTOR3D_H
26 
27 
28 #include <cmath>
29 #include <cfloat>
30 
31 #ifndef F_PI
32 # define F_PI 3.1415926f
33 #endif
34 
35 #ifndef D_PI
36 # define D_PI 3.141592653589793
37 #endif
38 
39 #ifndef FLOAT_MAX
40 # define FLOAT_MAX 3.402823466E+38F
41 #endif
42 
43 #ifndef FLOAT_MIN
44 # define FLOAT_MIN 1.175494351E-38F
45 #endif
46 
47 #ifndef DOUBLE_MAX
48 # define DOUBLE_MAX 1.7976931348623157E+308 /* max decimal value of a "double"*/
49 #endif
50 
51 #ifndef DOUBLE_MIN
52 # define DOUBLE_MIN 2.2250738585072014E-308 /* min decimal value of a "double"*/
53 #endif
54 
55 
56 namespace Base {
57 template <class numT>
58 struct float_traits { };
59 
60 template <>
61 struct float_traits<float> {
62  typedef float float_type;
63  static inline float_type pi() { return F_PI; }
64  static inline float_type epsilon() { return FLT_EPSILON; }
65  static inline float_type maximum() { return FLT_MAX; }
66 };
67 
68 template <>
69 struct float_traits<double> {
70  typedef double float_type;
71  static inline float_type pi() { return D_PI; }
72  static inline float_type epsilon() { return DBL_EPSILON; }
73  static inline float_type maximum() { return DBL_MAX; }
74 };
75 
77 template <class _Precision>
78 class Vector3
79 {
80 public:
81  typedef _Precision num_type;
83  static inline num_type epsilon() { return traits_type::epsilon(); }
84 
87  _Precision x;
88  _Precision y;
89  _Precision z;
91 
93  explicit Vector3 (_Precision fx = 0.0, _Precision fy = 0.0, _Precision fz = 0.0);
95  Vector3 (const Vector3<_Precision>& rcVct);
96 
99  _Precision & operator [] (unsigned short usIndex);
102  const _Precision & operator [] (unsigned short usIndex) const;
104  Vector3 operator + (const Vector3<_Precision>& rcVct) const;
105  Vector3 operator & (const Vector3<_Precision>& rcVct) const;
107  Vector3 operator - (const Vector3<_Precision>& rcVct) const;
109  Vector3 operator - (void) const;
111  Vector3 & operator += (const Vector3<_Precision>& rcVct);
113  Vector3 & operator -= (const Vector3<_Precision>& rcVct);
115  Vector3 operator * (_Precision fScale) const;
116  Vector3 operator / (_Precision fDiv) const;
117  Vector3 & operator *= (_Precision fScale);
118  Vector3 & operator /= (_Precision fDiv);
120  Vector3 & operator = (const Vector3<_Precision>& rcVct);
122  _Precision operator * (const Vector3<_Precision>& rcVct) const;
124  _Precision Dot (const Vector3<_Precision>& rcVct) const;
126  Vector3 operator % (const Vector3<_Precision>& rcVct) const;
128  Vector3 Cross (const Vector3<_Precision>& rcVct) const;
129 
131  bool operator != (const Vector3<_Precision>& rcVct) const;
133  bool operator == (const Vector3<_Precision>& rcVct) const;
135 
137  bool IsOnLineSegment (const Vector3<_Precision>& startVct, const Vector3<_Precision>& endVct) const;
138 
141  void ScaleX (_Precision f);
142  void ScaleY (_Precision f);
143  void ScaleZ (_Precision f);
144  void Scale (_Precision fX, _Precision fY, _Precision fZ);
145  void MoveX (_Precision f);
146  void MoveY (_Precision f);
147  void MoveZ (_Precision f);
148  void Move (_Precision fX, _Precision fY, _Precision fZ);
149  void RotateX (_Precision f);
150  void RotateY (_Precision f);
151  void RotateZ (_Precision f);
153 
154  void Set (_Precision fX, _Precision fY, _Precision fZ);
155 
158  _Precision Length (void) const;
161  _Precision Sqr (void) const;
163  Vector3 & Normalize (void);
165  _Precision GetAngle (const Vector3 &rcVect) const;
170  void TransformToCoordinateSystem (const Vector3 &rclBase, const Vector3 &rclDirX, const Vector3 &rclDirY);
179  bool IsEqual(const Vector3 &rclPnt, _Precision tol) const;
181  Vector3 & ProjectToPlane (const Vector3 &rclBase, const Vector3 &rclNorm);
186  void ProjectToPlane (const Vector3 &rclBase, const Vector3 &rclNorm, Vector3 &rclProj) const;
188 
194  Vector3 & ProjectToLine (const Vector3 &rclPoint, const Vector3 &rclLine);
199  Vector3 Perpendicular(const Vector3 &rclBase, const Vector3 &rclDir) const;
204  _Precision DistanceToPlane (const Vector3 &rclBase, const Vector3 &rclNorm) const;
206  _Precision DistanceToLine (const Vector3 &rclBase, const Vector3 &rclDirect) const;
212  Vector3 DistanceToLineSegment (const Vector3& rclP1, const Vector3& rclP2) const;
214 };
215 
216 // global functions
217 
219 template <class _Precision>
220 inline _Precision Distance (const Vector3<_Precision> &v1, const Vector3<_Precision> &v2)
221 {
222  _Precision x=v1.x-v2.x, y=v1.y-v2.y, z=v1.z-v2.z;
223  return static_cast<_Precision>(sqrt((x * x) + (y * y) + (z * z)));
224 }
225 
227 template <class _Precision>
228 inline _Precision DistanceP2 (const Vector3<_Precision> &v1, const Vector3<_Precision> &v2)
229 {
230  _Precision x=v1.x-v2.x, y=v1.y-v2.y, z=v1.z-v2.z;
231  return x * x + y * y + z * z;
232 }
233 
235 template <class _Precision>
236 inline Vector3<_Precision> operator * (_Precision fFac, const Vector3<_Precision> &rcVct)
237 {
238  return Vector3<_Precision>(rcVct.x * fFac, rcVct.y * fFac, rcVct.z * fFac);
239 }
240 
241 template <class _Pr1, class _Pr2>
243 {
244  return Vector3<_Pr1>(static_cast<_Pr1>(v.x),static_cast<_Pr1>(v.y),static_cast<_Pr1>(v.z));
245 }
246 
249 
250 } // namespace Base
251 
252 #endif // BASE_VECTOR3D_H
_Precision z
Definition: Vector3D.h:89
_Precision DistanceToPlane(const Vector3 &rclBase, const Vector3 &rclNorm) const
Vector3 operator &(const Vector3< _Precision > &rcVct) const
static float_type maximum()
Definition: Vector3D.h:73
Vector3 & operator+=(const Vector3< _Precision > &rcVct)
Vector summation.
#define F_PI
Definition: Vector3D.h:32
_Precision y
Definition: Vector3D.h:88
Vector3 & operator -=(const Vector3< _Precision > &rcVct)
Vector subtraction.
void Scale(_Precision fX, _Precision fY, _Precision fZ)
Vector3 operator *(_Precision fScale) const
Vector scaling.
void TransformToCoordinateSystem(const Vector3 &rclBase, const Vector3 &rclDirX, const Vector3 &rclDirY)
_Precision num_type
Definition: Vector3D.h:81
_Precision Dot(const Vector3< _Precision > &rcVct) const
Scalar product.
_Precision DistanceP2(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
Returns the squared distance between two points.
Definition: Vector3D.h:228
float_traits< num_type > traits_type
Definition: Vector3D.h:82
bool IsEqual(const Vector3 &rclPnt, _Precision tol) const
IsEqual.
Vector3< _Pr1 > toVector(const Vector3< _Pr2 > &v)
Definition: Vector3D.h:242
static num_type epsilon()
Definition: Vector3D.h:83
Vector3< double > Vector3d
Definition: Vector3D.h:248
_Precision x
Definition: Vector3D.h:87
_Precision DistanceToLine(const Vector3 &rclBase, const Vector3 &rclDirect) const
Computes the distance from this point to the line given by rclBase and rclDirect.
Vector3 operator -(void) const
Negative vector.
Vector3 & operator/=(_Precision fDiv)
void RotateX(_Precision f)
Vector3 & ProjectToPlane(const Vector3 &rclBase, const Vector3 &rclNorm)
Projects this point onto the plane given by the base rclBase and the normal rclNorm.
bool IsOnLineSegment(const Vector3< _Precision > &startVct, const Vector3< _Precision > &endVct) const
Check if Vector is on a line segment.
Vector3 operator %(const Vector3< _Precision > &rcVct) const
Cross product.
_Precision Sqr(void) const
Squared length of the vector.
static float_type maximum()
Definition: Vector3D.h:65
static float_type epsilon()
Definition: Vector3D.h:64
double float_type
Definition: Vector3D.h:70
Vector3< float > Vector3f
Definition: Vector3D.h:247
Vector3 Perpendicular(const Vector3 &rclBase, const Vector3 &rclDir) const
void MoveZ(_Precision f)
void ScaleZ(_Precision f)
static float_type pi()
Definition: Vector3D.h:63
_Precision Distance(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
Returns the distance between two points.
Definition: Vector3D.h:220
_Precision GetAngle(const Vector3 &rcVect) const
Get angle between both vectors. The returned value lies in the interval [0,pi].
bool operator==(const Vector3< _Precision > &rcVct) const
Comparing for equality.
Vector3 Cross(const Vector3< _Precision > &rcVct) const
Cross product.
void RotateZ(_Precision f)
Vector3 DistanceToLineSegment(const Vector3 &rclP1, const Vector3 &rclP2) const
float float_type
Definition: Vector3D.h:62
Vector3(_Precision fx=0.0, _Precision fy=0.0, _Precision fz=0.0)
Construction.
_Precision & operator [](unsigned short usIndex)
Returns a reference to a coordinate. usIndex must be in the range [0,2].
Vector3 & ProjectToLine(const Vector3 &rclPoint, const Vector3 &rclLine)
Projects this point onto the line given by the base rclPoint and the direction rclLine.
Vector3 operator/(_Precision fDiv) const
Vector3 & Normalize(void)
Set length to 1.
Vector3 & operator=(const Vector3< _Precision > &rcVct)
Assignment.
void Move(_Precision fX, _Precision fY, _Precision fZ)
static float_type pi()
Definition: Vector3D.h:71
bool operator !=(const Vector3< _Precision > &rcVct) const
Comparing for inequality.
void RotateY(_Precision f)
Vector3 & operator *=(_Precision fScale)
Definition: AbstractXMLReader.h:5
Definition: Vector3D.h:78
void MoveY(_Precision f)
Vector3 operator+(const Vector3< _Precision > &rcVct) const
Vector addition.
void Set(_Precision fX, _Precision fY, _Precision fZ)
static float_type epsilon()
Definition: Vector3D.h:72
#define D_PI
Definition: Vector3D.h:36
_Precision Length(void) const
Length of the vector.
Definition: Vector3D.h:58
void MoveX(_Precision f)
void ScaleY(_Precision f)
DualNumber operator *(DualNumber a, DualNumber b)
Definition: DualNumber.h:71
void ScaleX(_Precision f)