Vector4.h 6.0 KB

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