Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #ifndef __SP_DIMENSION_SIZE2D_H__
00009 #define __SP_DIMENSION_SIZE2D_H__
00010 
00011 
00012 #include "Base/spStandard.hpp"
00013 #include "Base/spMathCore.hpp"
00014 
00015 
00016 namespace sp
00017 {
00018 namespace dim
00019 {
00020 
00021 
00022 template <typename T> class size2d
00023 {
00024     
00025     public:
00026         
00027         size2d() :
00028             Width   (0),
00029             Height  (0)
00030         {
00031         }
00032         size2d(T Size) :
00033             Width   (Size),
00034             Height  (Size)
00035         {
00036         }
00037         size2d(T InitWidth, T InitHeight) :
00038             Width   (InitWidth  ),
00039             Height  (InitHeight )
00040         {
00041         }
00042         size2d(const size2d<T> &Other) :
00043             Width   (Other.Width    ),
00044             Height  (Other.Height   )
00045         {
00046         }
00047         ~size2d()
00048         {
00049         }
00050         
00051         
00052         
00053         inline bool operator == (const size2d<T> &Other) const
00054         {
00055             return Width == Other.Width && Height == Other.Height;
00056         }
00057         inline bool operator != (const size2d<T> &Other) const
00058         {
00059             return Width != Other.Width || Height != Other.Height;
00060         }
00061         
00063         inline bool operator > (const size2d<T> &Other) const
00064         {
00065             return getArea() > Other.getArea();
00066         }
00068         inline bool operator < (const size2d<T> &Other) const
00069         {
00070             return getArea() < Other.getArea();
00071         }
00072         
00074         inline bool operator >= (const size2d<T> &Other) const
00075         {
00076             return getArea() >= Other.getArea();
00077         }
00079         inline bool operator <= (const size2d<T> &Other) const
00080         {
00081             return getArea() <= Other.getArea();
00082         }
00083         
00084         
00085         
00086         inline size2d<T> operator + (const size2d<T> &Other) const
00087         {
00088             return size2d<T>(Width + Other.Width, Height + Other.Height);
00089         }
00090         inline size2d<T>& operator += (const size2d<T> &Other)
00091         {
00092             Width += Other.Width; Height += Other.Height; return *this;
00093         }
00094         
00095         inline size2d<T> operator - (const size2d<T> &Other) const
00096         {
00097             return size2d<T>(Width - Other.Width, Height - Other.Height);
00098         }
00099         inline size2d<T>& operator -= (const size2d<T> &Other)
00100         {
00101             Width -= Other.Width; Height -= Other.Height; return *this;
00102         }
00103         
00104         inline size2d<T> operator / (const size2d<T> &Other) const
00105         {
00106             return size2d<T>(Width / Other.Width, Height / Other.Height);
00107         }
00108         inline size2d<T>& operator /= (const size2d<T> &Other)
00109         {
00110             Width /= Other.Width; Height /= Other.Height; return *this;
00111         }
00112         
00113         inline size2d<T> operator * (const size2d<T> &Other) const
00114         {
00115             return size2d<T>(Width * Other.Width, Height * Other.Height);
00116         }
00117         inline size2d<T>& operator *= (const size2d<T> &Other)
00118         {
00119             Width *= Other.Width; Height *= Other.Height; return *this;
00120         }
00121         
00122         inline size2d<T> operator - () const
00123         {
00124             return size2d<T>(-Width, -Height);
00125         }
00126         
00127         
00128         
00129         inline T getArea() const
00130         {
00131             return Width * Height;
00132         }
00133         
00135         inline size2d<T> getClampedSize(const size2d<T> &MaxSize) const
00136         {
00137             if (Width < MaxSize.Width && Height < MaxSize.Height)
00138                 return *this;
00139             
00140             f64 Scale = 1.0;
00141             
00142             if (Width - MaxSize.Width > Height - MaxSize.Height)
00143                 Scale = static_cast<f64>(MaxSize.Width) / Width;
00144             else
00145                 Scale = static_cast<f64>(MaxSize.Height) / Height;
00146             
00147             return dim::size2d<T>(
00148                 math::Min(static_cast<T>(Scale * Width), MaxSize.Width),
00149                 math::Min(static_cast<T>(Scale * Height), MaxSize.Width)
00150             );
00151         }
00152         
00153         template <typename B> inline size2d<B> cast() const
00154         {
00155             return size2d<B>(static_cast<B>(Width), static_cast<B>(Height));
00156         }
00157         
00158         
00159         
00160         T Width, Height;
00161         
00162 };
00163 
00164 typedef size2d<s32> size2di;
00165 typedef size2d<f32> size2df;
00166 
00167 
00168 } 
00169 
00170 } 
00171 
00172 
00173 #endif
00174 
00175 
00176 
00177