/vol/vipdata/irtk/geometry++/include/irtkVector3D.h

00001 /*=========================================================================
00002  
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkVector3D.h 8 2009-03-02 16:12:58Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2009-03-02 16:12:58 +0000 (Mon, 02 Mar 2009) $
00008   Version   : $Revision: 8 $
00009   Changes   : $Author: dr $
00010  
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKVECTOR3D_H
00014 
00015 #define _IRTKVECTOR3D_H
00016 
00017 #include <cmath>
00018 #include <limits>
00019 
00020 #include <irtkObject.h>
00021 
00023 template <typename T> class irtkVector3D : public irtkObject
00024 {
00025 public:
00027   T _x;
00028 
00030   T _y;
00031 
00033   T _z;
00034 
00036   irtkVector3D(T x = 0, T y = 0, T z = 0);
00037 
00039   void Normalize();
00040 
00042   double Length() const;
00043 
00045   irtkVector3D operator*(double s);
00046 
00048   irtkVector3D operator+(const irtkVector3D& v);
00049 
00051   irtkVector3D operator-(const irtkVector3D& v);
00052 
00054   irtkVector3D& operator*=(double s);
00055 
00057   irtkVector3D& operator*=(const irtkVector3D& v);
00058 
00060   irtkVector3D& operator+=(const irtkVector3D& v);
00061 
00063   irtkVector3D& operator-=(const irtkVector3D& v);
00064 
00066   bool operator==(const irtkVector3D& v);
00067 
00069   bool operator!=(const irtkVector3D& v);
00070 
00072   bool operator<(const irtkVector3D& v);
00073 
00075   bool operator>(const irtkVector3D& v);
00076 
00078   bool operator<=(const irtkVector3D& v);
00079 
00081   bool operator>=(const irtkVector3D& v);
00082 
00084   irtkVector3D& operator/=(const irtkVector3D& v);
00085 
00087   irtkVector3D operator/(const irtkVector3D& v);
00088 
00090   irtkVector3D& operator/=(double s);
00091 
00093   irtkVector3D operator/(double s);
00094 
00096   static irtkVector3D CrossProduct(const irtkVector3D& v1, const irtkVector3D& v2);
00097 
00099   static double DotProduct(const irtkVector3D& v1, const irtkVector3D& v2);
00100 
00101 };
00102 
00103 template <typename T> inline irtkVector3D<T>::irtkVector3D(T x, T y, T z)
00104 {
00105   _x = x;
00106   _y = y;
00107   _z = z;
00108 }
00109 
00110 template <typename T> inline void irtkVector3D<T>::Normalize()
00111 {
00112   double length = sqrt(static_cast<double>(_x*_x + _y*_y + _z*_z));
00113 
00114   if (length != 0) {
00115     _x = static_cast<T>(_x/length);
00116     _y = static_cast<T>(_y/length);
00117     _z = static_cast<T>(_z/length);
00118   }
00119 }
00120 
00121 template <typename T> inline double irtkVector3D<T>::Length() const
00122 {
00123   return sqrt(static_cast<double>(_x*_x + _y*_y + _z*_z));
00124 }
00125 
00126 template<typename T> inline irtkVector3D<T> irtkVector3D<T>::CrossProduct(const irtkVector3D<T>& v1, const irtkVector3D<T>& v2)
00127 {
00128   irtkVector3D<T> cp;
00129 
00130   cp._x = v1._y*v2._z - v1._z*v2._y;
00131   cp._y = v1._z*v2._x - v1._x*v2._z;
00132   cp._z = v1._x*v2._y - v1._y*v2._x;
00133 
00134   return cp;
00135 }
00136 
00137 template <typename T> inline double irtkVector3D<T>::DotProduct(const irtkVector3D<T>& v1, const irtkVector3D<T>& v2)
00138 {
00139   return v1._x*v2._x + v1._y*v2._y + v1._z*v2._z;
00140 }
00141 
00142 template <typename T> inline irtkVector3D<T> irtkVector3D<T>::operator*(double s)
00143 {
00144   irtkVector3D<T> r;
00145 
00146   r._x = static_cast<T>(_x*s);
00147   r._y = static_cast<T>(_y*s);
00148   r._z = static_cast<T>(_z*s);
00149 
00150   return r;
00151 }
00152 
00153 template <typename T> inline irtkVector3D<T> irtkVector3D<T>::operator+(const irtkVector3D<T>& v)
00154 {
00155   irtkVector3D<T> r;
00156 
00157   r._x = _x + v._x;
00158   r._y = _y + v._y;
00159   r._z = _z + v._z;
00160 
00161   return r;
00162 }
00163 
00164 template <typename T> inline irtkVector3D<T> irtkVector3D<T>::operator-(const irtkVector3D<T>& v)
00165 {
00166   irtkVector3D<T> r;
00167 
00168   r._x = _x - v._x;
00169   r._y = _y - v._y;
00170   r._z = _z - v._z;
00171 
00172   return r;
00173 }
00174 
00175 
00176 template <typename T> inline irtkVector3D<T>& irtkVector3D<T>::operator*=(double s)
00177 {
00178   _x = static_cast<T>(_x*s);
00179   _y = static_cast<T>(_y*s);
00180   _z = static_cast<T>(_z*s);
00181 
00182   return *this;
00183 }
00184 
00185 template <typename T> inline irtkVector3D<T>& irtkVector3D<T>::operator*=(const irtkVector3D<T>& v)
00186 {
00187   _x *= v._x;
00188   _y *= v._y;
00189   _z *= v._z;
00190 
00191   return *this;
00192 }
00193 
00194 template <typename T> inline irtkVector3D<T>& irtkVector3D<T>::operator+=(const irtkVector3D<T>& v)
00195 {
00196   _x += v._x;
00197   _y += v._y;
00198   _z += v._z;
00199 
00200   return *this;
00201 }
00202 
00203 template <typename T> inline irtkVector3D<T>& irtkVector3D<T>::operator-=(const irtkVector3D<T>& v)
00204 {
00205   _x -= v._x;
00206   _y -= v._y;
00207   _z -= v._z;
00208 
00209   return *this;
00210 }
00211 
00212 template <typename T> inline bool irtkVector3D<T>::operator==(const irtkVector3D<T>& v)
00213 {
00214   return ((_z == v._z) && (_y == v._y) && (_x == v._x));
00215 }
00216 
00217 template <typename T> inline bool irtkVector3D<T>::operator!=(const irtkVector3D<T>& v)
00218 {
00219   return ((_z != v._z) || (_y != v._y) || (_x != v._x));
00220 }
00221 
00222 template <typename T> inline bool irtkVector3D<T>::operator<(const irtkVector3D<T>& v)
00223 {
00224   return ((_z < v._z) ||
00225           ((_z == v._z) && (_y < v._y)) ||
00226           ((_z == v._z) && (_y == v._y) && (_x < v._x)));
00227 }
00228 
00229 template <typename T> inline bool irtkVector3D<T>::operator>(const irtkVector3D<T>& v)
00230 {
00231   return ((_z > v._z) ||
00232           ((_z == v._z) && (_y > v._y)) ||
00233           ((_z == v._z) && (_y == v._y) && (_x > v._x)));
00234 }
00235 
00236 template <typename T> inline bool irtkVector3D<T>::operator<=(const irtkVector3D<T>& v)
00237 {
00238   return ((*this < v) || (*this == v));
00239 }
00240 
00241 template <typename T> inline bool irtkVector3D<T>::operator>=(const irtkVector3D<T>& v)
00242 {
00243   return ((*this > v) || (*this == v));
00244 }
00245 
00246 template <typename T> inline irtkVector3D<T>& irtkVector3D<T>::operator/=(double s)
00247 {
00248   _x = static_cast<T>(_x/s);
00249   _y = static_cast<T>(_y/s);
00250   _z = static_cast<T>(_z/s);
00251 
00252   return *this;
00253 }
00254 
00255 template <typename T> inline irtkVector3D<T> irtkVector3D<T>::operator/(double s)
00256 {
00257   return irtkVector3D<T>(static_cast<T>(_x/s), static_cast<T>(_y/s), static_cast<T>(_z/s));
00258 }
00259 
00260 #endif // __IRTKVECTOR3D_H