C#で区分線形整数から整数への曲線補間を実装する簡単で効率的な方法はありますか(それが重要な場合はUnity3Dの場合)?
詳細は次のとおりです。
- 区分線形曲線表現は、時間をかけて構築する必要があります。最初の補間リクエストは、すべてのデータ ポイントを取得する前に行われます
- 曲線は厳密に単調です
- 最初の点は常に (0, 0)
- データポイントの最初の座標も、到着時間に関して厳密に単調です。つまり、ポイントは最初の座標によって自然に順序付けられます。
- データ ポイントは、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# でこのような動作を行う方法はわかりますが、簡潔でも効率的でもありません。どんな助けでも大歓迎です。
編集:より正確である必要はなく、区分線形補間に完全に満足しているため、補間品質の向上はここでは問題ではありません。
私が探しているのは、これを行うための効率的で簡潔な方法です。効率的とは、次のようなことを意味します: データ ポイントが自然に順序付けられているという事実に依存して、二分探索を使用して適切なセグメントを見つけることができるようにする