|
楼主 |
发表于 2023-2-19 19:15:35
|
显示全部楼层
#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <unsupported/Eigen/Splines>
using namespace std;
using namespace Eigen;
int main()
{
// 构造输入点
vector<Vector3d> points;
points.push_back(Vector3d(0.0, 0.0, 0.0));
points.push_back(Vector3d(1.0, 0.0, 0.0));
points.push_back(Vector3d(1.0, 1.0, 0.0));
points.push_back(Vector3d(0.0, 1.0, 0.0));
points.push_back(Vector3d(0.0, 0.0, 1.0));
points.push_back(Vector3d(1.0, 0.0, 1.0));
points.push_back(Vector3d(1.0, 1.0, 1.0));
points.push_back(Vector3d(0.0, 1.0, 1.0));
// 构造样条曲线
int degree = 3;
int dim = 3;
int num_points = points.size();
int num_segments = num_points - degree - 1;
VectorXd knots(num_points + degree + 1);
for (int i = 0; i < num_points + degree + 1; i++) {
knots(i) = i;
}
MatrixXd points_mat(num_points, dim);
for (int i = 0; i < num_points; i++) {
points_mat.row(i) = points[i];
}
Spline<Vector3d, degree> spline = SplineFitting<Spline<Vector3d, degree> >::Interpolate(points, degree, knots);
// 输出样条曲线控制点和节点
cout << "Control points: " << endl << spline.control_points() << endl;
cout << "Knots: " << endl << spline.knots() << endl;
return 0;
}
此示例代码构造了一个包含八个三维点的点集,并通过 Eigen 的 SplineFitting 模块拟合了一个三次样条曲线。在构造样条曲线时,先构造了样条曲线的节点向量,并将点集转换为矩阵形式,然后通过 SplineFitting 模块的 Interpolate 方法拟合样条曲线。最后输出了样条曲线的控制点和节点向量。您可以根据需要进行修改和扩展。 |
|