VTK Learning Ten - VTK Drawing Points
引言
鼠标交互式绘制点。涉及的主要VTK
对象 vtkInteractorStyleTrackballCamera
和vtkPointPicker
。
Code
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPolyDataMapper.h>
#include <vtkPointPicker.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkVertex.h>
#include <vtkCellArray.h>
// Handle mouse events
class MouseInteractorDrawing : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorDrawing* New();
vtkTypeMacro(MouseInteractorDrawing, vtkInteractorStyleTrackballCamera);
MouseInteractorDrawing()
{
m_actor = vtkSmartPointer<vtkActor>::New();
m_polydata = vtkPolyData::New();
m_polydata->SetPoints(vtkSmartPointer<vtkPoints>::New());
m_polydata->SetVerts(vtkSmartPointer<vtkCellArray>::New());
}
virtual ~MouseInteractorDrawing()
{
}
virtual void OnLeftButtonUp() {
vtkPoints *points = m_polydata->GetPoints();
vtkCellArray *cells = m_polydata->GetVerts();
vtkIdType idx=points->InsertNextPoint(m_position);
vtkSmartPointer<vtkVertex>vertex = vtkSmartPointer<vtkVertex>::New();
vertex->GetPointIds()->InsertNextId(idx);
cells->InsertNextCell(vertex);
m_polydata->SetPoints(points);
m_polydata->SetVerts(cells);
vtkSmartPointer<vtkPolyDataMapper>mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(m_polydata);
mapper->Update();
m_actor->SetMapper(mapper);
m_actor->GetProperty()->SetColor(1, 0, 0);
m_actor->GetProperty()->SetPointSize(5);
GetDefaultRenderer()->AddActor(m_actor);
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
}
virtual void OnLeftButtonDown()
{
int x = GetInteractor()->GetEventPosition()[0];
int y = GetInteractor()->GetEventPosition()[1];
// Pick from this location.
vtkSmartPointer<vtkPointPicker> picker =
vtkSmartPointer<vtkPointPicker>::New();
picker->SetTolerance(0.001);
picker->Pick(x, y, 0, GetDefaultRenderer());
picker->GetPickPosition(m_position);
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
private:
vtkSmartPointer<vtkPolyData> m_polydata;
vtkSmartPointer<vtkActor> m_actor;
double m_position[3] = {0.0};
};
vtkStandardNewMacro(MouseInteractorDrawing);
// Execute application.
int main(int argc, char *argv[])
{
// A renderer and render window
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer ( renderer );
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow ( renderWindow );
// Set the custom type to use for interaction.
vtkSmartPointer<MouseInteractorDrawing> style =
vtkSmartPointer<MouseInteractorDrawing>::New();
style->SetDefaultRenderer(renderer);
renderWindowInteractor->SetInteractorStyle( style );
renderer->SetBackground ( .3, .4, .5 );
// Render and interact
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(drawvertex)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(drawvertex drawvertex.cxx )
target_link_libraries(drawvertex ${VTK_LIBRARIES})