vec3.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. struct Vec3fa;
  21. ////////////////////////////////////////////////////////////////////////////////
  22. /// Generic 3D vector Class
  23. ////////////////////////////////////////////////////////////////////////////////
  24. template<typename T> struct Vec3
  25. {
  26. T x, y, z;
  27. typedef T Scalar;
  28. enum { N = 3 };
  29. ////////////////////////////////////////////////////////////////////////////////
  30. /// Construction
  31. ////////////////////////////////////////////////////////////////////////////////
  32. __forceinline Vec3( ) {}
  33. __forceinline explicit Vec3( const T& a ) : x(a), y(a), z(a) {}
  34. __forceinline Vec3( const T& x, const T& y, const T& z ) : x(x), y(y), z(z) {}
  35. __forceinline Vec3( const Vec3& other ) { x = other.x; y = other.y; z = other.z; }
  36. __forceinline Vec3( const Vec3fa& other );
  37. template<typename T1> __forceinline Vec3( const Vec3<T1>& a ) : x(T(a.x)), y(T(a.y)), z(T(a.z)) {}
  38. template<typename T1> __forceinline Vec3& operator =(const Vec3<T1>& other) { x = other.x; y = other.y; z = other.z; return *this; }
  39. ////////////////////////////////////////////////////////////////////////////////
  40. /// Constants
  41. ////////////////////////////////////////////////////////////////////////////////
  42. __forceinline Vec3( ZeroTy ) : x(zero), y(zero), z(zero) {}
  43. __forceinline Vec3( OneTy ) : x(one), y(one), z(one) {}
  44. __forceinline Vec3( PosInfTy ) : x(pos_inf), y(pos_inf), z(pos_inf) {}
  45. __forceinline Vec3( NegInfTy ) : x(neg_inf), y(neg_inf), z(neg_inf) {}
  46. __forceinline const T& operator []( const size_t axis ) const { assert(axis < 3); return (&x)[axis]; }
  47. __forceinline T& operator []( const size_t axis ) { assert(axis < 3); return (&x)[axis]; }
  48. };
  49. ////////////////////////////////////////////////////////////////////////////////
  50. /// Unary Operators
  51. ////////////////////////////////////////////////////////////////////////////////
  52. template<typename T> __forceinline Vec3<T> operator +( const Vec3<T>& a ) { return Vec3<T>(+a.x, +a.y, +a.z); }
  53. template<typename T> __forceinline Vec3<T> operator -( const Vec3<T>& a ) { return Vec3<T>(-a.x, -a.y, -a.z); }
  54. template<typename T> __forceinline Vec3<T> abs ( const Vec3<T>& a ) { return Vec3<T>(abs (a.x), abs (a.y), abs (a.z)); }
  55. template<typename T> __forceinline Vec3<T> rcp ( const Vec3<T>& a ) { return Vec3<T>(rcp (a.x), rcp (a.y), rcp (a.z)); }
  56. template<typename T> __forceinline Vec3<T> rsqrt ( const Vec3<T>& a ) { return Vec3<T>(rsqrt(a.x), rsqrt(a.y), rsqrt(a.z)); }
  57. template<typename T> __forceinline Vec3<T> sqrt ( const Vec3<T>& a ) { return Vec3<T>(sqrt (a.x), sqrt (a.y), sqrt (a.z)); }
  58. template<typename T> __forceinline Vec3<T> zero_fix( const Vec3<T>& a )
  59. {
  60. return Vec3<T>(select(abs(a.x)<min_rcp_input,T(min_rcp_input),a.x),
  61. select(abs(a.y)<min_rcp_input,T(min_rcp_input),a.y),
  62. select(abs(a.z)<min_rcp_input,T(min_rcp_input),a.z));
  63. }
  64. template<typename T> __forceinline const Vec3<T> rcp_safe(const Vec3<T>& a) { return rcp(zero_fix(a)); }
  65. ////////////////////////////////////////////////////////////////////////////////
  66. /// Binary Operators
  67. ////////////////////////////////////////////////////////////////////////////////
  68. template<typename T> __forceinline Vec3<T> operator +( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<T>(a.x + b.x, a.y + b.y, a.z + b.z); }
  69. template<typename T> __forceinline Vec3<T> operator -( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<T>(a.x - b.x, a.y - b.y, a.z - b.z); }
  70. template<typename T> __forceinline Vec3<T> operator *( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<T>(a.x * b.x, a.y * b.y, a.z * b.z); }
  71. template<typename T> __forceinline Vec3<T> operator *( const T& a, const Vec3<T>& b ) { return Vec3<T>(a * b.x, a * b.y, a * b.z); }
  72. template<typename T> __forceinline Vec3<T> operator *( const Vec3<T>& a, const T& b ) { return Vec3<T>(a.x * b , a.y * b , a.z * b ); }
  73. template<typename T> __forceinline Vec3<T> operator /( const Vec3<T>& a, const T& b ) { return Vec3<T>(a.x / b , a.y / b , a.z / b ); }
  74. template<typename T> __forceinline Vec3<T> operator /( const T& a, const Vec3<T>& b ) { return Vec3<T>(a / b.x, a / b.y, a / b.z); }
  75. template<typename T> __forceinline Vec3<T> operator /( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<T>(a.x / b.x, a.y / b.y, a.z / b.z); }
  76. template<typename T> __forceinline Vec3<T> min(const Vec3<T>& a, const Vec3<T>& b) { return Vec3<T>(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)); }
  77. template<typename T> __forceinline Vec3<T> max(const Vec3<T>& a, const Vec3<T>& b) { return Vec3<T>(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)); }
  78. template<typename T> __forceinline Vec3<T> operator >>( const Vec3<T>& a, const int b ) { return Vec3<T>(a.x >> b, a.y >> b, a.z >> b); }
  79. template<typename T> __forceinline Vec3<T> operator <<( const Vec3<T>& a, const int b ) { return Vec3<T>(a.x << b, a.y << b, a.z << b); }
  80. ////////////////////////////////////////////////////////////////////////////////
  81. /// Ternary Operators
  82. ////////////////////////////////////////////////////////////////////////////////
  83. template<typename T> __forceinline const Vec3<T> madd ( const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>( madd(a.x,b.x,c.x), madd(a.y,b.y,c.y), madd(a.z,b.z,c.z)); }
  84. template<typename T> __forceinline const Vec3<T> msub ( const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>( msub(a.x,b.x,c.x), msub(a.y,b.y,c.y), msub(a.z,b.z,c.z)); }
  85. template<typename T> __forceinline const Vec3<T> nmadd ( const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>(nmadd(a.x,b.x,c.x),nmadd(a.y,b.y,c.y),nmadd(a.z,b.z,c.z));}
  86. template<typename T> __forceinline const Vec3<T> nmsub ( const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>(nmsub(a.x,b.x,c.x),nmsub(a.y,b.y,c.y),nmsub(a.z,b.z,c.z)); }
  87. template<typename T> __forceinline const Vec3<T> madd ( const T& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>( madd(a,b.x,c.x), madd(a,b.y,c.y), madd(a,b.z,c.z)); }
  88. template<typename T> __forceinline const Vec3<T> msub ( const T& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>( msub(a,b.x,c.x), msub(a,b.y,c.y), msub(a,b.z,c.z)); }
  89. template<typename T> __forceinline const Vec3<T> nmadd ( const T& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>(nmadd(a,b.x,c.x),nmadd(a,b.y,c.y),nmadd(a,b.z,c.z));}
  90. template<typename T> __forceinline const Vec3<T> nmsub ( const T& a, const Vec3<T>& b, const Vec3<T>& c) { return Vec3<T>(nmsub(a,b.x,c.x),nmsub(a,b.y,c.y),nmsub(a,b.z,c.z)); }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. /// Assignment Operators
  93. ////////////////////////////////////////////////////////////////////////////////
  94. template<typename T> __forceinline const Vec3<T>& operator +=( Vec3<T>& a, const T b ) { a.x += b; a.y += b; a.z += b; return a; }
  95. template<typename T> __forceinline const Vec3<T>& operator +=( Vec3<T>& a, const Vec3<T>& b ) { a.x += b.x; a.y += b.y; a.z += b.z; return a; }
  96. template<typename T> __forceinline const Vec3<T>& operator -=( Vec3<T>& a, const Vec3<T>& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; }
  97. template<typename T> __forceinline const Vec3<T>& operator *=( Vec3<T>& a, const T& b ) { a.x *= b ; a.y *= b ; a.z *= b ; return a; }
  98. template<typename T> __forceinline const Vec3<T>& operator /=( Vec3<T>& a, const T& b ) { a.x /= b ; a.y /= b ; a.z /= b ; return a; }
  99. ////////////////////////////////////////////////////////////////////////////////
  100. /// Reduction Operators
  101. ////////////////////////////////////////////////////////////////////////////////
  102. template<typename T> __forceinline T reduce_add( const Vec3<T>& a ) { return a.x + a.y + a.z; }
  103. template<typename T> __forceinline T reduce_mul( const Vec3<T>& a ) { return a.x * a.y * a.z; }
  104. template<typename T> __forceinline T reduce_min( const Vec3<T>& a ) { return min(a.x, a.y, a.z); }
  105. template<typename T> __forceinline T reduce_max( const Vec3<T>& a ) { return max(a.x, a.y, a.z); }
  106. ////////////////////////////////////////////////////////////////////////////////
  107. /// Comparison Operators
  108. ////////////////////////////////////////////////////////////////////////////////
  109. template<typename T> __forceinline bool operator ==( const Vec3<T>& a, const Vec3<T>& b ) { return a.x == b.x && a.y == b.y && a.z == b.z; }
  110. template<typename T> __forceinline bool operator !=( const Vec3<T>& a, const Vec3<T>& b ) { return a.x != b.x || a.y != b.y || a.z != b.z; }
  111. template<typename T> __forceinline bool operator < ( const Vec3<T>& a, const Vec3<T>& b ) {
  112. if (a.x != b.x) return a.x < b.x;
  113. if (a.y != b.y) return a.y < b.y;
  114. if (a.z != b.z) return a.z < b.z;
  115. return false;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. /// Select
  119. ////////////////////////////////////////////////////////////////////////////////
  120. template<typename T> __forceinline Vec3<T> select ( bool s, const Vec3<T>& t, const Vec3<T>& f ) {
  121. return Vec3<T>(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z));
  122. }
  123. template<typename T> __forceinline Vec3<T> select ( const Vec3<bool>& s, const Vec3<T>& t, const Vec3<T>& f ) {
  124. return Vec3<T>(select(s.x,t.x,f.x),select(s.y,t.y,f.y),select(s.z,t.z,f.z));
  125. }
  126. template<typename T> __forceinline Vec3<T> select ( const typename T::Bool& s, const Vec3<T>& t, const Vec3<T>& f ) {
  127. return Vec3<T>(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z));
  128. }
  129. template<typename T>
  130. __forceinline Vec3<T> lerp(const Vec3<T>& v0, const Vec3<T>& v1, const T& t) {
  131. return madd(Vec3<T>(T(1.0f)-t),v0,t*v1);
  132. }
  133. template<typename T> __forceinline int maxDim ( const Vec3<T>& a )
  134. {
  135. const Vec3<T> b = abs(a);
  136. if (b.x > b.y) {
  137. if (b.x > b.z) return 0; else return 2;
  138. } else {
  139. if (b.y > b.z) return 1; else return 2;
  140. }
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////
  143. /// Comparison Operators
  144. ////////////////////////////////////////////////////////////////////////////////
  145. template<typename T> __forceinline Vec3<bool> eq_mask( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<bool>(a.x==b.x,a.y==b.y,a.z==b.z); }
  146. template<typename T> __forceinline Vec3<bool> neq_mask(const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<bool>(a.x!=b.x,a.y!=b.y,a.z!=b.z); }
  147. template<typename T> __forceinline Vec3<bool> lt_mask( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<bool>(a.x< b.x,a.y< b.y,a.z< b.z); }
  148. template<typename T> __forceinline Vec3<bool> le_mask( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<bool>(a.x<=b.x,a.y<=b.y,a.z<=b.z); }
  149. template<typename T> __forceinline Vec3<bool> gt_mask( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<bool>(a.x> b.x,a.y> b.y,a.z> b.z); }
  150. template<typename T> __forceinline Vec3<bool> ge_mask( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<bool>(a.x>=b.x,a.y>=b.y,a.z>=b.z); }
  151. ////////////////////////////////////////////////////////////////////////////////
  152. /// Euclidian Space Operators
  153. ////////////////////////////////////////////////////////////////////////////////
  154. template<typename T> __forceinline T dot ( const Vec3<T>& a, const Vec3<T>& b ) { return madd(a.x,b.x,madd(a.y,b.y,a.z*b.z)); }
  155. template<typename T> __forceinline T length ( const Vec3<T>& a ) { return sqrt(dot(a,a)); }
  156. template<typename T> __forceinline T rcp_length( const Vec3<T>& a ) { return rsqrt(dot(a,a)); }
  157. template<typename T> __forceinline Vec3<T> normalize( const Vec3<T>& a ) { return a*rsqrt(dot(a,a)); }
  158. template<typename T> __forceinline T distance ( const Vec3<T>& a, const Vec3<T>& b ) { return length(a-b); }
  159. template<typename T> __forceinline Vec3<T> cross ( const Vec3<T>& a, const Vec3<T>& b ) { return Vec3<T>(msub(a.y,b.z,a.z*b.y), msub(a.z,b.x,a.x*b.z), msub(a.x,b.y,a.y*b.x)); }
  160. template<typename T> __forceinline Vec3<T> stable_triangle_normal( const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& c )
  161. {
  162. const T ab_x = a.z*b.y, ab_y = a.x*b.z, ab_z = a.y*b.x;
  163. const T bc_x = b.z*c.y, bc_y = b.x*c.z, bc_z = b.y*c.x;
  164. const Vec3<T> cross_ab(msub(a.y,b.z,ab_x), msub(a.z,b.x,ab_y), msub(a.x,b.y,ab_z));
  165. const Vec3<T> cross_bc(msub(b.y,c.z,bc_x), msub(b.z,c.x,bc_y), msub(b.x,c.y,bc_z));
  166. const auto sx = abs(ab_x) < abs(bc_x);
  167. const auto sy = abs(ab_y) < abs(bc_y);
  168. const auto sz = abs(ab_z) < abs(bc_z);
  169. return Vec3<T>(select(sx,cross_ab.x,cross_bc.x),
  170. select(sy,cross_ab.y,cross_bc.y),
  171. select(sz,cross_ab.z,cross_bc.z));
  172. }
  173. template<typename T> __forceinline T sum ( const Vec3<T>& a ) { return a.x+a.y+a.z; }
  174. template<typename T> __forceinline T halfArea ( const Vec3<T>& d ) { return madd(d.x,(d.y+d.z),d.y*d.z); }
  175. template<typename T> __forceinline T area ( const Vec3<T>& d ) { return 2.0f*halfArea(d); }
  176. template<typename T> __forceinline Vec3<T> normalize_safe( const Vec3<T>& a ) {
  177. const T d = dot(a,a); return select(d == T( zero ), a , a*rsqrt(d) );
  178. }
  179. template<typename T> __forceinline T sqr_point_to_line_distance(const Vec3<T>& P, const Vec3<T>& Q0, const Vec3<T>& Q1)
  180. {
  181. const Vec3<T> N = cross(P-Q0,Q1-Q0);
  182. const Vec3<T> D = Q1-Q0;
  183. return dot(N,N)*rcp(dot(D,D));
  184. }
  185. template<typename T> __forceinline T sqr_point_to_line_distance(const Vec3<T>& PmQ0, const Vec3<T>& Q1mQ0)
  186. {
  187. const Vec3<T> N = cross(PmQ0,Q1mQ0);
  188. const Vec3<T> D = Q1mQ0;
  189. return dot(N,N)*rcp(dot(D,D));
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////
  192. /// Output Operators
  193. ////////////////////////////////////////////////////////////////////////////////
  194. template<typename T> inline std::ostream& operator<<(std::ostream& cout, const Vec3<T>& a) {
  195. return cout << "(" << a.x << ", " << a.y << ", " << a.z << ")";
  196. }
  197. typedef Vec3<bool > Vec3b;
  198. typedef Vec3<int > Vec3i;
  199. typedef Vec3<float> Vec3f;
  200. }
  201. #include "vec3ba.h"
  202. #include "vec3ia.h"
  203. #include "vec3fa.h"
  204. ////////////////////////////////////////////////////////////////////////////////
  205. /// SSE / AVX / MIC specializations
  206. ////////////////////////////////////////////////////////////////////////////////
  207. #if defined __SSE__
  208. #include "../simd/sse.h"
  209. #endif
  210. #if defined __AVX__
  211. #include "../simd/avx.h"
  212. #endif
  213. #if defined(__AVX512F__)
  214. #include "../simd/avx512.h"
  215. #endif
  216. namespace embree
  217. {
  218. template<typename Out, typename In>
  219. __forceinline Vec3<Out> broadcast(const Vec3<In>& a, const size_t k) {
  220. return Vec3<Out>(Out(a.x[k]),Out(a.y[k]),Out(a.z[k]));
  221. }
  222. template<> __forceinline Vec3<float>::Vec3( const Vec3fa& a ) { x = a.x; y = a.y; z = a.z; }
  223. #if defined(__AVX__)
  224. template<> __forceinline Vec3<vfloat4>::Vec3( const Vec3fa& a ) {
  225. x = a.x; y = a.y; z = a.z;
  226. }
  227. #elif defined(__SSE__)
  228. template<> __forceinline Vec3<vfloat4>::Vec3( const Vec3fa& a ) {
  229. const vfloat4 v = vfloat4(a); x = shuffle<0,0,0,0>(v); y = shuffle<1,1,1,1>(v); z = shuffle<2,2,2,2>(v);
  230. }
  231. #endif
  232. #if defined(__SSE__)
  233. __forceinline Vec3<vfloat4> broadcast4f( const Vec3<vfloat4>& a, const size_t k ) {
  234. return Vec3<vfloat4>(vfloat4::broadcast(&a.x[k]), vfloat4::broadcast(&a.y[k]), vfloat4::broadcast(&a.z[k]));
  235. }
  236. template<>
  237. __forceinline Vec3<vfloat4> broadcast<vfloat4,vfloat4>( const Vec3<vfloat4>& a, const size_t k ) {
  238. return Vec3<vfloat4>(vfloat4::broadcast(&a.x[k]), vfloat4::broadcast(&a.y[k]), vfloat4::broadcast(&a.z[k]));
  239. }
  240. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const Vec3<vfloat4> shuffle( const Vec3<vfloat4>& b ) {
  241. return Vec3<vfloat4>(shuffle<i0,i1,i2,i3>(b.x),shuffle<i0,i1,i2,i3>(b.y),shuffle<i0,i1,i2,i3>(b.z));
  242. }
  243. #endif
  244. #if defined(__AVX__)
  245. template<> __forceinline Vec3<vfloat8>::Vec3( const Vec3fa& a ) {
  246. x = a.x; y = a.y; z = a.z;
  247. }
  248. __forceinline Vec3<vfloat4> broadcast4f( const Vec3<vfloat8>& a, const size_t k ) {
  249. return Vec3<vfloat4>(vfloat4::broadcast(&a.x[k]), vfloat4::broadcast(&a.y[k]), vfloat4::broadcast(&a.z[k]));
  250. }
  251. __forceinline Vec3<vfloat8> broadcast8f( const Vec3<vfloat4>& a, const size_t k ) {
  252. return Vec3<vfloat8>(vfloat8::broadcast(&a.x[k]), vfloat8::broadcast(&a.y[k]), vfloat8::broadcast(&a.z[k]));
  253. }
  254. __forceinline Vec3<vfloat8> broadcast8f( const Vec3<vfloat8>& a, const size_t k ) {
  255. return Vec3<vfloat8>(vfloat8::broadcast(&a.x[k]), vfloat8::broadcast(&a.y[k]), vfloat8::broadcast(&a.z[k]));
  256. }
  257. template<>
  258. __forceinline Vec3<vfloat8> broadcast<vfloat8,vfloat4>( const Vec3<vfloat4>& a, const size_t k ) {
  259. return Vec3<vfloat8>(vfloat8::broadcast(&a.x[k]), vfloat8::broadcast(&a.y[k]), vfloat8::broadcast(&a.z[k]));
  260. }
  261. template<>
  262. __forceinline Vec3<vfloat8> broadcast<vfloat8,vfloat8>( const Vec3<vfloat8>& a, const size_t k ) {
  263. return Vec3<vfloat8>(vfloat8::broadcast(&a.x[k]), vfloat8::broadcast(&a.y[k]), vfloat8::broadcast(&a.z[k]));
  264. }
  265. template<size_t i0, size_t i1, size_t i2, size_t i3> __forceinline const Vec3<vfloat8> shuffle( const Vec3<vfloat8>& b ) {
  266. return Vec3<vfloat8>(shuffle<i0,i1,i2,i3>(b.x),shuffle<i0,i1,i2,i3>(b.y),shuffle<i0,i1,i2,i3>(b.z));
  267. }
  268. #endif
  269. #if defined(__AVX512F__)
  270. template<> __forceinline Vec3<vfloat16>::Vec3( const Vec3fa& a ) : x(a.x), y(a.y), z(a.z) {}
  271. #endif
  272. }