CmVector4.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include "CmVector3.h"
  27. namespace CamelotFramework
  28. {
  29. class CM_UTILITY_EXPORT Vector4
  30. {
  31. public:
  32. float x, y, z, w;
  33. public:
  34. Vector4()
  35. { }
  36. Vector4(float x, float y, float z, float w)
  37. :x(x), y(y), z(z), w(w)
  38. { }
  39. /**
  40. * @brief Exchange the contents of this vector with another.
  41. */
  42. void swap(Vector4& other)
  43. {
  44. std::swap(x, other.x);
  45. std::swap(y, other.y);
  46. std::swap(z, other.z);
  47. std::swap(w, other.w);
  48. }
  49. float operator[] (size_t i) const
  50. {
  51. assert (i < 4);
  52. return *(&x+i);
  53. }
  54. float& operator[] (size_t i)
  55. {
  56. assert(i < 4);
  57. return *(&x+i);
  58. }
  59. float* ptr()
  60. {
  61. return &x;
  62. }
  63. const float* ptr() const
  64. {
  65. return &x;
  66. }
  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. Vector4& operator= (float rhs)
  76. {
  77. x = rhs;
  78. y = rhs;
  79. z = rhs;
  80. w = rhs;
  81. return *this;
  82. }
  83. bool operator== (const Vector4& rhs) const
  84. {
  85. return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
  86. }
  87. bool operator!= (const Vector4& rhs) const
  88. {
  89. return (x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w);
  90. }
  91. Vector4& operator= (const Vector3& rhs)
  92. {
  93. x = rhs.x;
  94. y = rhs.y;
  95. z = rhs.z;
  96. w = 1.0f;
  97. return *this;
  98. }
  99. Vector4 operator+ (const Vector4& rhs) const
  100. {
  101. return Vector4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
  102. }
  103. Vector4 operator- (const Vector4& rhs) const
  104. {
  105. return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
  106. }
  107. Vector4 operator* (float rhs) const
  108. {
  109. return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
  110. }
  111. Vector4 operator* (const Vector4& rhs) const
  112. {
  113. return Vector4(rhs.x * x, rhs.y * y, rhs.z * z, rhs.w * w);
  114. }
  115. Vector4 operator/ (float rhs) const
  116. {
  117. assert(rhs != 0.0f);
  118. float inv = 1.0f / rhs;
  119. return Vector4(x * inv, y * inv, z * inv, w * inv);
  120. }
  121. Vector4 operator/ (const Vector4& rhs) const
  122. {
  123. return Vector4(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
  124. }
  125. const Vector4& operator+ () const
  126. {
  127. return *this;
  128. }
  129. Vector4 operator- () const
  130. {
  131. return Vector4(-x, -y, -z, -w);
  132. }
  133. friend Vector4 operator* (float lhs, const Vector4& rhs)
  134. {
  135. return Vector4(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z, lhs * rhs.w);
  136. }
  137. friend Vector4 operator/ (float lhs, const Vector4& rhs)
  138. {
  139. return Vector4(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z, lhs / rhs.w);
  140. }
  141. friend Vector4 operator+ (const Vector4& lhs, float rhs)
  142. {
  143. return Vector4(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs, lhs.w + rhs);
  144. }
  145. friend Vector4 operator+ (float lhs, const Vector4& rhs)
  146. {
  147. return Vector4(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z, lhs + rhs.w);
  148. }
  149. friend Vector4 operator- (const Vector4& lhs, float rhs)
  150. {
  151. return Vector4(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs, lhs.w - rhs);
  152. }
  153. friend Vector4 operator- (float lhs, Vector4& rhs)
  154. {
  155. return Vector4(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z, lhs - rhs.w);
  156. }
  157. Vector4& operator+= (const Vector4& rhs)
  158. {
  159. x += rhs.x;
  160. y += rhs.y;
  161. z += rhs.z;
  162. w += rhs.w;
  163. return *this;
  164. }
  165. Vector4& operator-= (const Vector4& rhs)
  166. {
  167. x -= rhs.x;
  168. y -= rhs.y;
  169. z -= rhs.z;
  170. w -= rhs.w;
  171. return *this;
  172. }
  173. Vector4& operator*= (float rhs)
  174. {
  175. x *= rhs;
  176. y *= rhs;
  177. z *= rhs;
  178. w *= rhs;
  179. return *this;
  180. }
  181. Vector4& operator+= (float rhs)
  182. {
  183. x += rhs;
  184. y += rhs;
  185. z += rhs;
  186. w += rhs;
  187. return *this;
  188. }
  189. Vector4& operator-= (float rhs)
  190. {
  191. x -= rhs;
  192. y -= rhs;
  193. z -= rhs;
  194. w -= rhs;
  195. return *this;
  196. }
  197. Vector4& operator*= (Vector4& rhs)
  198. {
  199. x *= rhs.x;
  200. y *= rhs.y;
  201. z *= rhs.z;
  202. w *= rhs.w;
  203. return *this;
  204. }
  205. Vector4& operator/= (float rhs)
  206. {
  207. assert(rhs != 0.0f);
  208. float inv = 1.0f / rhs;
  209. x *= inv;
  210. y *= inv;
  211. z *= inv;
  212. w *= inv;
  213. return *this;
  214. }
  215. Vector4& operator/= (const Vector4& rhs)
  216. {
  217. x /= rhs.x;
  218. y /= rhs.y;
  219. z /= rhs.z;
  220. w /= rhs.w;
  221. return *this;
  222. }
  223. /**
  224. * @brief Calculates the dot (scalar) product of this vector with another.
  225. */
  226. float dot(const Vector4& vec) const
  227. {
  228. return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
  229. }
  230. bool isNaN() const
  231. {
  232. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  233. }
  234. static const Vector4 ZERO;
  235. };
  236. CM_ALLOW_MEMCPY_SERIALIZATION(Vector4);
  237. }