C++ 中的插值样条曲线
interpolating spline curve in c++
有人对创建插值样条曲线有任何建议吗?我正在尝试在opengl中开发一个游戏,但我无法让对象遵循曲线。该函数应该类似于.....
void interpolatePath(Vec3d startPos, Vec3d targetPos, float u, Vec3d &interpPos)
对象从一个位置开始,用户单击,对象移动到该点。我现在有了它,以便对象沿直线移动,但我希望它遵循曲线。
上述功能中的直线代码:
//interpPos.x = (u)*targetPos.x+(1-u)*startPos.x;
//interpPos.y = (u)*targetPos.y+(1-u)*startPos.y;
//interpPos.z = (u)*targetPos.z+(1-u)*startPos.z;
贝塞尔曲线会起作用吗?我将如何实现它?
[x,y]=(1–t)^3P0+3(1–t)^2tP1+3(1–t)t^2P2+t^3P3
谢谢
您可以使用 B 样条对给定的点集进行插值,其中多项式或三次插值需要 3 个点。如果不是,则应该是线性插值。在这里,我将给你一个使用 Eigen lib 进行 B 样条插值的示例。
#include <Eigen/Core>
#include <unsupported/Eigen/Splines>
typedef Eigen::Spline<float, 3> Spline3d;
int main(){
std::vector<Eigen::VectorXf> waypoints;
Eigen::Vector3f po1(2,3,4);
Eigen::Vector3f po2(2,5,4);
Eigen::Vector3f po3(2,8,9);
Eigen::Vector3f po4(2,8,23);
waypoints.push_back(po1);
waypoints.push_back(po2);
waypoints.push_back(po3);
waypoints.push_back(po4);
// The degree of the interpolating spline needs to be one less than the number of points
// that are fitted to the spline.
Eigen::MatrixXf points(3, waypoints.size());
int row_index = 0;
for(auto const way_point : waypoints){
points.col(row_index) << way_point[0], way_point[1], way_point[2];
row_index++;
}
Spline3d spline = Eigen::SplineFitting<Spline3d>::Interpolate(points, 2);
float time_ = 0;
for(int i=0; i<20; i++){
time_ += 1.0/(20*1.0);
Eigen::VectorXf values = spline(time_);
std::cout<< values << std::endl;
}
return 0;
}
相关文章:
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 将使用太多的纹理插值器 - 带旋转的着色器
- 升压插值条件变量可以虚假唤醒吗?
- 使用 GSL 库制作样条曲线并使用它们进行集成
- 如何获得插值极坐标?
- 如何线性插值到不恒定的目的地
- 扩充矩阵的行缩减-三维样条曲线计算
- 向心Catmull-Rom样条曲线插值alpha参数
- 任意数量控制点的B样条曲线
- 特征样条插值 - 如何在任意点 x 处获取样条曲线 y 值
- 生成Catmull-Rom样条曲线并返回垃圾
- 在二维中计算具有3个给定点的精确样条曲线.C++
- C++ 中的插值样条曲线
- 对 3D 空间中的样条插值感到困惑
- b样条曲线
- 三次样条:开始/结束段插值
- 在c++程序中评估从max导出的3d样条曲线
- 点数组的C++样条曲线插值
- 应该重新绘制B样条曲线,而不删除窗口的所有其余部分
- C#/Unity3D 中的分段线性整数曲线插值