vec2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "math.h"
  5. namespace embree
  6. {
  7. struct Vec2fa;
  8. ////////////////////////////////////////////////////////////////////////////////
  9. /// Generic 2D vector Class
  10. ////////////////////////////////////////////////////////////////////////////////
  11. template<typename T> struct Vec2
  12. {
  13. enum { N = 2 };
  14. union {
  15. struct { T x, y; };
  16. #if !(defined(__WIN32__) && _MSC_VER == 1800) // workaround for older VS 2013 compiler
  17. T components[N];
  18. #endif
  19. };
  20. typedef T Scalar;
  21. ////////////////////////////////////////////////////////////////////////////////
  22. /// Construction
  23. ////////////////////////////////////////////////////////////////////////////////
  24. __forceinline Vec2( ) {}
  25. __forceinline explicit Vec2( const T& a ) : x(a), y(a) {}
  26. __forceinline Vec2( const T& x, const T& y ) : x(x), y(y) {}
  27. __forceinline Vec2( const Vec2& other ) { x = other.x; y = other.y; }
  28. __forceinline Vec2( const Vec2fa& other );
  29. template<typename T1> __forceinline Vec2( const Vec2<T1>& a ) : x(T(a.x)), y(T(a.y)) {}
  30. template<typename T1> __forceinline Vec2& operator =( const Vec2<T1>& other ) { x = other.x; y = other.y; return *this; }
  31. __forceinline Vec2& operator =( const Vec2& other ) { x = other.x; y = other.y; return *this; }
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /// Constants
  34. ////////////////////////////////////////////////////////////////////////////////
  35. __forceinline Vec2( ZeroTy ) : x(zero), y(zero) {}
  36. __forceinline Vec2( OneTy ) : x(one), y(one) {}
  37. __forceinline Vec2( PosInfTy ) : x(pos_inf), y(pos_inf) {}
  38. __forceinline Vec2( NegInfTy ) : x(neg_inf), y(neg_inf) {}
  39. #if defined(__WIN32__) && _MSC_VER == 1800 // workaround for older VS 2013 compiler
  40. __forceinline const T& operator [](const size_t axis) const { assert(axis < 2); return (&x)[axis]; }
  41. __forceinline T& operator [](const size_t axis) { assert(axis < 2); return (&x)[axis]; }
  42. #else
  43. __forceinline const T& operator [](const size_t axis) const { assert(axis < 2); return components[axis]; }
  44. __forceinline T& operator [](const size_t axis ) { assert(axis < 2); return components[axis]; }
  45. #endif
  46. };
  47. ////////////////////////////////////////////////////////////////////////////////
  48. /// Unary Operators
  49. ////////////////////////////////////////////////////////////////////////////////
  50. template<typename T> __forceinline Vec2<T> operator +( const Vec2<T>& a ) { return Vec2<T>(+a.x, +a.y); }
  51. template<typename T> __forceinline Vec2<T> operator -( const Vec2<T>& a ) { return Vec2<T>(-a.x, -a.y); }
  52. template<typename T> __forceinline Vec2<T> abs ( const Vec2<T>& a ) { return Vec2<T>(abs (a.x), abs (a.y)); }
  53. template<typename T> __forceinline Vec2<T> rcp ( const Vec2<T>& a ) { return Vec2<T>(rcp (a.x), rcp (a.y)); }
  54. template<typename T> __forceinline Vec2<T> rsqrt ( const Vec2<T>& a ) { return Vec2<T>(rsqrt(a.x), rsqrt(a.y)); }
  55. template<typename T> __forceinline Vec2<T> sqrt ( const Vec2<T>& a ) { return Vec2<T>(sqrt (a.x), sqrt (a.y)); }
  56. template<typename T> __forceinline Vec2<T> frac ( const Vec2<T>& a ) { return Vec2<T>(frac (a.x), frac (a.y)); }
  57. ////////////////////////////////////////////////////////////////////////////////
  58. /// Binary Operators
  59. ////////////////////////////////////////////////////////////////////////////////
  60. template<typename T> __forceinline Vec2<T> operator +( const Vec2<T>& a, const Vec2<T>& b ) { return Vec2<T>(a.x + b.x, a.y + b.y); }
  61. template<typename T> __forceinline Vec2<T> operator +( const Vec2<T>& a, const T& b ) { return Vec2<T>(a.x + b , a.y + b ); }
  62. template<typename T> __forceinline Vec2<T> operator +( const T& a, const Vec2<T>& b ) { return Vec2<T>(a + b.x, a + b.y); }
  63. template<typename T> __forceinline Vec2<T> operator -( const Vec2<T>& a, const Vec2<T>& b ) { return Vec2<T>(a.x - b.x, a.y - b.y); }
  64. template<typename T> __forceinline Vec2<T> operator -( const Vec2<T>& a, const T& b ) { return Vec2<T>(a.x - b , a.y - b ); }
  65. template<typename T> __forceinline Vec2<T> operator -( const T& a, const Vec2<T>& b ) { return Vec2<T>(a - b.x, a - b.y); }
  66. template<typename T> __forceinline Vec2<T> operator *( const Vec2<T>& a, const Vec2<T>& b ) { return Vec2<T>(a.x * b.x, a.y * b.y); }
  67. template<typename T> __forceinline Vec2<T> operator *( const T& a, const Vec2<T>& b ) { return Vec2<T>(a * b.x, a * b.y); }
  68. template<typename T> __forceinline Vec2<T> operator *( const Vec2<T>& a, const T& b ) { return Vec2<T>(a.x * b , a.y * b ); }
  69. template<typename T> __forceinline Vec2<T> operator /( const Vec2<T>& a, const Vec2<T>& b ) { return Vec2<T>(a.x / b.x, a.y / b.y); }
  70. template<typename T> __forceinline Vec2<T> operator /( const Vec2<T>& a, const T& b ) { return Vec2<T>(a.x / b , a.y / b ); }
  71. template<typename T> __forceinline Vec2<T> operator /( const T& a, const Vec2<T>& b ) { return Vec2<T>(a / b.x, a / b.y); }
  72. template<typename T> __forceinline Vec2<T> min(const Vec2<T>& a, const Vec2<T>& b) { return Vec2<T>(min(a.x, b.x), min(a.y, b.y)); }
  73. template<typename T> __forceinline Vec2<T> max(const Vec2<T>& a, const Vec2<T>& b) { return Vec2<T>(max(a.x, b.x), max(a.y, b.y)); }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. /// Ternary Operators
  76. ////////////////////////////////////////////////////////////////////////////////
  77. template<typename T> __forceinline Vec2<T> madd ( const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>( madd(a.x,b.x,c.x), madd(a.y,b.y,c.y) ); }
  78. template<typename T> __forceinline Vec2<T> msub ( const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>( msub(a.x,b.x,c.x), msub(a.y,b.y,c.y) ); }
  79. template<typename T> __forceinline Vec2<T> nmadd ( const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>(nmadd(a.x,b.x,c.x),nmadd(a.y,b.y,c.y) ); }
  80. template<typename T> __forceinline Vec2<T> nmsub ( const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>(nmsub(a.x,b.x,c.x),nmsub(a.y,b.y,c.y) ); }
  81. template<typename T> __forceinline Vec2<T> madd ( const T& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>( madd(a,b.x,c.x), madd(a,b.y,c.y) ); }
  82. template<typename T> __forceinline Vec2<T> msub ( const T& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>( msub(a,b.x,c.x), msub(a,b.y,c.y) ); }
  83. template<typename T> __forceinline Vec2<T> nmadd ( const T& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>(nmadd(a,b.x,c.x),nmadd(a,b.y,c.y) ); }
  84. template<typename T> __forceinline Vec2<T> nmsub ( const T& a, const Vec2<T>& b, const Vec2<T>& c) { return Vec2<T>(nmsub(a,b.x,c.x),nmsub(a,b.y,c.y) ); }
  85. ////////////////////////////////////////////////////////////////////////////////
  86. /// Assignment Operators
  87. ////////////////////////////////////////////////////////////////////////////////
  88. template<typename T> __forceinline Vec2<T>& operator +=( Vec2<T>& a, const Vec2<T>& b ) { a.x += b.x; a.y += b.y; return a; }
  89. template<typename T> __forceinline Vec2<T>& operator -=( Vec2<T>& a, const Vec2<T>& b ) { a.x -= b.x; a.y -= b.y; return a; }
  90. template<typename T> __forceinline Vec2<T>& operator *=( Vec2<T>& a, const T& b ) { a.x *= b ; a.y *= b ; return a; }
  91. template<typename T> __forceinline Vec2<T>& operator /=( Vec2<T>& a, const T& b ) { a.x /= b ; a.y /= b ; return a; }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. /// Reduction Operators
  94. ////////////////////////////////////////////////////////////////////////////////
  95. template<typename T> __forceinline T reduce_add( const Vec2<T>& a ) { return a.x + a.y; }
  96. template<typename T> __forceinline T reduce_mul( const Vec2<T>& a ) { return a.x * a.y; }
  97. template<typename T> __forceinline T reduce_min( const Vec2<T>& a ) { return min(a.x, a.y); }
  98. template<typename T> __forceinline T reduce_max( const Vec2<T>& a ) { return max(a.x, a.y); }
  99. ////////////////////////////////////////////////////////////////////////////////
  100. /// Comparison Operators
  101. ////////////////////////////////////////////////////////////////////////////////
  102. template<typename T> __forceinline bool operator ==( const Vec2<T>& a, const Vec2<T>& b ) { return a.x == b.x && a.y == b.y; }
  103. template<typename T> __forceinline bool operator !=( const Vec2<T>& a, const Vec2<T>& b ) { return a.x != b.x || a.y != b.y; }
  104. template<typename T> __forceinline bool operator < ( const Vec2<T>& a, const Vec2<T>& b ) {
  105. if (a.x != b.x) return a.x < b.x;
  106. if (a.y != b.y) return a.y < b.y;
  107. return false;
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. /// Shift Operators
  111. ////////////////////////////////////////////////////////////////////////////////
  112. template<typename T> __forceinline Vec2<T> shift_right_1( const Vec2<T>& a ) {
  113. return Vec2<T>(shift_right_1(a.x),shift_right_1(a.y));
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////
  116. /// Euclidean Space Operators
  117. ////////////////////////////////////////////////////////////////////////////////
  118. template<typename T> __forceinline T dot ( const Vec2<T>& a, const Vec2<T>& b ) { return madd(a.x,b.x,a.y*b.y); }
  119. template<typename T> __forceinline Vec2<T> cross ( const Vec2<T>& a ) { return Vec2<T>(-a.y,a.x); }
  120. template<typename T> __forceinline T length ( const Vec2<T>& a ) { return sqrt(dot(a,a)); }
  121. template<typename T> __forceinline Vec2<T> normalize( const Vec2<T>& a ) { return a*rsqrt(dot(a,a)); }
  122. template<typename T> __forceinline T distance ( const Vec2<T>& a, const Vec2<T>& b ) { return length(a-b); }
  123. template<typename T> __forceinline T det ( const Vec2<T>& a, const Vec2<T>& b ) { return a.x*b.y - a.y*b.x; }
  124. template<typename T> __forceinline Vec2<T> normalize_safe( const Vec2<T>& a ) {
  125. const T d = dot(a,a); return select(d == T( zero ),a, a*rsqrt(d) );
  126. }
  127. ////////////////////////////////////////////////////////////////////////////////
  128. /// Select
  129. ////////////////////////////////////////////////////////////////////////////////
  130. template<typename T> __forceinline Vec2<T> select ( bool s, const Vec2<T>& t, const Vec2<T>& f ) {
  131. return Vec2<T>(select(s,t.x,f.x),select(s,t.y,f.y));
  132. }
  133. template<typename T> __forceinline Vec2<T> select ( const Vec2<bool>& s, const Vec2<T>& t, const Vec2<T>& f ) {
  134. return Vec2<T>(select(s.x,t.x,f.x),select(s.y,t.y,f.y));
  135. }
  136. template<typename T> __forceinline Vec2<T> select ( const typename T::Bool& s, const Vec2<T>& t, const Vec2<T>& f ) {
  137. return Vec2<T>(select(s,t.x,f.x),select(s,t.y,f.y));
  138. }
  139. template<typename T>
  140. __forceinline Vec2<T> lerp(const Vec2<T>& v0, const Vec2<T>& v1, const T& t) {
  141. return madd(Vec2<T>(T(1.0f)-t),v0,t*v1);
  142. }
  143. template<typename T> __forceinline int maxDim ( const Vec2<T>& a )
  144. {
  145. const Vec2<T> b = abs(a);
  146. if (b.x > b.y) return 0;
  147. else return 1;
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////
  150. /// Output Operators
  151. ////////////////////////////////////////////////////////////////////////////////
  152. template<typename T> __forceinline embree_ostream operator<<(embree_ostream cout, const Vec2<T>& a) {
  153. return cout << "(" << a.x << ", " << a.y << ")";
  154. }
  155. ////////////////////////////////////////////////////////////////////////////////
  156. /// Default template instantiations
  157. ////////////////////////////////////////////////////////////////////////////////
  158. typedef Vec2<bool > Vec2b;
  159. typedef Vec2<int > Vec2i;
  160. typedef Vec2<float> Vec2f;
  161. }
  162. #include "vec2fa.h"
  163. #if defined(__SSE__) || defined(__ARM_NEON)
  164. #include "../simd/sse.h"
  165. #endif
  166. #if defined(__AVX__)
  167. #include "../simd/avx.h"
  168. #endif
  169. #if defined(__AVX512F__)
  170. #include "../simd/avx512.h"
  171. #endif
  172. namespace embree
  173. {
  174. template<> __forceinline Vec2<float>::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {}
  175. #if defined(__SSE__) || defined(__ARM_NEON)
  176. template<> __forceinline Vec2<vfloat4>::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {}
  177. #endif
  178. #if defined(__AVX__)
  179. template<> __forceinline Vec2<vfloat8>::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {}
  180. #endif
  181. #if defined(__AVX512F__)
  182. template<> __forceinline Vec2<vfloat16>::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {}
  183. #endif
  184. }