tsTransform.h

Engine/source/ts/tsTransform.h

More...

Classes:

class

compressed quaternion class

class

class to handle general scaling case

class

struct for encapsulating ts transform related static functions

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 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
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TSTRANSFORM_H_
 25#define _TSTRANSFORM_H_
 26
 27#ifndef _MMATH_H_
 28#include "math/mMath.h"
 29#endif
 30
 31class Stream;
 32
 33/// compressed quaternion class
 34struct Quat16
 35{
 36   enum { MAX_VAL = 0x7fff };
 37
 38   S16 x, y, z, w;
 39
 40   void read(Stream *);
 41   void write(Stream *);
 42
 43   void identity();
 44   QuatF getQuatF() const;
 45   QuatF &getQuatF( QuatF * q ) const;
 46   void set( const QuatF & q );
 47   S32 operator==( const Quat16 & q ) const;
 48   S32 operator!=( const Quat16 & q ) const { return !(*this == q); }
 49};
 50
 51/// class to handle general scaling case
 52///
 53/// transform = rot * scale * inv(rot)
 54struct TSScale
 55{
 56   QuatF            mRotate;
 57   Point3F          mScale;
 58
 59   void identity() { mRotate.identity(); mScale.set( 1.0f,1.0f,1.0f ); }
 60   S32 operator==( const TSScale & other ) const { return mRotate==other.mRotate && mScale==other.mScale; }
 61};
 62
 63/// struct for encapsulating ts transform related static functions
 64struct TSTransform
 65{
 66   static Point3F & interpolate(const Point3F & p1, const Point3F & p2, F32 t, Point3F *);
 67   static F32       interpolate(F32             p1, F32             p2, F32 t);
 68   static QuatF   & interpolate(const QuatF   & q1, const QuatF   & q2, F32 t, QuatF   *);
 69   static TSScale & interpolate(const TSScale & s1, const TSScale & s2, F32 t, TSScale *);
 70
 71   static void      setMatrix(const QuatF     &  q, MatrixF *);
 72   static void      setMatrix(const QuatF     &  q, const Point3F & p, MatrixF *);
 73
 74   static void      applyScale(F32 scale, MatrixF *);
 75   static void      applyScale(const Point3F & scale, MatrixF *);
 76   static void      applyScale(const TSScale & scale, MatrixF *);
 77};
 78
 79inline Point3F & TSTransform::interpolate(const Point3F & p1, const Point3F & p2, F32 t, Point3F * p)
 80{
 81   p->x = p1.x + t * (p2.x-p1.x);
 82   p->y = p1.y + t * (p2.y-p1.y);
 83   p->z = p1.z + t * (p2.z-p1.z);
 84   return *p;
 85}
 86
 87inline F32 TSTransform::interpolate(F32 p1, F32 p2, F32 t)
 88{
 89   return p1 + t*(p2-p1);
 90}
 91
 92inline TSScale & TSTransform::interpolate(const TSScale & s1, const TSScale & s2, F32 t, TSScale * s)
 93{
 94   TSTransform::interpolate(s1.mRotate,s2.mRotate,t,&s->mRotate);
 95   TSTransform::interpolate(s1.mScale,s2.mScale,t,&s->mScale);
 96   return *s;
 97}
 98
 99inline void TSTransform::setMatrix( const QuatF & q, const Point3F & p, MatrixF * pDest )
100{
101   q.setMatrix(pDest);
102   pDest->setColumn(3,p);
103}
104
105inline void TSTransform::setMatrix( const QuatF & q, MatrixF * pDest )
106{
107   q.setMatrix(pDest);
108}
109
110#endif
111