linearspace3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "vec3.h"
  5. #include "quaternion.h"
  6. namespace embree
  7. {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. /// 3D Linear Transform (3x3 Matrix)
  10. ////////////////////////////////////////////////////////////////////////////////
  11. template<typename T> struct LinearSpace3
  12. {
  13. typedef T Vector;
  14. typedef typename T::Scalar Scalar;
  15. /*! default matrix constructor */
  16. __forceinline LinearSpace3 ( ) {}
  17. __forceinline LinearSpace3 ( const LinearSpace3& other ) { vx = other.vx; vy = other.vy; vz = other.vz; }
  18. __forceinline LinearSpace3& operator=( const LinearSpace3& other ) { vx = other.vx; vy = other.vy; vz = other.vz; return *this; }
  19. template<typename L1> __forceinline LinearSpace3( const LinearSpace3<L1>& s ) : vx(s.vx), vy(s.vy), vz(s.vz) {}
  20. /*! matrix construction from column vectors */
  21. __forceinline LinearSpace3(const Vector& vx, const Vector& vy, const Vector& vz)
  22. : vx(vx), vy(vy), vz(vz) {}
  23. /*! construction from quaternion */
  24. __forceinline LinearSpace3( const QuaternionT<Scalar>& q )
  25. : vx((q.r*q.r + q.i*q.i - q.j*q.j - q.k*q.k), 2.0f*(q.i*q.j + q.r*q.k), 2.0f*(q.i*q.k - q.r*q.j))
  26. , vy(2.0f*(q.i*q.j - q.r*q.k), (q.r*q.r - q.i*q.i + q.j*q.j - q.k*q.k), 2.0f*(q.j*q.k + q.r*q.i))
  27. , vz(2.0f*(q.i*q.k + q.r*q.j), 2.0f*(q.j*q.k - q.r*q.i), (q.r*q.r - q.i*q.i - q.j*q.j + q.k*q.k)) {}
  28. /*! matrix construction from row mayor data */
  29. __forceinline LinearSpace3(const Scalar& m00, const Scalar& m01, const Scalar& m02,
  30. const Scalar& m10, const Scalar& m11, const Scalar& m12,
  31. const Scalar& m20, const Scalar& m21, const Scalar& m22)
  32. : vx(m00,m10,m20), vy(m01,m11,m21), vz(m02,m12,m22) {}
  33. /*! compute the determinant of the matrix */
  34. __forceinline const Scalar det() const { return dot(vx,cross(vy,vz)); }
  35. /*! compute adjoint matrix */
  36. __forceinline const LinearSpace3 adjoint() const { return LinearSpace3(cross(vy,vz),cross(vz,vx),cross(vx,vy)).transposed(); }
  37. /*! compute inverse matrix */
  38. __forceinline const LinearSpace3 inverse() const { return adjoint()/det(); }
  39. /*! compute transposed matrix */
  40. __forceinline const LinearSpace3 transposed() const { return LinearSpace3(vx.x,vx.y,vx.z,vy.x,vy.y,vy.z,vz.x,vz.y,vz.z); }
  41. /*! returns first row of matrix */
  42. __forceinline Vector row0() const { return Vector(vx.x,vy.x,vz.x); }
  43. /*! returns second row of matrix */
  44. __forceinline Vector row1() const { return Vector(vx.y,vy.y,vz.y); }
  45. /*! returns third row of matrix */
  46. __forceinline Vector row2() const { return Vector(vx.z,vy.z,vz.z); }
  47. ////////////////////////////////////////////////////////////////////////////////
  48. /// Constants
  49. ////////////////////////////////////////////////////////////////////////////////
  50. __forceinline LinearSpace3( ZeroTy ) : vx(zero), vy(zero), vz(zero) {}
  51. __forceinline LinearSpace3( OneTy ) : vx(one, zero, zero), vy(zero, one, zero), vz(zero, zero, one) {}
  52. /*! return matrix for scaling */
  53. static __forceinline LinearSpace3 scale(const Vector& s) {
  54. return LinearSpace3(s.x, 0, 0,
  55. 0 , s.y, 0,
  56. 0 , 0, s.z);
  57. }
  58. /*! return matrix for rotation around arbitrary axis */
  59. static __forceinline LinearSpace3 rotate(const Vector& _u, const Scalar& r) {
  60. Vector u = normalize(_u);
  61. Scalar s = sin(r), c = cos(r);
  62. return LinearSpace3(u.x*u.x+(1-u.x*u.x)*c, u.x*u.y*(1-c)-u.z*s, u.x*u.z*(1-c)+u.y*s,
  63. u.x*u.y*(1-c)+u.z*s, u.y*u.y+(1-u.y*u.y)*c, u.y*u.z*(1-c)-u.x*s,
  64. u.x*u.z*(1-c)-u.y*s, u.y*u.z*(1-c)+u.x*s, u.z*u.z+(1-u.z*u.z)*c);
  65. }
  66. public:
  67. /*! the column vectors of the matrix */
  68. Vector vx,vy,vz;
  69. };
  70. /*! compute transposed matrix */
  71. template<> __forceinline const LinearSpace3<Vec3fa> LinearSpace3<Vec3fa>::transposed() const {
  72. vfloat4 rx,ry,rz; transpose((vfloat4&)vx,(vfloat4&)vy,(vfloat4&)vz,vfloat4(zero),rx,ry,rz);
  73. return LinearSpace3<Vec3fa>(Vec3fa(rx),Vec3fa(ry),Vec3fa(rz));
  74. }
  75. template<typename T>
  76. __forceinline const LinearSpace3<T> transposed(const LinearSpace3<T>& xfm) {
  77. return xfm.transposed();
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////
  80. // Unary Operators
  81. ////////////////////////////////////////////////////////////////////////////////
  82. template<typename T> __forceinline LinearSpace3<T> operator -( const LinearSpace3<T>& a ) { return LinearSpace3<T>(-a.vx,-a.vy,-a.vz); }
  83. template<typename T> __forceinline LinearSpace3<T> operator +( const LinearSpace3<T>& a ) { return LinearSpace3<T>(+a.vx,+a.vy,+a.vz); }
  84. template<typename T> __forceinline LinearSpace3<T> rcp ( const LinearSpace3<T>& a ) { return a.inverse(); }
  85. /* constructs a coordinate frame form a normalized normal */
  86. template<typename T> __forceinline LinearSpace3<T> frame(const T& N)
  87. {
  88. const T dx0(0,N.z,-N.y);
  89. const T dx1(-N.z,0,N.x);
  90. const T dx = normalize(select(dot(dx0,dx0) > dot(dx1,dx1),dx0,dx1));
  91. const T dy = normalize(cross(N,dx));
  92. return LinearSpace3<T>(dx,dy,N);
  93. }
  94. /* constructs a coordinate frame from a normal and approximate x-direction */
  95. template<typename T> __forceinline LinearSpace3<T> frame(const T& N, const T& dxi)
  96. {
  97. if (abs(dot(dxi,N)) > 0.99f) return frame(N); // fallback in case N and dxi are very parallel
  98. const T dx = normalize(cross(dxi,N));
  99. const T dy = normalize(cross(N,dx));
  100. return LinearSpace3<T>(dx,dy,N);
  101. }
  102. /* clamps linear space to range -1 to +1 */
  103. template<typename T> __forceinline LinearSpace3<T> clamp(const LinearSpace3<T>& space) {
  104. return LinearSpace3<T>(clamp(space.vx,T(-1.0f),T(1.0f)),
  105. clamp(space.vy,T(-1.0f),T(1.0f)),
  106. clamp(space.vz,T(-1.0f),T(1.0f)));
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////
  109. // Binary Operators
  110. ////////////////////////////////////////////////////////////////////////////////
  111. template<typename T> __forceinline LinearSpace3<T> operator +( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return LinearSpace3<T>(a.vx+b.vx,a.vy+b.vy,a.vz+b.vz); }
  112. template<typename T> __forceinline LinearSpace3<T> operator -( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return LinearSpace3<T>(a.vx-b.vx,a.vy-b.vy,a.vz-b.vz); }
  113. template<typename T> __forceinline LinearSpace3<T> operator*(const typename T::Scalar & a, const LinearSpace3<T>& b) { return LinearSpace3<T>(a*b.vx, a*b.vy, a*b.vz); }
  114. template<typename T> __forceinline T operator*(const LinearSpace3<T>& a, const T & b) { return madd(T(b.x),a.vx,madd(T(b.y),a.vy,T(b.z)*a.vz)); }
  115. template<typename T> __forceinline LinearSpace3<T> operator*(const LinearSpace3<T>& a, const LinearSpace3<T>& b) { return LinearSpace3<T>(a*b.vx, a*b.vy, a*b.vz); }
  116. template<typename T> __forceinline LinearSpace3<T> operator/(const LinearSpace3<T>& a, const typename T::Scalar & b) { return LinearSpace3<T>(a.vx/b, a.vy/b, a.vz/b); }
  117. template<typename T> __forceinline LinearSpace3<T> operator/(const LinearSpace3<T>& a, const LinearSpace3<T>& b) { return a * rcp(b); }
  118. template<typename T> __forceinline LinearSpace3<T>& operator *=( LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a = a * b; }
  119. template<typename T> __forceinline LinearSpace3<T>& operator /=( LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a = a / b; }
  120. template<typename T> __forceinline T xfmPoint (const LinearSpace3<T>& s, const T & a) { return madd(T(a.x),s.vx,madd(T(a.y),s.vy,T(a.z)*s.vz)); }
  121. template<typename T> __forceinline T xfmVector(const LinearSpace3<T>& s, const T & a) { return madd(T(a.x),s.vx,madd(T(a.y),s.vy,T(a.z)*s.vz)); }
  122. template<typename T> __forceinline T xfmNormal(const LinearSpace3<T>& s, const T & a) { return xfmVector(s.inverse().transposed(),a); }
  123. ////////////////////////////////////////////////////////////////////////////////
  124. /// Comparison Operators
  125. ////////////////////////////////////////////////////////////////////////////////
  126. template<typename T> __forceinline bool operator ==( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a.vx == b.vx && a.vy == b.vy && a.vz == b.vz; }
  127. template<typename T> __forceinline bool operator !=( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a.vx != b.vx || a.vy != b.vy || a.vz != b.vz; }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. /// Select
  130. ////////////////////////////////////////////////////////////////////////////////
  131. template<typename T> __forceinline LinearSpace3<T> select ( const typename T::Scalar::Bool& s, const LinearSpace3<T>& t, const LinearSpace3<T>& f ) {
  132. return LinearSpace3<T>(select(s,t.vx,f.vx),select(s,t.vy,f.vy),select(s,t.vz,f.vz));
  133. }
  134. /*! blending */
  135. template<typename T>
  136. __forceinline LinearSpace3<T> lerp(const LinearSpace3<T>& l0, const LinearSpace3<T>& l1, const float t)
  137. {
  138. return LinearSpace3<T>(lerp(l0.vx,l1.vx,t),
  139. lerp(l0.vy,l1.vy,t),
  140. lerp(l0.vz,l1.vz,t));
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////
  143. /// Output Operators
  144. ////////////////////////////////////////////////////////////////////////////////
  145. template<typename T> static embree_ostream operator<<(embree_ostream cout, const LinearSpace3<T>& m) {
  146. return cout << "{ vx = " << m.vx << ", vy = " << m.vy << ", vz = " << m.vz << "}";
  147. }
  148. /*! Shortcuts for common linear spaces. */
  149. typedef LinearSpace3<Vec3f> LinearSpace3f;
  150. typedef LinearSpace3<Vec3fa> LinearSpace3fa;
  151. typedef LinearSpace3<Vec3fx> LinearSpace3fx;
  152. typedef LinearSpace3<Vec3ff> LinearSpace3ff;
  153. template<int N> using LinearSpace3vf = LinearSpace3<Vec3<vfloat<N>>>;
  154. typedef LinearSpace3<Vec3<vfloat<4>>> LinearSpace3vf4;
  155. typedef LinearSpace3<Vec3<vfloat<8>>> LinearSpace3vf8;
  156. typedef LinearSpace3<Vec3<vfloat<16>>> LinearSpace3vf16;
  157. /*! blending */
  158. template<typename T, typename S>
  159. __forceinline LinearSpace3<T> lerp(const LinearSpace3<T>& l0,
  160. const LinearSpace3<T>& l1,
  161. const S& t)
  162. {
  163. return LinearSpace3<T>(lerp(l0.vx,l1.vx,t),
  164. lerp(l0.vy,l1.vy,t),
  165. lerp(l0.vz,l1.vz,t));
  166. }
  167. }