Vector4.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Math/Vector3.h"
  24. namespace Urho3D
  25. {
  26. /// Four-dimensional vector.
  27. class URHO3D_API Vector4
  28. {
  29. public:
  30. /// Construct a zero vector.
  31. Vector4() noexcept :
  32. x_(0.0f),
  33. y_(0.0f),
  34. z_(0.0f),
  35. w_(0.0f)
  36. {
  37. }
  38. /// Copy-construct from another vector.
  39. Vector4(const Vector4& vector) noexcept = default;
  40. /// Construct from a 3-dimensional vector and the W coordinate.
  41. Vector4(const Vector3& vector, float w) noexcept :
  42. x_(vector.x_),
  43. y_(vector.y_),
  44. z_(vector.z_),
  45. w_(w)
  46. {
  47. }
  48. /// Construct from coordinates.
  49. Vector4(float x, float y, float z, float w) noexcept :
  50. x_(x),
  51. y_(y),
  52. z_(z),
  53. w_(w)
  54. {
  55. }
  56. /// Construct from a float array.
  57. explicit Vector4(const float* data) noexcept :
  58. x_(data[0]),
  59. y_(data[1]),
  60. z_(data[2]),
  61. w_(data[3])
  62. {
  63. }
  64. /// Assign from another vector.
  65. Vector4& operator =(const Vector4& rhs) noexcept = default;
  66. /// Test for equality with another vector without epsilon.
  67. bool operator ==(const Vector4& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_ && w_ == rhs.w_; }
  68. /// Test for inequality with another vector without epsilon.
  69. bool operator !=(const Vector4& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_; }
  70. /// Add a vector.
  71. Vector4 operator +(const Vector4& rhs) const { return Vector4(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_, w_ + rhs.w_); }
  72. /// Return negation.
  73. Vector4 operator -() const { return Vector4(-x_, -y_, -z_, -w_); }
  74. /// Subtract a vector.
  75. Vector4 operator -(const Vector4& rhs) const { return Vector4(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_, w_ - rhs.w_); }
  76. /// Multiply with a scalar.
  77. Vector4 operator *(float rhs) const { return Vector4(x_ * rhs, y_ * rhs, z_ * rhs, w_ * rhs); }
  78. /// Multiply with a vector.
  79. Vector4 operator *(const Vector4& rhs) const { return Vector4(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_, w_ * rhs.w_); }
  80. /// Divide by a scalar.
  81. Vector4 operator /(float rhs) const { return Vector4(x_ / rhs, y_ / rhs, z_ / rhs, w_ / rhs); }
  82. /// Divide by a vector.
  83. Vector4 operator /(const Vector4& rhs) const { return Vector4(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_, w_ / rhs.w_); }
  84. /// Add-assign a vector.
  85. Vector4& operator +=(const Vector4& rhs)
  86. {
  87. x_ += rhs.x_;
  88. y_ += rhs.y_;
  89. z_ += rhs.z_;
  90. w_ += rhs.w_;
  91. return *this;
  92. }
  93. /// Subtract-assign a vector.
  94. Vector4& operator -=(const Vector4& rhs)
  95. {
  96. x_ -= rhs.x_;
  97. y_ -= rhs.y_;
  98. z_ -= rhs.z_;
  99. w_ -= rhs.w_;
  100. return *this;
  101. }
  102. /// Multiply-assign a scalar.
  103. Vector4& operator *=(float rhs)
  104. {
  105. x_ *= rhs;
  106. y_ *= rhs;
  107. z_ *= rhs;
  108. w_ *= rhs;
  109. return *this;
  110. }
  111. /// Multiply-assign a vector.
  112. Vector4& operator *=(const Vector4& rhs)
  113. {
  114. x_ *= rhs.x_;
  115. y_ *= rhs.y_;
  116. z_ *= rhs.z_;
  117. w_ *= rhs.w_;
  118. return *this;
  119. }
  120. /// Divide-assign a scalar.
  121. Vector4& operator /=(float rhs)
  122. {
  123. float invRhs = 1.0f / rhs;
  124. x_ *= invRhs;
  125. y_ *= invRhs;
  126. z_ *= invRhs;
  127. w_ *= invRhs;
  128. return *this;
  129. }
  130. /// Divide-assign a vector.
  131. Vector4& operator /=(const Vector4& rhs)
  132. {
  133. x_ /= rhs.x_;
  134. y_ /= rhs.y_;
  135. z_ /= rhs.z_;
  136. w_ /= rhs.w_;
  137. return *this;
  138. }
  139. /// Return const value by index.
  140. float operator[](unsigned index) const { return (&x_)[index]; }
  141. /// Return mutable value by index.
  142. float& operator[](unsigned index) { return (&x_)[index]; }
  143. /// Calculate dot product.
  144. float DotProduct(const Vector4& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_ + w_ * rhs.w_; }
  145. /// Calculate absolute dot product.
  146. float AbsDotProduct(const Vector4& rhs) const
  147. {
  148. return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_) + Urho3D::Abs(z_ * rhs.z_) + Urho3D::Abs(w_ * rhs.w_);
  149. }
  150. /// Project vector onto axis.
  151. float ProjectOntoAxis(const Vector3& axis) const { return DotProduct(Vector4(axis.Normalized(), 0.0f)); }
  152. /// Return absolute vector.
  153. Vector4 Abs() const { return Vector4(Urho3D::Abs(x_), Urho3D::Abs(y_), Urho3D::Abs(z_), Urho3D::Abs(w_)); }
  154. /// Linear interpolation with another vector.
  155. Vector4 Lerp(const Vector4& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  156. /// Test for equality with another vector with epsilon.
  157. bool Equals(const Vector4& rhs) const
  158. {
  159. return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_) && Urho3D::Equals(w_, rhs.w_);
  160. }
  161. /// Return whether any component is NaN.
  162. bool IsNaN() const { return Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_) || Urho3D::IsNaN(z_) || Urho3D::IsNaN(w_); }
  163. /// Return whether any component is Inf.
  164. bool IsInf() const { return Urho3D::IsInf(x_) || Urho3D::IsInf(y_) || Urho3D::IsInf(z_) || Urho3D::IsInf(w_); }
  165. /// Convert to Vector2.
  166. explicit operator Vector2() const { return { x_, y_ }; }
  167. /// Convert to Vector3.
  168. explicit operator Vector3() const { return { x_, y_, z_ }; }
  169. /// Return float data.
  170. const float* Data() const { return &x_; }
  171. /// Return as string.
  172. String ToString() const;
  173. /// Return hash value for HashSet & HashMap.
  174. unsigned ToHash() const
  175. {
  176. unsigned hash = 37;
  177. hash = 37 * hash + FloatToRawIntBits(x_);
  178. hash = 37 * hash + FloatToRawIntBits(y_);
  179. hash = 37 * hash + FloatToRawIntBits(z_);
  180. hash = 37 * hash + FloatToRawIntBits(w_);
  181. return hash;
  182. }
  183. /// X coordinate.
  184. float x_;
  185. /// Y coordinate.
  186. float y_;
  187. /// Z coordinate.
  188. float z_;
  189. /// W coordinate.
  190. float w_;
  191. /// Zero vector.
  192. static const Vector4 ZERO;
  193. /// (1,1,1) vector.
  194. static const Vector4 ONE;
  195. };
  196. /// Multiply Vector4 with a scalar.
  197. inline Vector4 operator *(float lhs, const Vector4& rhs) { return rhs * lhs; }
  198. /// Per-component linear interpolation between two 4-vectors.
  199. inline Vector4 VectorLerp(const Vector4& lhs, const Vector4& rhs, const Vector4& t) { return lhs + (rhs - lhs) * t; }
  200. /// Per-component min of two 4-vectors.
  201. inline Vector4 VectorMin(const Vector4& lhs, const Vector4& rhs) { return Vector4(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_), Min(lhs.z_, rhs.z_), Min(lhs.w_, rhs.w_)); }
  202. /// Per-component max of two 4-vectors.
  203. inline Vector4 VectorMax(const Vector4& lhs, const Vector4& rhs) { return Vector4(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_), Max(lhs.z_, rhs.z_), Max(lhs.w_, rhs.w_)); }
  204. /// Per-component floor of 4-vector.
  205. inline Vector4 VectorFloor(const Vector4& vec) { return Vector4(Floor(vec.x_), Floor(vec.y_), Floor(vec.z_), Floor(vec.w_)); }
  206. /// Per-component round of 4-vector.
  207. inline Vector4 VectorRound(const Vector4& vec) { return Vector4(Round(vec.x_), Round(vec.y_), Round(vec.z_), Round(vec.w_)); }
  208. /// Per-component ceil of 4-vector.
  209. inline Vector4 VectorCeil(const Vector4& vec) { return Vector4(Ceil(vec.x_), Ceil(vec.y_), Ceil(vec.z_), Ceil(vec.w_)); }
  210. }