Vector.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef __DARKRL__VECTOR_HPP__
  2. #define __DARKRL__VECTOR_HPP__
  3. #include <assert.h>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <stdint.h>
  7. #include "Math.hpp"
  8. template<class T>
  9. struct Vector2
  10. {
  11. Vector2() : x( 0 ), y( 0 ) {}
  12. Vector2( T v ) : x( v ), y( v ) {}
  13. Vector2( T _x, T _y ) : x( _x ), y( _y ) {}
  14. bool operator==( const Vector2<T>& rhs ) const { return x == rhs.x && y == rhs.y; }
  15. bool operator!=( const Vector2<T>& rhs ) const { return !( *this == rhs ); }
  16. Vector2<T>& operator+=( const Vector2<T>& rhs )
  17. {
  18. x += rhs.x;
  19. y += rhs.y;
  20. return *this;
  21. }
  22. Vector2<T>& operator-=( const Vector2<T>& rhs )
  23. {
  24. x -= rhs.x;
  25. y -= rhs.y;
  26. return *this;
  27. }
  28. Vector2<T>& operator*=( const Vector2<T>& rhs )
  29. {
  30. x *= rhs.x;
  31. y *= rhs.y;
  32. return *this;
  33. }
  34. T x, y;
  35. };
  36. template<class T>
  37. Vector2<T> operator+( const Vector2<T>& lhs, const Vector2<T>& rhs )
  38. {
  39. return Vector2<T>( lhs.x + rhs.x, lhs.y + rhs.y );
  40. }
  41. template<class T>
  42. Vector2<T> operator-( const Vector2<T>& lhs, const Vector2<T>& rhs )
  43. {
  44. return Vector2<T>( lhs.x - rhs.x, lhs.y - rhs.y );
  45. }
  46. template<class T>
  47. Vector2<T> operator*( const Vector2<T>& lhs, const float& rhs )
  48. {
  49. return Vector2<T>( lhs.x * rhs, lhs.y * rhs );
  50. }
  51. template<class T>
  52. Vector2<T> operator/( const Vector2<T>& lhs, const T& rhs )
  53. {
  54. return Vector2<T>( lhs.x / rhs, lhs.y / rhs );
  55. }
  56. typedef Vector2<int32_t> v2i;
  57. typedef Vector2<float> v2f;
  58. template<class T>
  59. struct Vector3
  60. {
  61. Vector3() : x( 0 ), y( 0 ), z( 0 ) {}
  62. Vector3( T v ) : x( v ), y( v ), z( v ) {}
  63. Vector3( T _x, T _y, T _z ) : x( _x ), y( _y ), z( _z ) {}
  64. template<class Y>
  65. Vector3( const Vector3<Y>& v ) : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ) {}
  66. T Luminance() const { return T( x * 0.3f + y * 0.59f + z * 0.11f ); }
  67. void Clamp()
  68. {
  69. x = std::min( T(1), std::max( T(0), x ) );
  70. y = std::min( T(1), std::max( T(0), y ) );
  71. z = std::min( T(1), std::max( T(0), z ) );
  72. }
  73. bool operator==( const Vector3<T>& rhs ) const { return x == rhs.x && y == rhs.y && z == rhs.z; }
  74. bool operator!=( const Vector2<T>& rhs ) const { return !( *this == rhs ); }
  75. T& operator[]( unsigned int idx ) { assert( idx < 3 ); return ((T*)this)[idx]; }
  76. const T& operator[]( unsigned int idx ) const { assert( idx < 3 ); return ((T*)this)[idx]; }
  77. Vector3<T> operator+=( const Vector3<T>& rhs )
  78. {
  79. x += rhs.x;
  80. y += rhs.y;
  81. z += rhs.z;
  82. return *this;
  83. }
  84. Vector3<T> operator*=( const Vector3<T>& rhs )
  85. {
  86. x *= rhs.x;
  87. y *= rhs.y;
  88. z *= rhs.z;
  89. return *this;
  90. }
  91. Vector3<T> operator*=( const float& rhs )
  92. {
  93. x *= rhs;
  94. y *= rhs;
  95. z *= rhs;
  96. return *this;
  97. }
  98. T x, y, z;
  99. T padding;
  100. };
  101. template<class T>
  102. Vector3<T> operator+( const Vector3<T>& lhs, const Vector3<T>& rhs )
  103. {
  104. return Vector3<T>( lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z );
  105. }
  106. template<class T>
  107. Vector3<T> operator-( const Vector3<T>& lhs, const Vector3<T>& rhs )
  108. {
  109. return Vector3<T>( lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z );
  110. }
  111. template<class T>
  112. Vector3<T> operator*( const Vector3<T>& lhs, const Vector3<T>& rhs )
  113. {
  114. return Vector3<T>( lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z );
  115. }
  116. template<class T>
  117. Vector3<T> operator*( const Vector3<T>& lhs, const float& rhs )
  118. {
  119. return Vector3<T>( T( lhs.x * rhs ), T( lhs.y * rhs ), T( lhs.z * rhs ) );
  120. }
  121. template<class T>
  122. Vector3<T> operator/( const Vector3<T>& lhs, const T& rhs )
  123. {
  124. return Vector3<T>( lhs.x / rhs, lhs.y / rhs, lhs.z / rhs );
  125. }
  126. template<class T>
  127. bool operator<( const Vector3<T>& lhs, const Vector3<T>& rhs )
  128. {
  129. return lhs.Luminance() < rhs.Luminance();
  130. }
  131. typedef Vector3<int32_t> v3i;
  132. typedef Vector3<float> v3f;
  133. typedef Vector3<uint8_t> v3b;
  134. static inline v3b v3f_to_v3b( const v3f& v )
  135. {
  136. return v3b( uint8_t( std::min( 1.f, v.x ) * 255 ), uint8_t( std::min( 1.f, v.y ) * 255 ), uint8_t( std::min( 1.f, v.z ) * 255 ) );
  137. }
  138. template<class T>
  139. Vector3<T> Mix( const Vector3<T>& v1, const Vector3<T>& v2, float amount )
  140. {
  141. return v1 + ( v2 - v1 ) * amount;
  142. }
  143. template<>
  144. inline v3b Mix( const v3b& v1, const v3b& v2, float amount )
  145. {
  146. return v3b( v3f( v1 ) + ( v3f( v2 ) - v3f( v1 ) ) * amount );
  147. }
  148. template<class T>
  149. Vector3<T> Desaturate( const Vector3<T>& v )
  150. {
  151. T l = v.Luminance();
  152. return Vector3<T>( l, l, l );
  153. }
  154. template<class T>
  155. Vector3<T> Desaturate( const Vector3<T>& v, float mul )
  156. {
  157. T l = T( v.Luminance() * mul );
  158. return Vector3<T>( l, l, l );
  159. }
  160. template<class T>
  161. Vector3<T> pow( const Vector3<T>& base, float exponent )
  162. {
  163. return Vector3<T>(
  164. pow( base.x, exponent ),
  165. pow( base.y, exponent ),
  166. pow( base.z, exponent ) );
  167. }
  168. template<class T>
  169. Vector3<T> sRGB2linear( const Vector3<T>& v )
  170. {
  171. return Vector3<T>(
  172. sRGB2linear( v.x ),
  173. sRGB2linear( v.y ),
  174. sRGB2linear( v.z ) );
  175. }
  176. template<class T>
  177. Vector3<T> linear2sRGB( const Vector3<T>& v )
  178. {
  179. return Vector3<T>(
  180. linear2sRGB( v.x ),
  181. linear2sRGB( v.y ),
  182. linear2sRGB( v.z ) );
  183. }
  184. #endif