mPoint4.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MPOINT4_H_
  23. #define _MPOINT4_H_
  24. #ifndef _MMATHFN_H_
  25. #include "math/mMathFn.h"
  26. #endif
  27. #ifndef _MPOINT3_H_
  28. #include "math/mPoint3.h"
  29. #endif
  30. //------------------------------------------------------------------------------
  31. /// 4D integer point
  32. ///
  33. /// Uses S32 internally. Currently storage only.
  34. class Point4I
  35. {
  36. public:
  37. Point4I() :x(0), y(0), z(0), w(0) {}
  38. Point4I(S32 _x, S32 _y, S32 _z, S32 _w);
  39. void zero(); ///< Zero all values
  40. S32 x;
  41. S32 y;
  42. S32 z;
  43. S32 w;
  44. //-------------------------------------- Public static constants
  45. public:
  46. const static Point4I One;
  47. const static Point4I Zero;
  48. };
  49. //------------------------------------------------------------------------------
  50. /// 4D floating-point point.
  51. ///
  52. /// Uses F32 internally.
  53. ///
  54. /// Useful for representing quaternions and other 4d beasties.
  55. class Point4F
  56. {
  57. //-------------------------------------- Public data
  58. public:
  59. F32 x; ///< X co-ordinate.
  60. F32 y; ///< Y co-ordinate.
  61. F32 z; ///< Z co-ordinate.
  62. F32 w; ///< W co-ordinate.
  63. public:
  64. Point4F(); ///< Create an uninitialized point.
  65. Point4F(const Point4F&); ///< Copy constructor.
  66. /// Create point from coordinates.
  67. Point4F(F32 _x, F32 _y, F32 _z, F32 _w);
  68. /// Set point's coordinates.
  69. void set(F32 _x, F32 _y, F32 _z, F32 _w);
  70. /// Interpolate from _pt1 to _pt2, based on _factor.
  71. ///
  72. /// @param _pt1 Starting point.
  73. /// @param _pt2 Ending point.
  74. /// @param _factor Interpolation factor (0.0 .. 1.0).
  75. void interpolate(const Point4F& _pt1, const Point4F& _pt2, F32 _factor);
  76. void zero();
  77. operator F32*() { return (&x); }
  78. operator const F32*() const { return &x; }
  79. F32 len() const;
  80. Point4F operator/(F32) const;
  81. Point4F operator*(F32) const;
  82. Point4F operator+(const Point4F&) const;
  83. Point4F& operator+=(const Point4F&);
  84. Point4F operator-(const Point4F&) const;
  85. Point4F operator*(const Point4F&) const;
  86. Point4F& operator*=(const Point4F&);
  87. Point4F& operator=(const Point3F&);
  88. Point4F& operator=(const Point4F&);
  89. Point3F asPoint3F() const { return Point3F(x,y,z); }
  90. //-------------------------------------- Public static constants
  91. public:
  92. const static Point4F One;
  93. const static Point4F Zero;
  94. };
  95. typedef Point4F Vector4F; ///< Points can be vectors!
  96. //------------------------------------------------------------------------------
  97. //-------------------------------------- Point4I
  98. inline void Point4I::zero()
  99. {
  100. x = y = z = w = 0;
  101. }
  102. //------------------------------------------------------------------------------
  103. //-------------------------------------- Point4F
  104. //
  105. inline Point4F::Point4F()
  106. :x(0.0f), y(0.0f), z(0.0f), w(0.0f)
  107. {
  108. }
  109. inline Point4F::Point4F(const Point4F& _copy)
  110. : x(_copy.x), y(_copy.y), z(_copy.z), w(_copy.w)
  111. {
  112. }
  113. inline Point4F::Point4F(F32 _x, F32 _y, F32 _z, F32 _w)
  114. : x(_x), y(_y), z(_z), w(_w)
  115. {
  116. }
  117. inline void Point4F::set(F32 _x, F32 _y, F32 _z, F32 _w)
  118. {
  119. x = _x;
  120. y = _y;
  121. z = _z;
  122. w = _w;
  123. }
  124. inline F32 Point4F::len() const
  125. {
  126. return mSqrt(x*x + y*y + z*z + w*w);
  127. }
  128. inline void Point4F::interpolate(const Point4F& _from, const Point4F& _to, F32 _factor)
  129. {
  130. x = (_from.x * (1.0f - _factor)) + (_to.x * _factor);
  131. y = (_from.y * (1.0f - _factor)) + (_to.y * _factor);
  132. z = (_from.z * (1.0f - _factor)) + (_to.z * _factor);
  133. w = (_from.w * (1.0f - _factor)) + (_to.w * _factor);
  134. }
  135. inline void Point4F::zero()
  136. {
  137. x = y = z = w = 0.0f;
  138. }
  139. inline Point4F& Point4F::operator=(const Point3F &_vec)
  140. {
  141. x = _vec.x;
  142. y = _vec.y;
  143. z = _vec.z;
  144. w = 1.0f;
  145. return *this;
  146. }
  147. inline Point4F& Point4F::operator=(const Point4F &_vec)
  148. {
  149. x = _vec.x;
  150. y = _vec.y;
  151. z = _vec.z;
  152. w = _vec.w;
  153. return *this;
  154. }
  155. inline Point4F Point4F::operator+(const Point4F& _add) const
  156. {
  157. return Point4F( x + _add.x, y + _add.y, z + _add.z, w + _add.w );
  158. }
  159. inline Point4F& Point4F::operator+=(const Point4F& _add)
  160. {
  161. x += _add.x;
  162. y += _add.y;
  163. z += _add.z;
  164. w += _add.w;
  165. return *this;
  166. }
  167. inline Point4F Point4F::operator-(const Point4F& _rSub) const
  168. {
  169. return Point4F( x - _rSub.x, y - _rSub.y, z - _rSub.z, w - _rSub.w );
  170. }
  171. inline Point4F Point4F::operator*(const Point4F &_vec) const
  172. {
  173. return Point4F(x * _vec.x, y * _vec.y, z * _vec.z, w * _vec.w);
  174. }
  175. inline Point4F Point4F::operator*(F32 _mul) const
  176. {
  177. return Point4F(x * _mul, y * _mul, z * _mul, w * _mul);
  178. }
  179. inline Point4F Point4F::operator /(F32 t) const
  180. {
  181. F32 f = 1.0f / t;
  182. return Point4F( x * f, y * f, z * f, w * f );
  183. }
  184. inline F32 mDot(const Point4F &p1, const Point4F &p2)
  185. {
  186. return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z + p1.w*p2.w);
  187. }
  188. //------------------------------------------------------------------------------
  189. //-------------------------------------- Point4F
  190. inline Point4I::Point4I(S32 _x, S32 _y, S32 _z, S32 _w) : x(_x), y(_y), z(_z), w(_w)
  191. {
  192. }
  193. //-------------------------------------------------------------------
  194. // Non-Member Operators
  195. //-------------------------------------------------------------------
  196. inline Point4F operator*(F32 mul, const Point4F& multiplicand)
  197. {
  198. return multiplicand * mul;
  199. }
  200. inline bool mIsNaN( const Point4F &p )
  201. {
  202. return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z ) || mIsNaN_F( p.w );
  203. }
  204. #endif // _MPOINT4_H_