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

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkPoint.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 _IRTKPOINT_H
00014 
00015 #define _IRTKPOINT_H
00016 
00023 class irtkPoint : public irtkObject
00024 {
00025 
00026 public:
00027 
00029   double _x;
00030 
00032   double _y;
00033 
00035   double _z;
00036 
00037   //
00038   // Constructors and destructor
00039   //
00040 
00042   irtkPoint();
00043 
00045   irtkPoint(double, double, double);
00046 
00048   irtkPoint(const irtkPoint &);
00049 
00051   irtkPoint(const irtkVector&);
00052 
00054   virtual ~irtkPoint(void);
00055 
00056   //
00057   // Operators for Point
00058   //
00059 
00061   irtkPoint& operator =(const irtkPoint&);
00062 
00064   irtkPoint& operator-=(const irtkPoint&);
00065 
00067   irtkPoint& operator+=(const irtkPoint&);
00068 
00070   irtkPoint& operator*=(const irtkPoint&);
00071 
00073   irtkPoint& operator/=(const irtkPoint&);
00074 
00076   irtkPoint  operator- (const irtkPoint&);
00077 
00079   irtkPoint  operator+ (const irtkPoint&);
00080 
00082   irtkPoint  operator* (const irtkPoint&);
00083 
00085   irtkPoint  operator/ (const irtkPoint&);
00086 
00087   //
00088   // Operators for comparison
00089   //
00090 
00092   int    operator==(const irtkPoint&) const;
00093 
00095   int    operator!=(const irtkPoint&);
00096 
00098   int    operator<(const irtkPoint&);
00099 
00101   int    operator>(const irtkPoint&);
00102 
00103   //
00104   // Operators for double
00105   //
00106 
00108   irtkPoint& operator-=(double);
00109 
00111   irtkPoint& operator+=(double);
00112 
00114   irtkPoint& operator*=(double);
00115 
00117   irtkPoint& operator/=(double);
00118 
00119   // Return result of substraction of double
00120   irtkPoint  operator- (double);
00121 
00122   // Return result of addition of double
00123   irtkPoint  operator+ (double);
00124 
00125   // Return result of multiplication with double
00126   irtkPoint  operator* (double);
00127 
00128   // Return result of division by double
00129   irtkPoint  operator/ (double);
00130 
00131   //
00132   // Operators for Vector
00133   //
00134 
00136   irtkPoint& operator =(const irtkVector&);
00137 
00139   irtkPoint& operator-=(const irtkVector&);
00140 
00142   irtkPoint& operator+=(const irtkVector&);
00143 
00145   irtkPoint& operator*=(const irtkVector&);
00146 
00148   irtkPoint& operator/=(const irtkVector&);
00149 
00150   // Return result of vector substraction
00151   irtkPoint  operator- (const irtkVector&);
00152 
00153   // Return result of vector addition
00154   irtkPoint  operator+ (const irtkVector&);
00155 
00156   // Return result of vector multiplication
00157   irtkPoint  operator* (const irtkVector&);
00158 
00159   // Return result of vector division
00160   irtkPoint  operator/ (const irtkVector&);
00161 
00162   //
00163   // Operators for Matrix
00164   //
00165 
00167   irtkPoint& operator*=(const irtkMatrix&);
00168 
00170   irtkPoint  operator* (const irtkMatrix&);
00171 
00172   //
00173   // Distance methods
00174   //
00175 
00177   double  Distance(void) const;
00178 
00180   double  Distance(const irtkPoint&) const;
00181 
00182   //
00183   // I/O methods
00184   //
00185 
00186   // Interface to output stream
00187   friend ostream& operator<< (ostream&, const irtkPoint&);
00188 
00190   friend istream& operator>> (istream&, irtkPoint&);
00191 };
00192 
00193 inline irtkPoint::irtkPoint()
00194 {
00195   _x = 0;
00196   _y = 0;
00197   _z = 0;
00198 }
00199 
00200 inline irtkPoint::irtkPoint(double x, double y, double z)
00201 {
00202   _x = x;
00203   _y = y;
00204   _z = z;
00205 }
00206 
00207 inline irtkPoint::irtkPoint(const irtkPoint& p)
00208 {
00209   _x = p._x;
00210   _y = p._y;
00211   _z = p._z;
00212 }
00213 
00214 inline irtkPoint::irtkPoint(const irtkVector& v)
00215 {
00216   if ((v.Rows() < 0) || (v.Rows() > 3)) {
00217     cerr << "irtkPoint::irtkPoint(const irtkVector&) Illegal dimension: " << v.Rows() << endl;
00218     exit(1);
00219   } else {
00220     if (v.Rows() == 1) {
00221       _x = v(0);
00222       _y = 0;
00223       _z = 0;
00224     }
00225     if (v.Rows() == 2) {
00226       _x = v(0);
00227       _y = v(1);
00228       _z = 0;
00229     }
00230     if (v.Rows() == 3) {
00231       _x = v(0);
00232       _y = v(1);
00233       _z = v(2);
00234     }
00235   }
00236 }
00237 
00238 inline irtkPoint::~irtkPoint()
00239 {
00240 }
00241 
00242 inline irtkPoint& irtkPoint::operator =(const irtkPoint& p)
00243 {
00244   _x = p._x;
00245   _y = p._y;
00246   _z = p._z;
00247   return *this;
00248 }
00249 
00250 inline irtkPoint& irtkPoint::operator+=(const irtkPoint& p)
00251 {
00252   _x += p._x;
00253   _y += p._y;
00254   _z += p._z;
00255   return *this;
00256 }
00257 
00258 inline irtkPoint& irtkPoint::operator-=(const irtkPoint& p)
00259 {
00260   _x -= p._x;
00261   _y -= p._y;
00262   _z -= p._z;
00263   return *this;
00264 }
00265 
00266 inline irtkPoint& irtkPoint::operator*=(const irtkPoint& p)
00267 {
00268   _x *= p._x;
00269   _y *= p._y;
00270   _z *= p._z;
00271   return *this;
00272 }
00273 
00274 inline irtkPoint& irtkPoint::operator/=(const irtkPoint& p)
00275 {
00276   _x /= p._x;
00277   _y /= p._y;
00278   _z /= p._z;
00279   return *this;
00280 }
00281 
00282 inline irtkPoint irtkPoint::operator+ (const irtkPoint& p)
00283 {
00284   irtkPoint tmp;
00285 
00286   tmp._x = _x + p._x;
00287   tmp._y = _y + p._y;
00288   tmp._z = _z + p._z;
00289   return tmp;
00290 }
00291 
00292 inline irtkPoint irtkPoint::operator- (const irtkPoint& p)
00293 {
00294   irtkPoint tmp;
00295 
00296   tmp._x = _x - p._x;
00297   tmp._y = _y - p._y;
00298   tmp._z = _z - p._z;
00299   return tmp;
00300 }
00301 
00302 inline irtkPoint irtkPoint::operator* (const irtkPoint& p)
00303 {
00304   irtkPoint tmp;
00305 
00306   tmp._x = _x * p._x;
00307   tmp._y = _y * p._y;
00308   tmp._z = _z * p._z;
00309   return tmp;
00310 }
00311 
00312 inline irtkPoint irtkPoint::operator/ (const irtkPoint& p)
00313 {
00314   irtkPoint tmp;
00315 
00316   tmp._x = _x / p._x;
00317   tmp._y = _y / p._y;
00318   tmp._z = _z / p._z;
00319   return tmp;
00320 }
00321 
00322 inline int irtkPoint::operator==(irtkPoint const &p) const
00323 {
00324   return ((_x == p._x) && (_y == p._y) && (_z == p._z));
00325 }
00326 
00327 inline int irtkPoint::operator!=(irtkPoint const &p)
00328 {
00329   return ((_x != p._x) || (_y != p._y) || (_z != p._z));
00330 }
00331 
00332 inline int irtkPoint::operator<(irtkPoint const& p)
00333 {
00334   return ((_x < p._x) && (_y < p._y) && (_z < p._z));
00335 }
00336 
00337 inline int irtkPoint::operator>(irtkPoint const& p)
00338 {
00339   return ((_x > p._x) && (_y > p._y) && (_z > p._z));
00340 }
00341 
00342 inline irtkPoint& irtkPoint::operator+=(double x)
00343 {
00344   _x += x;
00345   _y += x;
00346   _z += x;
00347   return *this;
00348 }
00349 
00350 inline irtkPoint& irtkPoint::operator-=(double x)
00351 {
00352   _x -= x;
00353   _y -= x;
00354   _z -= x;
00355   return *this;
00356 }
00357 
00358 inline irtkPoint& irtkPoint::operator/=(double x)
00359 {
00360   _x /= x;
00361   _y /= x;
00362   _z /= x;
00363   return *this;
00364 }
00365 
00366 inline irtkPoint& irtkPoint::operator*=(double x)
00367 {
00368   _x *= x;
00369   _y *= x;
00370   _z *= x;
00371   return *this;
00372 }
00373 
00374 inline irtkPoint irtkPoint::operator+ (double x)
00375 {
00376   irtkPoint p;
00377 
00378   p._x = _x + x;
00379   p._y = _y + x;
00380   p._z = _z + x;
00381   return p;
00382 }
00383 
00384 inline irtkPoint irtkPoint::operator- (double x)
00385 {
00386   irtkPoint p;
00387 
00388   p._x = _x - x;
00389   p._y = _y - x;
00390   p._z = _z - x;
00391   return p;
00392 }
00393 
00394 inline irtkPoint irtkPoint::operator* (double x)
00395 {
00396   irtkPoint p;
00397 
00398   p._x = _x * x;
00399   p._y = _y * x;
00400   p._z = _z * x;
00401   return p;
00402 }
00403 
00404 inline irtkPoint irtkPoint::operator/ (double x)
00405 {
00406   irtkPoint p;
00407 
00408   p._x = _x / x;
00409   p._y = _y / x;
00410   p._z = _z / x;
00411   return p;
00412 }
00413 
00414 inline irtkPoint& irtkPoint::operator+=(const irtkVector& v)
00415 {
00416   if (v.Rows() != 3) {
00417     cerr << "irtkPoint::operator+=(const irtkVector& v): Size mismatch" << endl;
00418     exit(1);
00419   }
00420   _x += v(0);
00421   _y += v(1);
00422   _z += v(2);
00423   return *this;
00424 }
00425 
00426 inline irtkPoint& irtkPoint::operator-=(const irtkVector& v)
00427 {
00428   if (v.Rows() != 3) {
00429     cerr << "irtkPoint::operator-=(const irtkVector& v): Size mismatch" << endl;
00430     exit(1);
00431   }
00432   _x -= v(0);
00433   _y -= v(1);
00434   _z -= v(2);
00435   return *this;
00436 }
00437 
00438 inline irtkPoint& irtkPoint::operator*=(const irtkVector& v)
00439 {
00440   if (v.Rows() != 3) {
00441     cerr << "irtkPoint::operator*=(const irtkVector& v): Size mismatch" << endl;
00442     exit(1);
00443   }
00444   _x *= v(0);
00445   _y *= v(1);
00446   _z *= v(2);
00447   return *this;
00448 }
00449 
00450 inline irtkPoint& irtkPoint::operator/=(const irtkVector& v)
00451 {
00452   if (v.Rows() != 3) {
00453     cerr << "irtkPoint::operator/=(const irtkVector& v): Size mismatch" << endl;
00454     exit(1);
00455   }
00456   _x /= v(0);
00457   _y /= v(1);
00458   _z /= v(2);
00459   return *this;
00460 }
00461 
00462 inline irtkPoint irtkPoint::operator+ (const irtkVector& v)
00463 {
00464   irtkPoint tmp;
00465 
00466   if (v.Rows() != 3) {
00467     cerr << "irtkPoint::operator+(const irtkVector& v): Size mismatch" << endl;
00468     exit(1);
00469   }
00470   tmp._x = _x + v(0);
00471   tmp._y = _y + v(1);
00472   tmp._z = _z + v(2);
00473   return tmp;
00474 }
00475 
00476 inline irtkPoint irtkPoint::operator- (const irtkVector& v)
00477 {
00478   irtkPoint tmp;
00479 
00480   if (v.Rows() != 3) {
00481     cerr << "irtkPoint::operator-(const irtkVector& v): Size mismatch" << endl;
00482     exit(1);
00483   }
00484   tmp._x = _x - v(0);
00485   tmp._y = _y - v(1);
00486   tmp._z = _z - v(2);
00487   return tmp;
00488 }
00489 
00490 inline irtkPoint irtkPoint::operator* (const irtkVector& v)
00491 {
00492   irtkPoint tmp;
00493 
00494   if (v.Rows() != 3) {
00495     cerr << "irtkPoint::operator*(const irtkVector& v): Size mismatch" << endl;
00496     exit(1);
00497   }
00498   tmp._x = _x * v(0);
00499   tmp._y = _y * v(1);
00500   tmp._z = _z * v(2);
00501   return tmp;
00502 }
00503 
00504 inline irtkPoint irtkPoint::operator/ (const irtkVector& v)
00505 {
00506   irtkPoint tmp;
00507 
00508   if (v.Rows() != 3) {
00509     cerr << "irtkPoint::operator/(const irtkVector& v): Size mismatch" << endl;
00510     exit(1);
00511   }
00512   tmp._x = _x / v(0);
00513   tmp._y = _y / v(1);
00514   tmp._z = _z / v(2);
00515   return tmp;
00516 }
00517 
00518 inline irtkPoint irtkPoint::operator* (const irtkMatrix& m)
00519 {
00520   irtkPoint tmp;
00521 
00522   if ((m.Rows() != 4) && (m.Cols() != 4)) {
00523     cerr << "irtkPoint::operator*(const irtkMatrix& m): Size mismatch" << endl;
00524     exit(1);
00525   }
00526   tmp._x = _x * m(0, 0) + _y * m(0, 1) + _z * m(0, 2) + m(0, 3);
00527   tmp._y = _x * m(1, 0) + _y * m(1, 1) + _z * m(1, 2) + m(1, 3);
00528   tmp._z = _x * m(2, 0) + _y * m(2, 1) + _z * m(2, 2) + m(2, 3);
00529 
00530   return tmp;
00531 }
00532 
00533 inline irtkPoint& irtkPoint::operator*=(const irtkMatrix& m)
00534 {
00535   irtkPoint tmp;
00536 
00537   if ((m.Rows() != 4) && (m.Cols() != 4)) {
00538     cerr << "irtkPoint::operator*(const irtkMatrix& m): Size mismatch" << endl;
00539     exit(1);
00540   }
00541   tmp._x = _x * m(0, 0) + _y * m(0, 1) + _z * m(0, 2) + m(0, 3);
00542   tmp._y = _x * m(1, 0) + _y * m(1, 1) + _z * m(1, 2) + m(1, 3);
00543   tmp._z = _x * m(2, 0) + _y * m(2, 1) + _z * m(2, 2) + m(2, 3);
00544   *this  = tmp;
00545 
00546   return *this;
00547 }
00548 
00549 inline double irtkPoint::Distance(void) const
00550 {
00551   return sqrt(_x*_x + _y*_y  + _z *_z);
00552 }
00553 
00554 inline double irtkPoint::Distance(const irtkPoint &p) const
00555 {
00556   return sqrt((_x - p._x)*(_x - p._x) + (_y - p._y)*(_y - p._y) +
00557               (_z - p._z)*(_z - p._z));
00558 }
00559 
00560 #endif