general lerp & vector nlerp

This commit is contained in:
Dander7BD 2014-01-20 18:45:34 +01:00
parent 24a6e7478e
commit 28fbcebf98
1 changed files with 79 additions and 0 deletions

View File

@ -146,6 +146,85 @@ namespace LinearAlgebra
targetMem = out * (in.GetAdjoint() /= d); targetMem = out * (in.GetAdjoint() /= d);
return true; return true;
} }
/********************************************************************
* Linear Interpolation
* @return start * (1-t) + end * t
********************************************************************/
template<typename PointType, typename ScalarType>
inline PointType Lerp( const PointType &start, const PointType &end, const ScalarType &t )
{
return end * t + start * ( 1 - t );
}
/********************************************************************
* Normalized Linear Interpolation
* @return nullvector if impossible to solve.
********************************************************************/
template<typename ScalarType>
inline Vector2<ScalarType> Nlerp( const Vector2<ScalarType> &start, const Vector2<ScalarType> &end, const ScalarType &t )
{
Vector2<ScalarType> output = Lerp( start, end, t );
ScalarType magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 )
{
ScalarType half = t * 0.5f;
output = Lerp( start, end, half );
magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 )
return output; // error: returning nullvector
return Nlerp( output /= magnitudeSquared, end, half / (1.0f - half) );
}
return output /= magnitudeSquared;
}
/********************************************************************
* Normalized Linear Interpolation
* @return nullvector if impossible to solve.
********************************************************************/
template<typename ScalarType>
inline Vector3<ScalarType> Nlerp( const Vector3<ScalarType> &start, const Vector3<ScalarType> &end, const ScalarType &t )
{
Vector3<ScalarType> output = Lerp( start, end, t );
ScalarType magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 )
{
ScalarType half = t * 0.5f;
output = Lerp( start, end, half );
magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 )
return output; // error: returning nullvector
return Nlerp( output /= magnitudeSquared, end, half / (1.0f - half) );
}
return output /= magnitudeSquared;
}
/********************************************************************
* Normalized Linear Interpolation
* @return nullvector if impossible to solve.
********************************************************************/
template<typename ScalarType>
inline Vector4<ScalarType> Nlerp( const Vector4<ScalarType> &start, const Vector4<ScalarType> &end, const ScalarType &t )
{
Vector4<ScalarType> output = Lerp( start, end, t );
ScalarType magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 )
{
ScalarType half = t * 0.5f;
output = Lerp( start, end, half );
magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 )
return output; // error: returning nullvector
return Nlerp( output /= magnitudeSquared, end, half / (1.0f - half) );
}
return output /= magnitudeSquared;
}
} }
namespace LinearAlgebra2D namespace LinearAlgebra2D