1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __IRR_DIMENSION2D_H_INCLUDED__
\r
6 #define __IRR_DIMENSION2D_H_INCLUDED__
\r
8 #include "irrTypes.h"
\r
9 #include "irrMath.h" // for irr::core::equals()
\r
18 //! Specifies a 2 dimensional size.
\r
23 //! Default constructor for empty dimension
\r
24 dimension2d() : Width(0), Height(0) {}
\r
25 //! Constructor with width and height
\r
26 dimension2d(const T& width, const T& height)
\r
27 : Width(width), Height(height) {}
\r
29 dimension2d(const vector2d<T>& other); // Defined in vector2d.h
\r
31 //! Use this constructor only where you are sure that the conversion is valid.
\r
33 explicit dimension2d(const dimension2d<U>& other) :
\r
34 Width((T)other.Width), Height((T)other.Height) { }
\r
37 dimension2d<T>& operator=(const dimension2d<U>& other)
\r
39 Width = (T) other.Width;
\r
40 Height = (T) other.Height;
\r
45 //! Equality operator
\r
46 bool operator==(const dimension2d<T>& other) const
\r
48 return core::equals(Width, other.Width) &&
\r
49 core::equals(Height, other.Height);
\r
52 //! Inequality operator
\r
53 bool operator!=(const dimension2d<T>& other) const
\r
55 return ! (*this == other);
\r
58 bool operator==(const vector2d<T>& other) const; // Defined in vector2d.h
\r
60 bool operator!=(const vector2d<T>& other) const
\r
62 return !(*this == other);
\r
65 //! Set to new values
\r
66 dimension2d<T>& set(const T& width, const T& height)
\r
73 //! Divide width and height by scalar
\r
74 dimension2d<T>& operator/=(const T& scale)
\r
81 //! Divide width and height by scalar
\r
82 dimension2d<T> operator/(const T& scale) const
\r
84 return dimension2d<T>(Width/scale, Height/scale);
\r
87 //! Multiply width and height by scalar
\r
88 dimension2d<T>& operator*=(const T& scale)
\r
95 //! Multiply width and height by scalar
\r
96 dimension2d<T> operator*(const T& scale) const
\r
98 return dimension2d<T>(Width*scale, Height*scale);
\r
101 //! Add another dimension to this one.
\r
102 dimension2d<T>& operator+=(const dimension2d<T>& other)
\r
104 Width += other.Width;
\r
105 Height += other.Height;
\r
109 //! Add two dimensions
\r
110 dimension2d<T> operator+(const dimension2d<T>& other) const
\r
112 return dimension2d<T>(Width+other.Width, Height+other.Height);
\r
115 //! Subtract a dimension from this one
\r
116 dimension2d<T>& operator-=(const dimension2d<T>& other)
\r
118 Width -= other.Width;
\r
119 Height -= other.Height;
\r
123 //! Subtract one dimension from another
\r
124 dimension2d<T> operator-(const dimension2d<T>& other) const
\r
126 return dimension2d<T>(Width-other.Width, Height-other.Height);
\r
132 return Width*Height;
\r
135 //! Get the optimal size according to some properties
\r
136 /** This is a function often used for texture dimension
\r
137 calculations. The function returns the next larger or
\r
138 smaller dimension which is a power-of-two dimension
\r
139 (2^n,2^m) and/or square (Width=Height).
\r
140 \param requirePowerOfTwo Forces the result to use only
\r
141 powers of two as values.
\r
142 \param requireSquare Makes width==height in the result
\r
143 \param larger Choose whether the result is larger or
\r
144 smaller than the current dimension. If one dimension
\r
145 need not be changed it is kept with any value of larger.
\r
146 \param maxValue Maximum texturesize. if value > 0 size is
\r
147 clamped to maxValue
\r
148 \return The optimal dimension under the given
\r
150 dimension2d<T> getOptimalSize(
\r
151 bool requirePowerOfTwo=true,
\r
152 bool requireSquare=false,
\r
154 u32 maxValue = 0) const
\r
158 if (requirePowerOfTwo)
\r
160 while (i<(u32)Width)
\r
162 if (!larger && i!=1 && i!=(u32)Width)
\r
164 while (j<(u32)Height)
\r
166 if (!larger && j!=1 && j!=(u32)Height)
\r
177 if ((larger && (i>j)) || (!larger && (i<j)))
\r
183 if ( maxValue > 0 && i > maxValue)
\r
186 if ( maxValue > 0 && j > maxValue)
\r
189 return dimension2d<T>((T)i,(T)j);
\r
192 //! Get the interpolated dimension
\r
193 /** \param other Other dimension to interpolate with.
\r
194 \param d Value between 0.0f and 1.0f. d=0 returns other, d=1 returns this, values between interpolate.
\r
195 \return Interpolated dimension. */
\r
196 dimension2d<T> getInterpolated(const dimension2d<T>& other, f32 d) const
\r
198 f32 inv = (1.0f - d);
\r
199 return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));
\r
203 //! Width of the dimension.
\r
205 //! Height of the dimension.
\r
209 //! Typedef for an f32 dimension.
\r
210 typedef dimension2d<f32> dimension2df;
\r
211 //! Typedef for an unsigned integer dimension.
\r
212 typedef dimension2d<u32> dimension2du;
\r
214 //! Typedef for an integer dimension.
\r
215 /** There are few cases where negative dimensions make sense. Please consider using
\r
216 dimension2du instead. */
\r
217 typedef dimension2d<s32> dimension2di;
\r
220 } // end namespace core
\r
221 } // end namespace irr
\r