SourceTransformRotate.h

Go to the documentation of this file.
00001 // $Id$
00002 
00003 #ifndef __SOURCETRANSFORMROTATE_H__
00004 #define __SOURCETRANSFORMROTATE_H__
00005 
00006 #include "SourceGenerics.h"
00007 #include <math.h>
00008 
00009 #undef DEBUG 
00010 #define DEBUG 0
00011 #include "libDebug.h"
00012 
00013 //============================================================================
00014 //============================================================================
00015 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00016 class SourceTransformRotateOf: public SourceTransformOf<DATA, DIMENSIONALITY,
00017     PRECISION, SOURCE>
00018 {
00019 };
00020 
00021 //============================================================================
00022 // 2d
00023 //============================================================================
00024 template<class DATA, class PRECISION, class SOURCE>
00025 class SourceTransformRotateOf<DATA, 2, PRECISION, SOURCE> : public SourceTransformOf<
00026     DATA, 2, PRECISION, SOURCE>
00027 {
00028   Real m_rAngle;
00029   mutable Real m_rCos;
00030   mutable Real m_rSin;
00031   mutable Real m_rXCenter;
00032   mutable Real m_rYCenter;
00033 
00034 public:
00035 SOURCE_ACTUALS_2D  ;
00036 
00037   SourceTransformRotateOf()
00038   : SourceTransformOf<DATA, DIMENSIONALITY, PRECISION, SOURCE> ()
00039   , m_rAngle(0)
00040     {}
00041 
00042   virtual String Describe() const
00043     {
00044     return (
00045         _LINE + SerializationId() + LINE_ +
00046         (_INDENT + "degrees: " + m_rAngle + LINE_) +
00047         this->DescribeCommon()
00048     );
00049     }
00050 
00051   virtual void RegisterParameters(RegistryOfParameters& reg)
00052     {
00053     reg.Register(&m_rAngle);
00054     }
00055 
00056   Real GetAngle() const
00057     {
00058     return m_rAngle;
00059     }
00060 
00061   void SetAngle(Real r)
00062     {
00063     m_rAngle=r;
00064     }
00065 
00066   static String SerializationId()
00067     {
00068     return "SourceTransformRotateOf(2D)";
00069     }
00070 
00071   void SerializeSelf(Stream& st) const
00072     {
00073     ::Serialize(st,m_rAngle);
00074     }
00075 
00076   void DeserializeSelf(Stream &st)
00077     {
00078     Deserialize(st,m_rAngle);
00079     }
00080 
00081   void Get(DATA& dataOut, const PRECISION& rX, const PRECISION& rY) const
00082     {
00083     Real dX=rX-m_rXCenter;
00084     Real dY=rY-m_rYCenter;
00085 
00086     ::Get(
00087         *(this->m_psource),
00088         dataOut,
00089         (( m_rCos * dX + m_rSin * dY) + m_rXCenter),
00090         ((-m_rSin * dX + m_rCos * dY) + m_rYCenter)
00091     );
00092     }
00093 
00094   void Set(const PRECISION &rX, const PRECISION &rY, const DATA& data)
00095     {
00096     Real dX=rX-m_rXCenter;
00097     Real dY=rY-m_rYCenter;
00098 
00099     ::Set(
00100         *this->m_psource,
00101         (( m_rCos * dX + m_rSin * dY) + m_rXCenter),
00102         ((-m_rSin * dX + m_rCos * dY) + m_rYCenter),
00103         data
00104     );
00105     }
00106 
00107   void PrepareForAccessAction() const
00108     {
00109     this->m_ptSize = this->PSource()->Size();
00110 
00111     m_rSin=sin(-m_rAngle / 360 * 2 * PI);
00112     m_rCos=cos(-m_rAngle / 360 * 2 * PI);
00113 
00114     m_rXCenter=Real(this->Size().X())/2;
00115     m_rYCenter=Real(this->Size().Y())/2;
00116     }
00117 
00118   };
00119 
00120 //============================================================================
00121 // 3d
00122 //============================================================================
00123 template<class DATA, class PRECISION, class SOURCE>
00124 class SourceTransformRotateOf<DATA, 3, PRECISION, SOURCE> : public SourceTransformOf<
00125     DATA, 3, PRECISION, SOURCE>
00126 {
00127   Real m_rAngleX;
00128   Real m_rAngleY;
00129   Real m_rAngleZ;
00130   mutable Real m_rCosX;
00131   mutable Real m_rSinX;
00132   mutable Real m_rCosY;
00133   mutable Real m_rSinY;
00134   mutable Real m_rCosZ;
00135   mutable Real m_rSinZ;
00136   mutable Real m_rXCenter;
00137   mutable Real m_rYCenter;
00138   mutable Real m_rZCenter;
00139 
00140 public:
00141 SOURCE_ACTUALS_3D  ;
00142 
00143   SourceTransformRotateOf()
00144   : SourceTransformOf<DATA, DIMENSIONALITY, PRECISION, SOURCE> ()
00145   , m_rAngleX(0)
00146   , m_rAngleY(0)
00147   , m_rAngleZ(0)
00148     {}
00149 
00150   virtual String Describe() const
00151     {
00152     return (
00153         _LINE + SerializationId() + LINE_ +
00154         (_INDENT + "degrees: " + m_rAngleX + ", " + m_rAngleY + ", " + m_rAngleZ + LINE_) +
00155         this->DescribeCommon()
00156     );
00157     }
00158 
00159   virtual void RegisterParameters(RegistryOfParameters& reg)
00160     {
00161     reg.Register(&m_rAngleX);
00162     reg.Register(&m_rAngleY);
00163     reg.Register(&m_rAngleZ);
00164     }
00165 
00166   Real GetAngleX() const
00167     {
00168     return m_rAngleX;
00169     }
00170   Real GetAngleY() const
00171     {
00172     return m_rAngleY;
00173     }
00174   Real GetAngleZ() const
00175     {
00176     return m_rAngleZ;
00177     }
00178 
00179   void SetAngle(Real rX, Real rY, Real rZ)
00180     {
00181     m_rAngleX=rX;
00182     m_rAngleY=rY;
00183     m_rAngleZ=rZ;
00184     }
00185 
00186   static String SerializationId()
00187     {
00188     return "SourceTransformRotateOf(3D)";
00189     }
00190 
00191   void SerializeSelf(Stream &st) const
00192     {
00193     ::Serialize(st,m_rAngleX);
00194     ::Serialize(st,m_rAngleY);
00195     ::Serialize(st,m_rAngleZ);
00196     }
00197 
00198   void DeserializeSelf(Stream& st)
00199     {
00200     Deserialize(st,m_rAngleX);
00201     Deserialize(st,m_rAngleY);
00202     Deserialize(st,m_rAngleZ);
00203     }
00204 
00205   /* rotation matricies in each separate dimension
00206 
00207    s* == sin of *
00208    c* == cos of *
00209 
00210    in X
00211    cx -sx  0
00212    sx  cx  0
00213    0   0   1
00214 
00215    in Y
00216    cy  0   sy
00217    0   1   0
00218    -sy  0   cy
00219 
00220    in Z
00221    1    0   0
00222    0    cz  -sz
00223    0    sz   cz
00224 
00225    from
00226 
00227    http://en.wikipedia.org/wiki/Rotation_matrix
00228    */
00229 
00230   void Get(DATA& dataOut, const PRECISION &rX, const PRECISION &rY, const PRECISION &rZ) const
00231     {
00232     Real dX0 = rX - m_rXCenter;
00233     Real dY0 = rY - m_rYCenter;
00234     Real dZ0 = rZ - m_rZCenter;
00235 
00236     Real dX1 = m_rCosX * dX0 - m_rSinX * dY0;
00237     Real dY1 = m_rSinX * dX0 + m_rCosX * dY0;
00238     Real&dZ1 = dZ0;
00239 
00240     Real dX2 = m_rCosY * dX1 + m_rSinY * dZ1;
00241     Real&dY2 = dY1;
00242     Real dZ2 = - m_rSinY * dX1 + m_rCosY * dZ1;
00243 
00244     Real&dX3 = dX2;
00245     Real dY3 = m_rCosZ * dY2 - m_rSinZ * dZ2;
00246     Real dZ3 = m_rSinZ * dY2 + m_rCosZ * dZ2;
00247 
00248     Real rXOut = dX3+m_rXCenter;
00249     Real rYOut = dY3+m_rYCenter;
00250     Real rZOut = dZ3+m_rZCenter;
00251 
00252     ::Get(
00253         *(this->m_psource),
00254         dataOut,
00255         rXOut,
00256         rYOut,
00257         rZOut
00258     );
00259 
00260     }
00261 
00262   void Set(const PRECISION &rX, const PRECISION &rY, const PRECISION &rZ, const DATA& data)
00263     {
00264     Real dX0 = rX - m_rXCenter;
00265     Real dY0 = rY - m_rYCenter;
00266     Real dZ0 = rZ - m_rZCenter;
00267 
00268     Real dX1 = m_rCosX * dX0 - m_rSinX * dY0;
00269     Real dY1 = m_rSinX * dX0 + m_rCosX * dY0;
00270     Real&dZ1 = dZ0;
00271 
00272     Real dX2 = m_rCosY * dX1 + m_rSinY * dZ1;
00273     Real&dY2 = dY1;
00274     Real dZ2 = - m_rSinY * dX1 + m_rCosY * dZ1;
00275 
00276     Real&dX3 = dX2;
00277     Real dY3 = m_rCosZ * dY2 - m_rSinZ * dZ2;
00278     Real dZ3 = m_rSinZ * dY2 + m_rCosZ * dZ2;
00279 
00280     Real rXOut = dX3+m_rXCenter;
00281     Real rYOut = dY3+m_rYCenter;
00282     Real rZOut = dZ3+m_rZCenter;
00283 
00284     ::Set(
00285         *(this->m_psource),
00286         rXOut,
00287         rYOut,
00288         rZOut,
00289         data
00290     );
00291     }
00292 
00293   void PrepareForAccessAction() const
00294     {
00295     this->m_ptSize=this->PSource()->Size();
00296 
00297     m_rSinX=sin(-m_rAngleX / 360 * 2 * PI);
00298     m_rCosX=cos(-m_rAngleX / 360 * 2 * PI);
00299 
00300     m_rSinY=sin(-m_rAngleY / 360 * 2 * PI);
00301     m_rCosY=cos(-m_rAngleY / 360 * 2 * PI);
00302 
00303     m_rSinZ=sin(-m_rAngleZ / 360 * 2 * PI);
00304     m_rCosZ=cos(-m_rAngleZ / 360 * 2 * PI);
00305 
00306     m_rXCenter=Real(this->Size().X())/2;
00307     m_rYCenter=Real(this->Size().Y())/2;
00308     m_rZCenter=Real(this->Size().Z())/2;
00309     }
00310 
00311   };
00312 
00313 //============================================================================
00314 // rotate assistant
00315 //============================================================================
00316 MAKE_TYPECHANGE_ASSISTANT(
00317     Rotate,
00318     SourceTransformRotateOf,
00319     TypeOfData(SOURCE),
00320     TypeOfDimensionality(SOURCE),
00321     Real,
00322       {psrcOut->SetAngle(rAngle);},
00323     const Real& rAngle,
00324 );
00325 
00326 MAKE_TYPECHANGE_ASSISTANT(
00327     Rotate,
00328     SourceTransformRotateOf,
00329     TypeOfData(SOURCE),
00330     TypeOfDimensionality(SOURCE),
00331     Real,
00332       {psrcOut->SetAngle(rAngleX,rAngleY,rAngleZ);},
00333     const Real& rAngleX,
00334     const Real& rAngleY,
00335     const Real& rAngleZ,
00336 );
00337 
00338 #endif // __SOURCETRANSFORMROTATE_H__

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1