From 28fbcebf98b627b4cfb46e317ca89f983c07778c Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Mon, 20 Jan 2014 18:45:34 +0100 Subject: [PATCH] general lerp & vector nlerp --- Code/OysterMath/LinearMath.h | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/Code/OysterMath/LinearMath.h b/Code/OysterMath/LinearMath.h index b5eab471..791999db 100644 --- a/Code/OysterMath/LinearMath.h +++ b/Code/OysterMath/LinearMath.h @@ -146,6 +146,85 @@ namespace LinearAlgebra targetMem = out * (in.GetAdjoint() /= d); return true; } + + /******************************************************************** + * Linear Interpolation + * @return start * (1-t) + end * t + ********************************************************************/ + template + 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 + inline Vector2 Nlerp( const Vector2 &start, const Vector2 &end, const ScalarType &t ) + { + Vector2 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 + inline Vector3 Nlerp( const Vector3 &start, const Vector3 &end, const ScalarType &t ) + { + Vector3 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 + inline Vector4 Nlerp( const Vector4 &start, const Vector4 &end, const ScalarType &t ) + { + Vector4 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