linearspace2.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "vec2.h"
  5. namespace embree
  6. {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. /// 2D Linear Transform (2x2 Matrix)
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template<typename T> struct LinearSpace2
  11. {
  12. typedef T Vector;
  13. typedef typename T::Scalar Scalar;
  14. /*! default matrix constructor */
  15. __forceinline LinearSpace2 ( ) {}
  16. __forceinline LinearSpace2 ( const LinearSpace2& other ) { vx = other.vx; vy = other.vy; }
  17. __forceinline LinearSpace2& operator=( const LinearSpace2& other ) { vx = other.vx; vy = other.vy; return *this; }
  18. template<typename L1> __forceinline LinearSpace2( const LinearSpace2<L1>& s ) : vx(s.vx), vy(s.vy) {}
  19. /*! matrix construction from column vectors */
  20. __forceinline LinearSpace2(const Vector& vx, const Vector& vy)
  21. : vx(vx), vy(vy) {}
  22. /*! matrix construction from row mayor data */
  23. __forceinline LinearSpace2(const Scalar& m00, const Scalar& m01,
  24. const Scalar& m10, const Scalar& m11)
  25. : vx(m00,m10), vy(m01,m11) {}
  26. /*! compute the determinant of the matrix */
  27. __forceinline const Scalar det() const { return vx.x*vy.y - vx.y*vy.x; }
  28. /*! compute adjoint matrix */
  29. __forceinline const LinearSpace2 adjoint() const { return LinearSpace2(vy.y,-vy.x,-vx.y,vx.x); }
  30. /*! compute inverse matrix */
  31. __forceinline const LinearSpace2 inverse() const { return adjoint()/det(); }
  32. /*! compute transposed matrix */
  33. __forceinline const LinearSpace2 transposed() const { return LinearSpace2(vx.x,vx.y,vy.x,vy.y); }
  34. /*! returns first row of matrix */
  35. __forceinline Vector row0() const { return Vector(vx.x,vy.x); }
  36. /*! returns second row of matrix */
  37. __forceinline Vector row1() const { return Vector(vx.y,vy.y); }
  38. ////////////////////////////////////////////////////////////////////////////////
  39. /// Constants
  40. ////////////////////////////////////////////////////////////////////////////////
  41. __forceinline LinearSpace2( ZeroTy ) : vx(zero), vy(zero) {}
  42. __forceinline LinearSpace2( OneTy ) : vx(one, zero), vy(zero, one) {}
  43. /*! return matrix for scaling */
  44. static __forceinline LinearSpace2 scale(const Vector& s) {
  45. return LinearSpace2(s.x, 0,
  46. 0 , s.y);
  47. }
  48. /*! return matrix for rotation */
  49. static __forceinline LinearSpace2 rotate(const Scalar& r) {
  50. Scalar s = sin(r), c = cos(r);
  51. return LinearSpace2(c, -s,
  52. s, c);
  53. }
  54. /*! return closest orthogonal matrix (i.e. a general rotation including reflection) */
  55. LinearSpace2 orthogonal() const
  56. {
  57. LinearSpace2 m = *this;
  58. // mirrored?
  59. Scalar mirror(one);
  60. if (m.det() < Scalar(zero)) {
  61. m.vx = -m.vx;
  62. mirror = -mirror;
  63. }
  64. // rotation
  65. for (int i = 0; i < 99; i++) {
  66. const LinearSpace2 m_next = 0.5 * (m + m.transposed().inverse());
  67. const LinearSpace2 d = m_next - m;
  68. m = m_next;
  69. // norm^2 of difference small enough?
  70. if (max(dot(d.vx, d.vx), dot(d.vy, d.vy)) < 1e-8)
  71. break;
  72. }
  73. // rotation * mirror_x
  74. return LinearSpace2(mirror*m.vx, m.vy);
  75. }
  76. public:
  77. /*! the column vectors of the matrix */
  78. Vector vx,vy;
  79. };
  80. ////////////////////////////////////////////////////////////////////////////////
  81. // Unary Operators
  82. ////////////////////////////////////////////////////////////////////////////////
  83. template<typename T> __forceinline LinearSpace2<T> operator -( const LinearSpace2<T>& a ) { return LinearSpace2<T>(-a.vx,-a.vy); }
  84. template<typename T> __forceinline LinearSpace2<T> operator +( const LinearSpace2<T>& a ) { return LinearSpace2<T>(+a.vx,+a.vy); }
  85. template<typename T> __forceinline LinearSpace2<T> rcp ( const LinearSpace2<T>& a ) { return a.inverse(); }
  86. ////////////////////////////////////////////////////////////////////////////////
  87. // Binary Operators
  88. ////////////////////////////////////////////////////////////////////////////////
  89. template<typename T> __forceinline LinearSpace2<T> operator +( const LinearSpace2<T>& a, const LinearSpace2<T>& b ) { return LinearSpace2<T>(a.vx+b.vx,a.vy+b.vy); }
  90. template<typename T> __forceinline LinearSpace2<T> operator -( const LinearSpace2<T>& a, const LinearSpace2<T>& b ) { return LinearSpace2<T>(a.vx-b.vx,a.vy-b.vy); }
  91. template<typename T> __forceinline LinearSpace2<T> operator*(const typename T::Scalar & a, const LinearSpace2<T>& b) { return LinearSpace2<T>(a*b.vx, a*b.vy); }
  92. template<typename T> __forceinline T operator*(const LinearSpace2<T>& a, const T & b) { return b.x*a.vx + b.y*a.vy; }
  93. template<typename T> __forceinline LinearSpace2<T> operator*(const LinearSpace2<T>& a, const LinearSpace2<T>& b) { return LinearSpace2<T>(a*b.vx, a*b.vy); }
  94. template<typename T> __forceinline LinearSpace2<T> operator/(const LinearSpace2<T>& a, const typename T::Scalar & b) { return LinearSpace2<T>(a.vx/b, a.vy/b); }
  95. template<typename T> __forceinline LinearSpace2<T> operator/(const LinearSpace2<T>& a, const LinearSpace2<T>& b) { return a * rcp(b); }
  96. template<typename T> __forceinline LinearSpace2<T>& operator *=( LinearSpace2<T>& a, const LinearSpace2<T>& b ) { return a = a * b; }
  97. template<typename T> __forceinline LinearSpace2<T>& operator /=( LinearSpace2<T>& a, const LinearSpace2<T>& b ) { return a = a / b; }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. /// Comparison Operators
  100. ////////////////////////////////////////////////////////////////////////////////
  101. template<typename T> __forceinline bool operator ==( const LinearSpace2<T>& a, const LinearSpace2<T>& b ) { return a.vx == b.vx && a.vy == b.vy; }
  102. template<typename T> __forceinline bool operator !=( const LinearSpace2<T>& a, const LinearSpace2<T>& b ) { return a.vx != b.vx || a.vy != b.vy; }
  103. ////////////////////////////////////////////////////////////////////////////////
  104. /// Output Operators
  105. ////////////////////////////////////////////////////////////////////////////////
  106. template<typename T> static embree_ostream operator<<(embree_ostream cout, const LinearSpace2<T>& m) {
  107. return cout << "{ vx = " << m.vx << ", vy = " << m.vy << "}";
  108. }
  109. /*! Shortcuts for common linear spaces. */
  110. typedef LinearSpace2<Vec2f> LinearSpace2f;
  111. typedef LinearSpace2<Vec2fa> LinearSpace2fa;
  112. }