【发布时间】:2018-12-03 00:28:51
【问题描述】:
我正在学习贝塞尔曲线,并想使用估计方法参数化距离方程。到目前为止,我的代码似乎适用于单点(EG Bezier(start=0, mid=1, end=5, nPoints=6) 产生 [0 1 2 3 4 5])。但是,当我尝试将其应用于多维曲线时,我的结果并不如预期。
C# 代码(在 Unity 中执行以实现可视化)。该函数(应该)在长度为 l% 的曲线上获取一个点(由点 pts 定义)。
Vector3 BezierL(Vector3[] pts, float l)
{
int i;
float[] tVals = new float[n];
Vector3[] points = new Vector3[n];
float[] cumDist = new float[n];
for (i = 1; i < n; i++)
{
tVals[i] = i / (float)(n - 1);
points[i] = Bezier(pts, tVals[i]);
cumDist[i] = cumDist[i - 1] +
(points[i] - points[i - 1]).magnitude;
}
// Interpolate to estimate t
float targetLen = l * cumDist[n - 1];
int ind = Array.BinarySearch(cumDist, targetLen);
if (ind < 0)
ind = ~ind;
float t = Mathf.Lerp(tVals[ind - 1], tVals[ind],
(targetLen - cumDist[ind - 1]) / (cumDist[ind] - cumDist[ind - 1]));
return Bezier(pts, t);
}
其中Bezier(Vector3[] pts, t) 在pts 定义的曲线上得到一个点t。无论出于何种原因,这在一定程度上是有效的,因为所有点都等距分布,但有些点在初始点堆叠,而不是沿曲线分布。
This 是我开发此算法的参考,所以我不确定我的实现是否不正确,或者它是否仅适用于低维曲线。
提前致谢!
【问题讨论】:
-
似乎从未计算过 points[0]。