Added Lerp functions.

This commit is contained in:
hyzboy 2024-07-29 22:54:09 +08:00
parent 9c4624cccd
commit aab3a32e7a
3 changed files with 214 additions and 0 deletions

111
inc/hgl/math/Lerp2D.h Normal file
View File

@ -0,0 +1,111 @@
#pragma once
#include<hgl/math/Vector.h>
namespace hgl
{
namespace graph
{
//*******************************************************************************************
//以下代码由Github Coplot自动生成尚未测试
//*******************************************************************************************
inline float LerpLinear(const float from,const float to,const float t)
{
return from+(to-from)*t;
}
inline float LerpCos(const float from,const float to,const float t)
{
float t2=(1.0f-cos(t*HGL_PI))/2.0f;
return from*(1.0f-t2)+to*t2;
}
inline float LerpCubic(const float from,const float to,const float t)
{
float t2=t*t;
float t3=t2*t;
return from*(2.0f*t3-3.0f*t2+1.0f)+to*(3.0f*t2-2.0f*t3);
}
inline float LerpHermite(const float from,const float to,const float t)
{
float t2=t*t;
float t3=t2*t;
return from*(2.0f*t3-3.0f*t2+1.0f)+to*(3.0f*t2-2.0f*t3);
}
inline Vector2f LerpLinear(const Vector2f &from,const Vector2f &to,const float t)
{
return from+(to-from)*t;
}
inline Vector2f LerpCos(const Vector2f &from,const Vector2f &to,const float t)
{
float t2=(1.0f-cos(t*HGL_PI))/2.0f;
return from*(1.0f-t2)+to*t2;
}
inline Vector2f LerpCubic(const Vector2f &from,const Vector2f &to,const float t)
{
float t2=t*t;
float t3=t2*t;
return from*(2.0f*t3-3.0f*t2+1.0f)+to*(3.0f*t2-2.0f*t3);
}
inline Vector2f LerpHermite(const Vector2f &from,const Vector2f &to,const float t)
{
float t2=t*t;
float t3=t2*t;
return from*(2.0f*t3-3.0f*t2+1.0f)+to*(3.0f*t2-2.0f*t3);
}
inline Vector2f LerpBezier(const Vector2f &p0,const Vector2f &p1,const Vector2f &p2,const Vector2f &p3,const float t)
{
float t2=t*t;
float t3=t2*t;
float t_1=1.0f-t;
float t2_1=t_1*t_1;
float t3_1=t2_1*t_1;
return p0*t3_1+3.0f*p1*t*t2_1+3.0f*p2*t2*t_1+p3*t3;
}
inline Vector2f LerpCatmullRom(const Vector2f &p0,const Vector2f &p1,const Vector2f &p2,const Vector2f &p3,const float t)
{
float t2=t*t;
float t3=t2*t;
float t_1=1.0f-t;
float t2_1=t_1*t_1;
float t3_1=t2_1*t_1;
return p0*((-t3+2.0f*t2-t)*0.5f)
+p1*((3.0f*t3-5.0f*t2+2.0f)*0.5f)
+p2*((-3.0f*t3+4.0f*t2+t)*0.5f)
+p3*((t3-t2)*0.5f);
}
inline Vector2f LerpBSpline(const Vector2f &p0,const Vector2f &p1,const Vector2f &p2,const Vector2f &p3,const float t)
{
float t2=t*t;
float t3=t2*t;
float t_1=1.0f-t;
float t2_1=t_1*t_1;
float t3_1=t2_1*t_1;
return p0*((-t3+3.0f*t2-3.0f*t+1.0f)/6.0f)
+p1*((3.0f*t3-6.0f*t2+4.0f)/6.0f)
+p2*((-3.0f*t3+3.0f*t2+3.0f*t+1.0f)/6.0f)
+p3*(t3)/6.0f;
}
}//namespace graph
}//namespace hgl

77
inc/hgl/math/Lerp3D.h Normal file
View File

@ -0,0 +1,77 @@
#pragma once
#include<hgl/math/Vector.h>
namespace hgl
{
namespace graph
{
//*******************************************************************************************
//以下代码由Github Coplot自动生成尚未测试
//*******************************************************************************************
inline Vector3f LerpLinear(const Vector3f &from,const Vector3f &to,const float t)
{
return from+(to-from)*t;
}
inline Vector3f LerpCos(const Vector3f &from,const Vector3f &to,const float t)
{
float t2=(1.0f-cos(t*HGL_PI))/2.0f;
return from*(1.0f-t2)+to*t2;
}
inline Vector3f LerpCubic(const Vector3f &from,const Vector3f &to,const float t)
{
float t2=t*t;
float t3=t2*t;
return from*(2.0f*t3-3.0f*t2+1.0f)+to*(3.0f*t2-2.0f*t3);
}
inline Vector3f LerpHermite(const Vector3f &from,const Vector3f &to,const float t)
{
float t2=t*t;
float t3=t2*t;
return from*(2.0f*t3-3.0f*t2+1.0f)+to*(3.0f*t2-2.0f*t3);
}
inline Vector3f LerpBezier(const Vector3f &from,const Vector3f &cp1,const Vector3f &cp2,const Vector3f &to,const float t)
{
float t2=t*t;
float t3=t2*t;
float it=1.0f-t;
float it2=it*it;
float it3=it2*it;
return from*it3+cp1*3.0f*it2*t+cp2*3.0f*it*t2+to*t3;
}
inline Vector3f LerpCatmullRom(const Vector3f &p0,const Vector3f &p1,const Vector3f &p2,const Vector3f &p3,const float t)
{
float t2=t*t;
float t3=t2*t;
float it=1.0f-t;
float it2=it*it;
float it3=it2*it;
return p0*it3+p1*(3.0f*it2*t)+p2*(3.0f*it*t2)+p3*t3;
}
inline Vector3f LerpBSpline(const Vector3f &p0,const Vector3f &p1,const Vector3f &p2,const Vector3f &p3,const float t)
{
float t2=t*t;
float t3=t2*t;
float it=1.0f-t;
float it2=it*it;
float it3=it2*it;
return p0*(-it3/6.0f)+p1*(3.0f*t3/6.0f+3.0f*t2/6.0f+1.0f/6.0f)+p2*(-3.0f*t3/6.0f+3.0f*t2/6.0f)+p3*(t3/6.0f);
}
}//namespace graph
}//namespace hgl

26
inc/hgl/math/LerpType.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include<hgl/math/Vector.h>
namespace hgl
{
namespace graph
{
/**
*
*/
enum class LerpType
{
None, ///<无插值
Linear, ///<线性插值
Cos, ///<Cos插值
Cubic, ///<三次插值
Hermite, ///<Hermite插值
Bezier, ///<贝塞尔插值
CatmullRom, ///<CatmullRom插值
BSpline, ///<B样条插值
ENUM_CLASS_RANGE(None,BSpline)
};
}//namespace graph
}//namespace hgl