/vol/vipdata/irtk/image++/include/irtkBaseImage.h

00001 /*=========================================================================
00002  
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkBaseImage.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 _IRTKBASEIMAGE_H
00014 
00015 #define _IRTKBASEIMAGE_H
00016 
00017 #include <irtkImageAttributes.h>
00018 
00028 class irtkBaseImage : public irtkObject
00029 {
00030 
00031 protected:
00032 
00034    irtkImageAttributes _attr;
00035 
00037   irtkMatrix _matI2W;
00038 
00040   irtkMatrix _matW2I;
00041 
00043   irtkBaseImage();
00044 
00046   irtkBaseImage(const irtkImageAttributes &);
00047 
00049   irtkBaseImage(const irtkBaseImage &);
00050 
00052   void Initialize(const irtkImageAttributes &);
00053 
00055   void UpdateMatrix();
00056 
00057 public:
00058 
00060   virtual ~irtkBaseImage();
00061 
00062   //
00063   // Access functions for image dimensions
00064   //
00065 
00067   virtual int  GetX() const;
00068 
00070   virtual int  GetY() const;
00071 
00073   virtual int  GetZ() const;
00074 
00076   virtual int  GetT() const;
00077 
00079   virtual int  GetNumberOfVoxels() const;
00080 
00082   virtual irtkImageAttributes GetImageAttributes() const;
00083 
00085   virtual void PutImageAttributes(const irtkImageAttributes &);
00086 
00087   //
00088   // Access functions for voxel dimensions
00089   //
00090 
00092   virtual double GetXSize() const;
00093 
00095   virtual double GetYSize() const;
00096 
00098   virtual double GetZSize() const;
00099 
00101   virtual double GetTSize() const;
00102 
00104   virtual void  GetPixelSize(double *, double *, double *) const;
00105 
00107   virtual void  GetPixelSize(double *, double *, double *, double *) const;
00108 
00110   virtual void  PutPixelSize(double, double, double);
00111 
00113   virtual void  PutPixelSize(double, double, double, double);
00114 
00116   virtual irtkPoint GetOrigin() const;
00117 
00119   virtual void  GetOrigin(double &, double &, double &) const;
00120 
00122   virtual void  GetOrigin(double &, double &, double &, double &) const;
00123 
00125   virtual void  PutOrigin(const irtkPoint &);
00126 
00128   virtual void  PutOrigin(double, double, double);
00129 
00131   virtual void  PutOrigin(double, double, double, double);
00132 
00134   virtual void  PutOrientation(double *, double *, double *);
00135 
00137   virtual void  GetOrientation(double *, double *, double *) const;
00138 
00140   virtual void  Orientation(int &, int &, int &) const;
00141 
00143   virtual void ImageToWorld(irtkPoint &) const;
00144 
00146   virtual void WorldToImage(irtkPoint &) const;
00147 
00149   virtual void ImageToWorld(double &, double &, double &) const;
00150 
00152   virtual void WorldToImage(double &, double &, double &) const;
00153 
00155   virtual irtkMatrix GetImageToWorldMatrix() const;
00156 
00158   virtual irtkMatrix GetWorldToImageMatrix() const;
00159 
00161   virtual double ImageToTime(double) const;
00162 
00164   virtual double TimeToImage(double) const;
00165 
00167   virtual int IsInFOV(double, double, double);
00168 
00170   virtual Bool IsEmpty() const;
00171 
00173   virtual void GetMinMaxAsDouble(double *, double *) const;
00174 
00176   virtual void PutMinMaxAsDouble(double, double);
00177 
00179   virtual double GetAsDouble(int, int, int, int = 0) const = 0;
00180 
00182   virtual void   PutAsDouble(int, int, int, double) = 0;
00183 
00185   virtual void   PutAsDouble(int, int, int, int, double) = 0;
00186 
00188   virtual const char *NameOfClass() = 0;
00189 
00191   virtual void *GetScalarPointer(int = 0, int = 0, int = 0, int = 0) const = 0;
00192 
00194   virtual int GetScalarType() const = 0;
00195 
00197   virtual double GetScalarTypeMin() const = 0;
00198 
00200   virtual double GetScalarTypeMax() const = 0;
00201 
00203   virtual void ReflectX() = 0;
00204 
00206   virtual void ReflectY() = 0;
00207 
00209   virtual void ReflectZ() = 0;
00210 
00212   virtual void FlipXY() = 0;
00213 
00215   virtual void FlipXZ() = 0;
00216 
00218   virtual void FlipYZ() = 0;
00219 
00221   virtual void FlipXT() = 0;
00222 
00224   virtual void FlipYT() = 0;
00225 
00227   virtual void FlipZT() = 0;
00228 
00230   static irtkBaseImage *New(const char *);
00231 
00233   virtual void Write(const char *) = 0;
00234 
00236   virtual void Print();
00237 
00238 };
00239 
00240 inline int irtkBaseImage::GetX() const
00241 {
00242   return _attr._x;
00243 }
00244 
00245 inline int irtkBaseImage::GetY() const
00246 {
00247   return _attr._y;
00248 }
00249 
00250 inline int irtkBaseImage::GetZ() const
00251 {
00252   return _attr._z;
00253 }
00254 
00255 inline int irtkBaseImage::GetT() const
00256 {
00257   return _attr._t;
00258 }
00259 
00260 inline irtkImageAttributes irtkBaseImage::GetImageAttributes() const
00261 {
00262   return _attr;
00263 }
00264 
00265 inline int irtkBaseImage::GetNumberOfVoxels(void) const
00266 {
00267   return _attr._x*_attr._y*_attr._z*_attr._t;
00268 }
00269 
00270 inline double irtkBaseImage::GetXSize(void) const
00271 {
00272   return _attr._dx;
00273 }
00274 
00275 inline double irtkBaseImage::GetYSize(void) const
00276 {
00277   return _attr._dy;
00278 }
00279 
00280 inline double irtkBaseImage::GetZSize(void) const
00281 {
00282   return _attr._dz;
00283 }
00284 
00285 inline double irtkBaseImage::GetTSize(void) const
00286 {
00287   return _attr._dt;
00288 }
00289 
00290 inline void irtkBaseImage::PutPixelSize(double dx, double dy, double dz)
00291 {
00292    _attr._dx = dx;
00293    _attr._dy = dy;
00294    _attr._dz = dz;
00295 
00296   // Update transformation matrix
00297   this->UpdateMatrix();
00298 }
00299 
00300 inline void irtkBaseImage::PutPixelSize(double dx, double dy, double dz, double dt)
00301 {
00302    _attr._dx = dx;
00303    _attr._dy = dy;
00304    _attr._dz = dz;
00305    _attr._dt = dt;
00306 
00307   // Update transformation matrix
00308   this->UpdateMatrix();
00309 }
00310 
00311 inline void irtkBaseImage::GetPixelSize(double *dx, double *dy, double *dz) const
00312 {
00313   *dx = _attr._dx;
00314   *dy = _attr._dy;
00315   *dz = _attr._dz;
00316 }
00317 
00318 inline void irtkBaseImage::GetPixelSize(double *dx, double *dy, double *dz, double *dt) const
00319 {
00320   *dx = _attr._dx;
00321   *dy = _attr._dy;
00322   *dz = _attr._dz;
00323   *dt = _attr._dt;
00324 }
00325 
00326 inline void irtkBaseImage::PutOrigin(const irtkPoint &p)
00327 {
00328    _attr._xorigin = p._x;
00329    _attr._yorigin = p._y;
00330    _attr._zorigin = p._z;
00331 
00332   // Update transformation matrix
00333   this->UpdateMatrix();
00334 }
00335 
00336 inline void irtkBaseImage::PutOrigin(double x, double y, double z)
00337 {
00338    _attr._xorigin = x;
00339    _attr._yorigin = y;
00340    _attr._zorigin = z;
00341 
00342   // Update transformation matrix
00343   this->UpdateMatrix();
00344 }
00345 
00346 inline void irtkBaseImage::PutOrigin(double x, double y, double z, double t)
00347 {
00348    _attr._xorigin = x;
00349    _attr._yorigin = y;
00350    _attr._zorigin = z;
00351 
00352   // Update transformation matrix
00353   this->UpdateMatrix();
00354 
00355   // Calculate origin
00356   _attr._torigin = t;
00357 }
00358 
00359 inline void irtkBaseImage::GetOrigin(double &x, double &y, double &z) const
00360 {
00361   x = _attr._xorigin;
00362   y = _attr._yorigin;
00363   z = _attr._zorigin;
00364 }
00365 
00366 inline void irtkBaseImage::GetOrigin(double &x, double &y, double &z, double &t) const
00367 {
00368   x = _attr._xorigin;
00369   y = _attr._yorigin;
00370   z = _attr._zorigin;
00371   t = _attr._torigin;
00372 }
00373 
00374 inline irtkPoint irtkBaseImage::GetOrigin() const
00375 {
00376   return irtkPoint(_attr._xorigin, _attr._yorigin, _attr._zorigin);
00377 }
00378 
00379 inline void irtkBaseImage::PutOrientation(double *xaxis, double *yaxis, double *zaxis)
00380 {
00381    _attr._xaxis[0] = xaxis[0];
00382    _attr._xaxis[1] = xaxis[1];
00383    _attr._xaxis[2] = xaxis[2];
00384    _attr._yaxis[0] = yaxis[0];
00385    _attr._yaxis[1] = yaxis[1];
00386    _attr._yaxis[2] = yaxis[2];
00387   if (zaxis == NULL) {
00388    _attr._zaxis[0] = _attr._xaxis[1]*_attr._yaxis[2] - _attr._xaxis[2]*_attr._yaxis[1];
00389    _attr._zaxis[1] = _attr._xaxis[2]*_attr._yaxis[0] - _attr._xaxis[0]*_attr._yaxis[2];
00390    _attr._zaxis[2] = _attr._xaxis[0]*_attr._yaxis[1] - _attr._xaxis[1]*_attr._yaxis[0];
00391   } else {
00392    _attr._zaxis[0] = zaxis[0];
00393    _attr._zaxis[1] = zaxis[1];
00394    _attr._zaxis[2] = zaxis[2];
00395   }
00396 
00397   // Update transformation matrix
00398   this->UpdateMatrix();
00399 }
00400 
00401 inline void irtkBaseImage::GetOrientation(double *xaxis, double *yaxis, double *zaxis) const
00402 {
00403   xaxis[0] = _attr._xaxis[0];
00404   xaxis[1] = _attr._xaxis[1];
00405   xaxis[2] = _attr._xaxis[2];
00406   yaxis[0] = _attr._yaxis[0];
00407   yaxis[1] = _attr._yaxis[1];
00408   yaxis[2] = _attr._yaxis[2];
00409   zaxis[0] = _attr._zaxis[0];
00410   zaxis[1] = _attr._zaxis[1];
00411   zaxis[2] = _attr._zaxis[2];
00412 }
00413 
00414 inline void irtkBaseImage::ImageToWorld(double &x, double &y, double &z) const
00415 {
00416   double a, b, c;
00417 
00418   // Pre-multiply point with transformation matrix
00419   a = _matI2W(0, 0)*x+_matI2W(0, 1)*y+_matI2W(0, 2)*z+_matI2W(0, 3);
00420   b = _matI2W(1, 0)*x+_matI2W(1, 1)*y+_matI2W(1, 2)*z+_matI2W(1, 3);
00421   c = _matI2W(2, 0)*x+_matI2W(2, 1)*y+_matI2W(2, 2)*z+_matI2W(2, 3);
00422 
00423   // Copy result back
00424   x = a;
00425   y = b;
00426   z = c;
00427 }
00428 
00429 inline void irtkBaseImage::ImageToWorld(irtkPoint &p) const
00430 {
00431   double a, b, c;
00432 
00433   // Pre-multiply point with transformation matrix
00434   a = _matI2W(0, 0)*p._x+_matI2W(0, 1)*p._y+_matI2W(0, 2)*p._z+_matI2W(0, 3);
00435   b = _matI2W(1, 0)*p._x+_matI2W(1, 1)*p._y+_matI2W(1, 2)*p._z+_matI2W(1, 3);
00436   c = _matI2W(2, 0)*p._x+_matI2W(2, 1)*p._y+_matI2W(2, 2)*p._z+_matI2W(2, 3);
00437 
00438   // Copy result back
00439   p._x = a;
00440   p._y = b;
00441   p._z = c;
00442 }
00443 
00444 inline void irtkBaseImage::WorldToImage(double &x, double &y, double &z) const
00445 {
00446   double a, b, c;
00447 
00448   // Pre-multiply point with transformation matrix
00449   a = _matW2I(0, 0)*x+_matW2I(0, 1)*y+_matW2I(0, 2)*z+_matW2I(0, 3);
00450   b = _matW2I(1, 0)*x+_matW2I(1, 1)*y+_matW2I(1, 2)*z+_matW2I(1, 3);
00451   c = _matW2I(2, 0)*x+_matW2I(2, 1)*y+_matW2I(2, 2)*z+_matW2I(2, 3);
00452 
00453   // Copy result back
00454   x = a;
00455   y = b;
00456   z = c;
00457 }
00458 
00459 inline void irtkBaseImage::WorldToImage(irtkPoint &p) const
00460 {
00461   double a, b, c;
00462 
00463   // Pre-multiply point with transformation matrix
00464   a = _matW2I(0, 0)*p._x+_matW2I(0, 1)*p._y+_matW2I(0, 2)*p._z+_matW2I(0, 3);
00465   b = _matW2I(1, 0)*p._x+_matW2I(1, 1)*p._y+_matW2I(1, 2)*p._z+_matW2I(1, 3);
00466   c = _matW2I(2, 0)*p._x+_matW2I(2, 1)*p._y+_matW2I(2, 2)*p._z+_matW2I(2, 3);
00467 
00468   // Copy result back
00469   p._x = a;
00470   p._y = b;
00471   p._z = c;
00472 }
00473 
00474 inline irtkMatrix irtkBaseImage::GetImageToWorldMatrix() const
00475 {
00476   return _matI2W;
00477 }
00478 
00479 inline irtkMatrix irtkBaseImage::GetWorldToImageMatrix() const
00480 {
00481   return _matW2I;
00482 }
00483 
00484 inline double irtkBaseImage::ImageToTime(double t) const
00485 {
00486   return _attr._torigin+t*_attr._dt;
00487 }
00488 
00489 inline double irtkBaseImage::TimeToImage(double t) const
00490 {
00491   return (t-_attr._torigin)/_attr._dt;
00492 }
00493 
00494 inline int irtkBaseImage::IsInFOV(double x, double y, double z)
00495 {
00496   this->WorldToImage(x, y, z);
00497   if ((x < -0.5) || (x >= _attr._x-0.5) ||
00498       (y < -0.5) || (y >= _attr._y-0.5) ||
00499       (z < -0.5) || (z >= _attr._z-0.5)) {
00500     return False;
00501   }
00502   return True;
00503 }
00504 
00505 inline Bool irtkBaseImage::IsEmpty() const
00506 {
00507   return ((_attr._x < 1) || (_attr._y < 1) || (_attr._z < 1) || (_attr._t < 1));
00508 }
00509 
00510 
00511 typedef class irtkBaseImage irtkImage;
00512 
00513 #endif