![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsVector4.cpp 00003 ******************************************************************************/ 00007 /****************************************************************************** 00008 SUKITTI PUNAK (09/10/2004) 00009 UPDATE (08/27/2010) 00010 ******************************************************************************/ 00011 #include "TAPsVector4.hpp" 00012 // Using Inclusion Model (i.e. definitions are included in declarations) 00013 // (this name.cpp is included in name.hpp) 00014 // Each friend is defined directly inside its declaration. 00015 00016 BEGIN_NAMESPACE_TAPs 00017 //============================================================================= 00018 // Constructors 00019 //----------------------------------------------------------------------------- 00020 template <typename T> 00021 Vector4<T>::Vector4 () 00022 { this->t[0] = this->t[1] = this->t[2] = this->t[3] = Math<T>::ZERO; } 00023 //----------------------------------------------------------------------------- 00024 template <typename T> 00025 Vector4<T>::Vector4 ( T const af[4] ) 00026 { this->t[0] = af[0]; this->t[1] = af[1]; this->t[2] = af[2]; this->t[3] = af[3]; } 00027 //----------------------------------------------------------------------------- 00028 template <typename T> 00029 Vector4<T>::Vector4 ( Vector4<T> const &v ) 00030 { this->t[0] = v.t[0]; this->t[1] = v.t[1]; this->t[2] = v.t[2]; this->t[3] = v.t[3]; } 00031 //----------------------------------------------------------------------------- 00032 template <typename T> 00033 Vector4<T>::Vector4 ( Vector3<T> const &v, T w ) 00034 { this->t[0] = v.GetX(); this->t[1] = v.GetY(); this->t[2] = v.GetZ(); this->t[3] = w; } 00035 //----------------------------------------------------------------------------- 00036 template <typename T> 00037 Vector4<T>::Vector4 ( T x, T y, T z, T w ) 00038 { this->t[0] = x; this->t[1] = y; this->t[2] = z; this->t[3] = w; } 00039 //----------------------------------------------------------------------------- 00040 //============================================================================= 00041 // Assignment Operator 00042 //----------------------------------------------------------------------------- 00043 template <typename T> 00044 Vector4<T> & Vector4<T>::operator= ( Vector4<T> const &v ) 00045 { 00046 this->t[0] = v.t[0]; 00047 this->t[1] = v.t[1]; 00048 this->t[2] = v.t[2]; 00049 this->t[3] = v.t[3]; 00050 return *this; 00051 } 00052 //============================================================================= 00053 // Comparison Operators 00054 //----------------------------------------------------------------------------- 00055 template <typename T> 00056 bool Vector4<T>::operator== ( Vector4<T> const &v ) const 00057 { return ( this->t[0] == v.t[0] && this->t[1] == v.t[1] && this->t[2] == v.t[2] && this->t[3] == v.t[3] ); } 00058 //----------------------------------------------------------------------------- 00059 template <typename T> 00060 bool Vector4<T>::operator!= ( Vector4<T> const &v ) const 00061 { return !( *this == v ); } 00062 //----------------------------------------------------------------------------- 00063 template <typename T> 00064 bool Vector4<T>::operator<= ( Vector4<T> const &v ) const 00065 { return ( this->t[0] <= v.t[0] && this->t[1] <= v.t[1] && this->t[2] <= v.t[2] && this->t[3] <= v.t[3] ); } 00066 //----------------------------------------------------------------------------- 00067 template <typename T> 00068 bool Vector4<T>::operator> ( Vector4<T> const &v ) const 00069 { return !( *this <= v ); } 00070 //----------------------------------------------------------------------------- 00071 template <typename T> 00072 bool Vector4<T>::operator>= ( Vector4<T> const &v ) const 00073 { return ( this->t[0] >= v.t[0] && this->t[1] >= v.t[1] && this->t[2] >= v.t[2] && this->t[3] >= v.t[3] ); } 00074 //----------------------------------------------------------------------------- 00075 template <typename T> 00076 bool Vector4<T>::operator< ( Vector4<T> const &v ) const 00077 { return !( *this >= v ); } 00078 //----------------------------------------------------------------------------- 00079 //============================================================================= 00080 // Arithmetic Operations 00081 //----------------------------------------------------------------------------- 00082 template <typename T> 00083 Vector4<T> Vector4<T>::operator- () const 00084 { return Vector4<T>( -(this->t[0]), -(this->t[1]), -(this->t[2]), -(this->t[3]) ); } 00085 //----------------------------------------------------------------------------- 00086 template <typename T> 00087 Vector4<T> Vector4<T>::operator+ ( Vector4<T> const &v ) const 00088 { return Vector4<T>( this->t[0]+v.t[0], this->t[1]+v.t[1], this->t[2]+v.t[2], this->t[3]+v.t[3] ); } 00089 //----------------------------------------------------------------------------- 00090 template <typename T> 00091 Vector4<T> Vector4<T>::operator- ( Vector4<T> const &v ) const 00092 { return Vector4<T>( this->t[0]-v.t[0], this->t[1]-v.t[1], this->t[2]-v.t[2], this->t[3]-v.t[3] ); } 00093 //----------------------------------------------------------------------------- 00094 template <typename T> 00095 Vector4<T> Vector4<T>::operator* ( T s ) const 00096 { return Vector4<T>( this->t[0]*s, this->t[1]*s, this->t[2]*s, this->t[3]*s ); } 00097 //----------------------------------------------------------------------------- 00098 template <typename T> 00099 Vector4<T> Vector4<T>::operator/ ( T s ) const 00100 { 00101 if ( s != Math<T>::ZERO ) 00102 return Vector4<T>( this->t[0]/s, this->t[1]/s, this->t[2]/s, this->t[3]/s ); 00103 else 00104 return Vector4<T>( Math<T>::INFINITY, Math<T>::INFINITY, 00105 Math<T>::INFINITY, Math<T>::INFINITY 00106 ); 00107 } 00108 //----------------------------------------------------------------------------- 00109 //============================================================================= 00110 // Arithmetic Update Operators 00111 //----------------------------------------------------------------------------- 00112 template <typename T> 00113 Vector4<T> & Vector4<T>::operator+= ( Vector4<T> const &v ) 00114 { 00115 this->t[0] += v.t[0]; 00116 this->t[1] += v.t[1]; 00117 this->t[2] += v.t[2]; 00118 this->t[3] += v.t[3]; 00119 return *this; 00120 } 00121 //----------------------------------------------------------------------------- 00122 template <typename T> 00123 Vector4<T> & Vector4<T>::operator-= ( Vector4<T> const &v ) 00124 { 00125 this->t[0] -= v.t[0]; 00126 this->t[1] -= v.t[1]; 00127 this->t[2] -= v.t[2]; 00128 this->t[3] -= v.t[3]; 00129 return *this; 00130 } 00131 //----------------------------------------------------------------------------- 00132 template <typename T> 00133 Vector4<T> & Vector4<T>::operator*= ( T s ) 00134 { 00135 this->t[0] *= s; 00136 this->t[1] *= s; 00137 this->t[2] *= s; 00138 this->t[3] *= s; 00139 return *this; 00140 } 00141 //----------------------------------------------------------------------------- 00142 template <typename T> 00143 Vector4<T> & Vector4<T>::operator/= ( T s ) 00144 { 00145 if ( s != Math<T>::ZERO ) 00146 { 00147 this->t[0] /= s; 00148 this->t[1] /= s; 00149 this->t[2] /= s; 00150 this->t[3] /= s; 00151 } 00152 else 00153 { 00154 this->t[0] = Math<T>::INFINITY; 00155 this->t[1] = Math<T>::INFINITY; 00156 this->t[2] = Math<T>::INFINITY; 00157 this->t[3] = Math<T>::INFINITY; 00158 } 00159 return *this; 00160 } 00161 //----------------------------------------------------------------------------- 00162 //============================================================================= 00163 // Vector Operations 00164 //----------------------------------------------------------------------------- 00165 template <typename T> 00166 T Vector4<T>::Length () const 00167 { return Math<T>::Sqrt( (this->t[0])*(this->t[0]) + (this->t[1])*(this->t[1]) + (this->t[2])*(this->t[2]) + (this->t[3])*(this->t[3]) ); } 00168 //----------------------------------------------------------------------------- 00169 template <typename T> 00170 T Vector4<T>::Magnitude () const 00171 { return Math<T>::Sqrt( (this->t[0])*(this->t[0]) + (this->t[1])*(this->t[1]) + (this->t[2])*(this->t[2]) + (this->t[3])*(this->t[3]) ); } 00172 //----------------------------------------------------------------------------- 00173 template <typename T> 00174 T Vector4<T>::SquaredLength () const 00175 { return ( (this->t[0])*(this->t[0]) + (this->t[1])*(this->t[1]) + (this->t[2])*(this->t[2]) + (this->t[3])*(this->t[3]) ); } 00176 //----------------------------------------------------------------------------- 00177 template <typename T> 00178 Vector4<T> Vector4<T>::GetUnit ( T tolerance ) const 00179 { 00180 T fLength = Vector4<T>::Length(); 00181 if ( fLength > tolerance ) 00182 return Vector4<T>( this->t[0]/fLength, this->t[1]/fLength, this->t[2]/fLength, this->t[3]/fLength ); 00183 else 00184 return Vector4<T>( Math<T>::ZERO, Math<T>::ZERO, 00185 Math<T>::ZERO, Math<T>::ZERO 00186 ); 00187 } 00188 //----------------------------------------------------------------------------- 00189 template <typename T> 00190 Vector4<T> & Vector4<T>::Normalized ( T tolerance ) 00191 { 00192 T fLength = Vector4<T>::Length(); 00193 if ( fLength > tolerance ) 00194 { 00195 this->t[0] /= fLength; 00196 this->t[1] /= fLength; 00197 this->t[2] /= fLength; 00198 this->t[3] /= fLength; 00199 } 00200 else 00201 { 00202 this->t[0] = Math<T>::ZERO; 00203 this->t[1] = Math<T>::ZERO; 00204 this->t[2] = Math<T>::ZERO; 00205 this->t[3] = Math<T>::ZERO; 00206 } 00207 return *this; 00208 } 00209 //----------------------------------------------------------------------------- 00210 template <typename T> 00211 T Vector4<T>::Dot ( Vector4<T> const &v ) const 00212 { return this->t[0]*v.t[0] + this->t[1]*v.t[1] + this->t[2]*v.t[2] + this->t[3]*v.t[3]; } 00213 //----------------------------------------------------------------------------- 00214 template <typename T> 00215 T Vector4<T>::operator* ( Vector4<T> const &v ) const 00216 { return this->Dot(v); } 00217 //----------------------------------------------------------------------------- 00218 template <typename T> 00219 Vector4<T> Vector4<T>::ComponentWiseMult ( Vector4<T> const &v ) const 00220 { 00221 Vector4<T> R; 00222 R.t[0] = t[0] * v.t[0]; 00223 R.t[1] = t[1] * v.t[1]; 00224 R.t[2] = t[2] * v.t[2]; 00225 R.t[3] = t[3] * v.t[3]; 00226 return R; 00227 } 00228 //----------------------------------------------------------------------------- 00229 template <typename T> 00230 Vector4<T> Vector4<T>::ComponentWiseDivs ( Vector4<T> const &v ) const 00231 { 00232 Vector4<T> R; 00233 R.t[0] = t[0] / v.t[0]; 00234 R.t[1] = t[1] / v.t[1]; 00235 R.t[2] = t[2] / v.t[2]; 00236 R.t[3] = t[3] / v.t[3]; 00237 return R; 00238 } 00239 //----------------------------------------------------------------------------- 00240 template <typename T> 00241 void Vector4<T>::UpdateByComponentWiseMult ( Vector4<T> const &v ) 00242 { 00243 t[0] *= v.t[0]; 00244 t[1] *= v.t[1]; 00245 t[2] *= v.t[2]; 00246 t[3] *= v.t[3]; 00247 } 00248 //----------------------------------------------------------------------------- 00249 template <typename T> 00250 void Vector4<T>::UpdateByComponentWiseDivs ( Vector4<T> const &v ) 00251 { 00252 t[0] /= v.t[0]; 00253 t[1] /= v.t[1]; 00254 t[2] /= v.t[2]; 00255 t[3] /= v.t[3]; 00256 } 00257 //----------------------------------------------------------------------------- 00258 //============================================================================= 00259 // Vector * ColMatrix 00260 //----------------------------------------------------------------------------- 00261 template <typename T> 00262 Vector4<T> Vector4<T>::operator* ( ColMatrix4x4<T> const &M ) const 00263 { 00264 return Vector4<T>( 00265 this->t[0]*M[ 0] + this->t[1]*M[ 1] + this->t[2]*M[ 2] + this->t[3]*M[ 3], 00266 this->t[0]*M[ 4] + this->t[1]*M[ 5] + this->t[2]*M[ 6] + this->t[3]*M[ 7], 00267 this->t[0]*M[ 8] + this->t[1]*M[ 9] + this->t[2]*M[10] + this->t[3]*M[11], 00268 this->t[0]*M[12] + this->t[1]*M[13] + this->t[2]*M[14] + this->t[3]*M[15] ); 00269 } 00270 //----------------------------------------------------------------------------- 00271 //============================================================================= 00272 END_NAMESPACE_TAPs 00273 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00274 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8