dimension2d.h 5.2 KB

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