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