00001 #include "Vec3.h"
00002
00003 namespace animal {
00004 namespace octree {
00005
00006
00007
00008
00012 template<class DataType>
00013 inline
00014 Vec3<DataType>::Vec3()
00015 : x(0.0f), y(0.0f), z(0.0f)
00016 {
00017 }
00021 template<class DataType>
00022 inline Vec3<DataType>::Vec3(const DataType x,const DataType y,const DataType z)
00023 : x(x), y(y), z(z)
00024 {
00025 }
00032 template<class DataType>
00033 inline Vec3<DataType>::Vec3(const Vec3& v)
00034 : x(v[0]),y(v[1]),z(v[2])
00035 {
00036 }
00037
00038
00039
00040
00041
00042
00043
00044
00048 template<class DataType>
00049 inline void
00050 Vec3<DataType>::set(const DataType ax,const DataType ay,const DataType az)
00051 {
00052 x = ax;
00053 y = ay;
00054 z = az;
00055 }
00056 template<class DataType>
00057 inline void Vec3<DataType>::set(const Vec3& v)
00058 {
00059 this->set( v[0], v[1], v[2] );
00060 }
00061
00065 template<class DataType>
00066 inline Vec3<DataType>::operator const DataType*() const
00067 {
00068 return static_cast<const DataType*>(&x);
00069 }
00070
00071
00072
00076 template<class DataType>
00077 inline bool
00078 Vec3<DataType>::operator==(const Vec3& b) const
00079 {
00080 return (x == b.x &&
00081 y == b.y &&
00082 z == b.z);
00083 }
00087 template<class DataType>
00088 inline bool
00089 Vec3<DataType>::operator!=(const Vec3& b) const
00090 {
00091 return (x != b.x ||
00092 y != b.y ||
00093 z != b.z);
00094 }
00098 template<class DataType>
00099 inline bool
00100 Vec3<DataType>::operator<(const Vec3& b) const
00101 {
00102 return ((x < b.x) ||
00103 ((x == b.x) && y < b.y) ||
00104 ((x == b.x && y == b.y) && z < b.z));
00105 }
00110 template<class DataType>
00111 inline const DataType&
00112 Vec3<DataType>::operator[](const int i) const
00113 {
00114 return static_cast<const DataType*>(&x)[i] ;
00115 }
00120 template<class DataType>
00121 inline DataType&
00122 Vec3<DataType>::operator[](const int i)
00123 {
00124 return static_cast<DataType*>(&x)[i] ;
00125 }
00126
00127
00128
00129
00130
00131 template<class DataType>
00132 inline Vec3<DataType>& Vec3<DataType>::operator*=(const DataType& k)
00133 {
00134 this->x *= k;
00135 this->y *= k;
00136 this->z *= k;
00137 return *this;
00138 }
00139 template<class DataType>
00140 inline Vec3<DataType>& Vec3<DataType>::operator/=(const DataType& k)
00141 {
00142 this->x /= k;
00143 this->y /= k;
00144 this->z /= k;
00145 return *this;
00146 }
00147
00148 template<class DataType>
00149 inline Vec3<DataType>& Vec3<DataType>::operator+=(const Vec3& a)
00150 {
00151 this->x += a.x;
00152 this->y += a.y;
00153 this->z += a.z;
00154 return *this;
00155 }
00156 template<class DataType>
00157 inline Vec3<DataType>& Vec3<DataType>::operator-=(const Vec3& a)
00158 {
00159 this->x -= a.x;
00160 this->y -= a.y;
00161 this->z -= a.z;
00162 return *this;
00163 }
00164
00165
00166 template<class DataType>
00167 Vec3<DataType> operator+(const Vec3<DataType>& a, const Vec3<DataType>& b)
00168 {
00169 return Vec3<DataType>( a.x+b.x, a.y+b.y, a.z+b.z );
00170 }
00171
00172 template<class DataType>
00173 Vec3<DataType> operator-(const Vec3<DataType>& a, const Vec3<DataType>& b)
00174 {
00175 return Vec3<DataType>( a.x-b.x, a.y-b.y, a.z-b.z );
00176 }
00177
00178 template<class DataType>
00179 Vec3<DataType> operator-(const Vec3<DataType>& a )
00180 {
00181 return Vec3<DataType>( -a.x, -a.y, -a.z );
00182 }
00183
00184
00185
00186
00187 template<class DataType>
00188 DataType operator*(const Vec3<DataType>& a, const Vec3<DataType>& b)
00189 {
00190 return b.x*a.x + b.y*a.y + b.z*a.z;
00191 }
00192 template<class DataType>
00193 Vec3<DataType> operator*(const DataType& k, const Vec3<DataType>& a)
00194 {
00195 return Vec3<DataType>( a.x*k, a.y*k, a.z*k );
00196 }
00197
00198 template<class DataType>
00199 Vec3<DataType> operator/(const Vec3<DataType>& a, const DataType& k)
00200 {
00201 return Vec3<DataType>( a.x/k, a.y/k, a.z/k );
00202 }
00203
00204
00205
00209 template<class DataType>
00210 Vec3<DataType> operator^(const Vec3<DataType>& a, const Vec3<DataType>& b)
00211 {
00212 return Vec3<DataType>(a.y*b.z - a.z*b.y,
00213 a.z*b.x - a.x*b.z,
00214 a.x*b.y - a.y*b.x);
00215 }
00216
00217
00218
00222 template<class DataType>
00223 inline DataType
00224 Vec3<DataType>::norm2() const
00225 {
00226 return x*x+y*y+z*z;
00227 }
00231 template<class DataType>
00232 inline DataType
00233 Vec3<DataType>::norm() const
00234 {
00235 return sqrtf(norm2()) ;
00236 }
00242 template<class DataType>
00243 inline void
00244 Vec3<DataType>::normalize()
00245 {
00246 const DataType f = norm();
00247 x /= f ;
00248 y /= f ;
00249 z /= f ;
00250 }
00251
00257 template<class DataType>
00258 inline Vec3<DataType>
00259 Vec3<DataType>::normalized() const
00260 {
00261 return *this / norm() ;
00262 }
00277 template<class DataType>
00278 inline Vec3<DataType>
00279 Vec3<DataType>::normalized(DataType& l) const
00280 {
00281 return *this / (l=norm()) ;
00282 }
00286 template<class DataType>
00287 inline Vec3<DataType>
00288 Vec3<DataType>::withX(const DataType theX) const
00289 {
00290 return Vec3(theX,y,z);
00291 }
00295 template<class DataType>
00296 inline Vec3<DataType>
00297 Vec3<DataType>::withY(const DataType theY) const
00298 {
00299 return Vec3(x,theY,z);
00300 }
00304 template<class DataType>
00305 inline Vec3<DataType>
00306 Vec3<DataType>::withZ(const DataType theZ) const
00307 {
00308 return Vec3(x,y,theZ);
00309 }
00310
00311 template<class DataType>
00312 inline const Vec3<DataType>&
00313 Vec3<DataType>::axis(const int i)
00314 {
00315 static const Vec3 axis[3] =
00316 {
00317 Vec3(1.0f,0.0f,0.0f),
00318 Vec3(0.0f,1.0f,0.0f),
00319 Vec3(0.0f,0.0f,1.0f)
00320 };
00321 return axis[i];
00322 };
00327 template<class DataType>
00328 inline Vec3<DataType>
00329 Vec3<DataType>::nonColinearVec() const
00330 {
00331 return ((*this)^Vec3<DataType>::axis(0)).norm2() > 0.0001f ?
00332 Vec3<DataType>::axis(0) :
00333 Vec3<DataType>::axis(1);
00334 }
00335
00336 }
00337 }