Vector4.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #ifndef MATH_VECTOR4_H
  24. #define MATH_VECTOR4_H
  25. #include "Vector3.h"
  26. //! A four-dimensional vector
  27. class Vector4
  28. {
  29. public:
  30. //! Construct an undefined vector
  31. Vector4()
  32. {
  33. }
  34. //! Copy-construct from another vector
  35. Vector4(const Vector4& vector) :
  36. mX(vector.mX),
  37. mY(vector.mY),
  38. mZ(vector.mZ),
  39. mW(vector.mW)
  40. {
  41. }
  42. //! Construct from a Vector3 and the W coordinate
  43. Vector4(const Vector3& vector, float w) :
  44. mX(vector.mX),
  45. mY(vector.mY),
  46. mZ(vector.mZ),
  47. mW(w)
  48. {
  49. }
  50. //! Construct from coordinates
  51. Vector4(float x, float y, float z, float w) :
  52. mX(x),
  53. mY(y),
  54. mZ(z),
  55. mW(w)
  56. {
  57. }
  58. //! Construct from a float array
  59. Vector4(const float* data) :
  60. mX(data[0]),
  61. mY(data[1]),
  62. mZ(data[2]),
  63. mW(data[3])
  64. {
  65. }
  66. //! Assign from another vector
  67. Vector4& operator = (const Vector4& rhs)
  68. {
  69. mX = rhs.mX;
  70. mY = rhs.mY;
  71. mZ = rhs.mZ;
  72. mW = rhs.mW;
  73. return *this;
  74. }
  75. //! Test for equality with another vector
  76. bool operator == (const Vector4& rhs) const
  77. {
  78. return (mX == rhs.mX) && (mY == rhs.mY) && (mZ == rhs.mZ) && (mW == rhs.mW);
  79. }
  80. //! Test for inequality with another vector
  81. bool operator != (const Vector4& rhs) const
  82. {
  83. return (mX != rhs.mX) || (mY != rhs.mY) || (mZ != rhs.mZ) || (mW != rhs.mW);
  84. }
  85. //! Add a vector
  86. Vector4 operator + (const Vector4& rhs) const
  87. {
  88. return Vector4(mX + rhs.mX, mY + rhs.mY, mZ + rhs.mZ, mW + rhs.mW);
  89. }
  90. //! Return negation
  91. Vector4 operator - () const
  92. {
  93. return Vector4(-mX, -mY, -mZ, -mW);
  94. }
  95. //! Subtract a vector
  96. Vector4 operator - (const Vector4& rhs) const
  97. {
  98. return Vector4(mX - rhs.mX, mY - rhs.mY, mZ - rhs.mZ, mW - rhs.mW);
  99. }
  100. //! Multiply with a scalar
  101. Vector4 operator * (float rhs) const
  102. {
  103. return Vector4(mX * rhs, mY * rhs, mZ * rhs, mW * rhs);
  104. }
  105. //! Multiply with a vector
  106. Vector4 operator * (const Vector4& rhs) const
  107. {
  108. return Vector4(mX * rhs.mX, mY * rhs.mY, mZ * rhs.mZ, mW * rhs.mW);
  109. }
  110. //! Divide by a scalar
  111. Vector4 operator / (float rhs) const
  112. {
  113. return Vector4(mX / rhs, mY / rhs, mZ / rhs, mW / rhs);
  114. }
  115. //! Divide by a vector
  116. Vector4 operator / (const Vector4& rhs) const
  117. {
  118. return Vector4(mX / rhs.mX, mY / rhs.mY, mZ / rhs.mZ, mW / rhs.mW);
  119. }
  120. //! Add-assign a vector
  121. Vector4& operator += (const Vector4& rhs)
  122. {
  123. mX += rhs.mX;
  124. mY += rhs.mY;
  125. mZ += rhs.mZ;
  126. mW += rhs.mW;
  127. return *this;
  128. }
  129. //! Subtract-assign a vector
  130. Vector4& operator -= (const Vector4& rhs)
  131. {
  132. mX -= rhs.mX;
  133. mY -= rhs.mY;
  134. mZ -= rhs.mZ;
  135. mW -= rhs.mW;
  136. return *this;
  137. }
  138. //! Multiply-assign a scalar
  139. Vector4& operator *= (float rhs)
  140. {
  141. mX *= rhs;
  142. mY *= rhs;
  143. mZ *= rhs;
  144. mW *= rhs;
  145. return *this;
  146. }
  147. //! Multiply-assign a vector
  148. Vector4& operator *= (const Vector4& rhs)
  149. {
  150. mX *= rhs.mX;
  151. mY *= rhs.mY;
  152. mZ *= rhs.mZ;
  153. mW *= rhs.mW;
  154. return *this;
  155. }
  156. //! Divide-assign a scalar
  157. Vector4& operator /= (float rhs)
  158. {
  159. mX /= rhs;
  160. mY /= rhs;
  161. mZ /= rhs;
  162. mW /= rhs;
  163. return *this;
  164. }
  165. //! Divide-assign a vector
  166. Vector4& operator /= (const Vector4& rhs)
  167. {
  168. mX /= rhs.mX;
  169. mY /= rhs.mY;
  170. mZ /= rhs.mZ;
  171. mW /= rhs.mW;
  172. return *this;
  173. }
  174. //! Calculate dot product
  175. float dotProduct(const Vector4& rhs) const
  176. {
  177. return mX * rhs.mX + mY * rhs.mY + mZ * rhs.mZ + mW * rhs.mW;
  178. }
  179. //! Calculate absolute dot product
  180. float absDotProduct(const Vector4& rhs) const
  181. {
  182. return fabsf(mX * rhs.mX) + fabsf(mY * rhs.mY) + fabsf(mZ * rhs.mZ) + fabsf(mW * rhs.mW);
  183. }
  184. //! Linear interpolation with another vector
  185. Vector4 lerp(const Vector4& rhs, float t) const
  186. {
  187. return *this * (1.0f - t) + rhs * t;
  188. }
  189. //! Return float data
  190. const float* getData() const { return &mX; }
  191. //! X coordinate
  192. float mX;
  193. //! Y coordinate
  194. float mY;
  195. //! Z coordinate
  196. float mZ;
  197. //! W coordinate
  198. float mW;
  199. //! Zero vector
  200. static const Vector4 sZero;
  201. //! (1,1,1) vector
  202. static const Vector4 sUnity;
  203. };
  204. inline Vector4 operator * (float lhs, const Vector4& rhs) { return rhs * lhs; }
  205. #endif // MATH_VECTOR4_H