程序示例精选
VS+C++VTK-VCG三维网格模型鼠标框选拉普拉斯局部平滑
如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助!
前言
这篇博客针对《VS+C++VTK-VCG三维网格模型鼠标框选拉普拉斯局部平滑》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
文章目录
一、所需工具软件
二、使用步骤
1. 主要代码
2. 运行结果
三、在线协助
一、所需工具软件
1. Visual Studio
2. C++, VTK, VCG
二、使用步骤
代码如下(示例):
#include <vtkSTLReader.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include "vtkDICOMImageReader.h"
#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolume.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageMapToColors.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkCutter.h"
#include "vtkPlane.h"
#include "vtkImageActor.h"
#include "vtkImageData.h"
#include "vtkImageCast.h"
#include "vtkPointData.h"
#include "vtkImageShiftScale.h"
#include "vtkPolyDataToImageStencil.h"
#include "vtkImageStencil.h"
#include "vtkLookupTable.h"
#include "vtkImagePlaneWidget.h"
#include "vtkCellArray.h"
#include "vtkType.h"
#include "vtkPlaneWidget.h"
using namespace vcg;
using namespace std;class MyFace;
class MyVertex;struct MyUsedTypes : public UsedTypes<Use<MyVertex>::AsVertexType, Use<MyFace>::AsFaceType> {};
class MyVertex : public Vertex<MyUsedTypes, vertex::VFAdj, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags> {};
class MyFace : public Face<MyUsedTypes, face::VFAdj, face::Normal3f, face::VertexRef, face::BitFlags> {};
class MyMesh : public vcg::tri::TriMesh<vector<MyVertex>, vector<MyFace>> {};// 加载选定的点的函数
void LoadSelectedPoints(const string& filename, vector<Point3f>& selectedPoints) {ifstream file(filename);if (!file.is_open()) {cerr << "Could not open the file: " << filename << endl;return;}string line;while (getline(file, line)) {stringstream ss(line);float x, y, z;if (ss >> x >> y >> z) { // 读取 x, y, z 坐标selectedPoints.emplace_back(x, y, z);}}file.close();
}
// 文件复制函数
bool CopyFile(const std::string& source, const std::string& destination)
{std::ifstream src(source, std::ios::binary);std::ofstream dest(destination, std::ios::binary);if (!src || !dest) {std::cerr << "文件复制失败:" << source << " 到 " << destination << std::endl;return false;}dest << src.rdbuf();return true;
}int main(int, char* [])
{/*MyMesh m;*/int mask;if (tri::io::ImporterSTL<MyMesh>::Open(m, "eros.stl", mask) != tri::io::Importer<MyMesh>::E_NOERROR) {std::cerr << "Error while opening the file" << std::endl;return 0;)vtkNew<vtkNamedColors> colors;//vtkNew<vtkPointSource> pointSource;//pointSource->SetNumberOfPoints(20);//pointSource->Update();// 代码中的适当位置加入以下内容std::string originalFilename = "eros.stl";std::string tempFilename = "customer_model_1temp.stl";// 复制文件if (!CopyFile(originalFilename, tempFilename)) {std::cerr << "复制文件失败,请检查文件路径。" << std::endl;return 0;}std::string inputFilename = "customer_model_1temp.stl";vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();reader->SetFileName(inputFilename.c_str());reader->Update();vtkNew<vtkIdFilter> idFilter;idFilter->SetInputConnection(reader->GetOutputPort());
#if VTK890idFilter->SetCellIdsArrayName("OriginalIds");idFilter->SetPointIdsArrayName("OriginalIds");
#elseidFilter->SetIdsArrayName("OriginalIds");
#endifidFilter->Update();vtkNew<vtkDataSetSurfaceFilter> surfaceFilter;surfaceFilter->SetInputConnection(idFilter->GetOutputPort());surfaceFilter->Update();vtkPolyData* input = surfaceFilter->GetOutput();// Create a mapper and actorvtkNew<vtkPolyDataMapper> mapper;mapper->SetInputData(input);mapper->ScalarVisibilityOff();vtkNew<vtkActor> actor;actor->SetMapper(mapper);actor->GetProperty()->SetPointSize(3);//actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());actor->GetProperty()->SetColor(0.60784313725, 0.60784313725, 0.60784313725);actor->GetProperty()->SetEdgeColor(0, 0, 1); // 设置边缘颜色为蓝色actor->GetProperty()->SetEdgeVisibility(1);// VisualizevtkNew<vtkRenderer> renderer;vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(renderer);renderWindow->SetWindowName("HighlightSelectedPoints");vtkNew<vtkAreaPicker> areaPicker;vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;renderWindowInteractor->SetPicker(areaPicker);renderWindowInteractor->SetRenderWindow(renderWindow);renderer->AddActor(actor);renderer->SetBackground(colors->GetColor3d("Black").GetData());renderWindow->Render();vtkNew<InteractorStyle> style;style->SetPoints(input);renderWindowInteractor->SetInteractorStyle(style);renderWindowInteractor->Start();return EXIT_SUCCESS;
}
运行结果
平滑之前
平滑之后
三、在线协助:
如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作
5)云服务器申请
6)网站制作
当前文章连接:https://blog.csdn.net/alicema1111/article/details/132666851
个人博客主页:https://blog.csdn.net/alicema1111?type=blog
博主所有文章点这里:https://blog.csdn.net/alicema1111?type=blog
博主推荐:
Python人脸识别考勤打卡系统:
https://blog.csdn.net/alicema1111/article/details/133434445
Python果树水果识别:https://blog.csdn.net/alicema1111/article/details/130862842
Python+Yolov8+Deepsort入口人流量统计:https://blog.csdn.net/alicema1111/article/details/130454430
Python+Qt人脸识别门禁管理系统:https://blog.csdn.net/alicema1111/article/details/130353433
Python+Qt指纹录入识别考勤系统:https://blog.csdn.net/alicema1111/article/details/129338432
Python Yolov5火焰烟雾识别源码分享:https://blog.csdn.net/alicema1111/article/details/128420453
Python+Yolov8路面桥梁墙体裂缝识别:https://blog.csdn.net/alicema1111/article/details/133434445
Python+Yolov5道路障碍物识别:https://blog.csdn.net/alicema1111/article/details/129589741
Python+Yolov5跌倒检测 摔倒检测 人物目标行为 人体特征识别:https://blog.csdn.net/alicema1111/article/details/129272048