linearspace3.h 11 KB

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