VTK Learning Fourteen - VTK Drawing Lines
Description
鼠标交互式画线段(vtkLine
)。
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>
#include <vtkLine.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());
m_polydata->SetLines(vtkSmartPointer<vtkCellArray>::New());
m_actor->GetProperty()->SetColor(1, 0, 0);
m_actor->GetProperty()->SetPointSize(5);
}
virtual ~MouseInteractorDrawing()
{
}
virtual void OnLeftButtonUp() {
int num = m_polydata->GetNumberOfPoints();
if (num>1) {
vtkCellArray *cells = m_polydata->GetLines();
vtkSmartPointer<vtkLine>line = vtkSmartPointer<vtkLine>::New();
line->GetPointIds()->SetId(0, num - 2);
line->GetPointIds()->SetId(1, num-1);
cells->InsertNextCell(line);
vtkNew<vtkPolyDataMapper>mapper;
mapper->SetInputData(m_polydata);
m_actor->SetMapper(mapper);
}
vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
}
virtual void OnLeftButtonDown()
{
int x = GetInteractor()->GetEventPosition()[0];
int y = GetInteractor()->GetEventPosition()[1];
double position[3] = { 0 };
// Pick from this location.
vtkSmartPointer<vtkPointPicker> picker =
vtkSmartPointer<vtkPointPicker>::New();
picker->SetTolerance(0.001);
picker->Pick(x, y, 0, GetDefaultRenderer());
picker->GetPickPosition(position);
vtkPoints *points = m_polydata->GetPoints();
vtkIdType idx=points->InsertNextPoint(position);
vtkCellArray *cells = m_polydata->GetVerts();
vtkNew<vtkVertex> vertex;
vertex->GetPointIds()->InsertNextId(idx);
cells->InsertNextCell(vertex);
vtkNew<vtkPolyDataMapper>mapper;
mapper->SetInputData(m_polydata);
m_actor->SetMapper(mapper);
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
virtual void OnMoveMove() {
vtkInteractorStyleTrackballCamera::OnMouseMove();
}
virtual void OnRightButtonDown() {
vtkInteractorStyleTrackballCamera::OnRightButtonDown();
}
void Init() {
GetDefaultRenderer()->AddActor(m_actor);
}
private:
vtkSmartPointer<vtkPolyData> m_polydata;
vtkSmartPointer<vtkActor> m_actor;
};
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);
style->Init();
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(DrawLine)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(DrawLine DrawLine.cxx)
TARGET_LINK_LIBRARIES(DrawLine ${VTK_LIBRARIES})