nux-1.16.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #ifndef VECTOR3_H 00024 #define VECTOR3_H 00025 00026 #include "../Exception.h" 00027 00028 #include "Vector4.h" 00029 00030 namespace nux 00031 { 00032 00033 template <typename T> 00034 class Vec3 00035 { 00036 public: 00037 inline Vec3(); 00038 inline Vec3 (const T &, const T &, const T &); 00039 inline ~Vec3(); 00040 inline Vec3 (const Vec3 &); 00041 00042 inline Vec3<T>& operator = (const Vec3<T>&); 00043 inline Vec3<T>& operator = (const Vec4<T>&); 00044 00045 inline t_bool operator == (const Vec3<T>&) const; 00046 inline t_bool operator != (const Vec3<T>&) const; 00047 inline Vec3<T> operator + (const Vec3<T>&) const; 00048 inline Vec3<T> operator * (const Vec3<T>&) const; 00049 inline Vec3<T> operator - (const Vec3<T>&) const; 00050 inline Vec3<T> operator - () const; 00051 00052 inline Vec3<T>& operator *= (const Vec3<T>&); 00053 inline Vec3<T>& operator += (const Vec3<T>&); 00054 inline Vec3<T>& operator -= (const Vec3<T>&); 00055 00056 inline Vec3<T> operator / (const T &) const; 00057 inline Vec3<T> operator * (const T &) const; 00058 inline Vec3<T>& operator /= (const T &); 00059 inline Vec3<T>& operator *= (const T &); 00060 00061 inline T &operator [] (int i); 00062 inline const T &operator [] (int i) const; 00063 00064 inline T Length() const; 00065 inline T LengthSquared() const; 00066 inline T DotProduct (const Vec3<T>&) const; 00067 inline Vec3 CrossProduct (const Vec3<T>&) const; 00068 inline void Normalize(); 00069 00070 template <typename U> friend Vec3<U> operator* (const U &, const Vec3<U>&); 00071 00072 //friend Vec3<T> operator * (T, Vec3&); 00073 00074 T x, y, z; 00075 }; 00076 00077 template <typename T> 00078 inline Vec3<T>::Vec3() 00079 { 00080 x = 0; 00081 y = 0; 00082 z = 0; 00083 } 00084 00085 template <typename T> 00086 inline Vec3<T>::~Vec3() 00087 { 00088 00089 } 00090 00091 template <typename T> 00092 inline Vec3<T>::Vec3 (const T &fx, const T &fy, const T &fz) 00093 { 00094 x = fx; 00095 y = fy; 00096 z = fz; 00097 } 00098 00099 //Vec3::Vec3(t_double fx, t_double fy, t_double fz) 00100 //{ 00101 // x = T(fx); 00102 // y = T(fy); 00103 // z = T(fz); 00104 //} 00105 // 00106 //Vec3::Vec3(t_int fx, t_int fy, t_int fz) 00107 //{ 00108 // x = T(fx); 00109 // y = T(fy); 00110 // z = T(fz); 00111 //} 00112 00113 template <typename T> 00114 Vec3<T>::Vec3 (const Vec3<T>& v) 00115 { 00116 x = v.x; 00117 y = v.y; 00118 z = v.z; 00119 } 00120 00121 template <typename T> 00122 Vec3<T>& Vec3<T>::operator = (const Vec3<T>& v) 00123 { 00124 x = v.x; 00125 y = v.y; 00126 z = v.z; 00127 return *this; 00128 } 00129 00130 template <typename T> 00131 Vec3<T>& Vec3<T>::operator = (const Vec4<T>& v) 00132 { 00133 x = v.x; 00134 y = v.y; 00135 z = v.z; 00136 return *this; 00137 } 00138 00139 template <typename T> 00140 t_bool Vec3<T>::operator == (const Vec3<T>& v) const 00141 { 00142 if ( (x == v.x) && 00143 (y == v.y) && 00144 (z == v.z) ) 00145 { 00146 return true; 00147 } 00148 00149 return false; 00150 } 00151 00152 template <typename T> 00153 t_bool Vec3<T>::operator != (const Vec3<T>& v) const 00154 { 00155 return ! (*this == v); 00156 } 00157 00158 template <typename T> 00159 Vec3<T> Vec3<T>::operator + (const Vec3<T>& v) const 00160 { 00161 return Vec3<T> (x + v.x, y + v.y, z + v.z); 00162 } 00163 00164 template <typename T> 00165 Vec3<T> Vec3<T>::operator * (const Vec3 &v) const 00166 { 00167 return Vec3<T> (x * v.x, y * v.y, z * v.z); 00168 } 00169 00170 template <typename T> 00171 Vec3<T> Vec3<T>::operator - (const Vec3 &v) const 00172 { 00173 return Vec3<T> (x - v.x, y - v.y, z - v.z); 00174 } 00175 00176 template <typename T> 00177 Vec3<T> Vec3<T>::operator - () const 00178 { 00179 return Vec3 (-x, -y, -z); 00180 } 00181 00182 template <typename T> 00183 Vec3<T>& Vec3<T>::operator *= (const Vec3 &v) 00184 { 00185 x *= v.x; 00186 y *= v.y; 00187 z *= v.z; 00188 return *this; 00189 } 00190 00191 template <typename T> 00192 Vec3<T>& Vec3<T>::operator += (const Vec3 &v) 00193 { 00194 x += v.x; 00195 y += v.y; 00196 z += v.z; 00197 return *this; 00198 } 00199 00200 template <typename T> 00201 Vec3<T>& Vec3<T>::operator -= (const Vec3 &v) 00202 { 00203 x -= v.x; 00204 y -= v.y; 00205 z -= v.z; 00206 return *this; 00207 } 00208 00209 template <typename T> 00210 Vec3<T> Vec3<T>::operator / (const T &f) const 00211 { 00212 if (f == 0) 00213 { 00214 throw DivisionByZeroException(); 00215 } 00216 00217 return Vec3<T> (x / f, y / f, z / f); 00218 } 00219 00220 template <typename T> 00221 Vec3<T> Vec3<T>::operator * (const T &f) const 00222 { 00223 return Vec3<T> (x * f, y * f, z * f); 00224 } 00225 00226 template <typename T> 00227 Vec3<T>& Vec3<T>::operator /= (const T &f) 00228 { 00229 if (f == 0) 00230 { 00231 throw DivisionByZeroException(); 00232 } 00233 00234 x = x / f; 00235 y = y / f; 00236 z = z / f; 00237 return *this; 00238 } 00239 00240 template <typename T> 00241 Vec3<T>& Vec3<T>::operator *= (const T &f) 00242 { 00243 x = x * f; 00244 y = y * f; 00245 z = z * f; 00246 return *this; 00247 } 00248 00250 template <typename T> 00251 T &Vec3<T>::operator [] (int i) 00252 { 00253 assert (i >= 0); 00254 assert (i <= 2); 00255 return * (&x + i); 00256 } 00257 00259 template <typename T> 00260 const T &Vec3<T>::operator [] (int i) const 00261 { 00262 assert (i >= 0); 00263 assert (i <= 2); 00264 return * (&x + i); 00265 } 00266 00267 template <typename T> 00268 T Vec3<T>::Length() const 00269 { 00270 return sqrt (x * x + y * y + z * z); 00271 } 00272 00273 template <typename T> 00274 T Vec3<T>::LengthSquared() const 00275 { 00276 return (x * x + y * y + z * z); 00277 } 00278 00279 template <typename T> 00280 T Vec3<T>::DotProduct (const Vec3<T>& v) const 00281 { 00282 return x * v.x + y * v.y + z * v.z; 00283 } 00284 00285 template <typename T> 00286 Vec3<T> Vec3<T>::CrossProduct (const Vec3<T>& v) const 00287 { 00288 return Vec3<T> (y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); 00289 } 00290 00291 template <typename T> 00292 void Vec3<T>::Normalize() 00293 { 00294 T l; 00295 l = Length(); 00296 00297 if (l == 0) 00298 { 00299 throw DivisionByZeroException(); 00300 } 00301 00302 x = x / l; 00303 y = y / l; 00304 z = z / l; 00305 } 00306 00307 template <typename T> 00308 T DotProduct (const Vec3<T>& lhs, const Vec3<T>& rhs) 00309 { 00310 T out; 00311 out = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; 00312 return out; 00313 } 00314 00315 template <typename T> 00316 const Vec3<T> CrossProduct (const Vec3<T>& lhs, const Vec3<T>& rhs) 00317 { 00318 Vec3<T> out; 00319 out.x = lhs.y * rhs.z - lhs.z * rhs.y; 00320 out.y = lhs.z * rhs.x - lhs.x * rhs.z; 00321 out.z = lhs.x * rhs.y - lhs.y * rhs.x; 00322 00323 return out; 00324 } 00325 00326 template <typename U> 00327 inline Vec3<U> operator * (const U &f, const Vec3<U>& v) 00328 { 00329 return v * f; 00330 } 00331 00332 typedef Vec3<float> Vector3; 00333 typedef Vec3<float> Vertex3; 00334 00335 } 00336 00337 #endif // VECTOR3_H