mPoint4.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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() {}
  38. Point4I(S32 _x, S32 _y, S32 _z, S32 _w);
  39. S32 x;
  40. S32 y;
  41. S32 z;
  42. S32 w;
  43. //-------------------------------------- Public static constants
  44. public:
  45. const static Point4I One;
  46. const static Point4I Zero;
  47. };
  48. //------------------------------------------------------------------------------
  49. /// 4D floating-point point.
  50. ///
  51. /// Uses F32 internally.
  52. ///
  53. /// Useful for representing quaternions and other 4d beasties.
  54. class Point4F
  55. {
  56. //-------------------------------------- Public data
  57. public:
  58. F32 x; ///< X co-ordinate.
  59. F32 y; ///< Y co-ordinate.
  60. F32 z; ///< Z co-ordinate.
  61. F32 w; ///< W co-ordinate.
  62. public:
  63. Point4F(); ///< Create an uninitialized point.
  64. Point4F(const Point4F&); ///< Copy constructor.
  65. /// Create point from coordinates.
  66. Point4F(F32 _x, F32 _y, F32 _z, F32 _w);
  67. /// Set point's coordinates.
  68. void set(F32 _x, F32 _y, F32 _z, F32 _w);
  69. /// Interpolate from _pt1 to _pt2, based on _factor.
  70. ///
  71. /// @param _pt1 Starting point.
  72. /// @param _pt2 Ending point.
  73. /// @param _factor Interpolation factor (0.0 .. 1.0).
  74. void interpolate(const Point4F& _pt1, const Point4F& _pt2, F32 _factor);
  75. operator F32*() { return (&x); }
  76. operator const F32*() const { return &x; }
  77. F32 len() const;
  78. Point4F operator/(F32) const;
  79. Point4F operator*(F32) const;
  80. Point4F operator+(const Point4F&) const;
  81. Point4F& operator+=(const Point4F&);
  82. Point4F operator-(const Point4F&) const;
  83. Point4F operator*(const Point4F&) const;
  84. Point4F& operator*=(const Point4F&);
  85. Point4F& operator=(const Point3F&);
  86. Point4F& operator=(const Point4F&);
  87. Point3F asPoint3F() const { return Point3F(x,y,z); }
  88. //-------------------------------------- Public static constants
  89. public:
  90. const static Point4F One;
  91. const static Point4F Zero;
  92. };
  93. typedef Point4F Vector4F; ///< Points can be vectors!
  94. //------------------------------------------------------------------------------
  95. //-------------------------------------- Point4F
  96. //
  97. inline Point4F::Point4F()
  98. {
  99. }
  100. inline Point4F::Point4F(const Point4F& _copy)
  101. : x(_copy.x), y(_copy.y), z(_copy.z), w(_copy.w)
  102. {
  103. }
  104. inline Point4F::Point4F(F32 _x, F32 _y, F32 _z, F32 _w)
  105. : x(_x), y(_y), z(_z), w(_w)
  106. {
  107. }
  108. inline void Point4F::set(F32 _x, F32 _y, F32 _z, F32 _w)
  109. {
  110. x = _x;
  111. y = _y;
  112. z = _z;
  113. w = _w;
  114. }
  115. inline F32 Point4F::len() const
  116. {
  117. return mSqrt(x*x + y*y + z*z + w*w);
  118. }
  119. inline void Point4F::interpolate(const Point4F& _from, const Point4F& _to, F32 _factor)
  120. {
  121. x = (_from.x * (1.0f - _factor)) + (_to.x * _factor);
  122. y = (_from.y * (1.0f - _factor)) + (_to.y * _factor);
  123. z = (_from.z * (1.0f - _factor)) + (_to.z * _factor);
  124. w = (_from.w * (1.0f - _factor)) + (_to.w * _factor);
  125. }
  126. inline Point4F& Point4F::operator=(const Point3F &_vec)
  127. {
  128. x = _vec.x;
  129. y = _vec.y;
  130. z = _vec.z;
  131. w = 1.0f;
  132. return *this;
  133. }
  134. inline Point4F& Point4F::operator=(const Point4F &_vec)
  135. {
  136. x = _vec.x;
  137. y = _vec.y;
  138. z = _vec.z;
  139. w = _vec.w;
  140. return *this;
  141. }
  142. inline Point4F Point4F::operator+(const Point4F& _add) const
  143. {
  144. return Point4F( x + _add.x, y + _add.y, z + _add.z, w + _add.w );
  145. }
  146. inline Point4F& Point4F::operator+=(const Point4F& _add)
  147. {
  148. x += _add.x;
  149. y += _add.y;
  150. z += _add.z;
  151. w += _add.w;
  152. return *this;
  153. }
  154. inline Point4F Point4F::operator-(const Point4F& _rSub) const
  155. {
  156. return Point4F( x - _rSub.x, y - _rSub.y, z - _rSub.z, w - _rSub.w );
  157. }
  158. inline Point4F Point4F::operator*(const Point4F &_vec) const
  159. {
  160. return Point4F(x * _vec.x, y * _vec.y, z * _vec.z, w * _vec.w);
  161. }
  162. inline Point4F Point4F::operator*(F32 _mul) const
  163. {
  164. return Point4F(x * _mul, y * _mul, z * _mul, w * _mul);
  165. }
  166. inline Point4F Point4F::operator /(F32 t) const
  167. {
  168. F32 f = 1.0f / t;
  169. return Point4F( x * f, y * f, z * f, w * f );
  170. }
  171. //------------------------------------------------------------------------------
  172. //-------------------------------------- Point4F
  173. inline Point4I::Point4I(S32 _x, S32 _y, S32 _z, S32 _w) : x(_x), y(_y), z(_z), w(_w)
  174. {
  175. }
  176. //-------------------------------------------------------------------
  177. // Non-Member Operators
  178. //-------------------------------------------------------------------
  179. inline Point4F operator*(F32 mul, const Point4F& multiplicand)
  180. {
  181. return multiplicand * mul;
  182. }
  183. inline bool mIsNaN( const Point4F &p )
  184. {
  185. return mIsNaN_F( p.x ) || mIsNaN_F( p.y ) || mIsNaN_F( p.z ) || mIsNaN_F( p.w );
  186. }
  187. #endif // _MPOINT4_H_