1.vtkSelectedNode
也可以用于鼠标选择部分,更精确,比画矩形选取;
2.实现方案
CellPickerInteractorStyle类派生于vtkInteraTrackballCamera,并通过重载该函数类的OnLeftButtonDown()函数来处理鼠标左键消息。PolyData:被拾取模型数据,需要通过外部设置。在响应鼠标左键消息时,首先定义vtkCellPicker对象,使用Pick()函数实现拾取。拾取完毕后,即可通过GetCellId()函数来得到当前拾取的单元索取号; 可以实现单元格选择;
这里就涉及了vtkPolyData的局部数据提取功能。实现该功能时使用了几个新的类。
- vtkIdTypeArray:对象存储当前选中的单位的索引号,每次只选取一个单元,因此每次该对象仅有一个索引号;
- vtkSelectedNode对象与vtkSelection对象通常搭配使用,vtkSelection实际上是一个vtkSelectionNode的数组,而vtkSelectionNode则声明了要提取的数据的类型,这里SetFiledType()设置数据的类型为单元,SeiContentType()设置数据的内容为索引号。
- vtkExtractSelection:实现了数据提取功能,其第一个输入为被提取的vtkPolyData,第二个输入为vtkSelection对象,标记要提取的数据类型。
提取完毕,即可将提取的结果保存至一个vtkActor对象,并添加至当前的vtkRenderer中显示。
3.效果
4 code
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkCellPicker.h>
#include <vtkSelectionNode.h>
#include <vtkSelection.h>
#include <vtkRendererCollection.h>
#include <vtkExtractSelection.h>
#include <vtkObjectFactory.h>
#include <vtkNamedColors.h>
#include <vtkUnstructuredGrid.h>
#include <vtkNamedColors.h>
// Catch mouse events
class MouseInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorStyle* New();
MouseInteractorStyle()
{
selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();
selectedActor = vtkSmartPointer<vtkActor>::New();
}
virtual void OnLeftButtonDown() override
{
vtkNew<vtkNamedColors> colors;
// Get the location of the click (in window coordinates)
int* pos = this->GetInteractor()->GetEventPosition();
vtkNew<vtkCellPicker> picker;
picker->SetTolerance(0.0005);
// Pick from this location.
picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
double* worldPosition = picker->GetPickPosition();
std::cout << "Cell id is: " << picker->GetCellId() << std::endl;
if (picker->GetCellId() != -1)
{
std::cout << "Pick position is: (" << worldPosition[0] << ", "
<< worldPosition[1] << ", " << worldPosition[2] << ")" << endl;
vtkNew<vtkIdTypeArray> ids;
ids->SetNumberOfComponents(1);
ids->InsertNextValue(picker->GetCellId());
vtkNew<vtkSelectionNode> selectionNode;
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
vtkNew<vtkSelection> selection;
selection->AddNode(selectionNode);
vtkNew<vtkExtractSelection> extractSelection;
extractSelection->SetInputData(0, this->Data);
extractSelection->SetInputData(1, selection);
extractSelection->Update();
// In selection
vtkNew<vtkUnstructuredGrid> selected;
selected->ShallowCopy(extractSelection->GetOutput());
std::cout << "Number of points in the selection: "
<< selected->GetNumberOfPoints() << std::endl;
std::cout << "Number of cells in the selection : "
<< selected->GetNumberOfCells() << std::endl;
selectedMapper->SetInputData(selected);
selectedActor->SetMapper(selectedMapper);
selectedActor->GetProperty()->EdgeVisibilityOn();
selectedActor->GetProperty()->SetColor(
colors->GetColor3d("Tomato").GetData());
selectedActor->GetProperty()->SetLineWidth(3);
this->Interactor->GetRenderWindow()
->GetRenderers()
->GetFirstRenderer()
->AddActor(selectedActor);
}
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
vtkSmartPointer<vtkPolyData> Data;
vtkSmartPointer<vtkDataSetMapper> selectedMapper;
vtkSmartPointer<vtkActor> selectedActor;
};
vtkStandardNewMacro(MouseInteractorStyle);
//vtkSmartPointer <vtkPolyData> ReadStl(char *fileName)
//{
// vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
// reader->SetFileName(fileName);
// reader->Update();
// vtkSmartPointer <vtkPolyData> poly = reader->GetOutput();
// return poly;
//}
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
//vtkNew<vtkPlaneSource> planeSource;
//planeSource->Update();
//vtkSmartPointer<vtkXMLPolyDataReader> reader =
// vtkSmartPointer<vtkXMLPolyDataReader>::New();
//reader->SetFileName("./KneeCTFemurSeg.vtp");
//reader->Update();
auto polyjg = ReadStl("./soft-Femur.stl");
//vtkNew<vtkTriangleFilter> triangleFilter;
triangleFilter->SetInputData
//triangleFilter->SetInputData(reader->GetOutput());
//triangleFilter->Update();
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->GetProperty()->SetColor(colors->GetColor3d("SeaGreen").GetData());
actor->SetMapper(mapper);
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("CellPicking");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
// Set the custom stype to use for interaction.
vtkNew<MouseInteractorStyle> style;
style->SetDefaultRenderer(renderer);
style->Data = sphereSource->GetOutput();
renderWindowInteractor->SetInteractorStyle(style);
renderer->AddActor(actor);
renderer->ResetCamera();
renderer->SetBackground(colors->GetColor3d("PaleTurquoise").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}