00001 // Copyright (C) 2002-2018 Nikolaus Gebhardt 00002 // This file is part of the "irrKlang" library. 00003 // For conditions of distribution and use, see copyright notice in irrKlang.h 00004 00005 #ifndef __IRR_IRRKLANG_VEC_3D_H_INCLUDED__ 00006 #define __IRR_IRRKLANG_VEC_3D_H_INCLUDED__ 00007 00008 #include <math.h> 00009 #include "ik_irrKlangTypes.h" 00010 00011 00012 namespace irrklang 00013 { 00014 00016 template <class T> 00017 class vec3d 00018 { 00019 public: 00020 00021 vec3d(): X(0), Y(0), Z(0) {}; 00022 vec3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}; 00023 vec3d(const vec3d<T>& other) :X(other.X), Y(other.Y), Z(other.Z) {}; 00024 00026 #ifdef __IRR_POINT_3D_H_INCLUDED__ 00027 template<class B> 00028 vec3d(const B& other) :X(other.X), Y(other.Y), Z(other.Z) {}; 00029 #endif // __IRR_POINT_3D_H_INCLUDED__ 00030 00031 // operators 00032 00033 vec3d<T> operator-() const { return vec3d<T>(-X, -Y, -Z); } 00034 00035 vec3d<T>& operator=(const vec3d<T>& other) { X = other.X; Y = other.Y; Z = other.Z; return *this; } 00036 00037 vec3d<T> operator+(const vec3d<T>& other) const { return vec3d<T>(X + other.X, Y + other.Y, Z + other.Z); } 00038 vec3d<T>& operator+=(const vec3d<T>& other) { X+=other.X; Y+=other.Y; Z+=other.Z; return *this; } 00039 00040 vec3d<T> operator-(const vec3d<T>& other) const { return vec3d<T>(X - other.X, Y - other.Y, Z - other.Z); } 00041 vec3d<T>& operator-=(const vec3d<T>& other) { X-=other.X; Y-=other.Y; Z-=other.Z; return *this; } 00042 00043 vec3d<T> operator*(const vec3d<T>& other) const { return vec3d<T>(X * other.X, Y * other.Y, Z * other.Z); } 00044 vec3d<T>& operator*=(const vec3d<T>& other) { X*=other.X; Y*=other.Y; Z*=other.Z; return *this; } 00045 vec3d<T> operator*(const T v) const { return vec3d<T>(X * v, Y * v, Z * v); } 00046 vec3d<T>& operator*=(const T v) { X*=v; Y*=v; Z*=v; return *this; } 00047 00048 vec3d<T> operator/(const vec3d<T>& other) const { return vec3d<T>(X / other.X, Y / other.Y, Z / other.Z); } 00049 vec3d<T>& operator/=(const vec3d<T>& other) { X/=other.X; Y/=other.Y; Z/=other.Z; return *this; } 00050 vec3d<T> operator/(const T v) const { T i=(T)1.0/v; return vec3d<T>(X * i, Y * i, Z * i); } 00051 vec3d<T>& operator/=(const T v) { T i=(T)1.0/v; X*=i; Y*=i; Z*=i; return *this; } 00052 00053 bool operator<=(const vec3d<T>&other) const { return X<=other.X && Y<=other.Y && Z<=other.Z;}; 00054 bool operator>=(const vec3d<T>&other) const { return X>=other.X && Y>=other.Y && Z>=other.Z;}; 00055 00056 bool operator==(const vec3d<T>& other) const { return other.X==X && other.Y==Y && other.Z==Z; } 00057 bool operator!=(const vec3d<T>& other) const { return other.X!=X || other.Y!=Y || other.Z!=Z; } 00058 00059 // functions 00060 00062 bool equals(const vec3d<T>& other) 00063 { 00064 return equalsfloat(X, other.X) && 00065 equalsfloat(Y, other.Y) && 00066 equalsfloat(Z, other.Z); 00067 } 00068 00069 void set(const T nx, const T ny, const T nz) {X=nx; Y=ny; Z=nz; } 00070 void set(const vec3d<T>& p) { X=p.X; Y=p.Y; Z=p.Z;} 00071 00073 ik_f64 getLength() const { return sqrt(X*X + Y*Y + Z*Z); } 00074 00076 00078 ik_f64 getLengthSQ() const { return X*X + Y*Y + Z*Z; } 00079 00081 T dotProduct(const vec3d<T>& other) const 00082 { 00083 return X*other.X + Y*other.Y + Z*other.Z; 00084 } 00085 00087 00088 ik_f64 getDistanceFrom(const vec3d<T>& other) const 00089 { 00090 ik_f64 vx = X - other.X; ik_f64 vy = Y - other.Y; ik_f64 vz = Z - other.Z; 00091 return sqrt(vx*vx + vy*vy + vz*vz); 00092 } 00093 00095 00096 ik_f32 getDistanceFromSQ(const vec3d<T>& other) const 00097 { 00098 ik_f32 vx = X - other.X; ik_f32 vy = Y - other.Y; ik_f32 vz = Z - other.Z; 00099 return (vx*vx + vy*vy + vz*vz); 00100 } 00101 00103 vec3d<T> crossProduct(const vec3d<T>& p) const 00104 { 00105 return vec3d<T>(Y * p.Z - Z * p.Y, Z * p.X - X * p.Z, X * p.Y - Y * p.X); 00106 } 00107 00109 00110 bool isBetweenPoints(const vec3d<T>& begin, const vec3d<T>& end) const 00111 { 00112 ik_f32 f = (ik_f32)(end - begin).getLengthSQ(); 00113 return (ik_f32)getDistanceFromSQ(begin) < f && 00114 (ik_f32)getDistanceFromSQ(end) < f; 00115 } 00116 00118 vec3d<T>& normalize() 00119 { 00120 T l = (T)getLength(); 00121 if (l == 0) 00122 return *this; 00123 00124 l = (T)1.0 / l; 00125 X *= l; 00126 Y *= l; 00127 Z *= l; 00128 return *this; 00129 } 00130 00132 void setLength(T newlength) 00133 { 00134 normalize(); 00135 *this *= newlength; 00136 } 00137 00139 void invert() 00140 { 00141 X *= -1.0f; 00142 Y *= -1.0f; 00143 Z *= -1.0f; 00144 } 00145 00150 void rotateXZBy(ik_f64 degrees, const vec3d<T>& center) 00151 { 00152 degrees *= IK_DEGTORAD64; 00153 T cs = (T)cos(degrees); 00154 T sn = (T)sin(degrees); 00155 X -= center.X; 00156 Z -= center.Z; 00157 set(X*cs - Z*sn, Y, X*sn + Z*cs); 00158 X += center.X; 00159 Z += center.Z; 00160 } 00161 00166 void rotateXYBy(ik_f64 degrees, const vec3d<T>& center) 00167 { 00168 degrees *= IK_DEGTORAD64; 00169 T cs = (T)cos(degrees); 00170 T sn = (T)sin(degrees); 00171 X -= center.X; 00172 Y -= center.Y; 00173 set(X*cs - Y*sn, X*sn + Y*cs, Z); 00174 X += center.X; 00175 Y += center.Y; 00176 } 00177 00182 void rotateYZBy(ik_f64 degrees, const vec3d<T>& center) 00183 { 00184 degrees *= IK_DEGTORAD64; 00185 T cs = (T)cos(degrees); 00186 T sn = (T)sin(degrees); 00187 Z -= center.Z; 00188 Y -= center.Y; 00189 set(X, Y*cs - Z*sn, Y*sn + Z*cs); 00190 Z += center.Z; 00191 Y += center.Y; 00192 } 00193 00195 00197 vec3d<T> getInterpolated(const vec3d<T>& other, ik_f32 d) const 00198 { 00199 ik_f32 inv = 1.0f - d; 00200 return vec3d<T>(other.X*inv + X*d, 00201 other.Y*inv + Y*d, 00202 other.Z*inv + Z*d); 00203 } 00204 00206 00209 vec3d<T> getHorizontalAngle() 00210 { 00211 vec3d<T> angle; 00212 00213 angle.Y = (T)atan2(X, Z); 00214 angle.Y *= (ik_f32)IK_RADTODEG; 00215 00216 if (angle.Y < 0.0f) angle.Y += 360.0f; 00217 if (angle.Y >= 360.0f) angle.Y -= 360.0f; 00218 00219 ik_f32 z1 = (T)sqrt(X*X + Z*Z); 00220 00221 angle.X = (T)atan2(z1, Y); 00222 angle.X *= (ik_f32)IK_RADTODEG; 00223 angle.X -= 90.0f; 00224 00225 if (angle.X < 0.0f) angle.X += 360.0f; 00226 if (angle.X >= 360) angle.X -= 360.0f; 00227 00228 return angle; 00229 } 00230 00232 00234 void getAs4Values(T* array) 00235 { 00236 array[0] = X; 00237 array[1] = Y; 00238 array[2] = Z; 00239 array[3] = 0; 00240 } 00241 00242 00243 // member variables 00244 00245 T X, Y, Z; 00246 }; 00247 00248 00250 typedef vec3d<ik_f32> vec3df; 00251 00253 typedef vec3d<ik_s32> vec3di; 00254 00255 template<class S, class T> vec3d<T> operator*(const S scalar, const vec3d<T>& vector) { return vector*scalar; } 00256 00257 } // end namespace irrklang 00258 00259 00260 #endif 00261
The irrKlang
Engine Documentation © 2003-2018 by Nikolaus Gebhardt. Generated
by Doxygen
(1.6.1) |