Vector Nlerp fix

This commit is contained in:
Dander7BD 2014-01-21 08:57:18 +01:00
parent 2ec8662a83
commit 748023bf9e
1 changed files with 15 additions and 36 deletions

View File

@ -159,71 +159,50 @@ namespace LinearAlgebra
/******************************************************************** /********************************************************************
* Normalized Linear Interpolation * Normalized Linear Interpolation
* @return nullvector if impossible to solve. * @return nullvector if Lerp( start, end, t ) is nullvector.
********************************************************************/ ********************************************************************/
template<typename ScalarType> template<typename ScalarType>
inline Vector2<ScalarType> Nlerp( const Vector2<ScalarType> &start, const Vector2<ScalarType> &end, const ScalarType &t ) inline Vector2<ScalarType> Nlerp( const Vector2<ScalarType> &start, const Vector2<ScalarType> &end, const ScalarType &t )
{ {
Vector2<ScalarType> output = Lerp( start, end, t ); Vector2<ScalarType> output = Lerp( start, end, t );
ScalarType magnitudeSquared = output.Dot( output ); ScalarType magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 ) if( magnitudeSquared != 0 )
{ {
ScalarType half = t * 0.5f; return output /= (ScalarType)::std::sqrt( magnitudeSquared );
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; return output; // error: returning nullvector
} }
/******************************************************************** /********************************************************************
* Normalized Linear Interpolation * Normalized Linear Interpolation
* @return nullvector if impossible to solve. * @return nullvector if Lerp( start, end, t ) is nullvector.
********************************************************************/ ********************************************************************/
template<typename ScalarType> template<typename ScalarType>
inline Vector3<ScalarType> Nlerp( const Vector3<ScalarType> &start, const Vector3<ScalarType> &end, const ScalarType &t ) inline Vector3<ScalarType> Nlerp( const Vector3<ScalarType> &start, const Vector3<ScalarType> &end, const ScalarType &t )
{ {
Vector3<ScalarType> output = Lerp( start, end, t ); Vector3<ScalarType> output = Lerp( start, end, t );
ScalarType magnitudeSquared = output.Dot( output ); ScalarType magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 ) if( magnitudeSquared != 0 )
{ {
ScalarType half = t * 0.5f; return output /= (ScalarType)::std::sqrt( magnitudeSquared );
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; return output; // error: returning nullvector
} }
/******************************************************************** /********************************************************************
* Normalized Linear Interpolation * Normalized Linear Interpolation
* @return nullvector if impossible to solve. * @return nullvector if Lerp( start, end, t ) is nullvector.
********************************************************************/ ********************************************************************/
template<typename ScalarType> template<typename ScalarType>
inline Vector4<ScalarType> Nlerp( const Vector4<ScalarType> &start, const Vector4<ScalarType> &end, const ScalarType &t ) inline Vector4<ScalarType> Nlerp( const Vector4<ScalarType> &start, const Vector4<ScalarType> &end, const ScalarType &t )
{ {
Vector4<ScalarType> output = Lerp( start, end, t ); Vector4<ScalarType> output = Lerp( start, end, t );
ScalarType magnitudeSquared = output.Dot( output ); ScalarType magnitudeSquared = output.Dot( output );
if( magnitudeSquared == 0 ) if( magnitudeSquared != 0 )
{ {
ScalarType half = t * 0.5f; return output /= (ScalarType)::std::sqrt( magnitudeSquared );
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; return output; // error: returning nullvector
} }
} }