Vector3.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/String.h>
  8. #include <math.h>
  9. namespace Gfx {
  10. template<typename T>
  11. class Vector3 final {
  12. public:
  13. constexpr Vector3() = default;
  14. constexpr Vector3(T x, T y, T z)
  15. : m_x(x)
  16. , m_y(y)
  17. , m_z(z)
  18. {
  19. }
  20. constexpr T x() const { return m_x; }
  21. constexpr T y() const { return m_y; }
  22. constexpr T z() const { return m_z; }
  23. constexpr void set_x(T value) { m_x = value; }
  24. constexpr void set_y(T value) { m_y = value; }
  25. constexpr void set_z(T value) { m_z = value; }
  26. constexpr Vector3& operator+=(const Vector3& other)
  27. {
  28. m_x += other.m_x;
  29. m_y += other.m_y;
  30. m_z += other.m_z;
  31. return *this;
  32. }
  33. constexpr Vector3& operator-=(const Vector3& other)
  34. {
  35. m_x -= other.m_x;
  36. m_y -= other.m_y;
  37. m_z -= other.m_z;
  38. return *this;
  39. }
  40. constexpr Vector3 operator+(const Vector3& other) const
  41. {
  42. return Vector3(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z);
  43. }
  44. constexpr Vector3 operator-(const Vector3& other) const
  45. {
  46. return Vector3(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z);
  47. }
  48. constexpr Vector3 operator*(const Vector3& other) const
  49. {
  50. return Vector3(m_x * other.m_x, m_y * other.m_y, m_z * other.m_z);
  51. }
  52. constexpr Vector3 operator/(const Vector3& other) const
  53. {
  54. return Vector3(m_x / other.m_x, m_y / other.m_y, m_z / other.m_z);
  55. }
  56. constexpr Vector3 operator*(T f) const
  57. {
  58. return Vector3(m_x * f, m_y * f, m_z * f);
  59. }
  60. constexpr Vector3 operator/(T f) const
  61. {
  62. return Vector3(m_x / f, m_y / f, m_z / f);
  63. }
  64. constexpr T dot(const Vector3& other) const
  65. {
  66. return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z;
  67. }
  68. constexpr Vector3 cross(const Vector3& other) const
  69. {
  70. return Vector3(
  71. m_y * other.m_z - m_z * other.m_y,
  72. m_z * other.m_x - m_x * other.m_z,
  73. m_x * other.m_y - m_y * other.m_x);
  74. }
  75. constexpr Vector3 normalized() const
  76. {
  77. T inv_length = 1 / length();
  78. return *this * inv_length;
  79. }
  80. constexpr Vector3 clamped(T m, T x) const
  81. {
  82. Vector3 copy { *this };
  83. copy.clamp(m, x);
  84. return copy;
  85. }
  86. constexpr void clamp(T min_value, T max_value)
  87. {
  88. m_x = max(min_value, m_x);
  89. m_y = max(min_value, m_y);
  90. m_z = max(min_value, m_z);
  91. m_x = min(max_value, m_x);
  92. m_y = min(max_value, m_y);
  93. m_z = min(max_value, m_z);
  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. }
  102. constexpr T length() const
  103. {
  104. return sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
  105. }
  106. String to_string() const
  107. {
  108. return String::formatted("[{},{},{}]", x(), y(), z());
  109. }
  110. private:
  111. T m_x;
  112. T m_y;
  113. T m_z;
  114. };
  115. typedef Vector3<float> FloatVector3;
  116. typedef Vector3<double> DoubleVector3;
  117. }
  118. namespace AK {
  119. template<typename T>
  120. struct Formatter<Gfx::Vector3<T>> : Formatter<StringView> {
  121. void format(FormatBuilder& builder, const Gfx::Vector3<T>& value)
  122. {
  123. Formatter<StringView>::format(builder, value.to_string());
  124. }
  125. };
  126. }
  127. using Gfx::DoubleVector3;
  128. using Gfx::FloatVector3;
  129. using Gfx::Vector3;