Vector4.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  25. {
  26. /// Four-dimensional vector.
  27. class ATOMIC_API Vector4
  28. {
  29. public:
  30. /// Construct a zero vector.
  31. Vector4() :
  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) :
  40. x_(vector.x_),
  41. y_(vector.y_),
  42. z_(vector.z_),
  43. w_(vector.w_)
  44. {
  45. }
  46. /// Construct from a 3-dimensional vector and the W coordinate.
  47. Vector4(const Vector3& vector, float w) :
  48. x_(vector.x_),
  49. y_(vector.y_),
  50. z_(vector.z_),
  51. w_(w)
  52. {
  53. }
  54. /// Construct from coordinates.
  55. Vector4(float x, float y, float z, float w) :
  56. x_(x),
  57. y_(y),
  58. z_(z),
  59. w_(w)
  60. {
  61. }
  62. /// Construct from a float array.
  63. explicit Vector4(const float* data) :
  64. x_(data[0]),
  65. y_(data[1]),
  66. z_(data[2]),
  67. w_(data[3])
  68. {
  69. }
  70. /// Assign from another vector.
  71. Vector4& operator =(const Vector4& rhs)
  72. {
  73. x_ = rhs.x_;
  74. y_ = rhs.y_;
  75. z_ = rhs.z_;
  76. w_ = rhs.w_;
  77. return *this;
  78. }
  79. /// Test for equality with another vector without epsilon.
  80. bool operator ==(const Vector4& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_ && w_ == rhs.w_; }
  81. /// Test for inequality with another vector without epsilon.
  82. bool operator !=(const Vector4& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_; }
  83. /// Add a vector.
  84. Vector4 operator +(const Vector4& rhs) const { return Vector4(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_, w_ + rhs.w_); }
  85. /// Return negation.
  86. Vector4 operator -() const { return Vector4(-x_, -y_, -z_, -w_); }
  87. /// Subtract a vector.
  88. Vector4 operator -(const Vector4& rhs) const { return Vector4(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_, w_ - rhs.w_); }
  89. /// Multiply with a scalar.
  90. Vector4 operator *(float rhs) const { return Vector4(x_ * rhs, y_ * rhs, z_ * rhs, w_ * rhs); }
  91. /// Multiply with a vector.
  92. Vector4 operator *(const Vector4& rhs) const { return Vector4(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_, w_ * rhs.w_); }
  93. /// Divide by a scalar.
  94. Vector4 operator /(float rhs) const { return Vector4(x_ / rhs, y_ / rhs, z_ / rhs, w_ / rhs); }
  95. /// Divide by a vector.
  96. Vector4 operator /(const Vector4& rhs) const { return Vector4(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_, w_ / rhs.w_); }
  97. /// Add-assign a vector.
  98. Vector4& operator +=(const Vector4& rhs)
  99. {
  100. x_ += rhs.x_;
  101. y_ += rhs.y_;
  102. z_ += rhs.z_;
  103. w_ += rhs.w_;
  104. return *this;
  105. }
  106. /// Subtract-assign a vector.
  107. Vector4& operator -=(const Vector4& rhs)
  108. {
  109. x_ -= rhs.x_;
  110. y_ -= rhs.y_;
  111. z_ -= rhs.z_;
  112. w_ -= rhs.w_;
  113. return *this;
  114. }
  115. /// Multiply-assign a scalar.
  116. Vector4& operator *=(float rhs)
  117. {
  118. x_ *= rhs;
  119. y_ *= rhs;
  120. z_ *= rhs;
  121. w_ *= rhs;
  122. return *this;
  123. }
  124. /// Multiply-assign a vector.
  125. Vector4& operator *=(const Vector4& rhs)
  126. {
  127. x_ *= rhs.x_;
  128. y_ *= rhs.y_;
  129. z_ *= rhs.z_;
  130. w_ *= rhs.w_;
  131. return *this;
  132. }
  133. /// Divide-assign a scalar.
  134. Vector4& operator /=(float rhs)
  135. {
  136. float invRhs = 1.0f / rhs;
  137. x_ *= invRhs;
  138. y_ *= invRhs;
  139. z_ *= invRhs;
  140. w_ *= invRhs;
  141. return *this;
  142. }
  143. /// Divide-assign a vector.
  144. Vector4& operator /=(const Vector4& rhs)
  145. {
  146. x_ /= rhs.x_;
  147. y_ /= rhs.y_;
  148. z_ /= rhs.z_;
  149. w_ /= rhs.w_;
  150. return *this;
  151. }
  152. /// Calculate dot product.
  153. float DotProduct(const Vector4& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_ + w_ * rhs.w_; }
  154. /// Calculate absolute dot product.
  155. float AbsDotProduct(const Vector4& rhs) const
  156. {
  157. return Atomic::Abs(x_ * rhs.x_) + Atomic::Abs(y_ * rhs.y_) + Atomic::Abs(z_ * rhs.z_) + Atomic::Abs(w_ * rhs.w_);
  158. }
  159. /// Project vector onto axis.
  160. float ProjectOntoAxis(const Vector3& axis) const { return DotProduct(Vector4(axis.Normalized(), 0.0f)); }
  161. /// Return absolute vector.
  162. Vector4 Abs() const { return Vector4(Atomic::Abs(x_), Atomic::Abs(y_), Atomic::Abs(z_), Atomic::Abs(w_)); }
  163. /// Linear interpolation with another vector.
  164. Vector4 Lerp(const Vector4& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  165. /// Test for equality with another vector with epsilon.
  166. bool Equals(const Vector4& rhs) const
  167. {
  168. return Atomic::Equals(x_, rhs.x_) && Atomic::Equals(y_, rhs.y_) && Atomic::Equals(z_, rhs.z_) && Atomic::Equals(w_, rhs.w_);
  169. }
  170. /// Return whether is NaN.
  171. bool IsNaN() const { return Atomic::IsNaN(x_) || Atomic::IsNaN(y_) || Atomic::IsNaN(z_) || Atomic::IsNaN(w_); }
  172. /// Return float data.
  173. const float* Data() const { return &x_; }
  174. /// Return as string.
  175. String ToString() const;
  176. /// X coordinate.
  177. float x_;
  178. /// Y coordinate.
  179. float y_;
  180. /// Z coordinate.
  181. float z_;
  182. /// W coordinate.
  183. float w_;
  184. /// Zero vector.
  185. static const Vector4 ZERO;
  186. /// (1,1,1) vector.
  187. static const Vector4 ONE;
  188. };
  189. /// Multiply Vector4 with a scalar.
  190. inline Vector4 operator *(float lhs, const Vector4& rhs) { return rhs * lhs; }
  191. /// Per-component linear interpolation between two 4-vectors.
  192. inline Vector4 VectorLerp(const Vector4& lhs, const Vector4& rhs, const Vector4& t) { return lhs + (rhs - lhs) * t; }
  193. /// Per-component min of two 4-vectors.
  194. 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_)); }
  195. /// Per-component max of two 4-vectors.
  196. 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_)); }
  197. /// Per-component floor of 4-vector.
  198. inline Vector4 VectorFloor(const Vector4& vec) { return Vector4(Floor(vec.x_), Floor(vec.y_), Floor(vec.z_), Floor(vec.w_)); }
  199. /// Per-component round of 4-vector.
  200. inline Vector4 VectorRound(const Vector4& vec) { return Vector4(Round(vec.x_), Round(vec.y_), Round(vec.z_), Round(vec.w_)); }
  201. /// Per-component ceil of 4-vector.
  202. inline Vector4 VectorCeil(const Vector4& vec) { return Vector4(Ceil(vec.x_), Ceil(vec.y_), Ceil(vec.z_), Ceil(vec.w_)); }
  203. }