Vector4.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Vector3.h"
  25. namespace Urho3D
  26. {
  27. /// Four-dimensional vector.
  28. class Vector4
  29. {
  30. public:
  31. /// Construct undefined.
  32. Vector4()
  33. {
  34. }
  35. /// Copy-construct from another vector.
  36. Vector4(const Vector4& vector) :
  37. x_(vector.x_),
  38. y_(vector.y_),
  39. z_(vector.z_),
  40. w_(vector.w_)
  41. {
  42. }
  43. /// Construct from a 3-dimensional vector and the W coordinate.
  44. Vector4(const Vector3& vector, float w) :
  45. x_(vector.x_),
  46. y_(vector.y_),
  47. z_(vector.z_),
  48. w_(w)
  49. {
  50. }
  51. /// Construct from coordinates.
  52. Vector4(float x, float y, float z, float w) :
  53. x_(x),
  54. y_(y),
  55. z_(z),
  56. w_(w)
  57. {
  58. }
  59. /// Construct from a float array.
  60. Vector4(const float* data) :
  61. x_(data[0]),
  62. y_(data[1]),
  63. z_(data[2]),
  64. w_(data[3])
  65. {
  66. }
  67. /// Assign from another vector.
  68. Vector4& operator = (const Vector4& rhs)
  69. {
  70. x_ = rhs.x_;
  71. y_ = rhs.y_;
  72. z_ = rhs.z_;
  73. w_ = rhs.w_;
  74. return *this;
  75. }
  76. /// Test for equality with another vector without epsilon.
  77. bool operator == (const Vector4& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_ && w_ == rhs.w_; }
  78. /// Test for inequality with another vector without epsilon.
  79. bool operator != (const Vector4& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_ || w_ != rhs.w_; }
  80. /// Add a vector.
  81. Vector4 operator + (const Vector4& rhs) const { return Vector4(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_, w_ + rhs.w_); }
  82. /// Return negation.
  83. Vector4 operator - () const { return Vector4(-x_, -y_, -z_, -w_); }
  84. /// Subtract a vector.
  85. Vector4 operator - (const Vector4& rhs) const { return Vector4(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_, w_ - rhs.w_); }
  86. /// Multiply with a scalar.
  87. Vector4 operator * (float rhs) const { return Vector4(x_ * rhs, y_ * rhs, z_ * rhs, w_ * rhs); }
  88. /// Multiply with a vector.
  89. Vector4 operator * (const Vector4& rhs) const { return Vector4(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_, w_ * rhs.w_); }
  90. /// Divide by a scalar.
  91. Vector4 operator / (float rhs) const { return Vector4(x_ / rhs, y_ / rhs, z_ / rhs, w_ / rhs); }
  92. /// Divide by a vector.
  93. Vector4 operator / (const Vector4& rhs) const { return Vector4(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_, w_ / rhs.w_); }
  94. /// Add-assign a vector.
  95. Vector4& operator += (const Vector4& rhs)
  96. {
  97. x_ += rhs.x_;
  98. y_ += rhs.y_;
  99. z_ += rhs.z_;
  100. w_ += rhs.w_;
  101. return *this;
  102. }
  103. /// Subtract-assign a vector.
  104. Vector4& operator -= (const Vector4& rhs)
  105. {
  106. x_ -= rhs.x_;
  107. y_ -= rhs.y_;
  108. z_ -= rhs.z_;
  109. w_ -= rhs.w_;
  110. return *this;
  111. }
  112. /// Multiply-assign a scalar.
  113. Vector4& operator *= (float rhs)
  114. {
  115. x_ *= rhs;
  116. y_ *= rhs;
  117. z_ *= rhs;
  118. w_ *= rhs;
  119. return *this;
  120. }
  121. /// Multiply-assign a vector.
  122. Vector4& operator *= (const Vector4& rhs)
  123. {
  124. x_ *= rhs.x_;
  125. y_ *= rhs.y_;
  126. z_ *= rhs.z_;
  127. w_ *= rhs.w_;
  128. return *this;
  129. }
  130. /// Divide-assign a scalar.
  131. Vector4& operator /= (float rhs)
  132. {
  133. float invRhs = 1.0f / rhs;
  134. x_ *= invRhs;
  135. y_ *= invRhs;
  136. z_ *= invRhs;
  137. w_ *= invRhs;
  138. return *this;
  139. }
  140. /// Divide-assign a vector.
  141. Vector4& operator /= (const Vector4& rhs)
  142. {
  143. x_ /= rhs.x_;
  144. y_ /= rhs.y_;
  145. z_ /= rhs.z_;
  146. w_ /= rhs.w_;
  147. return *this;
  148. }
  149. /// Calculate dot product.
  150. float DotProduct(const Vector4& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_ + w_ * rhs.w_; }
  151. /// Calculate absolute dot product.
  152. 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_); }
  153. /// Return absolute vector.
  154. Vector4 Abs() const { return Vector4(Urho3D::Abs(x_), Urho3D::Abs(y_), Urho3D::Abs(z_), Urho3D::Abs(w_)); }
  155. /// Linear interpolation with another vector.
  156. Vector4 Lerp(const Vector4& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  157. /// Test for equality with another vector with epsilon.
  158. 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_); }
  159. /// Return float data.
  160. const float* Data() const { return &x_; }
  161. /// Return as string.
  162. String ToString() const;
  163. /// X coordinate.
  164. float x_;
  165. /// Y coordinate.
  166. float y_;
  167. /// Z coordinate.
  168. float z_;
  169. /// W coordinate.
  170. float w_;
  171. /// Zero vector.
  172. static const Vector4 ZERO;
  173. /// (1,1,1) vector.
  174. static const Vector4 ONE;
  175. };
  176. /// Multiply Vector4 with a scalar.
  177. inline Vector4 operator * (float lhs, const Vector4& rhs) { return rhs * lhs; }
  178. }