# SplineCurves
样条曲线:多段三阶贝塞尔曲线的联合。三阶插值(取自 Unreal)
FVector FMath::CubicInterp(const FVector& P0, const FVector& T0, const FVector& P1, const FVector& T1, const float& A)
{
static_assert(PLATFORM_ENABLE_VECTORINTRINSICS == 1, "Requires SSE intrinsics.");
FVector res;
const float A2 = A * A;
const float A3 = A2 * A;
float s0 = (2 * A3) - (3 * A2) + 1;
float s1 = A3 - (2 * A2) + A;
float s2 = (A3 - A2);
float s3 = (-2 * A3) + (3 * A2);
VectorRegister v0 = VectorMultiply(VectorLoadFloat1(&s0), VectorLoadFloat3(&P0));
VectorRegister v1 = VectorMultiply(VectorLoadFloat1(&s1), VectorLoadFloat3(&T0));
VectorRegister v2 = VectorMultiply(VectorLoadFloat1(&s2), VectorLoadFloat3(&T1));
VectorRegister v3 = VectorMultiply(VectorLoadFloat1(&s3), VectorLoadFloat3(&P1));
VectorStoreFloat3(VectorAdd(VectorAdd(v0, v1), VectorAdd(v2, v3)), &res);
return res;
}
- 其中 P0、P1、T0、T1 分别是起点、终点、起点、起点切线、终点切线。
样条曲线插值推导:
- 三阶贝塞尔曲线公式,其中 t 取值范围为 [0~1],标识插值系数:
假定 P0、P1、T0、T1 对曲线的影响系数随插值系数的关系如下:
该多项式要满足这四个条件:
- t = 0 时 H (t) = P0;s0 (0) = 1,s1 (0) = 0,s2 (0) = 0,s3 (0) = 0
- t = 1 时 H (t) = P1;s0 (1) = 0,s1 (1) = 0,s2 (1) = 1,s3 (1) = 0
- t = 0 时 H (t)' = T0;s0'(0) = 0,s1'(0) = 1,s2'(0) = 0,s3'(0) = 0
- t = 1 时 H (t)' = T1;s0'(1) = 0,s1'(1) = 0,s2'(1) = 0,s3'(1) = 1
分别带入求解 s0、s1、s2、s3,以 s0 为例:
- 依次求解 s0、s1、s2、s3:
最终得到以上 Unreal 的三阶插值函数。