1 // Copyright (C) 2002-2009 Nikolaus Gebhardt 2 // This file is part of the "irrKlang" library. 3 // For conditions of distribution and use, see copyright notice in irrKlang.h 4 5 #ifndef __IRR_IRRKLANG_VEC_3D_H_INCLUDED__ 6 #define __IRR_IRRKLANG_VEC_3D_H_INCLUDED__ 7 8 #include <math.h> 9 #include "ik_irrKlangTypes.h" 10 11 12 namespace irrklang 13 { 14 15 //! a 3d vector template class for representing vectors and points in 3d 16 template <class T> 17 class vec3d 18 { 19 public: 20 vec3d()21 vec3d(): X(0), Y(0), Z(0) {}; vec3d(T nx,T ny,T nz)22 vec3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}; vec3d(const vec3d<T> & other)23 vec3d(const vec3d<T>& other) :X(other.X), Y(other.Y), Z(other.Z) {}; 24 25 //! constructor creating an irrklang vec3d from an irrlicht vector. 26 #ifdef __IRR_POINT_3D_H_INCLUDED__ 27 template<class B> vec3d(const B & other)28 vec3d(const B& other) :X(other.X), Y(other.Y), Z(other.Z) {}; 29 #endif // __IRR_POINT_3D_H_INCLUDED__ 30 31 // operators 32 33 vec3d<T> operator-() const { return vec3d<T>(-X, -Y, -Z); } 34 35 vec3d<T>& operator=(const vec3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; } 36 37 vec3d<T> operator+(const vec3d<T>& other) const { return vec3d<T>(X + other.X, Y + other.Y, Z + other.Z); } 38 vec3d<T>& operator+=(const vec3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; } 39 40 vec3d<T> operator-(const vec3d<T>& other) const { return vec3d<T>(X - other.X, Y - other.Y, Z - other.Z); } 41 vec3d<T>& operator-=(const vec3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; } 42 43 vec3d<T> operator*(const vec3d<T>& other) const { return vec3d<T>(X * other.X, Y * other.Y, Z * other.Z); } 44 vec3d<T>& operator*=(const vec3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; } 45 vec3d<T> operator*(const T v) const { return vec3d<T>(X * v, Y * v, Z * v); } 46 vec3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; } 47 48 vec3d<T> operator/(const vec3d<T>& other) const { return vec3d<T>(X / other.X, Y / other.Y, Z / other.Z); } 49 vec3d<T>& operator/=(const vec3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; } 50 vec3d<T> operator/(const T v) const { T i=(T)1.0/v; return vec3d<T>(X * i, Y * i, Z * i); } 51 vec3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; } 52 53 bool operator<=(const vec3d<T>&other) const { return X<=other.X && Y<=other.Y && Z<=other.Z;}; 54 bool operator>=(const vec3d<T>&other) const { return X>=other.X && Y>=other.Y && Z>=other.Z;}; 55 56 bool operator==(const vec3d<T>& other) const { return other.X==X && other.Y==Y && other.Z==Z; } 57 bool operator!=(const vec3d<T>& other) const { return other.X!=X || other.Y!=Y || other.Z!=Z; } 58 59 // functions 60 61 //! returns if this vector equalsfloat the other one, taking floating point rounding errors into account equals(const vec3d<T> & other)62 bool equals(const vec3d<T>& other) 63 { 64 return equalsfloat(X, other.X) && 65 equalsfloat(Y, other.Y) && 66 equalsfloat(Z, other.Z); 67 } 68 set(const T nx,const T ny,const T nz)69 void set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; } set(const vec3d<T> & p)70 void set(const vec3d<T>& p) { X=p.X; Y=p.Y; Z=p.Z;} 71 72 //! Returns length of the vector. getLength()73 ik_f64 getLength() const { return sqrt(X*X + Y*Y + Z*Z); } 74 75 //! Returns squared length of the vector. 76 /** This is useful because it is much faster then 77 getLength(). */ getLengthSQ()78 ik_f64 getLengthSQ() const { return X*X + Y*Y + Z*Z; } 79 80 //! Returns the dot product with another vector. dotProduct(const vec3d<T> & other)81 T dotProduct(const vec3d<T>& other) const 82 { 83 return X*other.X + Y*other.Y + Z*other.Z; 84 } 85 86 //! Returns distance from an other point. 87 /** Here, the vector is interpreted as point in 3 dimensional space. */ getDistanceFrom(const vec3d<T> & other)88 ik_f64 getDistanceFrom(const vec3d<T>& other) const 89 { 90 ik_f64 vx = X - other.X; ik_f64 vy = Y - other.Y; ik_f64 vz = Z - other.Z; 91 return sqrt(vx*vx + vy*vy + vz*vz); 92 } 93 94 //! Returns squared distance from an other point. 95 /** Here, the vector is interpreted as point in 3 dimensional space. */ getDistanceFromSQ(const vec3d<T> & other)96 ik_f32 getDistanceFromSQ(const vec3d<T>& other) const 97 { 98 ik_f32 vx = X - other.X; ik_f32 vy = Y - other.Y; ik_f32 vz = Z - other.Z; 99 return (vx*vx + vy*vy + vz*vz); 100 } 101 102 //! Calculates the cross product with another vector crossProduct(const vec3d<T> & p)103 vec3d<T> crossProduct(const vec3d<T>& p) const 104 { 105 return vec3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X); 106 } 107 108 //! Returns if this vector interpreted as a point is on a line between two other points. 109 /** It is assumed that the point is on the line. */ isBetweenPoints(const vec3d<T> & begin,const vec3d<T> & end)110 bool isBetweenPoints(const vec3d<T>& begin, const vec3d<T>& end) const 111 { 112 ik_f32 f = (ik_f32)(end - begin).getLengthSQ(); 113 return (ik_f32)getDistanceFromSQ(begin) < f && 114 (ik_f32)getDistanceFromSQ(end) < f; 115 } 116 117 //! Normalizes the vector. normalize()118 vec3d<T>& normalize() 119 { 120 T l = (T)getLength(); 121 if (l == 0) 122 return *this; 123 124 l = (T)1.0 / l; 125 X *= l; 126 Y *= l; 127 Z *= l; 128 return *this; 129 } 130 131 //! Sets the lenght of the vector to a new value setLength(T newlength)132 void setLength(T newlength) 133 { 134 normalize(); 135 *this *= newlength; 136 } 137 138 //! Inverts the vector. invert()139 void invert() 140 { 141 X *= -1.0f; 142 Y *= -1.0f; 143 Z *= -1.0f; 144 } 145 146 //! Rotates the vector by a specified number of degrees around the Y 147 //! axis and the specified center. 148 //! \param degrees: Number of degrees to rotate around the Y axis. 149 //! \param center: The center of the rotation. rotateXZBy(ik_f64 degrees,const vec3d<T> & center)150 void rotateXZBy(ik_f64 degrees, const vec3d<T>& center) 151 { 152 degrees *= IK_DEGTORAD64; 153 T cs = (T)cos(degrees); 154 T sn = (T)sin(degrees); 155 X -= center.X; 156 Z -= center.Z; 157 set(X*cs - Z*sn, Y, X*sn + Z*cs); 158 X += center.X; 159 Z += center.Z; 160 } 161 162 //! Rotates the vector by a specified number of degrees around the Z 163 //! axis and the specified center. 164 //! \param degrees: Number of degrees to rotate around the Z axis. 165 //! \param center: The center of the rotation. rotateXYBy(ik_f64 degrees,const vec3d<T> & center)166 void rotateXYBy(ik_f64 degrees, const vec3d<T>& center) 167 { 168 degrees *= IK_DEGTORAD64; 169 T cs = (T)cos(degrees); 170 T sn = (T)sin(degrees); 171 X -= center.X; 172 Y -= center.Y; 173 set(X*cs - Y*sn, X*sn + Y*cs, Z); 174 X += center.X; 175 Y += center.Y; 176 } 177 178 //! Rotates the vector by a specified number of degrees around the X 179 //! axis and the specified center. 180 //! \param degrees: Number of degrees to rotate around the X axis. 181 //! \param center: The center of the rotation. rotateYZBy(ik_f64 degrees,const vec3d<T> & center)182 void rotateYZBy(ik_f64 degrees, const vec3d<T>& center) 183 { 184 degrees *= IK_DEGTORAD64; 185 T cs = (T)cos(degrees); 186 T sn = (T)sin(degrees); 187 Z -= center.Z; 188 Y -= center.Y; 189 set(X, Y*cs - Z*sn, Y*sn + Z*cs); 190 Z += center.Z; 191 Y += center.Y; 192 } 193 194 //! Returns interpolated vector. 195 /** \param other: other vector to interpolate between 196 \param d: value between 0.0f and 1.0f. */ getInterpolated(const vec3d<T> & other,ik_f32 d)197 vec3d<T> getInterpolated(const vec3d<T>& other, ik_f32 d) const 198 { 199 ik_f32 inv = 1.0f - d; 200 return vec3d<T>(other.X*inv + X*d, 201 other.Y*inv + Y*d, 202 other.Z*inv + Z*d); 203 } 204 205 //! Gets the Y and Z rotations of a vector. 206 /** Thanks to Arras on the Irrlicht forums to add this method. 207 \return A vector representing the rotation in degrees of 208 this vector. The Z component of the vector will always be 0. */ getHorizontalAngle()209 vec3d<T> getHorizontalAngle() 210 { 211 vec3d<T> angle; 212 213 angle.Y = (T)atan2(X, Z); 214 angle.Y *= (ik_f32)IK_RADTODEG; 215 216 if (angle.Y < 0.0f) angle.Y += 360.0f; 217 if (angle.Y >= 360.0f) angle.Y -= 360.0f; 218 219 ik_f32 z1 = (T)sqrt(X*X + Z*Z); 220 221 angle.X = (T)atan2(z1, Y); 222 angle.X *= (ik_f32)IK_RADTODEG; 223 angle.X -= 90.0f; 224 225 if (angle.X < 0.0f) angle.X += 360.0f; 226 if (angle.X >= 360) angle.X -= 360.0f; 227 228 return angle; 229 } 230 231 //! Fills an array of 4 values with the vector data (usually floats). 232 /** Useful for setting in shader constants for example. The fourth value 233 will always be 0. */ getAs4Values(T * array)234 void getAs4Values(T* array) 235 { 236 array[0] = X; 237 array[1] = Y; 238 array[2] = Z; 239 array[3] = 0; 240 } 241 242 243 // member variables 244 245 T X, Y, Z; 246 }; 247 248 249 //! Typedef for a ik_f32 3d vector, a vector using floats for X, Y and Z 250 typedef vec3d<ik_f32> vec3df; 251 252 //! Typedef for an integer 3d vector, a vector using ints for X, Y and Z 253 typedef vec3d<ik_s32> vec3di; 254 255 template<class S, class T> vec3d<T> operator*(const S scalar, const vec3d<T>& vector) { return vector*scalar; } 256 257 } // end namespace irrklang 258 259 260 #endif 261 262