用到主要vtk类有vtkSTLReader,读取stl模型文件,vtkAppendPolyData,合并读取的stl文件,vtkFeatureEdges,提取模型边线。
相关代码如下:
#include “stdafx.h”
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkInteractionStyle)
#include <vtkFeatureEdges.h>
#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkAppendPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
int main()
{
//读取stl文件
vtkSmartPointer stlreader1 = vtkSmartPointer::New();
stlreader1->SetFileName(“D:\moxing\a12.stl”);//设置stl文件路径
//stlreader1->SetMerging(1);
stlreader1->Update();
vtkSmartPointer<vtkSTLReader> stlreader2 = vtkSmartPointer<vtkSTLReader>::New(); stlreader2->SetFileName("D:\\moxing\\a13.stl"); //stlreader2->SetMerging(1); stlreader2->Update(); vtkSmartPointer<vtkSTLReader> stlreader3 = vtkSmartPointer<vtkSTLReader>::New(); stlreader3->SetFileName("D:\\moxing\\a14.stl"); //stlreader3->SetMerging(1); stlreader3->Update(); vtkSmartPointer<vtkSTLReader> stlreader4 = vtkSmartPointer<vtkSTLReader>::New(); stlreader4->SetFileName("D:\\moxing\\a15.stl"); stlreader4->SetMerging(1); stlreader4->Update(); vtkSmartPointer<vtkSTLReader> stlreader5 = vtkSmartPointer<vtkSTLReader>::New(); stlreader5->SetFileName("D:\\moxing\\a16.stl"); stlreader5->SetMerging(1); stlreader5->Update(); vtkSmartPointer<vtkSTLReader> stlreader6 = vtkSmartPointer<vtkSTLReader>::New(); stlreader6->SetFileName("D:\\moxing\\a17.stl"); stlreader6->SetMerging(1); stlreader6->Update();
//合并读取的stl文件
vtkSmartPointer appendfilter = vtkSmartPointer::New();
appendfilter->AddInputData(stlreader1->GetOutput());
appendfilter->AddInputData(stlreader2->GetOutput());
appendfilter->AddInputData(stlreader3->GetOutput());
appendfilter->AddInputData(stlreader4->GetOutput());
appendfilter->AddInputData(stlreader5->GetOutput());
appendfilter->AddInputData(stlreader6->GetOutput());
appendfilter->Update();
//提取模型边线
vtkSmartPointer featureedges = vtkSmartPointer::New();
featureedges->SetInputConnection(appendfilter->GetOutputPort());
featureedges->Update();
vtkSmartPointer<vtkPolyDataMapper> appendfiltermapper = vtkSmartPointer<vtkPolyDataMapper>::New(); appendfiltermapper->SetInputConnection(appendfilter->GetOutputPort()); appendfiltermapper->SetScalarVisibility(0); vtkSmartPointer<vtkPolyDataMapper> featureedgesmapper = vtkSmartPointer<vtkPolyDataMapper>::New(); featureedgesmapper->SetInputConnection(featureedges->GetOutputPort()); featureedgesmapper->SetScalarVisibility(0); vtkSmartPointer<vtkActor> appendfilteractor = vtkSmartPointer<vtkActor>::New(); appendfilteractor->SetMapper(appendfiltermapper); appendfilteractor->GetProperty()->SetColor(0,1,1); vtkSmartPointer<vtkActor> featureedgesactor = vtkSmartPointer<vtkActor>::New(); featureedgesactor->SetMapper(featureedgesmapper); featureedgesactor->GetProperty()->SetColor(1.0,0,1.0); featureedgesactor->GetProperty()->SetLineWidth(2); vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New(); ren1->AddActor(appendfilteractor); ren1->SetViewport(0,0.0,0.5,1); ren1->SetBackground(1,1,1); ren1->ResetCamera(); vtkSmartPointer<vtkRenderer> ren2 = vtkSmartPointer<vtkRenderer>::New(); ren2->AddActor(featureedgesactor); ren2->SetBackground(1, 1, 1); ren2->SetViewport(0.5,0.0, 1.0, 1); vtkSmartPointer<vtkRenderWindow> renwin = vtkSmartPointer<vtkRenderWindow>::New(); renwin->AddRenderer(ren1); renwin->AddRenderer(ren2); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renwin); vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); renderWindowInteractor->SetInteractorStyle(style); renderWindowInteractor->Initialize(); renderWindowInteractor->Start(); return 0;
最综效果:
————————————————
版权声明:本文为CSDN博主「muye2356」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/muye2356/article/details/113097609