c# - C#/Unity3D中的分段线性整数曲线插值

标签 c# c++ unity3d interpolation linear-interpolation

有没有简单有效的方法在 C# 中实现分段线性整数到整数曲线插值(对于 Unity3D,如果重要的话)?
详情如下:

  • 必须随着时间的推移构建分段线性曲线表示。第一个插值请求在我们拥有所有数据点之前出现
  • 曲线是严格单调的
  • 第一个点总是 (0, 0)
  • 数据点的第一个坐标也是严格单调的 w.r.t 到达时间,即这些点按其第一个坐标自然排序。
  • 数据点不在会导致 4 字节整数溢出问题的范围内
  • 输出不必 100% 准确,因此舍入误差不是问题。

在 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;
}

我知道如何在 C# 中完成类似这样的工作,但没有那么简洁或高效。任何帮助将不胜感激。

编辑: 我不需要更准确,并且对分段线性插值非常满意,所以更好的插值质量不是我的问题。
我正在寻找的是一种高效、简洁的方法。所谓高效,我的意思是这样的:依靠数据点自然排序的事实,以便能够使用二进制搜索来找到合适的段

最佳答案

我会使用这个插值立方体:

x=a0+a1*t+a2*t*t+a3*t*t*t
y=b0+b1*t+b2*t*t+b3*t*t*t

哪里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以相同的方式计算,但使用 y当然坐标
p0..p3是三次插值曲线的控制点
t = < 0.0 , 1.0 >是来自 p1 的曲线参数至 p2

这确保位置和一阶导数是连续的 (c1)。 如果你想在整数数学上这样做,那么只需缩放 ai,bi Ant t相应地。您还可以以相同的方式根据需要添加任意数量的维度

现在您需要一些参数来遍历您的插值点,例如 u = <0 , N-1>


p(0..N-1)是你的控制点列表
u = 0表示起点 p(0)
u = N-1表示终点 p(N-1)
P0..P3是用于插值的控制点

所以你需要计算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);

如果你想在整数数学上做这个,那么只需缩放 u,t 如果N<3然后使用线性插值...或重复端点直到N>=3

[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;
    }

添加边缘案例处理,如 x超出点范围或点列表太小

关于c# - C#/Unity3D中的分段线性整数曲线插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22803762/

相关文章:

c# - 清除部分输出缓存

c# - 如何使用 Python 和 .NET/C# 自动化 Princeton Instruments LightField

c++ - 可以通过其他获取操作获取负载重新排序吗? cppreference 说只有非原子的和宽松的通过 acquire 排序

c# - 三次贝塞尔曲线上的恒速

ios - 如何摆脱 Xcode 中的 "Unknown type name ' __declspec'"错误

c# - 在 ContinueWith() block 中使用 await

C# 如何获取selectedindex并进行sql查询

c++ - 如何在 Windows 上为 Dev-C++ 安装 C++ 库

c++ - 由于多个抽象基类,实现两个同名但不同的非协变返回类型的函数

android - Unity 构建错误 : Missing 'package' key attribute on element package at [:arcore_client:] AndroidManifest. xml:30:9-54