CmVector4.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #ifndef __Vector4_H__
  25. #define __Vector4_H__
  26. #include "CmPrerequisitesUtil.h"
  27. #include "CmVector3.h"
  28. namespace CamelotFramework
  29. {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Math
  34. * @{
  35. */
  36. /** 4-dimensional homogeneous vector.
  37. */
  38. class CM_UTILITY_EXPORT Vector4
  39. {
  40. public:
  41. float x, y, z, w;
  42. public:
  43. inline Vector4()
  44. {
  45. }
  46. inline Vector4( const float fX, const float fY, const float fZ, const float fW )
  47. : x( fX ), y( fY ), z( fZ ), w( fW)
  48. {
  49. }
  50. inline explicit Vector4( const float afCoordinate[4] )
  51. : x( afCoordinate[0] ),
  52. y( afCoordinate[1] ),
  53. z( afCoordinate[2] ),
  54. w( afCoordinate[3] )
  55. {
  56. }
  57. inline explicit Vector4( const int afCoordinate[4] )
  58. {
  59. x = (float)afCoordinate[0];
  60. y = (float)afCoordinate[1];
  61. z = (float)afCoordinate[2];
  62. w = (float)afCoordinate[3];
  63. }
  64. inline explicit Vector4( float* const r )
  65. : x( r[0] ), y( r[1] ), z( r[2] ), w( r[3] )
  66. {
  67. }
  68. inline explicit Vector4( const float scaler )
  69. : x( scaler )
  70. , y( scaler )
  71. , z( scaler )
  72. , w( scaler )
  73. {
  74. }
  75. inline explicit Vector4(const Vector3& rhs)
  76. : x(rhs.x), y(rhs.y), z(rhs.z), w(1.0f)
  77. {
  78. }
  79. /** Exchange the contents of this vector with another.
  80. */
  81. inline void swap(Vector4& other)
  82. {
  83. std::swap(x, other.x);
  84. std::swap(y, other.y);
  85. std::swap(z, other.z);
  86. std::swap(w, other.w);
  87. }
  88. inline float operator [] ( const size_t i ) const
  89. {
  90. assert( i < 4 );
  91. return *(&x+i);
  92. }
  93. inline float& operator [] ( const size_t i )
  94. {
  95. assert( i < 4 );
  96. return *(&x+i);
  97. }
  98. /// Pointer accessor for direct copying
  99. inline float* ptr()
  100. {
  101. return &x;
  102. }
  103. /// Pointer accessor for direct copying
  104. inline const float* ptr() const
  105. {
  106. return &x;
  107. }
  108. /** Assigns the value of the other vector.
  109. @param
  110. rkVector The other vector
  111. */
  112. inline Vector4& operator = ( const Vector4& rkVector )
  113. {
  114. x = rkVector.x;
  115. y = rkVector.y;
  116. z = rkVector.z;
  117. w = rkVector.w;
  118. return *this;
  119. }
  120. inline Vector4& operator = ( const float fScalar)
  121. {
  122. x = fScalar;
  123. y = fScalar;
  124. z = fScalar;
  125. w = fScalar;
  126. return *this;
  127. }
  128. inline bool operator == ( const Vector4& rkVector ) const
  129. {
  130. return ( x == rkVector.x &&
  131. y == rkVector.y &&
  132. z == rkVector.z &&
  133. w == rkVector.w );
  134. }
  135. inline bool operator != ( const Vector4& rkVector ) const
  136. {
  137. return ( x != rkVector.x ||
  138. y != rkVector.y ||
  139. z != rkVector.z ||
  140. w != rkVector.w );
  141. }
  142. inline Vector4& operator = (const Vector3& rhs)
  143. {
  144. x = rhs.x;
  145. y = rhs.y;
  146. z = rhs.z;
  147. w = 1.0f;
  148. return *this;
  149. }
  150. // arithmetic operations
  151. inline Vector4 operator + ( const Vector4& rkVector ) const
  152. {
  153. return Vector4(
  154. x + rkVector.x,
  155. y + rkVector.y,
  156. z + rkVector.z,
  157. w + rkVector.w);
  158. }
  159. inline Vector4 operator - ( const Vector4& rkVector ) const
  160. {
  161. return Vector4(
  162. x - rkVector.x,
  163. y - rkVector.y,
  164. z - rkVector.z,
  165. w - rkVector.w);
  166. }
  167. inline Vector4 operator * ( const float fScalar ) const
  168. {
  169. return Vector4(
  170. x * fScalar,
  171. y * fScalar,
  172. z * fScalar,
  173. w * fScalar);
  174. }
  175. inline Vector4 operator * ( const Vector4& rhs) const
  176. {
  177. return Vector4(
  178. rhs.x * x,
  179. rhs.y * y,
  180. rhs.z * z,
  181. rhs.w * w);
  182. }
  183. inline Vector4 operator / ( const float fScalar ) const
  184. {
  185. assert( fScalar != 0.0 );
  186. float fInv = 1.0f / fScalar;
  187. return Vector4(
  188. x * fInv,
  189. y * fInv,
  190. z * fInv,
  191. w * fInv);
  192. }
  193. inline Vector4 operator / ( const Vector4& rhs) const
  194. {
  195. return Vector4(
  196. x / rhs.x,
  197. y / rhs.y,
  198. z / rhs.z,
  199. w / rhs.w);
  200. }
  201. inline const Vector4& operator + () const
  202. {
  203. return *this;
  204. }
  205. inline Vector4 operator - () const
  206. {
  207. return Vector4(-x, -y, -z, -w);
  208. }
  209. inline friend Vector4 operator * ( const float fScalar, const Vector4& rkVector )
  210. {
  211. return Vector4(
  212. fScalar * rkVector.x,
  213. fScalar * rkVector.y,
  214. fScalar * rkVector.z,
  215. fScalar * rkVector.w);
  216. }
  217. inline friend Vector4 operator / ( const float fScalar, const Vector4& rkVector )
  218. {
  219. return Vector4(
  220. fScalar / rkVector.x,
  221. fScalar / rkVector.y,
  222. fScalar / rkVector.z,
  223. fScalar / rkVector.w);
  224. }
  225. inline friend Vector4 operator + (const Vector4& lhs, const float rhs)
  226. {
  227. return Vector4(
  228. lhs.x + rhs,
  229. lhs.y + rhs,
  230. lhs.z + rhs,
  231. lhs.w + rhs);
  232. }
  233. inline friend Vector4 operator + (const float lhs, const Vector4& rhs)
  234. {
  235. return Vector4(
  236. lhs + rhs.x,
  237. lhs + rhs.y,
  238. lhs + rhs.z,
  239. lhs + rhs.w);
  240. }
  241. inline friend Vector4 operator - (const Vector4& lhs, float rhs)
  242. {
  243. return Vector4(
  244. lhs.x - rhs,
  245. lhs.y - rhs,
  246. lhs.z - rhs,
  247. lhs.w - rhs);
  248. }
  249. inline friend Vector4 operator - (const float lhs, const Vector4& rhs)
  250. {
  251. return Vector4(
  252. lhs - rhs.x,
  253. lhs - rhs.y,
  254. lhs - rhs.z,
  255. lhs - rhs.w);
  256. }
  257. // arithmetic updates
  258. inline Vector4& operator += ( const Vector4& rkVector )
  259. {
  260. x += rkVector.x;
  261. y += rkVector.y;
  262. z += rkVector.z;
  263. w += rkVector.w;
  264. return *this;
  265. }
  266. inline Vector4& operator -= ( const Vector4& rkVector )
  267. {
  268. x -= rkVector.x;
  269. y -= rkVector.y;
  270. z -= rkVector.z;
  271. w -= rkVector.w;
  272. return *this;
  273. }
  274. inline Vector4& operator *= ( const float fScalar )
  275. {
  276. x *= fScalar;
  277. y *= fScalar;
  278. z *= fScalar;
  279. w *= fScalar;
  280. return *this;
  281. }
  282. inline Vector4& operator += ( const float fScalar )
  283. {
  284. x += fScalar;
  285. y += fScalar;
  286. z += fScalar;
  287. w += fScalar;
  288. return *this;
  289. }
  290. inline Vector4& operator -= ( const float fScalar )
  291. {
  292. x -= fScalar;
  293. y -= fScalar;
  294. z -= fScalar;
  295. w -= fScalar;
  296. return *this;
  297. }
  298. inline Vector4& operator *= ( const Vector4& rkVector )
  299. {
  300. x *= rkVector.x;
  301. y *= rkVector.y;
  302. z *= rkVector.z;
  303. w *= rkVector.w;
  304. return *this;
  305. }
  306. inline Vector4& operator /= ( const float fScalar )
  307. {
  308. assert( fScalar != 0.0 );
  309. float fInv = 1.0f / fScalar;
  310. x *= fInv;
  311. y *= fInv;
  312. z *= fInv;
  313. w *= fInv;
  314. return *this;
  315. }
  316. inline Vector4& operator /= ( const Vector4& rkVector )
  317. {
  318. x /= rkVector.x;
  319. y /= rkVector.y;
  320. z /= rkVector.z;
  321. w /= rkVector.w;
  322. return *this;
  323. }
  324. /** Calculates the dot (scalar) product of this vector with another.
  325. @param
  326. vec Vector with which to calculate the dot product (together
  327. with this one).
  328. @returns
  329. A float representing the dot product value.
  330. */
  331. inline float dotProduct(const Vector4& vec) const
  332. {
  333. return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
  334. }
  335. /// Check whether this vector contains valid values
  336. inline bool isNaN() const
  337. {
  338. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  339. }
  340. /** Function for writing to a stream.
  341. */
  342. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  343. ( std::ostream& o, const Vector4& v )
  344. {
  345. o << "Vector4(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
  346. return o;
  347. }
  348. // special
  349. static const Vector4 ZERO;
  350. };
  351. /** @} */
  352. /** @} */
  353. CM_ALLOW_MEMCPY_SERIALIZATION(Vector4);
  354. }
  355. #endif