如何适应平面并将它们投射到C++与特征

发布于 2025-01-21 05:31:40 字数 1598 浏览 1 评论 0原文

抱歉,如果这是错误的地方,但是我在数学上很挣扎,并想使用C ++实现3D拟合的圆圈。我在其他编程语言中发现了很多东西,但并不真正了解那里发生了什么。谁能指导我如何做?如果解释尽可能简单

(或试图做大声笑),

  1. 并不是数学上最好的,
  2. 我 我将不胜感激。失败)
  3. 项目n点2D到拟合平面
  4. 适合一个2D圆的
  5. 项目。圆心中心的坐标回到3D,

我尝试使用普通的最小二乘步骤2,并用特征SVD测试了结果,这给了我完全不同的值与OSL相比。所以我猜我一定要做些完全错误的事情。

这是代码:

std::vector<Eigen::Vector4f> points;

Eigen::MatrixXf A(3,3);

Eigen::VectorXf b(3);

float a00 = 0;
float a01 = 0;
float a02 = 0;

float a10 = 0;
float a11 = 0;
float a12 = 0;

float a20 = 0;
float a21 = 0;
float a22 = 0;

float b0 = 0;
float b1 = 0;
float b2 = 0;

for (int i = 0; i < points.size(); i++) {

    a00 += points[i].x() * points[i].x();
    a01 += points[i].x() * points[i].y();
    a02 += points[i].x();

    a10 += points[i].x() * points[i].y();
    a11 += points[i].y() * points[i].y();
    a12 += points[i].y();

    a20 += points[i].x();
    a21 += points[i].y();
    
    b0 += points[i].x() * points[i].z();
    b1 += points[i].y() * points[i].z();
    b2 += points[i].z();
    
}

A(0,0) = a00;
A(0, 1) = a01;
A(0, 2) = a02;

A(1,0) = a10;
A(1, 1) = a11;
A(1, 2) = a12;

A(2, 0) = a20;
A(2, 1) = a21;
A(2, 2) = points.size();


b(0) = b0;
b(1) = b1;
b(2) = b2;

auto leastsquaresOSL = (A.transpose() * A).ldlt().solve(A.transpose() * b);

auto leastsquaresSVD = A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);

编辑:有人建议我编辑我的帖子,然后专注于平面拟合部分。不过,我将把我的原始问题留下来。所以我去了:

我有很多要点,想使用eigen ::向量和矩阵将飞机装入它。然后,我想将这些点投射到飞机上。我真的不知道如何解决这个问题,大多数帖子(如果不是全部)太复杂了,使用Python或其他语言。如果可能的话:任何人都可以将其归结为如何使用eigen在C ++中实施吗?

Sorry if this is the wrong place but I struggle a lot with the math and want to implement a circle fit in 3D using C++. I found lots of things in other programming languages but don't really understand whats going on there. Can anyone guide me on how to do that? I am not the best at math I would appreciate it if the explanation would be as simple as possible

What I tried (or tried to do lol) is as followed:

  1. Get n points with x,y,z coordinates
  2. fit a plane (which seems to fail)
  3. project n points to 2d onto the fitted plane
  4. fit a 2d circle
  5. project the coordinates of the circle center back to 3d

I tried to use ordinary least squares for step 2 and also tested the results with eigens SVD which gave me completely different values compared to OSL. So I must be doing something completely wrong I guess.

Here is the code:

std::vector<Eigen::Vector4f> points;

Eigen::MatrixXf A(3,3);

Eigen::VectorXf b(3);

float a00 = 0;
float a01 = 0;
float a02 = 0;

float a10 = 0;
float a11 = 0;
float a12 = 0;

float a20 = 0;
float a21 = 0;
float a22 = 0;

float b0 = 0;
float b1 = 0;
float b2 = 0;

for (int i = 0; i < points.size(); i++) {

    a00 += points[i].x() * points[i].x();
    a01 += points[i].x() * points[i].y();
    a02 += points[i].x();

    a10 += points[i].x() * points[i].y();
    a11 += points[i].y() * points[i].y();
    a12 += points[i].y();

    a20 += points[i].x();
    a21 += points[i].y();
    
    b0 += points[i].x() * points[i].z();
    b1 += points[i].y() * points[i].z();
    b2 += points[i].z();
    
}

A(0,0) = a00;
A(0, 1) = a01;
A(0, 2) = a02;

A(1,0) = a10;
A(1, 1) = a11;
A(1, 2) = a12;

A(2, 0) = a20;
A(2, 1) = a21;
A(2, 2) = points.size();


b(0) = b0;
b(1) = b1;
b(2) = b2;

auto leastsquaresOSL = (A.transpose() * A).ldlt().solve(A.transpose() * b);

auto leastsquaresSVD = A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b);

EDIT: Someone suggested I edit my post and just focus on the plane fitting part. I will leave my original question up though. So here I go:

I have lots of points and want to fit a plane into it using Eigen::Vectors and Matrices. Then I would want to project the points onto the plane. I really don't know how to tackle this problem and most posts (if not all) were too complex and in Python or another language. If possible: Can anyone boil it down how to implement it in C++ using Eigen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文