Vector4.inl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2014 Markus Schöngart
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <type_traits>
  29. namespace Rml {
  30. template <typename Type>
  31. Vector4<Type>::Vector4() : x{}, y{}, z{}, w{}
  32. {}
  33. template <typename Type>
  34. Vector4<Type>::Vector4(Type v) : x(v), y(v), z(v), w(v)
  35. {}
  36. template <typename Type>
  37. Vector4<Type>::Vector4(Type x, Type y, Type z, Type w) : x(x), y(y), z(z), w(w)
  38. {}
  39. template <typename Type>
  40. Vector4<Type>::Vector4(Vector3<Type> const& v, Type w) : x(v.x), y(v.y), z(v.z), w(w)
  41. {}
  42. template <typename Type>
  43. float Vector4<Type>::Magnitude() const
  44. {
  45. return Math::SquareRoot(static_cast<float>(SquaredMagnitude()));
  46. }
  47. template <typename Type>
  48. Type Vector4<Type>::SquaredMagnitude() const
  49. {
  50. return x * x + y * y + z * z + w * w;
  51. }
  52. template <typename Type>
  53. inline Vector4<Type> Vector4<Type>::Normalise() const
  54. {
  55. static_assert(std::is_same<Type, float>::value, "Normalise only implemented for Vector<float>.");
  56. return *this;
  57. }
  58. template <>
  59. inline Vector4<float> Vector4<float>::Normalise() const
  60. {
  61. const float magnitude = Magnitude();
  62. if (Math::IsCloseToZero(magnitude))
  63. return *this;
  64. return *this / magnitude;
  65. }
  66. template <typename Type>
  67. Type Vector4<Type>::DotProduct(const Vector4<Type>& rhs) const
  68. {
  69. return x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w;
  70. }
  71. template <typename Type>
  72. Vector3<Type> Vector4<Type>::PerspectiveDivide() const
  73. {
  74. return Vector3<Type>(x / w, y / w, z / w);
  75. }
  76. template <typename Type>
  77. Vector4<Type> Vector4<Type>::operator-() const
  78. {
  79. return Vector4(-x, -y, -z, -w);
  80. }
  81. template <typename Type>
  82. Vector4<Type> Vector4<Type>::operator+(const Vector4<Type>& rhs) const
  83. {
  84. return Vector4<Type>(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
  85. }
  86. template <typename Type>
  87. Vector4<Type> Vector4<Type>::operator-(const Vector4<Type>& rhs) const
  88. {
  89. return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
  90. }
  91. template <typename Type>
  92. Vector4<Type> Vector4<Type>::operator*(Type rhs) const
  93. {
  94. return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
  95. }
  96. template <typename Type>
  97. Vector4<Type> Vector4<Type>::operator/(Type rhs) const
  98. {
  99. return Vector4(x / rhs, y / rhs, z / rhs, w / rhs);
  100. }
  101. template <typename Type>
  102. Vector4<Type>& Vector4<Type>::operator+=(const Vector4& rhs)
  103. {
  104. x += rhs.x;
  105. y += rhs.y;
  106. z += rhs.z;
  107. w += rhs.w;
  108. return *this;
  109. }
  110. template <typename Type>
  111. Vector4<Type>& Vector4<Type>::operator-=(const Vector4& rhs)
  112. {
  113. x -= rhs.x;
  114. y -= rhs.y;
  115. z -= rhs.z;
  116. w -= rhs.w;
  117. return *this;
  118. }
  119. template <typename Type>
  120. Vector4<Type>& Vector4<Type>::operator*=(const Type& rhs)
  121. {
  122. x *= rhs;
  123. y *= rhs;
  124. z *= rhs;
  125. w *= rhs;
  126. return *this;
  127. }
  128. template <typename Type>
  129. Vector4<Type>& Vector4<Type>::operator/=(const Type& rhs)
  130. {
  131. x /= rhs;
  132. y /= rhs;
  133. z /= rhs;
  134. w /= rhs;
  135. return *this;
  136. }
  137. template <typename Type>
  138. bool Vector4<Type>::operator==(const Vector4& rhs) const
  139. {
  140. return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
  141. }
  142. template <typename Type>
  143. bool Vector4<Type>::operator!=(const Vector4& rhs) const
  144. {
  145. return (x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w);
  146. }
  147. template <typename Type>
  148. Vector4<Type>::operator const Type*() const
  149. {
  150. return &x;
  151. }
  152. template <typename Type>
  153. Vector4<Type>::operator Type*()
  154. {
  155. return &x;
  156. }
  157. template <typename Type>
  158. template <typename U>
  159. inline Vector4<Type>::operator Vector4<U>() const
  160. {
  161. return Vector4<U>(static_cast<U>(x), static_cast<U>(y), static_cast<U>(z), static_cast<U>(w));
  162. }
  163. template <typename Type>
  164. Vector4<Type>::operator Vector3<Type>() const
  165. {
  166. return Vector3<Type>(x, y, z);
  167. }
  168. template <typename Type>
  169. Vector4<Type>::operator Vector2<Type>() const
  170. {
  171. return Vector2<Type>(x, y);
  172. }
  173. template <typename Type>
  174. inline Vector4<Type> operator*(Type lhs, Vector4<Type> rhs)
  175. {
  176. return rhs * lhs;
  177. }
  178. } // namespace Rml