Vector4.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2013 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 "Vector3.h"
  24. namespace Urho3D
  25. {
  26. /// Four-dimensional vector.
  27. class URHO3D_API Vector4
  28. {
  29. public:
  30. /// Construct undefined.
  31. Vector4()
  32. {
  33. }
  34. /// Copy-construct from another vector.
  35. Vector4(const Vector4& vector) :
  36. x_(vector.x_),
  37. y_(vector.y_),
  38. z_(vector.z_),
  39. w_(vector.w_)
  40. {
  41. }
  42. /// Construct from a 3-dimensional vector and the W coordinate.
  43. Vector4(const Vector3& vector, float w) :
  44. x_(vector.x_),
  45. y_(vector.y_),
  46. z_(vector.z_),
  47. w_(w)
  48. {
  49. }
  50. /// Construct from coordinates.
  51. Vector4(float x, float y, float z, float w) :
  52. x_(x),
  53. y_(y),
  54. z_(z),
  55. w_(w)
  56. {
  57. }
  58. /// Construct from a float array.
  59. Vector4(const float* data) :
  60. x_(data[0]),
  61. y_(data[1]),
  62. z_(data[2]),
  63. w_(data[3])
  64. {
  65. }
  66. /// Assign from another vector.
  67. Vector4& operator = (const Vector4& rhs)
  68. {
  69. x_ = rhs.x_;
  70. y_ = rhs.y_;
  71. z_ = rhs.z_;
  72. w_ = rhs.w_;
  73. return *this;
  74. }
  75. /// Test for equality with another vector without epsilon.
  76. bool operator == (const Vector4& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_ && w_ == rhs.w_; }
  77. /// Test for inequality with another vector without epsilon.
  78. bool operator != (const Vector4& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_; }
  79. /// Add a vector.
  80. Vector4 operator + (const Vector4& rhs) const { return Vector4(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_, w_ + rhs.w_); }
  81. /// Return negation.
  82. Vector4 operator - () const { return Vector4(-x_, -y_, -z_, -w_); }
  83. /// Subtract a vector.
  84. Vector4 operator - (const Vector4& rhs) const { return Vector4(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_, w_ - rhs.w_); }
  85. /// Multiply with a scalar.
  86. Vector4 operator * (float rhs) const { return Vector4(x_ * rhs, y_ * rhs, z_ * rhs, w_ * rhs); }
  87. /// Multiply with a vector.
  88. Vector4 operator * (const Vector4& rhs) const { return Vector4(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_, w_ * rhs.w_); }
  89. /// Divide by a scalar.
  90. Vector4 operator / (float rhs) const { return Vector4(x_ / rhs, y_ / rhs, z_ / rhs, w_ / rhs); }
  91. /// Divide by a vector.
  92. Vector4 operator / (const Vector4& rhs) const { return Vector4(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_, w_ / rhs.w_); }
  93. /// Add-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. /// Subtract-assign a vector.
  103. Vector4& operator -= (const Vector4& rhs)
  104. {
  105. x_ -= rhs.x_;
  106. y_ -= rhs.y_;
  107. z_ -= rhs.z_;
  108. w_ -= rhs.w_;
  109. return *this;
  110. }
  111. /// Multiply-assign a scalar.
  112. Vector4& operator *= (float rhs)
  113. {
  114. x_ *= rhs;
  115. y_ *= rhs;
  116. z_ *= rhs;
  117. w_ *= rhs;
  118. return *this;
  119. }
  120. /// Multiply-assign a vector.
  121. Vector4& operator *= (const Vector4& rhs)
  122. {
  123. x_ *= rhs.x_;
  124. y_ *= rhs.y_;
  125. z_ *= rhs.z_;
  126. w_ *= rhs.w_;
  127. return *this;
  128. }
  129. /// Divide-assign a scalar.
  130. Vector4& operator /= (float rhs)
  131. {
  132. float invRhs = 1.0f / rhs;
  133. x_ *= invRhs;
  134. y_ *= invRhs;
  135. z_ *= invRhs;
  136. w_ *= invRhs;
  137. return *this;
  138. }
  139. /// Divide-assign a vector.
  140. Vector4& operator /= (const Vector4& rhs)
  141. {
  142. x_ /= rhs.x_;
  143. y_ /= rhs.y_;
  144. z_ /= rhs.z_;
  145. w_ /= rhs.w_;
  146. return *this;
  147. }
  148. /// Calculate dot product.
  149. float DotProduct(const Vector4& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_ + w_ * rhs.w_; }
  150. /// Calculate absolute dot product.
  151. float AbsDotProduct(const Vector4& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_) + Urho3D::Abs(z_ * rhs.z_) + Urho3D::Abs(w_ * rhs.w_); }
  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 { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_) && Urho3D::Equals(w_, rhs.w_); }
  158. /// Return float data.
  159. const float* Data() const { return &x_; }
  160. /// Return as string.
  161. String ToString() const;
  162. /// X coordinate.
  163. float x_;
  164. /// Y coordinate.
  165. float y_;
  166. /// Z coordinate.
  167. float z_;
  168. /// W coordinate.
  169. float w_;
  170. /// Zero vector.
  171. static const Vector4 ZERO;
  172. /// (1,1,1) vector.
  173. static const Vector4 ONE;
  174. };
  175. /// Multiply Vector4 with a scalar.
  176. inline Vector4 operator * (float lhs, const Vector4& rhs) { return rhs * lhs; }
  177. }