[英]Piecewise linear integer curve interpolation in C#/Unity3D
Is there a simple, efficient way to implement a piecewise linear integer-to-integer curve interpolation in C# (for Unity3D, if it matters) ? 是否有一种简单有效的方法在C#中实现分段线性整数到整数曲线插值(对于Unity3D,如果重要的话)?
Details are as follows: 详细信息如下:
In C++, I would do something like this: 在C ++中,我将执行以下操作:
#include <algorithm>
#include <vector>
#include <cassert>
using namespace std;
typedef pair<int, int> tDataPoint;
typedef vector<tDataPoint> tPLC;
void appendData(tPLC& curve, const tDataPoint& point) {
assert(curve.empty() || curve.back().first < point.first);
curve.push_back(point);
}
int interpolate(const tPLC& curve, int cursor) {
assert(!curve.empty());
int result = 0;
// below zero, the value is a constant 0
if (cursor > 0) {
// find the first data point above the cursor
const auto upper = upper_bound(begin(curve), end(curve), cursor);
// above the last data point, the value is a constant 0
if (upper == end(curve)) {
result = curve.back().second;
} else {
// get the point below or equal to the cursor
const auto lower = upper - 1;
// lerp between
float linear = float((cursor - lower.first) * (upper.second - lower.second)) / (upper.first - lower.first);
result = lower.second + int(linear);
}
}
return result;
}
I can see how I could do something that work sort of like this in C#, but nothing as concise or efficient. 我可以看到如何在C#中执行类似的工作,但没有任何简洁或有效的方法。 Any help will be appreciated.
任何帮助将不胜感激。
EDIT: I do not need to be more accurate, and am perfectly happy with piecewise linear interpolation, so better interpolation quality is not my problem here. 编辑:我不需要更加准确,并且对分段线性插值非常满意,因此更好的插值质量不是我的问题。
What I am looking for is an efficient, concise way of doing this. 我正在寻找的是一种高效,简洁的方法。 By efficient, I mean things like: relying on the fact that the data points are naturally ordered to be able to use binary search to find the proper segment
高效,我的意思是:依靠数据点自然排序的事实,以便能够使用二进制搜索找到正确的段
I would use this interpolation cubic: 我将使用以下插值三次方:
x=a0+a1*t+a2*t*t+a3*t*t*t
y=b0+b1*t+b2*t*t+b3*t*t*t
where a0..a3
are computed like this: 其中
a0..a3
的计算方式如下:
d1=0.5*(p2.x-p0.x);
d2=0.5*(p3.x-p1.x);
a0=p1.x;
a1=d1;
a2=(3.0*(p2.x-p1.x))-(2.0*d1)-d2;
a3=d1+d2+(2.0*(-p2.x+p1.x));
b0 .. b3
are computed in same way but use y
coordinates of course b0 .. b3
的计算方法相同,但是当然使用y
坐标
p0..p3
are control points for cubic interpolation curve p0..p3
是三次插值曲线的控制点
t = < 0.0 , 1.0 >
is curve parameter from p1
to p2
t = < 0.0 , 1.0 >
是从p1
到p2
曲线参数
This ensures that position and first derivation is continuous (c1). 这样可以确保位置和一阶导数是连续的(c1)。 If you want to do this on integer math then just scale
ai,bi
ant t
accordingly. 如果要在整数数学上执行此操作,则只需相应地缩放
ai,bi
ant t
。 You can also add as many dimensions as you need in the same manner 您也可以按照需要添加任意多个尺寸
Now you need some parameter to go through your interpolation points for example u = <0 , N-1>
现在,您需要一些参数来遍历插值点,例如
u = <0 , N-1>
p(0..N-1)
are your control points list p(0..N-1)
是您的控制点列表
u = 0
means start point p(0)
u = 0
表示起点p(0)
u = N-1
means end point p(N-1)
u = N-1
表示终点p(N-1)
P0..P3
are control points used for interpolation P0..P3
是用于插补的控制点
So you need to compute t
and select which points to use for interpolation 因此,您需要计算
t
并选择用于插值的点
double t=u-floor(u); // fractional part between control points
int i=floor(u); // integer part points to starting control point used
if (i<1) { P0=p( 0),P1=p( 0),P2=p( 1),P3=p( 2); } // handle start edge case
else if (i==N-1) { P0=p(N-2),P1=p(N-1),P2=p(N-1),P3=p(N-1); } // handle end edge case
else if (i>=N-2) { P0=p(N-3),P1=p(N-2),P2=p(N-1),P3=p(N-1); } // handle end edge case
else { P0=p(i-1),P1=p(i ),P2=p(i+1),P3=p(i+2); }
(x,y) = interpolation (P0,P1,P2,P3,t);
If you want to do this on integer math then just scale u,t
accordingly. 如果要在整数数学上执行此操作,则只需相应地缩放
u,t
。 If N<3
then use linear interpolation ... or duplicate end points until N>=3
如果
N<3
则使用线性插值...或重复终点,直到N>=3
[edit1] linear interpolation approach [edit1]线性插值方法
struct pnt { int x,y; };
pnt interpolate (pnt *p,int N,int x)
{
int i,j;
pnt p;
for (j=1,i=N-1;j<i;j<<=1); j>>=1; if (!j) j=1; // this just determine max mask for binary search ... can do it on p[] size change
for (i=0;j;j>>=1) // binary search by x coordinate output is i as point index with p[i].x<=x
{
i|=j;
if (i>=N) { i-=j; continue; }
if (p[i].x==x) break;
if (p[i].x> x) i-=j;
}
p.x=x;
p.y=p[i].y+((p[i+1].y-p[i].y)*(x-p[i].x)/(p[i+1].x-p[i].x))
return p;
}
add edge cases handling like x
is out of points bound or point list is too small 添加边缘情况处理,例如
x
超出点边界或点列表太小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.