Featured program examples
VS+QT+VTK three-dimensional surface mesh point selection cutting
If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!
foreword
This blog writes code for <<VS+QT+VTK 3D surface mesh point selection>>, the code is neat, regular and easy to read. The first choice for learning and application recommendation.
Function: Read 3D grid data, and select the part to be cut by clicking on the grid surface .
Article directory
1. Required tool software
2. Use steps
1. Import library
2. Code implementation
3. Running results
3. Online assistance
1. Required tool software
1. VS, Qt
2. VTK
2. Use steps
1. Import library
#include <iostream>
#include <fstream>
#include <vtkSelectPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkIdTypeArray.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkSphereSource.h>
#include <vtkCellPicker.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkProperty.h>
#include <vtkSelectionNode.h>
#include <vtkSelection.h>
#include <vtkExtractSelection.h>
#include <vtkObjectFactory.h>
#include <vtkPointData.h>
#include <iostream>
#include <vector>
2. Code implementation
code show as below:
class CellPickerInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static CellPickerInteractorStyle* New();
CellPickerInteractorStyle()
{
selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
selectedActor = vtkSmartPointer<vtkActor>::New();
polyData = vtkPolyData::New();
//xvec.resize(5);
//yvec.resize(5);
//zvec.resize(5);
last_picked_actor = NULL;
}
virtual void OnLeftButtonDown()
{
//打印鼠标左键像素位置
if (this->Interactor->GetControlKey())
{
std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0]
<< " " << this->Interactor->GetEventPosition()[1] << std::endl;
//注册拾取点函数
this->Interactor->GetPicker()->Pick(
this->Interactor->GetEventPosition()[0],
this->Interactor->GetEventPosition()[1], 0, // always zero.
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()
);
//打印拾取点空间位置
this->Interactor->GetPicker()->GetPickPosition(picked);
cout << "Picked value: " << picked[0] << " " << picked[1] << " " << picked[2] << endl;
//获取此次的标记点。
this->last_picked_actor = GetActor();
markPoint.push_back(last_picked_actor);
if (this->last_picked_actor)
{
//标记出来
this->last_picked_actor->SetScale(0.02);
this->last_picked_actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(this->last_picked_actor);
}
}
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
};
int main(int, char* [])
{
// Read a stl file.
reader1->SetFileName("bunny.stl");
reader1->Update();
TheStlPoly->DeepCopy(reader1->GetOutput());
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(TheStlPoly);
actor->SetMapper(mapper);
actor->GetProperty()->SetOpacity(1);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->Render();
renderWindow->SetWindowName("PointPicker");
renderWindow->AddRenderer(renderer);
vtkNew<vtkNamedColors> colors;
renderer->SetBackground(colors->GetColor3d("CadetBlue").GetData());
vtkSmartPointer<CellPickerInteractorStyle> pointPicker = vtkSmartPointer<CellPickerInteractorStyle>::New();
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetInteractorStyle(pointPicker);
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
//renderer->SetBackground(0, 0, 0);
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
3. Running results
3. Online assistance:
If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!
1) Remote installation and operation environment, code debugging
2) Qt, C++, Python entry guide
3) Interface beautification
4) Software production
Current article link: Python+Qt desktop and webpage human customer service communication tool_alicema1111's blog-CSDN blog
Blogger recommended article: python face recognition statistics qt form - CSDN Blog
Blogger recommended article: Python Yolov5 flame smoke recognition source code sharing - CSDN blog
Personal blog homepage: alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages
Click here for all the blogger's articles : alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages