tsTransform.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 _TSTRANSFORM_H_
  23. #define _TSTRANSFORM_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. class Stream;
  28. /// compressed quaternion class
  29. struct Quat16
  30. {
  31. enum { MAX_VAL = 0x7fff };
  32. S16 x, y, z, w;
  33. void read(Stream *);
  34. void write(Stream *);
  35. void identity();
  36. QuatF getQuatF() const;
  37. QuatF &getQuatF( QuatF * q ) const;
  38. void set( const QuatF & q );
  39. S32 operator==( const Quat16 & q ) const;
  40. S32 operator!=( const Quat16 & q ) const { return !(*this == q); }
  41. };
  42. /// class to handle general scaling case
  43. ///
  44. /// transform = rot * scale * inv(rot)
  45. struct TSScale
  46. {
  47. QuatF mRotate;
  48. Point3F mScale;
  49. void identity() { mRotate.identity(); mScale.set( 1.0f,1.0f,1.0f ); }
  50. S32 operator==( const TSScale & other ) const { return mRotate==other.mRotate && mScale==other.mScale; }
  51. };
  52. /// struct for encapsulating ts transform related static functions
  53. struct TSTransform
  54. {
  55. static Point3F & interpolate(const Point3F & p1, const Point3F & p2, F32 t, Point3F *);
  56. static F32 interpolate(F32 p1, F32 p2, F32 t);
  57. static QuatF & interpolate(const QuatF & q1, const QuatF & q2, F32 t, QuatF *);
  58. static TSScale & interpolate(const TSScale & s1, const TSScale & s2, F32 t, TSScale *);
  59. static void setMatrix(const QuatF & q, MatrixF *);
  60. static void setMatrix(const QuatF & q, const Point3F & p, MatrixF *);
  61. static void applyScale(F32 scale, MatrixF *);
  62. static void applyScale(const Point3F & scale, MatrixF *);
  63. static void applyScale(const TSScale & scale, MatrixF *);
  64. };
  65. inline Point3F & TSTransform::interpolate(const Point3F & p1, const Point3F & p2, F32 t, Point3F * p)
  66. {
  67. p->x = p1.x + t * (p2.x-p1.x);
  68. p->y = p1.y + t * (p2.y-p1.y);
  69. p->z = p1.z + t * (p2.z-p1.z);
  70. return *p;
  71. }
  72. inline F32 TSTransform::interpolate(F32 p1, F32 p2, F32 t)
  73. {
  74. return p1 + t*(p2-p1);
  75. }
  76. inline TSScale & TSTransform::interpolate(const TSScale & s1, const TSScale & s2, F32 t, TSScale * s)
  77. {
  78. TSTransform::interpolate(s1.mRotate,s2.mRotate,t,&s->mRotate);
  79. TSTransform::interpolate(s1.mScale,s2.mScale,t,&s->mScale);
  80. return *s;
  81. }
  82. inline void TSTransform::setMatrix( const QuatF & q, const Point3F & p, MatrixF * pDest )
  83. {
  84. q.setMatrix(pDest);
  85. pDest->setColumn(3,p);
  86. }
  87. inline void TSTransform::setMatrix( const QuatF & q, MatrixF * pDest )
  88. {
  89. q.setMatrix(pDest);
  90. }
  91. #endif