当前位置: 首页 > news >正文

点云拟合球体

前言:在很多情况下,需要根据点云来拟合球体,本博文主要介绍各种方法的拟合情况及优缺点,希望对各位小伙伴有所帮助!

目录

1. vtkFitImplicitFunction进行球拟合

2. 四点求解球


1. vtkFitImplicitFunction进行球拟合

缺点:需要输入待拟合球的半径和结构,即在已知最优解的情况下求解。

vtkMath::RandomSeed(4355412); // for test result consistencydouble radius = 1.0;
vtkSmartPointer<vtkBoundedPointSource> points =vtkSmartPointer<vtkBoundedPointSource>::New();
points->SetNumberOfPoints(1000000);
points->SetBounds(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
points->Update();vtkSmartPointer<vtkSphere> sphere =vtkSmartPointer<vtkSphere>::New();
sphere->SetRadius(radius*2);vtkSmartPointer<vtkFitImplicitFunction> fit =vtkSmartPointer<vtkFitImplicitFunction>::New();
fit->SetInputConnection(points->GetOutputPort());
fit->SetImplicitFunction(sphere);
fit->SetThreshold(.01);
fit->Update();
std::cout << fit->GetOutput()->GetNumberOfPoints() << " out of "<< points->GetNumberOfPoints() << " points are within "<< fit->GetThreshold() << " of the implicit function" << std::endl;vtkSmartPointer<vtkSphereSource> sphereSource =vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(radius * .05);vtkSmartPointer<vtkGlyph3D> glyph3D =vtkSmartPointer<vtkGlyph3D>::New();
glyph3D->SetInputConnection(fit->GetOutputPort());
glyph3D->SetSourceConnection(sphereSource->GetOutputPort());
glyph3D->ScalingOff();
glyph3D->Update();vtkSmartPointer<vtkPolyDataMapper> glyph3DMapper =vtkSmartPointer<vtkPolyDataMapper>::New();
glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());vtkSmartPointer<vtkActor> glyph3DActor =vtkSmartPointer<vtkActor>::New();
glyph3DActor->SetMapper(glyph3DMapper);
glyph3DActor->GetProperty()->SetColor(0.8900, 0.8100, 0.3400);m_viewer->renderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(glyph3DActor);
m_viewer->renderWindow()->Render();

2. 四点求解球

缺点:要求输入的四点为精确球上的点;否则计算错误。

void fitSphere(vtkPoints* points, double center[3], double& radius) {double p1[3];double p2[3];double p3[3];double p4[3];points->GetPoint(0, p1);points->GetPoint(1, p2);points->GetPoint(2, p3);points->GetPoint(3, p4);double a = p1[0] - p2[0], b = p1[1] - p2[1], c = p1[2] - p2[2];double a1 = p3[0] - p4[0], b1 = p3[1] - p4[1], c1 = p3[2] - p3[2];double a2 = p2[0] - p3[0], b2 = p2[1] - p3[1], c2 = p2[2] - p3[2];double D = a * b1 * c2 + a2 * b * c1 + c * a1 * b2 - (a2 * b1 * c + a1 * b * c2 + a * b2 * c1);if (D == 0){return;}double A = p1[0] * p1[0] - p2[0] * p2[0];double B = p1[1] * p1[1] - p2[1] * p2[1];double C = p1[2] * p1[2] - p2[2] * p2[2];double A1 = p3[0] * p3[0] - p4[0] * p4[0];double B1 = p3[1] * p3[1] - p4[1] * p4[1];double C1 = p3[2] * p3[2] - p4[2] * p4[2];double A2 = p2[0] * p2[0] - p3[0] * p3[0];double B2 = p2[1] * p2[1] - p3[1] * p3[1];double C2 = p2[2] * p2[2] - p3[2] * p3[2];double P = (A + B + C) / 2;double Q = (A1 + B1 + C1) / 2;double R = (A2 + B2 + C2) / 2;double Dx = P * b1 * c2 + b * c1 * R + c * Q * b2 - (c * b1 * R + P * c1 * b2 + Q * b * c2);double Dy = a * Q * c2 + P * c1 * a2 + c * a1 * R - (c * Q * a2 + a * c1 * R + c2 * P * a1);double Dz = a * b1 * R + b * Q * a2 + P * a1 * b2 - (a2 * b1 * P + a * Q * b2 + R * b * a1);center[0] = Dx / D;center[1] = Dy / D;center[2] = Dz / D;radius = sqrt((p1[0] - center[0]) * (p1[0] - center[0]) +(p1[1] - center[1]) * (p1[1] - center[1]) +(p1[2] - center[2]) * (p1[2] - center[2]));}

http://www.lryc.cn/news/95599.html

相关文章:

  • 基于动态规划(DP)算法的增程式EV能量管理策略研究(MATLAB编程)
  • 前端知识点视频补充
  • python多线程—终止子线程
  • #P1012. [NOIP2015提高组] 神奇的幻方
  • (学习笔记-IP)Ping的工作原理
  • php 进程间通信:管道、uds
  • Stable Diffusion如何生成高质量的图-prompt写法介绍
  • MySQL 高级SQL语句(一)
  • SkyWalking链路追踪-技术文档首页
  • AndroidStudio Memory profiler(内存分析器)
  • 【C++模板进阶】
  • (一)RabbitMQ概念-优势、劣势、应用场景 、AMQP、工作原理
  • JetBrains全家桶:如何自定义实现类TODO注释?
  • 【技术干货】工业级BLE5.2蓝牙模块SKB378 使用教程,AT指令集
  • 零基础深度学习——学习笔记1 (逻辑回归)
  • I want to know on what switchport is connected my computer (10.8.0.2)
  • OpenCv之人脸操作
  • C++[第五章]--指针和引用
  • 用i18next使你的应用国际化-React
  • TSN -促进IT/OT 融合的网络技术
  • 改进的北方苍鹰算法优化BP神经网络---回归+分类两种案例
  • 等保工作如何和企业创新业务发展相结合,实现“安全”和“创新”的火花碰撞?
  • 23.7.25 杭电暑期多校3部分题解
  • 【设计模式——学习笔记】23种设计模式——桥接模式Bridge(原理讲解+应用场景介绍+案例介绍+Java代码实现)
  • 文档翻译软件那么多,哪个能满足你的多语言需求?
  • MySQL 中NULL和空值的区别
  • 阿里云容器镜像仓库(ACR)的创建和使用
  • 工业的相机与镜头(简单选型)
  • numpy广播机制介绍
  • RocketMQ 5.0 无状态实时性消费详解