VTK通过线段裁剪

线段拆分网格

void retrustMesh(vtkSmartPointer<vtkPolyData> polydata, vtkSmartPointer<vtkPoints> intermediatePoint)
{vtkSmartPointer<vtkPoints> srcPoints = polydata->GetPoints();int pointSize = intermediatePoint->GetNumberOfPoints();//创建PointLocator并构建索引vtkNew< vtkPointLocator> pointLocator;pointLocator->SetDataSet(polydata);pointLocator->BuildLocator();//执行查询   找到选取点在源数据集中的索引vtkNew< vtkIdList> result;vtkNew< vtkIdList> segLinePointList;vtkNew<vtkPoints> retPoints;for (int i = 0; i < pointSize; i++){pointLocator->FindClosestNPoints(1, intermediatePoint->GetPoint(i), result);//vecId.push_back(result->GetId(0));segLinePointList->InsertNextId(result->GetId(0));retPoints->InsertNextPoint(srcPoints->GetPoint(result->GetId(0)));}int retPointSize = retPoints->GetNumberOfPoints();vtkNew<vtkPoints> segPoints;std::map<vtkIdType, vtkIdType> mapSrc2Seg;std::map<vtkIdType, vtkIdType> mapLineSrc2Seg;int srcPointSize = srcPoints->GetNumberOfPoints();for (int i = 0; i < srcPointSize; i++){//不在路径上,添加到字典中(key=源索引,value=分割后的索引)if (segLinePointList->FindIdLocation(i) == -1){int startIndex = segPoints->GetNumberOfPoints();segPoints->InsertNextPoint(srcPoints->GetPoint(i)[0], srcPoints->GetPoint(i)[1], srcPoints->GetPoint(i)[2]);mapSrc2Seg[i] = startIndex;}}vtkNew<vtkPoints> segLinePoints;std::set<vtkIdType> setLinePointIdType;int lineSrcPointSize = segLinePointList->GetNumberOfIds();for (int i = 0; i < lineSrcPointSize; i++){mapLineSrc2Seg[segLinePointList->GetId(i)] = i;segLinePoints->InsertNextPoint(srcPoints->GetPoint(segLinePointList->GetId(i))[0],srcPoints->GetPoint(segLinePointList->GetId(i))[1], srcPoints->GetPoint(segLinePointList->GetId(i))[2]);}// vtkSmartPointer<vtkCellArray> srcCellArr = polydata->GetPolys();int srcDataSize = srcCellArr->GetNumberOfCells();//创建单元数组,用于存储以上创建的线段vtkSmartPointer<vtkCellArray> segCellArr = vtkSmartPointer<vtkCellArray>::New();vtkSmartPointer<vtkCellArray> segLineCellArr = vtkSmartPointer<vtkCellArray>::New();vtkSmartPointer<vtkCellArray> segLinePointsCellArr = vtkSmartPointer<vtkCellArray>::New();vtkNew<vtkIdList> idList;vtkNew<vtkPolygon> polygon;for (int i = 0; i < srcDataSize; i++){srcCellArr->GetCellAtId(i, idList);int listSize = idList->GetNumberOfIds();bool bInsert = true;if (listSize > 0){for (int j = 0; j < listSize; j++){if (segLinePointList->FindIdLocation(idList->GetId(j)) != -1){bInsert = false;break;}}}else{bInsert = false;}if (bInsert){polygon->GetPointIds()->SetNumberOfIds(listSize);//设置点数for (int j = 0; j < listSize; j++){polygon->GetPointIds()->SetId(j, mapSrc2Seg[idList->GetId(j)]);}segCellArr->InsertNextCell(polygon);}else{polygon->GetPointIds()->SetNumberOfIds(listSize);//设置点数for (int j = 0; j < listSize; j++){polygon->GetPointIds()->SetId(j, idList->GetId(j));}segLineCellArr->InsertNextCell(polygon);}}// 构建新的网格vtkNew<vtkPolyData> lineNewPolyData;lineNewPolyData->SetPoints(srcPoints);lineNewPolyData->SetPolys(segLineCellArr);std::string linefile_path = "segLineData.ply";vtkNew<vtkPolyData> lineNewPointsData;lineNewPointsData->SetPoints(segLinePoints);lineNewPointsData->SetPolys(segLinePointsCellArr);std::string linePointsfile_path = "segLinePointsData.ply";// 构建新的网格vtkNew<vtkPolyData> newPolyData;newPolyData->SetPoints(segPoints);newPolyData->SetPolys(segCellArr);std::string file_path = "segSphere.ply";//写出为PLY格式文件vtkSmartPointer<vtkPLYWriter>  plywriter = vtkSmartPointer<vtkPLYWriter>::New();plywriter->SetFileName(file_path.c_str());plywriter->SetInputData(newPolyData);plywriter->Write();plywriter->Update();plywriter->SetFileName(linefile_path.c_str());plywriter->SetInputData(lineNewPolyData);plywriter->Write();plywriter->Update();plywriter->SetFileName(linePointsfile_path.c_str());plywriter->SetInputData(lineNewPointsData);plywriter->Write();plywriter->Update();
}

效果如图:
在这里插入图片描述

连通域分割

使用连通域将已拆分的网格数据进行分割处理

int testClip()
{PolyData to processvtkSmartPointer<vtkPolyData> polyData;vtkNew<vtkPLYReader> reader;reader->SetFileName("E:\\work\\project\\VTKdemo\\build\\segSphere.ply");reader->Update();polyData = reader->GetOutput();double bounds[6];polyData->GetPoints()->GetBounds(bounds);vtkNew<vtkPolyDataConnectivityFilter> connectivityFilter;connectivityFilter->SetInputData(polyData);connectivityFilter->SetExtractionModeToAllRegions();connectivityFilter->Update();int num = connectivityFilter->GetNumberOfExtractedRegions();cout << "连通分区的总顶点数量: " << connectivityFilter->GetOutput()->GetNumberOfPoints() << endl;if (num != 2){return 1;}vtkNew<vtkCleanPolyData> clean;ExtractionRegion(connectivityFilter, 0, clean);//需要清除孤立点vtkNew<vtkCleanPolyData> subClean;ExtractionRegion(connectivityFilter, 1, subClean);vtkNew<vtkPolyData> mainPoly;vtkNew<vtkPolyData> subPoly;JointMesh(clean, subClean, mainPoly, subPoly);// Create a mapper and actor for original data.vtkNew<vtkPolyDataMapper> originalMapper;originalMapper->SetInputData(polyData);vtkNew<vtkNamedColors> colors;vtkNew<vtkActor> originalActor;originalActor->SetMapper(originalMapper);originalActor->GetProperty()->BackfaceCullingOn();originalActor->GetProperty()->SetOpacity(0.6);originalActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());// Create a mapper and actor for extracted data.vtkNew<vtkPolyDataMapper> extractedMapper;extractedMapper->SetInputData(mainPoly);vtkNew<vtkActor> extractedActor;extractedActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());extractedActor->SetMapper(extractedMapper);extractedActor->GetProperty()->SetOpacity(0.6);extractedActor->GetProperty()->BackfaceCullingOn();// Create a mapper and actor for extracted data.vtkNew<vtkPolyDataMapper> subExtractedMapper;subExtractedMapper->SetInputData(subPoly);vtkNew<vtkActor> subExtractedActor;subExtractedActor->GetProperty()->SetColor(colors->GetColor3d("Beige").GetData());subExtractedActor->SetMapper(subExtractedMapper);subExtractedActor->GetProperty()->SetOpacity(0.6);subExtractedActor->GetProperty()->BackfaceCullingOn();// Create a renderer.vtkNew<vtkRenderer> renderer;renderer->AddActor(originalActor);renderer->AddActor(extractedActor);renderer->AddActor(subExtractedActor);renderer->GradientBackgroundOn();renderer->SetBackground2(colors->GetColor3d("Beige").GetData());renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());extractedActor->SetPosition((bounds[1] - bounds[0]) / 1.9, 0, 0);subExtractedActor->SetPosition((bounds[1] - bounds[0]) / 1.9, 0, 0);originalActor->SetPosition(-(bounds[1] - bounds[0]) / 1.9, 0, 0);// Create a render window.vtkNew<vtkRenderWindow> renwin;renwin->AddRenderer(renderer);renwin->SetSize(512, 512);renwin->SetWindowName("ExtractOutsideSurface");// Create an interactor.vtkNew<vtkRenderWindowInteractor> iren;// 交互方式vtkNew<vtkInteractorStyleTrackballCamera> style;iren->SetInteractorStyle(style);iren->SetRenderWindow(renwin);renwin->Render();iren->Initialize();iren->Start();return EXIT_SUCCESS;
}

效果如下:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/479153.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小迪安全2023最新版笔记集合--续更

小迪安全2023最新版笔记集合–续更 小迪安全2023最新笔记集合 章节一 ---- 基础入门&#xff1a; 知识点集合&#xff1a; 应用架构&#xff1a;Web/APP/云应用/三方服务/负载均衡等 安全产品&#xff1a;CDN/WAF/IDS/IPS/蜜罐/防火墙/杀毒等 渗透命令&#xff1a;文件上传下…

欠定方程组及其求解

欠定方程组是指方程的数量少于未知数的数量的方程组。在这种情况下&#xff0c;通常有无限多个解&#xff0c;因为给定的方程不足以唯一确定所有未知数的值。在某些情况下&#xff0c;我们可以利用额外的信息或假设&#xff0c;如稀疏性或其他约束&#xff0c;来找到一个合理的…

如何保护文件夹数据?怎么加密文件夹数据?

文件夹是电脑储存、管理数据的重要工具&#xff0c;为了避免文件夹数据泄露&#xff0c;我们需要加密保护文件夹。那么&#xff0c;怎么加密文件夹呢&#xff1f;下面我们就来了解一下。 如何避免文件夹数据泄露&#xff1f; 保护文件夹数据安全的方法有很多&#xff0c;例如…

169基于matlab的小波神经网络预测

基于matlab的小波神经网络预测&#xff0c;通过权值参数更新得到误差较小模型&#xff0c;进行多输出单输出预测。输出预测可视化结果。程序已调通&#xff0c;可直接运行。 169matlab小波神经网络预测 多输入单输出 (xiaohongshu.com)

【Go语言】Go语言的数据类型

GO 语言的数据类型 Go 语言内置对以下这些基本数据类型的支持&#xff1a; 布尔类型&#xff1a;bool 整型&#xff1a;int8、byte、int16、int、uint、uintptr 等 浮点类型&#xff1a;float32、float64 复数类型&#xff1a;complex64、complex128 字符串&#xff1a;st…

Ubuntu环境安装MySQL数据库

打开终端&#xff08;Terminal&#xff09;窗口&#xff0c;使用以下命令更新系统软件包&#xff1a; sudo apt update ubuntu环境安装mysql-server和mysql开发包&#xff0c;包括mysql头文件和动态库文件&#xff0c;命令如下&#xff1a; sudo apt-get install mysql-serv…

【Redis,Java】Redis的两种序列化方式—nosql数据库

redis和mysql的区别&#xff1a; redis是属于nosql的数据库&#xff0c;而mysql是属于sql数据库&#xff0c;redis是属于nosql数据库。mysql是存储在磁盘中的&#xff0c;redis是存储在内存中的&#xff0c;所以redis的读取书读快。这里所说的redis代表nosql&#xff0c;而mysq…

嵌入式调试学习

JTAG/SWD调试原理概述 Cortex-M内核含有硬件调试模块&#xff0c;该模块可在取指&#xff08;指令断点&#xff09;或访问数据&#xff08;数据断点&#xff09;时停止。 内核停止时&#xff0c;可以查询内核的内部状态和系统的外部状态。完成查询后&#xff0c;可恢复程序执行…

golang实现图片上传

实现个图片上传&#xff0c;记录一下 我这里是采用的调试工具&#xff0c;apipost去直接上传的&#xff0c;参数值选择file即可 上传的图片&#xff0c;两种方式&#xff0c;一是用原来的文件名&#xff0c;直接file.Filename即可。 func Upload(c *gin.Context) {file, _ : …

冬日探访冠州梨园,感受传统文化魅力

为深入了解中国梨文化的底蕴及其在当代的价值&#xff0c;2024年2月19日&#xff0c;曲阜师范大学计算机学院“古韵新声&#xff0c;格物致‘知’”实践队的实践队队员丁元顺走进山东省冠县冠州梨园展开社会实践&#xff0c;展开了一场探寻传统文化之旅。 冬季的冠州梨园&…

Idea启动Gradle报错: Please, re-import the Gradle project and try again

Idea启动Gradle报错&#xff1a;Warning:Unable to make the module: reading, related gradle configuration was not found. Please, re-import the Gradle project and try again. 解决办法&#xff1a; 开启步骤&#xff1a;View -> Tool Windows -> Gradle 点击refe…

南卡品牌宣布:2024年停售传统耳机,全面拥抱开放式耳机!

2月18日&#xff0c;中国领先的音频品牌南卡发布重磅消息&#xff0c;自2024年起将全面停止研发传统入耳式耳机&#xff0c;并全力投入开放式耳机的研发与生产。此举旨在为消费者提供更健康、安全、舒适的听音体验&#xff0c;更标志着南卡在音讯技术领域迈出了全新的步伐。 南…