Irrlicht 3D Engine
dimension2d.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __IRR_DIMENSION2D_H_INCLUDED__
6 #define __IRR_DIMENSION2D_H_INCLUDED__
7 
8 #include "irrTypes.h"
9 #include "irrMath.h" // for irr::core::equals()
10 
11 namespace irr
12 {
13 namespace core
14 {
15  template <class T>
16  class vector2d;
17 
19  template <class T>
21  {
22  public:
24  dimension2d() : Width(0), Height(0) {}
26  dimension2d(const T& width, const T& height)
27  : Width(width), Height(height) {}
28 
29  dimension2d(const vector2d<T>& other); // Defined in vector2d.h
30 
32  template <class U>
33  explicit dimension2d(const dimension2d<U>& other) :
34  Width((T)other.Width), Height((T)other.Height) { }
35 
36  template <class U>
38  {
39  Width = (T) other.Width;
40  Height = (T) other.Height;
41  return *this;
42  }
43 
44 
46  bool operator==(const dimension2d<T>& other) const
47  {
48  return core::equals(Width, other.Width) &&
49  core::equals(Height, other.Height);
50  }
51 
53  bool operator!=(const dimension2d<T>& other) const
54  {
55  return ! (*this == other);
56  }
57 
58  bool operator==(const vector2d<T>& other) const; // Defined in vector2d.h
59 
60  bool operator!=(const vector2d<T>& other) const
61  {
62  return !(*this == other);
63  }
64 
66  dimension2d<T>& set(const T& width, const T& height)
67  {
68  Width = width;
69  Height = height;
70  return *this;
71  }
72 
74  dimension2d<T>& operator/=(const T& scale)
75  {
76  Width /= scale;
77  Height /= scale;
78  return *this;
79  }
80 
82  dimension2d<T> operator/(const T& scale) const
83  {
84  return dimension2d<T>(Width/scale, Height/scale);
85  }
86 
88  dimension2d<T>& operator*=(const T& scale)
89  {
90  Width *= scale;
91  Height *= scale;
92  return *this;
93  }
94 
96  dimension2d<T> operator*(const T& scale) const
97  {
98  return dimension2d<T>(Width*scale, Height*scale);
99  }
100 
103  {
104  Width += other.Width;
105  Height += other.Height;
106  return *this;
107  }
108 
111  {
112  return dimension2d<T>(Width+other.Width, Height+other.Height);
113  }
114 
117  {
118  Width -= other.Width;
119  Height -= other.Height;
120  return *this;
121  }
122 
125  {
126  return dimension2d<T>(Width-other.Width, Height-other.Height);
127  }
128 
130  T getArea() const
131  {
132  return Width*Height;
133  }
134 
136 
151  bool requirePowerOfTwo=true,
152  bool requireSquare=false,
153  bool larger=true,
154  u32 maxValue = 0) const
155  {
156  u32 i=1;
157  u32 j=1;
158  if (requirePowerOfTwo)
159  {
160  while (i<(u32)Width)
161  i<<=1;
162  if (!larger && i!=1 && i!=(u32)Width)
163  i>>=1;
164  while (j<(u32)Height)
165  j<<=1;
166  if (!larger && j!=1 && j!=(u32)Height)
167  j>>=1;
168  }
169  else
170  {
171  i=(u32)Width;
172  j=(u32)Height;
173  }
174 
175  if (requireSquare)
176  {
177  if ((larger && (i>j)) || (!larger && (i<j)))
178  j=i;
179  else
180  i=j;
181  }
182 
183  if ( maxValue > 0 && i > maxValue)
184  i = maxValue;
185 
186  if ( maxValue > 0 && j > maxValue)
187  j = maxValue;
188 
189  return dimension2d<T>((T)i,(T)j);
190  }
191 
193 
197  {
198  f32 inv = (1.0f - d);
199  return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));
200  }
201 
202 
204  T Width;
207  };
208 
213 
215 
218 
219 
220 } // end namespace core
221 } // end namespace irr
222 
223 #endif
224 
dimension2d< u32 > dimension2du
Typedef for an unsigned integer dimension.
Definition: dimension2d.h:212
bool operator!=(const vector2d< T > &other) const
Definition: dimension2d.h:60
dimension2d()
Default constructor for empty dimension.
Definition: dimension2d.h:24
dimension2d< s32 > dimension2di
Typedef for an integer dimension.
Definition: dimension2d.h:217
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
T getArea() const
Get area.
Definition: dimension2d.h:130
dimension2d< T > getInterpolated(const dimension2d< T > &other, f32 d) const
Get the interpolated dimension.
Definition: dimension2d.h:196
dimension2d< T > operator-(const dimension2d< T > &other) const
Subtract one dimension from another.
Definition: dimension2d.h:124
dimension2d< T > & operator+=(const dimension2d< T > &other)
Add another dimension to this one.
Definition: dimension2d.h:102
dimension2d< T > & operator/=(const T &scale)
Divide width and height by scalar.
Definition: dimension2d.h:74
dimension2d< T > operator *(const T &scale) const
Multiply width and height by scalar.
Definition: dimension2d.h:96
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
Specifies a 2 dimensional size.
Definition: dimension2d.h:20
dimension2d< T > operator/(const T &scale) const
Divide width and height by scalar.
Definition: dimension2d.h:82
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:246
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
dimension2d< T > getOptimalSize(bool requirePowerOfTwo=true, bool requireSquare=false, bool larger=true, u32 maxValue=0) const
Get the optimal size according to some properties.
Definition: dimension2d.h:150
dimension2d< f32 > dimension2df
Typedef for an f32 dimension.
Definition: dimension2d.h:210
bool operator==(const dimension2d< T > &other) const
Equality operator.
Definition: dimension2d.h:46
dimension2d< T > & operator-=(const dimension2d< T > &other)
Subtract a dimension from this one.
Definition: dimension2d.h:116
T Width
Width of the dimension.
Definition: dimension2d.h:204
dimension2d< T > & operator *=(const T &scale)
Multiply width and height by scalar.
Definition: dimension2d.h:88
dimension2d< T > operator+(const dimension2d< T > &other) const
Add two dimensions.
Definition: dimension2d.h:110
2d vector template class with lots of operators and methods.
Definition: dimension2d.h:16
dimension2d< T > & operator=(const dimension2d< U > &other)
Definition: dimension2d.h:37
bool operator!=(const dimension2d< T > &other) const
Inequality operator.
Definition: dimension2d.h:53
T Height
Height of the dimension.
Definition: dimension2d.h:206
dimension2d< T > & set(const T &width, const T &height)
Set to new values.
Definition: dimension2d.h:66
dimension2d(const dimension2d< U > &other)
Use this constructor only where you are sure that the conversion is valid.
Definition: dimension2d.h:33
dimension2d(const T &width, const T &height)
Constructor with width and height.
Definition: dimension2d.h:26