affinespace.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "linearspace2.h"
  5. #include "linearspace3.h"
  6. #include "quaternion.h"
  7. #include "bbox.h"
  8. #include "vec4.h"
  9. namespace embree
  10. {
  11. #define VectorT typename L::Vector
  12. #define ScalarT typename L::Vector::Scalar
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // Affine Space
  15. ////////////////////////////////////////////////////////////////////////////////
  16. template<typename L>
  17. struct AffineSpaceT
  18. {
  19. L l; /*< linear part of affine space */
  20. VectorT p; /*< affine part of affine space */
  21. ////////////////////////////////////////////////////////////////////////////////
  22. // Constructors, Assignment, Cast, Copy Operations
  23. ////////////////////////////////////////////////////////////////////////////////
  24. __forceinline AffineSpaceT ( ) { }
  25. __forceinline AffineSpaceT ( const AffineSpaceT& other ) { l = other.l; p = other.p; }
  26. __forceinline AffineSpaceT ( const L & other ) { l = other ; p = VectorT(zero); }
  27. __forceinline AffineSpaceT& operator=( const AffineSpaceT& other ) { l = other.l; p = other.p; return *this; }
  28. __forceinline AffineSpaceT( const VectorT& vx, const VectorT& vy, const VectorT& vz, const VectorT& p ) : l(vx,vy,vz), p(p) {}
  29. __forceinline AffineSpaceT( const L& l, const VectorT& p ) : l(l), p(p) {}
  30. template<typename L1> __forceinline AffineSpaceT( const AffineSpaceT<L1>& s ) : l(s.l), p(s.p) {}
  31. ////////////////////////////////////////////////////////////////////////////////
  32. // Constants
  33. ////////////////////////////////////////////////////////////////////////////////
  34. __forceinline AffineSpaceT( ZeroTy ) : l(zero), p(zero) {}
  35. __forceinline AffineSpaceT( OneTy ) : l(one), p(zero) {}
  36. /*! return matrix for scaling */
  37. static __forceinline AffineSpaceT scale(const VectorT& s) { return L::scale(s); }
  38. /*! return matrix for translation */
  39. static __forceinline AffineSpaceT translate(const VectorT& p) { return AffineSpaceT(one,p); }
  40. /*! return matrix for rotation, only in 2D */
  41. static __forceinline AffineSpaceT rotate(const ScalarT& r) { return L::rotate(r); }
  42. /*! return matrix for rotation around arbitrary point (2D) or axis (3D) */
  43. static __forceinline AffineSpaceT rotate(const VectorT& u, const ScalarT& r) { return L::rotate(u,r); }
  44. /*! return matrix for rotation around arbitrary axis and point, only in 3D */
  45. static __forceinline AffineSpaceT rotate(const VectorT& p, const VectorT& u, const ScalarT& r) { return translate(+p) * rotate(u,r) * translate(-p); }
  46. /*! return matrix for looking at given point, only in 3D */
  47. static __forceinline AffineSpaceT lookat(const VectorT& eye, const VectorT& point, const VectorT& up) {
  48. VectorT Z = normalize(point-eye);
  49. VectorT U = normalize(cross(up,Z));
  50. VectorT V = normalize(cross(Z,U));
  51. return AffineSpaceT(L(U,V,Z),eye);
  52. }
  53. };
  54. // template specialization to get correct identity matrix for type AffineSpace3fa
  55. template<>
  56. __forceinline AffineSpaceT<LinearSpace3ff>::AffineSpaceT( OneTy ) : l(one), p(0.f, 0.f, 0.f, 1.f) {}
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // Unary Operators
  59. ////////////////////////////////////////////////////////////////////////////////
  60. template<typename L> __forceinline AffineSpaceT<L> operator -( const AffineSpaceT<L>& a ) { return AffineSpaceT<L>(-a.l,-a.p); }
  61. template<typename L> __forceinline AffineSpaceT<L> operator +( const AffineSpaceT<L>& a ) { return AffineSpaceT<L>(+a.l,+a.p); }
  62. template<typename L> __forceinline AffineSpaceT<L> rcp( const AffineSpaceT<L>& a ) { L il = rcp(a.l); return AffineSpaceT<L>(il,-(il*a.p)); }
  63. ////////////////////////////////////////////////////////////////////////////////
  64. // Binary Operators
  65. ////////////////////////////////////////////////////////////////////////////////
  66. template<typename L> __forceinline const AffineSpaceT<L> operator +( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l+b.l,a.p+b.p); }
  67. template<typename L> __forceinline const AffineSpaceT<L> operator -( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l-b.l,a.p-b.p); }
  68. template<typename L> __forceinline const AffineSpaceT<L> operator *( const ScalarT & a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a*b.l,a*b.p); }
  69. template<typename L> __forceinline const AffineSpaceT<L> operator *( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l*b.l,a.l*b.p+a.p); }
  70. template<typename L> __forceinline const AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a * rcp(b); }
  71. template<typename L> __forceinline const AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const ScalarT & b ) { return a * rcp(b); }
  72. template<typename L> __forceinline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a * b; }
  73. template<typename L> __forceinline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a * b; }
  74. template<typename L> __forceinline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a / b; }
  75. template<typename L> __forceinline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a / b; }
  76. template<typename L> __forceinline VectorT xfmPoint (const AffineSpaceT<L>& m, const VectorT& p) { return madd(VectorT(p.x),m.l.vx,madd(VectorT(p.y),m.l.vy,madd(VectorT(p.z),m.l.vz,m.p))); }
  77. template<typename L> __forceinline VectorT xfmVector(const AffineSpaceT<L>& m, const VectorT& v) { return xfmVector(m.l,v); }
  78. template<typename L> __forceinline VectorT xfmNormal(const AffineSpaceT<L>& m, const VectorT& n) { return xfmNormal(m.l,n); }
  79. __forceinline const BBox<Vec3fa> xfmBounds(const AffineSpaceT<LinearSpace3<Vec3fa> >& m, const BBox<Vec3fa>& b)
  80. {
  81. BBox3fa dst = empty;
  82. const Vec3fa p0(b.lower.x,b.lower.y,b.lower.z); dst.extend(xfmPoint(m,p0));
  83. const Vec3fa p1(b.lower.x,b.lower.y,b.upper.z); dst.extend(xfmPoint(m,p1));
  84. const Vec3fa p2(b.lower.x,b.upper.y,b.lower.z); dst.extend(xfmPoint(m,p2));
  85. const Vec3fa p3(b.lower.x,b.upper.y,b.upper.z); dst.extend(xfmPoint(m,p3));
  86. const Vec3fa p4(b.upper.x,b.lower.y,b.lower.z); dst.extend(xfmPoint(m,p4));
  87. const Vec3fa p5(b.upper.x,b.lower.y,b.upper.z); dst.extend(xfmPoint(m,p5));
  88. const Vec3fa p6(b.upper.x,b.upper.y,b.lower.z); dst.extend(xfmPoint(m,p6));
  89. const Vec3fa p7(b.upper.x,b.upper.y,b.upper.z); dst.extend(xfmPoint(m,p7));
  90. return dst;
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. /// Comparison Operators
  94. ////////////////////////////////////////////////////////////////////////////////
  95. template<typename L> __forceinline bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l == b.l && a.p == b.p; }
  96. template<typename L> __forceinline bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l != b.l || a.p != b.p; }
  97. ////////////////////////////////////////////////////////////////////////////////
  98. /// Select
  99. ////////////////////////////////////////////////////////////////////////////////
  100. template<typename L> __forceinline AffineSpaceT<L> select ( const typename L::Vector::Scalar::Bool& s, const AffineSpaceT<L>& t, const AffineSpaceT<L>& f ) {
  101. return AffineSpaceT<L>(select(s,t.l,f.l),select(s,t.p,f.p));
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // Output Operators
  105. ////////////////////////////////////////////////////////////////////////////////
  106. template<typename L> static embree_ostream operator<<(embree_ostream cout, const AffineSpaceT<L>& m) {
  107. return cout << "{ l = " << m.l << ", p = " << m.p << " }";
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // Template Instantiations
  111. ////////////////////////////////////////////////////////////////////////////////
  112. typedef AffineSpaceT<LinearSpace2f> AffineSpace2f;
  113. typedef AffineSpaceT<LinearSpace3f> AffineSpace3f;
  114. typedef AffineSpaceT<LinearSpace3fa> AffineSpace3fa;
  115. typedef AffineSpaceT<LinearSpace3fx> AffineSpace3fx;
  116. typedef AffineSpaceT<LinearSpace3ff> AffineSpace3ff;
  117. typedef AffineSpaceT<Quaternion3f > OrthonormalSpace3f;
  118. template<int N> using AffineSpace3vf = AffineSpaceT<LinearSpace3<Vec3<vfloat<N>>>>;
  119. typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<4>>>> AffineSpace3vf4;
  120. typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<8>>>> AffineSpace3vf8;
  121. typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<16>>>> AffineSpace3vf16;
  122. template<int N> using AffineSpace3vff = AffineSpaceT<LinearSpace3<Vec4<vfloat<N>>>>;
  123. typedef AffineSpaceT<LinearSpace3<Vec4<vfloat<4>>>> AffineSpace3vfa4;
  124. typedef AffineSpaceT<LinearSpace3<Vec4<vfloat<8>>>> AffineSpace3vfa8;
  125. typedef AffineSpaceT<LinearSpace3<Vec4<vfloat<16>>>> AffineSpace3vfa16;
  126. //////////////////////////////////////////////////////////////////////////////
  127. /// Interpolation
  128. //////////////////////////////////////////////////////////////////////////////
  129. template<typename T, typename R>
  130. __forceinline AffineSpaceT<T> lerp(const AffineSpaceT<T>& M0,
  131. const AffineSpaceT<T>& M1,
  132. const R& t)
  133. {
  134. return AffineSpaceT<T>(lerp(M0.l,M1.l,t),lerp(M0.p,M1.p,t));
  135. }
  136. // slerp interprets the 16 floats of the matrix M = D * R * S as components of
  137. // three matrizes (D, R, S) that are interpolated individually.
  138. template<typename T> __forceinline AffineSpaceT<LinearSpace3<Vec3<T>>>
  139. slerp(const AffineSpaceT<LinearSpace3<Vec4<T>>>& M0,
  140. const AffineSpaceT<LinearSpace3<Vec4<T>>>& M1,
  141. const T& t)
  142. {
  143. QuaternionT<T> q0(M0.p.w, M0.l.vx.w, M0.l.vy.w, M0.l.vz.w);
  144. QuaternionT<T> q1(M1.p.w, M1.l.vx.w, M1.l.vy.w, M1.l.vz.w);
  145. QuaternionT<T> q = slerp(q0, q1, t);
  146. AffineSpaceT<LinearSpace3<Vec3<T>>> S = lerp(M0, M1, t);
  147. AffineSpaceT<LinearSpace3<Vec3<T>>> D(one);
  148. D.p.x = S.l.vx.y;
  149. D.p.y = S.l.vx.z;
  150. D.p.z = S.l.vy.z;
  151. S.l.vx.y = 0;
  152. S.l.vx.z = 0;
  153. S.l.vy.z = 0;
  154. AffineSpaceT<LinearSpace3<Vec3<T>>> R = LinearSpace3<Vec3<T>>(q);
  155. return D * R * S;
  156. }
  157. // this is a specialized version for Vec3fa because that does
  158. // not play along nicely with the other templated Vec3/Vec4 types
  159. __forceinline AffineSpace3fa slerp(const AffineSpace3ff& M0,
  160. const AffineSpace3ff& M1,
  161. const float& t)
  162. {
  163. Quaternion3f q0(M0.p.w, M0.l.vx.w, M0.l.vy.w, M0.l.vz.w);
  164. Quaternion3f q1(M1.p.w, M1.l.vx.w, M1.l.vy.w, M1.l.vz.w);
  165. Quaternion3f q = slerp(q0, q1, t);
  166. AffineSpace3fa S = lerp(M0, M1, t);
  167. AffineSpace3fa D(one);
  168. D.p.x = S.l.vx.y;
  169. D.p.y = S.l.vx.z;
  170. D.p.z = S.l.vy.z;
  171. S.l.vx.y = 0;
  172. S.l.vx.z = 0;
  173. S.l.vy.z = 0;
  174. AffineSpace3fa R = LinearSpace3fa(q);
  175. return D * R * S;
  176. }
  177. __forceinline AffineSpace3fa quaternionDecompositionToAffineSpace(const AffineSpace3ff& qd)
  178. {
  179. // compute affine transform from quaternion decomposition
  180. Quaternion3f q(qd.p.w, qd.l.vx.w, qd.l.vy.w, qd.l.vz.w);
  181. AffineSpace3fa M = qd;
  182. AffineSpace3fa D(one);
  183. D.p.x = M.l.vx.y;
  184. D.p.y = M.l.vx.z;
  185. D.p.z = M.l.vy.z;
  186. M.l.vx.y = 0;
  187. M.l.vx.z = 0;
  188. M.l.vy.z = 0;
  189. AffineSpace3fa R = LinearSpace3fa(q);
  190. return D * R * M;
  191. }
  192. __forceinline void quaternionDecomposition(const AffineSpace3ff& qd, Vec3fa& T, Quaternion3f& q, AffineSpace3fa& S)
  193. {
  194. q = Quaternion3f(qd.p.w, qd.l.vx.w, qd.l.vy.w, qd.l.vz.w);
  195. S = qd;
  196. T.x = qd.l.vx.y;
  197. T.y = qd.l.vx.z;
  198. T.z = qd.l.vy.z;
  199. S.l.vx.y = 0;
  200. S.l.vx.z = 0;
  201. S.l.vy.z = 0;
  202. }
  203. __forceinline AffineSpace3fx quaternionDecomposition(Vec3fa const& T, Quaternion3f const& q, AffineSpace3fa const& S)
  204. {
  205. AffineSpace3ff M = S;
  206. M.l.vx.w = q.i;
  207. M.l.vy.w = q.j;
  208. M.l.vz.w = q.k;
  209. M.p.w = q.r;
  210. M.l.vx.y = T.x;
  211. M.l.vx.z = T.y;
  212. M.l.vy.z = T.z;
  213. return M;
  214. }
  215. struct __aligned(16) QuaternionDecomposition
  216. {
  217. float scale_x = 1.f;
  218. float scale_y = 1.f;
  219. float scale_z = 1.f;
  220. float skew_xy = 0.f;
  221. float skew_xz = 0.f;
  222. float skew_yz = 0.f;
  223. float shift_x = 0.f;
  224. float shift_y = 0.f;
  225. float shift_z = 0.f;
  226. float quaternion_r = 1.f;
  227. float quaternion_i = 0.f;
  228. float quaternion_j = 0.f;
  229. float quaternion_k = 0.f;
  230. float translation_x = 0.f;
  231. float translation_y = 0.f;
  232. float translation_z = 0.f;
  233. };
  234. __forceinline QuaternionDecomposition quaternionDecomposition(AffineSpace3ff const& M)
  235. {
  236. QuaternionDecomposition qd;
  237. qd.scale_x = M.l.vx.x;
  238. qd.scale_y = M.l.vy.y;
  239. qd.scale_z = M.l.vz.z;
  240. qd.shift_x = M.p.x;
  241. qd.shift_y = M.p.y;
  242. qd.shift_z = M.p.z;
  243. qd.translation_x = M.l.vx.y;
  244. qd.translation_y = M.l.vx.z;
  245. qd.translation_z = M.l.vy.z;
  246. qd.skew_xy = M.l.vy.x;
  247. qd.skew_xz = M.l.vz.x;
  248. qd.skew_yz = M.l.vz.y;
  249. qd.quaternion_r = M.p.w;
  250. qd.quaternion_i = M.l.vx.w;
  251. qd.quaternion_j = M.l.vy.w;
  252. qd.quaternion_k = M.l.vz.w;
  253. return qd;
  254. }
  255. ////////////////////////////////////////////////////////////////////////////////
  256. /*
  257. * ! Template Specialization for 2D: return matrix for rotation around point
  258. * (rotation around arbitrarty vector is not meaningful in 2D)
  259. */
  260. template<> __forceinline
  261. AffineSpace2f AffineSpace2f::rotate(const Vec2f& p, const float& r) {
  262. return translate(+p)*AffineSpace2f(LinearSpace2f::rotate(r))*translate(-p);
  263. }
  264. ////////////////////////////////////////////////////////////////////////////////
  265. // Similarity Transform
  266. //
  267. // checks, if M is a similarity transformation, i.e if there exists a factor D
  268. // such that for all x,y: distance(Mx, My) = D * distance(x, y)
  269. ////////////////////////////////////////////////////////////////////////////////
  270. __forceinline bool similarityTransform(const AffineSpace3fa& M, float* D)
  271. {
  272. if (D) *D = 0.f;
  273. if (abs(dot(M.l.vx, M.l.vy)) > 1e-5f) return false;
  274. if (abs(dot(M.l.vx, M.l.vz)) > 1e-5f) return false;
  275. if (abs(dot(M.l.vy, M.l.vz)) > 1e-5f) return false;
  276. const float D_x = dot(M.l.vx, M.l.vx);
  277. const float D_y = dot(M.l.vy, M.l.vy);
  278. const float D_z = dot(M.l.vz, M.l.vz);
  279. if (abs(D_x - D_y) > 1e-5f ||
  280. abs(D_x - D_z) > 1e-5f ||
  281. abs(D_y - D_z) > 1e-5f)
  282. return false;
  283. if (D) *D = sqrtf(D_x);
  284. return true;
  285. }
  286. __forceinline void AffineSpace3fa_store_unaligned(const AffineSpace3fa &source, AffineSpace3fa* ptr)
  287. {
  288. Vec3fa::storeu(&ptr->l.vx, source.l.vx);
  289. Vec3fa::storeu(&ptr->l.vy, source.l.vy);
  290. Vec3fa::storeu(&ptr->l.vz, source.l.vz);
  291. Vec3fa::storeu(&ptr->p, source.p);
  292. }
  293. __forceinline AffineSpace3fa AffineSpace3fa_load_unaligned(AffineSpace3fa* ptr)
  294. {
  295. AffineSpace3fa space;
  296. space.l.vx = Vec3fa::loadu(&ptr->l.vx);
  297. space.l.vy = Vec3fa::loadu(&ptr->l.vy);
  298. space.l.vz = Vec3fa::loadu(&ptr->l.vz);
  299. space.p = Vec3fa::loadu(&ptr->p);
  300. return space;
  301. }
  302. #undef VectorT
  303. #undef ScalarT
  304. }