Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials

dimension2d.h

Go to the documentation of this file.
00001 // Copyright (C) 2002-2010 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
00004 
00005 #ifndef __IRR_DIMENSION2D_H_INCLUDED__
00006 #define __IRR_DIMENSION2D_H_INCLUDED__
00007 
00008 #include "irrTypes.h"
00009 #include "irrMath.h" // for irr::core::equals()
00010 
00011 namespace irr
00012 {
00013 namespace core
00014 {
00015         template <class T>
00016         class vector2d;
00017 
00019         template <class T>
00020         class dimension2d
00021         {
00022                 public:
00024                         dimension2d() : Width(0), Height(0) {}
00026                         dimension2d(const T& width, const T& height)
00027                                 : Width(width), Height(height) {}
00028 
00029                         dimension2d(const vector2d<T>& other); // Defined in vector2d.h
00030 
00032                         template <class U>
00033                         explicit dimension2d(const dimension2d<U>& other) :
00034                                 Width((T)other.Width), Height((T)other.Height) { }
00035 
00036                         template <class U>
00037                         dimension2d<T>& operator=(const dimension2d<U>& other)
00038                         { 
00039                                 Width = (T) other.Width;
00040                                 Height = (T) other.Height;
00041                                 return *this;
00042                         }
00043 
00044 
00046                         bool operator==(const dimension2d<T>& other) const
00047                         {
00048                                 return core::equals(Width, other.Width) &&
00049                                                 core::equals(Height, other.Height);
00050                         }
00051 
00053                         bool operator!=(const dimension2d<T>& other) const
00054                         {
00055                                 return ! (*this == other);
00056                         }
00057 
00058                         bool operator==(const vector2d<T>& other) const;  // Defined in vector2d.h
00059 
00060                         bool operator!=(const vector2d<T>& other) const
00061                         {
00062                                 return !(*this == other);
00063                         }
00064 
00066                         dimension2d<T>& set(const T& width, const T& height)
00067                         {
00068                                 Width = width;
00069                                 Height = height;
00070                                 return *this;
00071                         }
00072 
00074                         dimension2d<T>& operator/=(const T& scale)
00075                         {
00076                                 Width /= scale;
00077                                 Height /= scale;
00078                                 return *this;
00079                         }
00080 
00082                         dimension2d<T> operator/(const T& scale) const
00083                         {
00084                                 return dimension2d<T>(Width/scale, Height/scale);
00085                         }
00086 
00088                         dimension2d<T>& operator*=(const T& scale)
00089                         {
00090                                 Width *= scale;
00091                                 Height *= scale;
00092                                 return *this;
00093                         }
00094 
00096                         dimension2d<T> operator*(const T& scale) const
00097                         {
00098                                 return dimension2d<T>(Width*scale, Height*scale);
00099                         }
00100 
00102                         dimension2d<T>& operator+=(const dimension2d<T>& other)
00103                         {
00104                                 Width += other.Width;
00105                                 Height += other.Height;
00106                                 return *this;
00107                         }
00108 
00110                         dimension2d<T>& operator-=(const dimension2d<T>& other)
00111                         {
00112                                 Width -= other.Width;
00113                                 Height -= other.Height;
00114                                 return *this;
00115                         }
00116 
00117 
00119                         dimension2d<T> operator+(const dimension2d<T>& other) const
00120                         {
00121                                 return dimension2d<T>(Width+other.Width, Height+other.Height);
00122                         }
00123 
00125                         T getArea() const
00126                         {
00127                                 return Width*Height;
00128                         }
00129 
00131 
00145                         dimension2d<T> getOptimalSize(
00146                                         bool requirePowerOfTwo=true,
00147                                         bool requireSquare=false,
00148                                         bool larger=true,
00149                                         u32 maxValue = 0) const
00150                         {
00151                                 u32 i=1;
00152                                 u32 j=1;
00153                                 if (requirePowerOfTwo)
00154                                 {
00155                                         while (i<(u32)Width)
00156                                                 i<<=1;
00157                                         if (!larger && i!=1 && i!=(u32)Width)
00158                                                 i>>=1;
00159                                         while (j<(u32)Height)
00160                                                 j<<=1;
00161                                         if (!larger && j!=1 && j!=(u32)Height)
00162                                                 j>>=1;
00163                                 }
00164                                 else
00165                                 {
00166                                         i=(u32)Width;
00167                                         j=(u32)Height;
00168                                 }
00169 
00170                                 if (requireSquare)
00171                                 {
00172                                         if ((larger && (i>j)) || (!larger && (i<j)))
00173                                                 j=i;
00174                                         else
00175                                                 i=j;
00176                                 }
00177 
00178                                 if ( maxValue > 0 && i > maxValue)
00179                                         i = maxValue;
00180 
00181                                 if ( maxValue > 0 && j > maxValue)
00182                                         j = maxValue;
00183 
00184                                 return dimension2d<T>((T)i,(T)j);
00185                         }
00186 
00188 
00191                         dimension2d<T> getInterpolated(const dimension2d<T>& other, f32 d) const
00192                         {
00193                                 f32 inv = (1.0f - d);
00194                                 return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));
00195                         }
00196 
00197 
00199                         T Width;
00201                         T Height;
00202         };
00203 
00205         typedef dimension2d<f32> dimension2df;
00207         typedef dimension2d<u32> dimension2du;
00208 
00210 
00212         typedef dimension2d<s32> dimension2di;
00213 
00214 
00215 } // end namespace core
00216 } // end namespace irr
00217 
00218 #endif
00219 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2010 by Nikolaus Gebhardt. Generated on Sun Oct 24 12:41:56 2010 by Doxygen (1.6.2)