Vector4.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <math.h>
  9. namespace Gfx {
  10. template<typename T>
  11. class Vector4 final {
  12. public:
  13. constexpr Vector4() = default;
  14. constexpr Vector4(T x, T y, T z, T w)
  15. : m_x(x)
  16. , m_y(y)
  17. , m_z(z)
  18. , m_w(w)
  19. {
  20. }
  21. constexpr T x() const { return m_x; }
  22. constexpr T y() const { return m_y; }
  23. constexpr T z() const { return m_z; }
  24. constexpr T w() const { return m_w; }
  25. constexpr void set_x(T value) { m_x = value; }
  26. constexpr void set_y(T value) { m_y = value; }
  27. constexpr void set_z(T value) { m_z = value; }
  28. constexpr void set_w(T value) { m_w = value; }
  29. constexpr Vector4& operator+=(const Vector4& other)
  30. {
  31. m_x += other.m_x;
  32. m_y += other.m_y;
  33. m_z += other.m_z;
  34. m_w += other.m_w;
  35. return *this;
  36. }
  37. constexpr Vector4& operator-=(const Vector4& other)
  38. {
  39. m_x -= other.m_x;
  40. m_y -= other.m_y;
  41. m_z -= other.m_z;
  42. m_w -= other.m_w;
  43. return *this;
  44. }
  45. constexpr Vector4 operator+(const Vector4& other) const
  46. {
  47. return Vector4(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z, m_w + other.m_w);
  48. }
  49. constexpr Vector4 operator-(const Vector4& other) const
  50. {
  51. return Vector4(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z, m_w - other.m_w);
  52. }
  53. constexpr Vector4 operator*(const Vector4& other) const
  54. {
  55. return Vector4(m_x * other.m_x, m_y * other.m_y, m_z * other.m_z, m_w * other.m_w);
  56. }
  57. constexpr Vector4 operator/(const Vector4& other) const
  58. {
  59. return Vector4(m_x / other.m_x, m_y / other.m_y, m_z / other.m_z, m_w / other.m_w);
  60. }
  61. constexpr Vector4 operator*(T f) const
  62. {
  63. return Vector4(m_x * f, m_y * f, m_z * f, m_w * f);
  64. }
  65. constexpr Vector4 operator/(T f) const
  66. {
  67. return Vector4(m_x / f, m_y / f, m_z / f, m_w / f);
  68. }
  69. constexpr T dot(const Vector4& other) const
  70. {
  71. return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z + m_w * other.m_w;
  72. }
  73. constexpr Vector4 normalized() const
  74. {
  75. T inv_length = 1 / length();
  76. return *this * inv_length;
  77. }
  78. constexpr Vector4 clamped(T m, T x) const
  79. {
  80. Vector4 copy { *this };
  81. copy.clamp(m, x);
  82. return copy;
  83. }
  84. constexpr void clamp(T min_value, T max_value)
  85. {
  86. m_x = max(min_value, m_x);
  87. m_y = max(min_value, m_y);
  88. m_z = max(min_value, m_z);
  89. m_w = max(min_value, m_w);
  90. m_x = min(max_value, m_x);
  91. m_y = min(max_value, m_y);
  92. m_z = min(max_value, m_z);
  93. m_w = min(max_value, m_w);
  94. }
  95. constexpr void normalize()
  96. {
  97. T inv_length = 1 / length();
  98. m_x *= inv_length;
  99. m_y *= inv_length;
  100. m_z *= inv_length;
  101. m_w *= inv_length;
  102. }
  103. constexpr T length() const
  104. {
  105. return sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w);
  106. }
  107. String to_string() const
  108. {
  109. return String::formatted("[{},{},{},{}]", x(), y(), z(), w());
  110. }
  111. private:
  112. T m_x;
  113. T m_y;
  114. T m_z;
  115. T m_w;
  116. };
  117. typedef Vector4<float> FloatVector4;
  118. typedef Vector4<double> DoubleVector4;
  119. }
  120. namespace AK {
  121. template<typename T>
  122. struct Formatter<Gfx::Vector4<T>> : Formatter<StringView> {
  123. void format(FormatBuilder& builder, const Gfx::Vector4<T>& value)
  124. {
  125. Formatter<StringView>::format(builder, value.to_string());
  126. }
  127. };
  128. }
  129. using Gfx::DoubleVector4;
  130. using Gfx::FloatVector4;
  131. using Gfx::Vector4;