Torque3D Documentation / _generateds / tsTransform.cpp

tsTransform.cpp

Engine/source/ts/tsTransform.cpp

More...

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#include "ts/tsTransform.h"
 25#include "core/stream/stream.h"
 26
 27void Quat16::identity()
 28{
 29   x = y = z = 0;
 30   w = MAX_VAL;
 31}
 32
 33QuatF Quat16::getQuatF() const
 34{
 35   return QuatF ( F32(x) / F32(MAX_VAL),
 36                  F32(y) / F32(MAX_VAL),
 37                  F32(z) / F32(MAX_VAL),
 38                  F32(w) / F32(MAX_VAL) );
 39}
 40
 41QuatF & Quat16::getQuatF( QuatF * q ) const
 42{
 43   q->x = F32( x ) / F32(MAX_VAL);
 44   q->y = F32( y ) / F32(MAX_VAL);
 45   q->z = F32( z ) / F32(MAX_VAL);
 46   q->w = F32( w ) / F32(MAX_VAL);
 47   return *q;
 48}
 49
 50void Quat16::set( const QuatF & q )
 51{
 52   x = (S16)(q.x * F32(MAX_VAL));
 53   y = (S16)(q.y * F32(MAX_VAL));
 54   z = (S16)(q.z * F32(MAX_VAL));
 55   w = (S16)(q.w * F32(MAX_VAL));
 56}
 57
 58S32 Quat16::operator==( const Quat16 & q ) const
 59{
 60   return( x == q.x && y == q.y && z == q.z && w == q.w );
 61}
 62
 63void Quat16::read(Stream * s)
 64{
 65    s->read(&x);
 66    s->read(&y);
 67    s->read(&z);
 68    s->read(&w);
 69}
 70
 71void Quat16::write(Stream * s)
 72{
 73    s->write(x);
 74    s->write(y);
 75    s->write(z);
 76    s->write(w);
 77}
 78
 79QuatF & TSTransform::interpolate( const QuatF & q1, const QuatF & q2, F32 interp, QuatF * q )
 80{
 81   F32 Dot;
 82   F32 Dist2;
 83   F32 OneOverL;
 84   F32 x1,y1,z1,w1;
 85   F32 x2,y2,z2,w2;
 86
 87   //
 88   // This is a linear interpolation with a fast renormalization.
 89   //
 90   x1 = q1.x;
 91   y1 = q1.y;
 92   z1 = q1.z;
 93   w1 = q1.w;
 94   x2 = q2.x;
 95   y2 = q2.y;
 96   z2 = q2.z;
 97   w2 = q2.w;
 98
 99   // Determine if quats are further than 90 degrees
100   Dot = x1*x2 + y1*y2 + z1*z2 + w1*w2;
101
102   // If dot is negative flip one of the quaterions
103   if( Dot < 0.0f )
104   {
105       x1 = -x1;
106       y1 = -y1;
107       z1 = -z1;
108       w1 = -w1;
109   }
110
111   // Compute interpolated values
112   x1 = x1 + interp*(x2 - x1);
113   y1 = y1 + interp*(y2 - y1);
114   z1 = z1 + interp*(z2 - z1);
115   w1 = w1 + interp*(w2 - w1);
116
117   // Get squared distance of new quaternion
118   Dist2 = x1*x1 + y1*y1 + z1*z1 + w1*w1;
119
120   // Use home-baked polynomial to compute 1/sqrt(Dist2)
121   // since we know the range is 0.707 >= Dist2 <= 1.0
122   // we'll split in half.
123
124   if( Dist2<0.857f )
125      OneOverL = (((0.699368f)*Dist2) + -1.819985f)*Dist2 + 2.126369f;    //0.0000792
126   else
127      OneOverL = (((0.454012f)*Dist2) + -1.403517f)*Dist2 + 1.949542f;    //0.0000373
128
129   // Renormalize
130   q->x = x1*OneOverL;
131   q->y = y1*OneOverL;
132   q->z = z1*OneOverL;
133   q->w = w1*OneOverL;
134
135   return *q;
136}
137
138void TSTransform::applyScale(F32 scale, MatrixF * mat)
139{
140   mat->scale(Point3F(scale,scale,scale));
141}
142
143void TSTransform::applyScale(const Point3F & scale, MatrixF * mat)
144{
145   mat->scale(scale);
146}
147
148void TSTransform::applyScale(const TSScale & scale, MatrixF * mat)
149{
150   MatrixF mat2;
151   TSTransform::setMatrix(scale.mRotate,&mat2);
152   MatrixF mat3(mat2);
153   mat3.inverse();
154   mat2.scale(scale.mScale);
155   mat2.mul(mat3);
156   mat->mul(mat2);
157}
158