Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lerp.h File Reference
#include <assert.h>
#include "vec.h"
#include "quaternion.h"

Go to the source code of this file.

Functions

Transformation linear_interpolate_uncapped (const Transformation &a, const Transformation &b, double blend)
 
Transformation linear_interpolate (const Transformation &a, const Transformation &b, double blend)
 

Function Documentation

Transformation linear_interpolate ( const Transformation a,
const Transformation b,
double  blend 
)
inline

Definition at line 12 of file lerp.h.

References linear_interpolate_uncapped().

Referenced by GameStarSystem::Draw(), and GameUnit< UnitType >::Draw().

13 {
14  return linear_interpolate_uncapped( a, b, ( (blend > 1.0) ? 1.0 : blend ) );
15 }
Transformation linear_interpolate_uncapped ( const Transformation a,
const Transformation b,
double  blend 
)

Definition at line 3 of file lerp.cpp.

References a, UniverseUtil::acos(), b, DotProduct(), f, float, Quaternion::Normalize(), Transformation::orientation, Transformation::position, Quaternion::s, UniverseUtil::sin(), and Quaternion::v.

Referenced by LinearPrediction::Interpolate(), and linear_interpolate().

4 {
5  Quaternion result;
6  if (0 /*A.orientation==B.orientation*/) {
7  result = A.orientation;
8  } else {
9  const Quaternion &a = A.orientation;
10  const Quaternion &b = B.orientation;
11  double f = blend, f0, f1, sadj=1;
12  double cos_omega = DotProduct( a.v, b.v )+a.s*b.s;
13  //Adjust signs if necessary.
14  if (cos_omega < 0) {
15  cos_omega = -cos_omega;
16  sadj = -1;
17  }
18  if (cos_omega < 0.99) {
19  //Do the spherical interp.
20  double omega = acos( cos_omega );
21  double isin_omega = 1.0 / sin( omega );
22  f0 = sin( (1-f)*omega ) * isin_omega;
23  f1 = sin( f*omega ) * isin_omega;
24  } else {
25  //Quaternions are close; just do straight lerp and avoid division by near-zero.
26  f0 = 1-f;
27  f1 = f;
28  }
29  result.s = a.s*float(f0)+b.s*float(f1*sadj);
30  result.v = a.v*float(f0)+b.v*float(f1*sadj);
31  result.Normalize();
32  }
33  return Transformation( result, A.position+(B.position-A.position)*blend );
34 }