Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #ifndef __SP_SCENE_TRANSFORMATION_2D_H__
00009 #define __SP_SCENE_TRANSFORMATION_2D_H__
00010 
00011 
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spDimensionPoint2D.hpp"
00014 #include "Base/spDimensionSize2D.hpp"
00015 #include "Base/spDimensionMatrix4.hpp"
00016 
00017 
00018 namespace sp
00019 {
00020 namespace scene
00021 {
00022 
00023 
00032 template <typename T> class Transformation2D
00033 {
00034     
00035     public:
00036         
00037         Transformation2D() :
00038             Rotation_   (T(0)),
00039             Scale_      (T(1)),
00040             HasChanged_ (true)
00041         {
00042         }
00043         Transformation2D(const dim::matrix4<T> &Matrix) :
00044             Position_   (Matrix.getPosition()   ),
00045             Rotation_   (Matrix.getRotation().Z ),
00046             Scale_      (Matrix.getScale()      ),
00047             Matrix_     (Matrix                 ),
00048             HasChanged_ (false                  )
00049         {
00050         }
00051         Transformation2D(
00052             const dim::point2d<T> &Position, const T &Rotation, const dim::size2d<T> &Scale) :
00053             Position_   (Position   ),
00054             Rotation_   (Rotation   ),
00055             Scale_      (Scale      ),
00056             HasChanged_ (true       )
00057         {
00058         }
00059         Transformation2D(const Transformation2D<T> &Other) :
00060             Position_   (Other.Position_    ),
00061             Rotation_   (Other.Rotation_    ),
00062             Scale_      (Other.Scale_       ),
00063             Matrix_     (Other.Matrix_      ),
00064             HasChanged_ (Other.HasChanged_  )
00065         {
00066         }
00067         ~Transformation2D()
00068         {
00069         }
00070         
00071         
00072         
00073         Transformation2D<T>& operator = (const Transformation2D<T> &Other)
00074         {
00075             Position_   = Other.Position_;
00076             Rotation_   = Other.Rotation_;
00077             Scale_      = Other.Scale_;
00078             Matrix_     = Other.Matrix_;
00079             HasChanged_ = Other.HasChanged_;
00080             return *this;
00081         }
00082         
00083         Transformation2D<T>& operator *= (const Transformation2D<T> &Other)
00084         {
00085             operator = (getMatrix() * Other.getMatrix());
00086             return *this;
00087         }
00088         
00089         Transformation2D<T> operator * (const Transformation2D<T> &Other) const
00090         {
00091             Transformation2D<T> Temp(*this);
00092             Temp *= Other;
00093             return Temp;
00094         }
00095         
00096         
00097         
00099         dim::matrix4<T> getMatrix() const
00100         {
00101             if (HasChanged_)
00102             {
00103                 Matrix_.reset(dim::vector3d<T>(Position_.X, Position_.Y, T(0)));
00104                 Matrix_.rotateZ(Rotation_);
00105                 Matrix_.scale(dim::vector3d<T>(Scale_.Width, Scale_.Height, T(1)));
00106                 HasChanged_ = false;
00107             }
00108             return Matrix_;
00109         }
00110         
00112         Transformation2D<T> getInverse() const
00113         {
00114             dim::matrix4<T> Mat;
00115             getMatrix(Mat);
00116             Mat.setInverse();
00117             return Transformation2D<T>(Mat);
00118         }
00119         
00121         void interpolate(const Transformation2D<T> &From, const Transformation2D<T> &To, const T &Interpolation)
00122         {
00123             math::Lerp(Position_,   From.Position_, To.Position_,   Interpolation);
00124             math::Lerp(Rotation_,   From.Rotation_, To.Rotation_,   Interpolation);
00125             math::Lerp(Scale_,      From.Scale_,    To.Scale_,      Interpolation);
00126             HasChanged_ = true;
00127         }
00128         
00130         void move(const dim::point2d<T> &Direction)
00131         {
00132             Position_ += (getRotationMatrix() * Direction);
00133             HasChanged_ = true;
00134         }
00136         void turn(const T &Rotation)
00137         {
00138             Rotation_ += Rotation;
00139             HasChanged_ = true;
00140         }
00141         
00142         
00143         
00145         inline void setPosition(const dim::point2d<T> &Position)
00146         {
00147             Position_ = Position;
00148             HasChanged_ = true;
00149         }
00151         inline dim::point2d<T> getPosition() const
00152         {
00153             return Position_;
00154         }
00155         
00157         inline void setRotation(const T &Rotation)
00158         {
00159             Rotation_ = Rotation;
00160             HasChanged_ = true;
00161         }
00163         inline T getRotation() const
00164         {
00165             return Rotation_;
00166         }
00168         inline dim::matrix4<T> getRotationMatrix() const
00169         {
00170             dim::matrix4<T> Mat;
00171             Mat.rotateZ(Rotation_);
00172             return Mat;
00173         }
00174         
00176         inline void setScale(const dim::size2d<T> &Scale)
00177         {
00178             Scale_ = Scale;
00179             HasChanged_ = true;
00180         }
00182         inline dim::size2d<T> getScale() const
00183         {
00184             return Scale_;
00185         }
00186         
00188         inline void translate(const dim::point2d<T> &Direction)
00189         {
00190             Position_ += Direction;
00191             HasChanged_ = true;
00192         }
00194         inline void transform(const dim::size2d<T> &Size)
00195         {
00196             Scale_ += Size;
00197             HasChanged_ = true;
00198         }
00199         
00201         inline void getMatrix(dim::matrix4<T> &Matrix) const
00202         {
00203             Matrix *= getMatrix();
00204         }
00205         
00210         inline dim::matrix4<T> getInverseMatrix() const
00211         {
00212             return getMatrix().getInverse();
00213         }
00214         
00219         inline void setMatrixDirect(const dim::matrix4<T> &Matrix)
00220         {
00221             Matrix_ = Matrix;
00222         }
00224         inline dim::matrix4<T>& getMatrixDirect()
00225         {
00226             return Matrix_;
00227         }
00229         inline dim::matrix4<T> getMatrixDirect() const
00230         {
00231             return Matrix_;
00232         }
00233         
00234     private:
00235         
00236         
00237         
00238         dim::point2d<T> Position_;
00239         T Rotation_;
00240         dim::size2d<T> Scale_;
00241         
00242         mutable dim::matrix4<T> Matrix_;
00243         mutable bool HasChanged_;
00244         
00245 };
00246 
00247 
00248 typedef Transformation2D<f32> ScreenSpaceTransformation;
00249 
00250 
00251 } 
00252 
00253 } 
00254 
00255 
00256 #endif
00257 
00258 
00259 
00260