dimension2d.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #ifndef IRR_DIMENSION2D_H_INCLUDED
  5. #define IRR_DIMENSION2D_H_INCLUDED
  6. #include "irrTypes.h"
  7. #include "irrMath.h" // for irr::core::equals()
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. template <class T>
  13. class vector2d;
  14. //! Specifies a 2 dimensional size.
  15. template <class T>
  16. class dimension2d
  17. {
  18. public:
  19. //! Default constructor for empty dimension
  20. dimension2d() : Width(0), Height(0) {}
  21. //! Constructor with width and height
  22. dimension2d(const T& width, const T& height)
  23. : Width(width), Height(height) {}
  24. dimension2d(const vector2d<T>& other); // Defined in vector2d.h
  25. //! Use this constructor only where you are sure that the conversion is valid.
  26. template <class U>
  27. explicit dimension2d(const dimension2d<U>& other) :
  28. Width((T)other.Width), Height((T)other.Height) { }
  29. template <class U>
  30. dimension2d<T>& operator=(const dimension2d<U>& other)
  31. {
  32. Width = (T) other.Width;
  33. Height = (T) other.Height;
  34. return *this;
  35. }
  36. //! Equality operator
  37. bool operator==(const dimension2d<T>& other) const
  38. {
  39. return core::equals(Width, other.Width) &&
  40. core::equals(Height, other.Height);
  41. }
  42. //! Inequality operator
  43. bool operator!=(const dimension2d<T>& other) const
  44. {
  45. return ! (*this == other);
  46. }
  47. bool operator==(const vector2d<T>& other) const; // Defined in vector2d.h
  48. bool operator!=(const vector2d<T>& other) const
  49. {
  50. return !(*this == other);
  51. }
  52. //! Set to new values
  53. dimension2d<T>& set(const T& width, const T& height)
  54. {
  55. Width = width;
  56. Height = height;
  57. return *this;
  58. }
  59. //! Divide width and height by scalar
  60. dimension2d<T>& operator/=(const T& scale)
  61. {
  62. Width /= scale;
  63. Height /= scale;
  64. return *this;
  65. }
  66. //! Divide width and height by scalar
  67. dimension2d<T> operator/(const T& scale) const
  68. {
  69. return dimension2d<T>(Width/scale, Height/scale);
  70. }
  71. //! Multiply width and height by scalar
  72. dimension2d<T>& operator*=(const T& scale)
  73. {
  74. Width *= scale;
  75. Height *= scale;
  76. return *this;
  77. }
  78. //! Multiply width and height by scalar
  79. dimension2d<T> operator*(const T& scale) const
  80. {
  81. return dimension2d<T>(Width*scale, Height*scale);
  82. }
  83. //! Add another dimension to this one.
  84. dimension2d<T>& operator+=(const dimension2d<T>& other)
  85. {
  86. Width += other.Width;
  87. Height += other.Height;
  88. return *this;
  89. }
  90. //! Add two dimensions
  91. dimension2d<T> operator+(const dimension2d<T>& other) const
  92. {
  93. return dimension2d<T>(Width+other.Width, Height+other.Height);
  94. }
  95. //! Subtract a dimension from this one
  96. dimension2d<T>& operator-=(const dimension2d<T>& other)
  97. {
  98. Width -= other.Width;
  99. Height -= other.Height;
  100. return *this;
  101. }
  102. //! Subtract one dimension from another
  103. dimension2d<T> operator-(const dimension2d<T>& other) const
  104. {
  105. return dimension2d<T>(Width-other.Width, Height-other.Height);
  106. }
  107. //! Get area
  108. T getArea() const
  109. {
  110. return Width*Height;
  111. }
  112. //! Get the optimal size according to some properties
  113. /** This is a function often used for texture dimension
  114. calculations. The function returns the next larger or
  115. smaller dimension which is a power-of-two dimension
  116. (2^n,2^m) and/or square (Width=Height).
  117. \param requirePowerOfTwo Forces the result to use only
  118. powers of two as values.
  119. \param requireSquare Makes width==height in the result
  120. \param larger Choose whether the result is larger or
  121. smaller than the current dimension. If one dimension
  122. need not be changed it is kept with any value of larger.
  123. \param maxValue Maximum texturesize. if value > 0 size is
  124. clamped to maxValue
  125. \return The optimal dimension under the given
  126. constraints. */
  127. dimension2d<T> getOptimalSize(
  128. bool requirePowerOfTwo=true,
  129. bool requireSquare=false,
  130. bool larger=true,
  131. u32 maxValue = 0) const
  132. {
  133. u32 i=1;
  134. u32 j=1;
  135. if (requirePowerOfTwo)
  136. {
  137. while (i<(u32)Width)
  138. i<<=1;
  139. if (!larger && i!=1 && i!=(u32)Width)
  140. i>>=1;
  141. while (j<(u32)Height)
  142. j<<=1;
  143. if (!larger && j!=1 && j!=(u32)Height)
  144. j>>=1;
  145. }
  146. else
  147. {
  148. i=(u32)Width;
  149. j=(u32)Height;
  150. }
  151. if (requireSquare)
  152. {
  153. if ((larger && (i>j)) || (!larger && (i<j)))
  154. j=i;
  155. else
  156. i=j;
  157. }
  158. if ( maxValue > 0 && i > maxValue)
  159. i = maxValue;
  160. if ( maxValue > 0 && j > maxValue)
  161. j = maxValue;
  162. return dimension2d<T>((T)i,(T)j);
  163. }
  164. //! Get the interpolated dimension
  165. /** \param other Other dimension to interpolate with.
  166. \param d Value between 0.0f and 1.0f. d=0 returns other, d=1 returns this, values between interpolate.
  167. \return Interpolated dimension. */
  168. dimension2d<T> getInterpolated(const dimension2d<T>& other, f32 d) const
  169. {
  170. const f32 inv = (1.0f - d);
  171. return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));
  172. }
  173. //! Width of the dimension.
  174. T Width;
  175. //! Height of the dimension.
  176. T Height;
  177. };
  178. //! Typedef for an f32 dimension.
  179. typedef dimension2d<f32> dimension2df;
  180. //! Typedef for an unsigned integer dimension.
  181. typedef dimension2d<u32> dimension2du;
  182. //! Typedef for an integer dimension.
  183. /** There are few cases where negative dimensions make sense. Please consider using
  184. dimension2du instead. */
  185. typedef dimension2d<s32> dimension2di;
  186. } // end namespace core
  187. } // end namespace irr
  188. #endif