Vector3D.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef BASE_VECTOR3D_H
00025 #define BASE_VECTOR3D_H
00026
00027
00028 #include <math.h>
00029
00030 #define FLOAT_EPS 1.0e-4f
00031
00032 #ifndef F_PI
00033 # define F_PI 3.1415926f
00034 #endif
00035
00036 #ifndef D_PI
00037 # define D_PI 3.141592653589793
00038 #endif
00039
00040 #ifndef FLOAT_MAX
00041 # define FLOAT_MAX 1e30f
00042 #endif
00043
00044 #ifndef DOUBLE_MAX
00045 # define DOUBLE_MAX 1.7976931348623157E+308
00046 #endif
00047
00048 #ifndef DOUBLE_MIN
00049 # define DOUBLE_MIN 2.2250738585072014E-308
00050 #endif
00051
00052
00053 namespace Base {
00054 template <class numT>
00055 struct float_traits { };
00056
00057 template <>
00058 struct float_traits<float> {
00059 typedef float float_type;
00060 static inline float_type pi() { return F_PI; }
00061 static inline float_type epsilon() { return FLOAT_EPS; }
00062 static inline float_type maximum() { return FLOAT_MAX; }
00063 };
00064
00065 template <>
00066 struct float_traits<double> {
00067 typedef double float_type;
00068 static inline float_type pi() { return D_PI; }
00069 static inline float_type epsilon() { return FLOAT_EPS; }
00070 static inline float_type maximum() { return FLOAT_MAX; }
00071 };
00072
00074 template <class _Precision>
00075 class Vector3
00076 {
00077 public:
00078 typedef _Precision num_type;
00079 typedef float_traits<num_type> traits_type;
00080 static inline num_type epsilon() { return traits_type::epsilon(); }
00081
00084 _Precision x;
00085 _Precision y;
00086 _Precision z;
00088
00090 explicit Vector3 (_Precision fx = 0.0f, _Precision fy = 0.0f, _Precision fz = 0.0f);
00092 Vector3 (const Vector3<_Precision>& rcVct);
00093
00096
00097 _Precision & operator [] (unsigned short usIndex);
00099 const _Precision & operator [] (unsigned short usIndex) const;
00101 Vector3 operator + (const Vector3<_Precision>& rcVct) const;
00102 Vector3 operator & (const Vector3<_Precision>& rcVct) const;
00104 Vector3 operator - (const Vector3<_Precision>& rcVct) const;
00106 Vector3 operator - (void) const;
00108 Vector3 & operator += (const Vector3<_Precision>& rcVct);
00110 Vector3 & operator -= (const Vector3<_Precision>& rcVct);
00112 Vector3 operator * (_Precision fScale) const;
00113 Vector3 operator / (_Precision fDiv) const;
00114 Vector3 & operator *= (_Precision fScale);
00115 Vector3 & operator /= (_Precision fDiv);
00117 Vector3 & operator = (const Vector3<_Precision>& rcVct);
00119 _Precision operator * (const Vector3<_Precision>& rcVct) const;
00121 Vector3 operator % (const Vector3<_Precision>& rcVct) const;
00123 bool operator != (const Vector3<_Precision>& rcVct) const;
00125 bool operator == (const Vector3<_Precision>& rcVct) const;
00127
00130 void ScaleX (_Precision f);
00131 void ScaleY (_Precision f);
00132 void ScaleZ (_Precision f);
00133 void Scale (_Precision fX, _Precision fY, _Precision fZ);
00134 void MoveX (_Precision f);
00135 void MoveY (_Precision f);
00136 void MoveZ (_Precision f);
00137 void Move (_Precision fX, _Precision fY, _Precision fZ);
00138 void RotateX (_Precision f);
00139 void RotateY (_Precision f);
00140 void RotateZ (_Precision f);
00142
00143 void Set (_Precision fX, _Precision fY, _Precision fZ);
00144
00147
00148 _Precision Length (void) const;
00150 _Precision Sqr (void) const;
00152 Vector3 & Normalize (void);
00154 _Precision GetAngle (const Vector3 &rcVect) const;
00159 void TransformToCoordinateSystem (const Vector3 &rclBase, const Vector3 &rclDirX, const Vector3 &rclDirY);
00160
00162 Vector3 & ProjToPlane (const Vector3 &rclBase, const Vector3 &rclNorm);
00164
00170 Vector3 & ProjToLine (const Vector3 &rclPoint, const Vector3 &rclLine);
00175 Vector3 Perpendicular(const Vector3 &rclBase, const Vector3 &rclDir) const;
00180 _Precision DistanceToPlane (const Vector3 &rclBase, const Vector3 &rclNorm) const;
00182 _Precision DistanceToLine (const Vector3 &rclBase, const Vector3 &rclDirect) const;
00184 };
00185
00186
00187
00189 template <class _Precision>
00190 inline _Precision Distance (const Vector3<_Precision> &v1, const Vector3<_Precision> &v2)
00191 {
00192 _Precision x=v1.x-v2.x, y=v1.y-v2.y, z=v1.z-v2.z;
00193 return (_Precision)sqrt((x * x) + (y * y) + (z * z));
00194 }
00195
00197 template <class _Precision>
00198 inline _Precision DistanceP2 (const Vector3<_Precision> &v1, const Vector3<_Precision> &v2)
00199 {
00200 _Precision x=v1.x-v2.x, y=v1.y-v2.y, z=v1.z-v2.z;
00201 return x * x + y * y + z * z;
00202 }
00203
00205 template <class _Precision>
00206 inline Vector3<_Precision> operator * (_Precision fFac, const Vector3<_Precision> &rcVct)
00207 {
00208 return Vector3<_Precision>(rcVct.x * fFac, rcVct.y * fFac, rcVct.z * fFac);
00209 }
00210
00211 template <class _Pr1, class _Pr2>
00212 inline Vector3<_Pr1> toVector(const Vector3<_Pr2>& v)
00213 {
00214 return Vector3<_Pr1>((_Pr1)v.x,(_Pr1)v.y,(_Pr1)v.z);
00215 };
00216
00217 typedef Vector3<float> Vector3f;
00218 typedef Vector3<double> Vector3d;
00219
00220 template <class vecT>
00221 struct vec_traits { };
00222
00223 template <>
00224 struct vec_traits<Vector3f> {
00225 typedef Vector3f vec_type;
00226 typedef float float_type;
00227 vec_traits(const vec_type& v) : v(v){}
00228 inline float_type x() { return v.x; }
00229 inline float_type y() { return v.y; }
00230 inline float_type z() { return v.z; }
00231 private:
00232 const vec_type& v;
00233 };
00234
00235 template <>
00236 struct vec_traits<Vector3d> {
00237 typedef Vector3d vec_type;
00238 typedef double float_type;
00239 vec_traits(const vec_type& v) : v(v){}
00240 inline float_type x() { return v.x; }
00241 inline float_type y() { return v.y; }
00242 inline float_type z() { return v.z; }
00243 private:
00244 const vec_type& v;
00245 };
00246
00247 template <class _Vec1, class _Vec2>
00248 inline _Vec1 convertTo(const _Vec2& v)
00249 {
00250 typedef _Vec2 vec_type;
00251 typedef vec_traits<vec_type> traits_type;
00252 typedef typename traits_type::float_type float_type;
00253 traits_type t(v);
00254 return _Vec1((float_type)t.x(),(float_type)t.y(),(float_type)t.z());
00255 };
00256
00257
00258 }
00259
00260 #endif // BASE_VECTOR3D_H
00261