Matrix4x4.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Math.h>
  8. #include <LibGfx/AffineTransform.h>
  9. #include <LibGfx/Matrix.h>
  10. #include <LibGfx/Vector3.h>
  11. #include <LibGfx/Vector4.h>
  12. namespace Gfx {
  13. template<typename T>
  14. using Matrix4x4 = Matrix<4, T>;
  15. template<typename T>
  16. constexpr static Vector4<T> operator*(Matrix4x4<T> const& m, Vector4<T> const& v)
  17. {
  18. auto const& elements = m.elements();
  19. return Vector4<T>(
  20. v.x() * elements[0][0] + v.y() * elements[0][1] + v.z() * elements[0][2] + v.w() * elements[0][3],
  21. v.x() * elements[1][0] + v.y() * elements[1][1] + v.z() * elements[1][2] + v.w() * elements[1][3],
  22. v.x() * elements[2][0] + v.y() * elements[2][1] + v.z() * elements[2][2] + v.w() * elements[2][3],
  23. v.x() * elements[3][0] + v.y() * elements[3][1] + v.z() * elements[3][2] + v.w() * elements[3][3]);
  24. }
  25. // FIXME: this is a specific Matrix4x4 * Vector3 interaction that implies W=1; maybe move this out of LibGfx
  26. // or replace a Matrix4x4 * Vector4 operation?
  27. template<typename T>
  28. constexpr static Vector3<T> transform_point(Matrix4x4<T> const& m, Vector3<T> const& p)
  29. {
  30. auto const& elements = m.elements();
  31. return Vector3<T>(
  32. p.x() * elements[0][0] + p.y() * elements[0][1] + p.z() * elements[0][2] + elements[0][3],
  33. p.x() * elements[1][0] + p.y() * elements[1][1] + p.z() * elements[1][2] + elements[1][3],
  34. p.x() * elements[2][0] + p.y() * elements[2][1] + p.z() * elements[2][2] + elements[2][3]);
  35. }
  36. template<typename T>
  37. constexpr static Matrix4x4<T> translation_matrix(Vector3<T> const& p)
  38. {
  39. return Matrix4x4<T>(
  40. 1, 0, 0, p.x(),
  41. 0, 1, 0, p.y(),
  42. 0, 0, 1, p.z(),
  43. 0, 0, 0, 1);
  44. }
  45. template<typename T>
  46. constexpr static Matrix4x4<T> scale_matrix(Vector3<T> const& s)
  47. {
  48. return Matrix4x4<T>(
  49. s.x(), 0, 0, 0,
  50. 0, s.y(), 0, 0,
  51. 0, 0, s.z(), 0,
  52. 0, 0, 0, 1);
  53. }
  54. template<typename T>
  55. constexpr static Matrix4x4<T> rotation_matrix(Vector3<T> const& axis, T angle)
  56. {
  57. T c, s;
  58. AK::sincos(angle, s, c);
  59. T t = 1 - c;
  60. T x = axis.x();
  61. T y = axis.y();
  62. T z = axis.z();
  63. return Matrix4x4<T>(
  64. t * x * x + c, t * x * y - z * s, t * x * z + y * s, 0,
  65. t * x * y + z * s, t * y * y + c, t * y * z - x * s, 0,
  66. t * x * z - y * s, t * y * z + x * s, t * z * z + c, 0,
  67. 0, 0, 0, 1);
  68. }
  69. template<typename T>
  70. Gfx::AffineTransform extract_2d_affine_transform(Matrix4x4<T> const& matrix)
  71. {
  72. auto* m = matrix.elements();
  73. return Gfx::AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][3], m[1][3]);
  74. }
  75. typedef Matrix4x4<float> FloatMatrix4x4;
  76. typedef Matrix4x4<double> DoubleMatrix4x4;
  77. }
  78. using Gfx::DoubleMatrix4x4;
  79. using Gfx::FloatMatrix4x4;
  80. using Gfx::Matrix4x4;