vec2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "math.h"
  18. namespace embree
  19. {
  20. ////////////////////////////////////////////////////////////////////////////////
  21. /// Generic 2D vector Class
  22. ////////////////////////////////////////////////////////////////////////////////
  23. template<typename T> struct Vec2
  24. {
  25. T x, y;
  26. typedef T Scalar;
  27. enum { N = 2 };
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// Construction
  30. ////////////////////////////////////////////////////////////////////////////////
  31. __forceinline Vec2( ) {}
  32. __forceinline explicit Vec2( const T& a ) : x(a), y(a) {}
  33. __forceinline Vec2( const T& x, const T& y ) : x(x), y(y) {}
  34. __forceinline Vec2( const Vec2& other ) { x = other.x; y = other.y; }
  35. template<typename T1> __forceinline Vec2( const Vec2<T1>& a ) : x(T(a.x)), y(T(a.y)) {}
  36. template<typename T1> __forceinline Vec2& operator =( const Vec2<T1>& other ) { x = other.x; y = other.y; return *this; }
  37. ////////////////////////////////////////////////////////////////////////////////
  38. /// Constants
  39. ////////////////////////////////////////////////////////////////////////////////
  40. __forceinline Vec2( ZeroTy ) : x(zero), y(zero) {}
  41. __forceinline Vec2( OneTy ) : x(one), y(one) {}
  42. __forceinline Vec2( PosInfTy ) : x(pos_inf), y(pos_inf) {}
  43. __forceinline Vec2( NegInfTy ) : x(neg_inf), y(neg_inf) {}
  44. __forceinline const T& operator []( const size_t axis ) const { assert(axis < 2); return (&x)[axis]; }
  45. __forceinline T& operator []( const size_t axis ) { assert(axis < 2); return (&x)[axis]; }
  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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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. /// Euclidian Space Operators
  111. ////////////////////////////////////////////////////////////////////////////////
  112. template<typename T> __forceinline T dot ( const Vec2<T>& a, const Vec2<T>& b ) { return madd(a.x,b.x,a.y*b.y); }
  113. template<typename T> __forceinline T length ( const Vec2<T>& a ) { return sqrt(dot(a,a)); }
  114. template<typename T> __forceinline Vec2<T> normalize( const Vec2<T>& a ) { return a*rsqrt(dot(a,a)); }
  115. template<typename T> __forceinline T distance ( const Vec2<T>& a, const Vec2<T>& b ) { return length(a-b); }
  116. template<typename T> __forceinline T det ( const Vec2<T>& a, const Vec2<T>& b ) { return a.x*b.y - a.y*b.x; }
  117. template<typename T> __forceinline Vec2<T> normalize_safe( const Vec2<T>& a ) {
  118. const T d = dot(a,a); return select(d == T( zero ),a, a*rsqrt(d) );
  119. }
  120. ////////////////////////////////////////////////////////////////////////////////
  121. /// Select
  122. ////////////////////////////////////////////////////////////////////////////////
  123. template<typename T> __forceinline Vec2<T> select ( bool s, const Vec2<T>& t, const Vec2<T>& f ) {
  124. return Vec2<T>(select(s,t.x,f.x),select(s,t.y,f.y));
  125. }
  126. template<typename T> __forceinline Vec2<T> select ( const Vec2<bool>& s, const Vec2<T>& t, const Vec2<T>& f ) {
  127. return Vec2<T>(select(s.x,t.x,f.x),select(s.y,t.y,f.y));
  128. }
  129. template<typename T> __forceinline Vec2<T> select ( const typename T::Bool& s, const Vec2<T>& t, const Vec2<T>& f ) {
  130. return Vec2<T>(select(s,t.x,f.x),select(s,t.y,f.y));
  131. }
  132. template<typename T> __forceinline int maxDim ( const Vec2<T>& a )
  133. {
  134. const Vec2<T> b = abs(a);
  135. if (b.x > b.y) return 0;
  136. else return 1;
  137. }
  138. ////////////////////////////////////////////////////////////////////////////////
  139. /// Output Operators
  140. ////////////////////////////////////////////////////////////////////////////////
  141. template<typename T> inline std::ostream& operator<<(std::ostream& cout, const Vec2<T>& a) {
  142. return cout << "(" << a.x << ", " << a.y << ")";
  143. }
  144. ////////////////////////////////////////////////////////////////////////////////
  145. /// Default template instantiations
  146. ////////////////////////////////////////////////////////////////////////////////
  147. typedef Vec2<bool > Vec2b;
  148. typedef Vec2<int > Vec2i;
  149. typedef Vec2<float> Vec2f;
  150. }