Merge branch 'LERP' into Physics

This commit is contained in:
Dander7BD 2014-01-21 11:23:32 +01:00
commit ea647c185c
3 changed files with 97 additions and 0 deletions

View File

@ -327,6 +327,10 @@ namespace Utility
inline ValueType Min( const ValueType &valueA, const ValueType &valueB ) inline ValueType Min( const ValueType &valueA, const ValueType &valueB )
{ return valueA < valueB ? valueA : valueB; } { return valueA < valueB ? valueA : valueB; }
template<typename ValueType>
inline ValueType Clamp( const ValueType &value, const ValueType &min, const ValueType &max )
{ return value < min ? Max( value, max ) : min; }
template<typename ValueType> template<typename ValueType>
inline ValueType Average( const ValueType &valueA, const ValueType &valueB ) inline ValueType Average( const ValueType &valueA, const ValueType &valueB )
{ return (valueA + valueB) * 0.5f; } { return (valueA + valueB) * 0.5f; }

View File

@ -146,6 +146,64 @@ 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 Lerp( start, end, t ) is nullvector.
********************************************************************/
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 )
{
return output /= (ScalarType)::std::sqrt( magnitudeSquared );
}
return output; // error: returning nullvector
}
/********************************************************************
* Normalized Linear Interpolation
* @return nullvector if Lerp( start, end, t ) is nullvector.
********************************************************************/
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 )
{
return output /= (ScalarType)::std::sqrt( magnitudeSquared );
}
return output; // error: returning nullvector
}
/********************************************************************
* Normalized Linear Interpolation
* @return nullvector if Lerp( start, end, t ) is nullvector.
********************************************************************/
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 )
{
return output /= (ScalarType)::std::sqrt( magnitudeSquared );
}
return output; // error: returning nullvector
}
} }
namespace LinearAlgebra2D namespace LinearAlgebra2D
@ -668,6 +726,35 @@ namespace LinearAlgebra3D
template<typename ScalarType> template<typename ScalarType>
inline ::LinearAlgebra::Vector4<ScalarType> NormalProjection( const ::LinearAlgebra::Vector4<ScalarType> &vector, const ::LinearAlgebra::Vector4<ScalarType> &normalizedAxis ) inline ::LinearAlgebra::Vector4<ScalarType> NormalProjection( const ::LinearAlgebra::Vector4<ScalarType> &vector, const ::LinearAlgebra::Vector4<ScalarType> &normalizedAxis )
{ return normalizedAxis * ( vector.Dot(normalizedAxis) ); } { return normalizedAxis * ( vector.Dot(normalizedAxis) ); }
template<typename ScalarType>
::LinearAlgebra::Matrix4x4<ScalarType> & SnapAxisYToNormal_UsingNlerp( ::LinearAlgebra::Matrix4x4<ScalarType> &rotation, const ::LinearAlgebra::Vector4<ScalarType> &normalizedAxis )
{
ScalarType projectedMagnitude = rotation.v[0].Dot( normalizedAxis );
if( projectedMagnitude == 1 )
{ // infinite possible solutions -> roadtrip!
::LinearAlgebra::Vector4<ScalarType> interpolated = ::LinearAlgebra::Nlerp( rotation.v[1], normalizedAxis, t );
// interpolated.Dot( interpolated ) == 0 should be impossible at this point
projectedMagnitude = rotation.v[0].Dot( interpolated );
rotation.v[0] -= projectedMagnitude * interpolated;
rotation.v[0].Normalize();
projectedMagnitude = rotation.v[0].Dot( normalizedAxis );
}
rotation.v[0] -= projectedMagnitude * normalizedAxis;
rotation.v[0].Normalize();
rotation.v[1] = normalizedAxis;
rotation.v[2] = rotation.v[0].Cross( rotation.v[1] );
}
template<typename ScalarType>
::LinearAlgebra::Matrix4x4<ScalarType> & InterpolateAxisYToNormal_UsingNlerp( ::LinearAlgebra::Matrix4x4<ScalarType> &rotation, const ::LinearAlgebra::Vector4<ScalarType> &normalizedAxis, ScalarType t )
{
::LinearAlgebra::Vector4<ScalarType> interpolated = ::LinearAlgebra::Nlerp( rotation.v[1], normalizedAxis, t );
if( interpolated.Dot(interpolated) == 0 )
return rotation; // return no change
return SnapAxisYToAxis_Nlerp( rotation, interpolated );
}
} }
#include "Utilities.h" #include "Utilities.h"

View File

@ -45,6 +45,9 @@ namespace Oyster { namespace Math //! Oyster's native math library
//! Creates a solution matrix for 'out´= 'targetMem' * 'in'. //! Creates a solution matrix for 'out´= 'targetMem' * 'in'.
//! Returns false if there is no explicit solution. //! Returns false if there is no explicit solution.
bool SuperpositionMatrix( const Float4x4 &in, const Float4x4 &out, Float4x4 &targetMem ); bool SuperpositionMatrix( const Float4x4 &in, const Float4x4 &out, Float4x4 &targetMem );
using ::LinearAlgebra::Lerp;
using ::LinearAlgebra::Nlerp;
} } } }
inline ::Oyster::Math::Float2 & operator *= ( ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right ) inline ::Oyster::Math::Float2 & operator *= ( ::Oyster::Math::Float2 &left, const ::Oyster::Math::Float2 &right )
@ -328,6 +331,9 @@ namespace Oyster { namespace Math3D //! Oyster's native math library specialized
//! Helper inline function that sets and then returns targetMem = transformer * transformee //! Helper inline function that sets and then returns targetMem = transformer * transformee
inline Float4 & TransformVector( const Float4x4 &transformer, const Float4 &transformee, Float4 &targetMem = Float4() ) inline Float4 & TransformVector( const Float4x4 &transformer, const Float4 &transformee, Float4 &targetMem = Float4() )
{ return targetMem = transformer * transformee; } { return targetMem = transformer * transformee; }
using ::LinearAlgebra3D::SnapAxisYToNormal_UsingNlerp;
using ::LinearAlgebra3D::InterpolateAxisYToNormal_UsingNlerp;
} } } }
#endif #endif