nux-1.14.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 VECTOR4_H 00024 #define VECTOR4_H 00025 00026 namespace nux 00027 { 00028 00029 template <typename T> 00030 class Vec4 00031 { 00032 public: 00033 inline Vec4<T>(); 00034 inline ~Vec4<T>(); 00035 inline Vec4<T> (const T &, const T &, const T &, const T &); 00036 00037 inline Vec4 (const Vec4<T>&); 00038 00039 inline Vec4<T>& operator = (const Vec4<T>&); 00040 inline t_bool operator == (const Vec4<T>&) const; 00041 inline t_bool operator != (const Vec4<T>&) const; 00042 inline Vec4<T> operator + (const Vec4<T>&) const; 00043 inline Vec4<T> operator * (const Vec4<T>&) const; 00044 inline Vec4<T> operator - (const Vec4<T>&) const; 00045 inline Vec4<T> operator - () const; 00046 00047 inline Vec4<T>& operator *= (const Vec4<T>&); 00048 inline Vec4<T>& operator += (const Vec4<T>&); 00049 inline Vec4<T>& operator -= (const Vec4<T>&); 00050 00051 inline Vec4<T> operator / (const T &) const; 00052 inline Vec4<T> operator * (const T &) const; 00053 inline Vec4<T>& operator /= (const T &); 00054 inline Vec4<T>& operator *= (const T &); 00055 00056 inline T &operator [] (int i); 00057 inline const T &operator [] (int i) const; 00058 00059 void divide_xyz_by_w(); 00060 00061 template <typename U> friend Vec4<U> operator* (const U &, const Vec4<U>&); 00062 00063 T x, y, z, w; 00064 }; 00065 00066 00067 template <typename T> 00068 inline Vec4<T>::Vec4() 00069 { 00070 x = 0; 00071 y = 0; 00072 z = 0; 00073 w = 1; 00074 } 00075 00076 template <typename T> 00077 inline Vec4<T>::~Vec4() 00078 { 00079 00080 } 00081 00082 template <typename T> 00083 inline Vec4<T>::Vec4 (const T &fx, const T &fy, const T &fz, const T &fw) 00084 { 00085 x = fx; 00086 y = fy; 00087 z = fz; 00088 w = fw; 00089 } 00090 00091 //Vec4::Vec4(t_double fx, t_double fy, t_double fz, t_double fw) 00092 //{ 00093 // x = T(fx); 00094 // y = T(fy); 00095 // z = T(fz); 00096 // w = T(fw); 00097 //} 00098 // 00099 //Vec4::Vec4(t_int fx, t_int fy, t_int fz, t_int fw) 00100 //{ 00101 // x = T(fx); 00102 // y = T(fy); 00103 // z = T(fz); 00104 // w = T(fw); 00105 //} 00106 00107 template <typename T> 00108 inline Vec4<T>::Vec4 (const Vec4<T>& v) 00109 { 00110 x = v.x; 00111 y = v.y; 00112 z = v.z; 00113 w = v.w; 00114 } 00115 00116 template <typename T> 00117 inline Vec4<T>& Vec4<T>::operator= (const Vec4<T>& v) 00118 { 00119 x = v.x; 00120 y = v.y; 00121 z = v.z; 00122 w = v.w; 00123 return (*this); 00124 } 00125 00126 template <typename T> 00127 inline t_bool Vec4<T>::operator == (const Vec4<T>& v) const 00128 { 00129 if ( (x == v.x) && 00130 (y == v.y) && 00131 (z == v.z) && 00132 (w == v.w) ) 00133 { 00134 return true; 00135 } 00136 00137 return false; 00138 } 00139 00140 template <typename T> 00141 inline t_bool Vec4<T>::operator != (const Vec4<T>& v) const 00142 { 00143 return ! (*this == v); 00144 } 00145 00146 template <typename T> 00147 inline Vec4<T> Vec4<T>::operator+ (const Vec4<T>& v) const 00148 { 00149 return Vec4 (x + v.x, y + v.y, z + v.z, w + v.w); 00150 } 00151 00152 template <typename T> 00153 inline Vec4<T> Vec4<T>::operator* (const Vec4<T>& v) const 00154 { 00155 return Vec4<T> (x * v.x, y * v.y, z * v.z, w * v.w); 00156 } 00157 00158 template <typename T> 00159 inline Vec4<T> Vec4<T>::operator- (const Vec4<T>& v) const 00160 { 00161 return Vec4<T> (x - v.x, y - v.y, z - v.z, w - v.w); 00162 } 00163 00164 template <typename T> 00165 inline Vec4<T> Vec4<T>::operator- () const 00166 { 00167 //Do that for Matices too 00168 return Vec4<T> (-x, -y, -z, -w); 00169 } 00170 00171 template <typename T> 00172 inline Vec4<T>& Vec4<T>::operator*= (const Vec4<T>& v) 00173 { 00174 x *= v.x; 00175 y *= v.y; 00176 z *= v.z; 00177 w *= v.w; 00178 return *this; 00179 } 00180 00181 template <typename T> 00182 inline Vec4<T>& Vec4<T>::operator+= (const Vec4<T>& v) 00183 { 00184 x += v.x; 00185 y += v.y; 00186 z += v.z; 00187 w += v.w; 00188 return *this; 00189 } 00190 00191 template <typename T> 00192 inline Vec4<T>& Vec4<T>::operator-= (const Vec4<T>& v) 00193 { 00194 x -= v.x; 00195 y -= v.y; 00196 z -= v.z; 00197 w -= v.w; 00198 return *this; 00199 } 00200 00201 template <typename T> 00202 inline Vec4<T> Vec4<T>::operator / (const T &f) const 00203 { 00204 if (f == 0) 00205 { 00206 throw DivisionByZeroException(); 00207 } 00208 00209 return Vec4 (x / f, y / f, z / f, w / f); 00210 } 00211 00212 template <typename T> 00213 inline Vec4<T> Vec4<T>::operator * (const T &f) const 00214 { 00215 return Vec4<T> (x * f, y * f, z * f, w * f); 00216 } 00217 00218 template <typename T> 00219 inline Vec4<T>& Vec4<T>::operator /= (const T &f) 00220 { 00221 if (f == 0) 00222 { 00223 throw DivisionByZeroException(); 00224 } 00225 00226 x = x / f; 00227 y = y / f; 00228 z = z / f; 00229 w = w / f; 00230 return *this; 00231 } 00232 00233 template <typename T> 00234 inline Vec4<T>& Vec4<T>::operator *= (const T &f) 00235 { 00236 x = x * f; 00237 y = y * f; 00238 z = z * f; 00239 w = w * f; 00240 return *this; 00241 } 00242 00243 template <typename T> 00244 inline Vec4<T> operator* (T f , const Vec4<T>& v) 00245 { 00246 return Vec4<T> (f * v.x, f * v.y, f * v.z, f * v.w); 00247 } 00249 template <typename T> 00250 inline T &Vec4<T>::operator [] (int i) 00251 { 00252 assert (i >= 0); 00253 assert (i <= 3); 00254 return * (&x + i); 00255 } 00256 00258 template <typename T> 00259 inline const T &Vec4<T>::operator [] (int i) const 00260 { 00261 assert (i >= 0); 00262 assert (i <= 3); 00263 return * (&x + i); 00264 } 00265 00266 template <typename T> 00267 void Vec4<T>::divide_xyz_by_w() 00268 { 00269 if (w == 0) 00270 { 00271 throw DivisionByZeroException(); 00272 } 00273 00274 x = x / w; 00275 y = y / w; 00276 z = z / w; 00277 } 00278 00279 template <typename U> 00280 Vec4<U> operator* (const U &f, const Vec4<U>& v) 00281 { 00282 return v * f; 00283 } 00284 00285 typedef Vec4<float> Vector4; 00286 00287 } 00288 00289 #endif // VECTOR4_H 00290