VTK鼠标选点
来源:互联网 发布:linux连接数据库命令 编辑:程序博客网 时间:2024/04/30 14:35
打开点云后用鼠标选点并输出选中点信息:
#include <vtkVersion.h>#include <vtkSmartPointer.h>#include <vtkRendererCollection.h>#include <vtkDataSetMapper.h>#include <vtkUnstructuredGrid.h>#include <vtkIdTypeArray.h>#include <vtkTriangleFilter.h>#include <vtkPolyDataMapper.h>#include <vtkActor.h>#include <vtkCommand.h>#include <vtkRenderWindow.h>#include <vtkRenderer.h>#include <vtkRenderWindowInteractor.h>#include <vtkPolyData.h>#include <vtkPoints.h>#include <vtkCellArray.h>#include <vtkPlaneSource.h>#include <vtkCellPicker.h>#include <vtkInteractorStyleTrackballCamera.h>#include <vtkProperty.h>#include <vtkSelectionNode.h>#include <vtkSelection.h>#include <vtkExtractSelection.h>#include <vtkObjectFactory.h>#include <vtkPointSource.h>#include <vtkDoubleArray.h>#include <vtkPoints.h>#include <vtkPointData.h>//#include "vtkHeadFile.h"// Catch mouse eventsclass MouseInteractorStyle : public vtkInteractorStyleTrackballCamera{public:static MouseInteractorStyle* New();MouseInteractorStyle(){selectedMapper = vtkSmartPointer<vtkDataSetMapper>::New();selectedActor = vtkSmartPointer<vtkActor>::New();}virtual void OnLeftButtonDown(){// Get the location of the click (in window coordinates)int* pos = this->GetInteractor()->GetEventPosition();vtkSmartPointer<vtkCellPicker> picker =vtkSmartPointer<vtkCellPicker>::New();picker->SetTolerance(/*0.0005*/ 0.001);// 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;vtkSmartPointer<vtkIdTypeArray> ids =vtkSmartPointer<vtkIdTypeArray>::New();ids->SetNumberOfComponents(1);ids->InsertNextValue(picker->GetCellId());vtkSmartPointer<vtkSelectionNode> selectionNode =vtkSmartPointer<vtkSelectionNode>::New();selectionNode->SetFieldType(vtkSelectionNode::CELL);selectionNode->SetContentType(vtkSelectionNode::INDICES);selectionNode->SetSelectionList(ids);vtkSmartPointer<vtkSelection> selection =vtkSmartPointer<vtkSelection>::New();selection->AddNode(selectionNode);vtkSmartPointer<vtkExtractSelection> extractSelection =vtkSmartPointer<vtkExtractSelection>::New();#if VTK_MAJOR_VERSION <= 5extractSelection->SetInput(0, this->Data);extractSelection->SetInput(1, selection);#elseextractSelection->SetInputData(0, this->Data);extractSelection->SetInputData(1, selection);#endifextractSelection->Update();// In selectionvtkSmartPointer<vtkUnstructuredGrid> selected =vtkSmartPointer<vtkUnstructuredGrid>::New();selected->ShallowCopy(extractSelection->GetOutput());std::cout << "There are " << selected->GetNumberOfPoints()<< " points in the selection." << std::endl;std::cout << "There are " << selected->GetNumberOfCells()<< " cells in the selection." << std::endl;#if VTK_MAJOR_VERSION <= 5selectedMapper->SetInputConnection(selected->GetProducerPort());#elseselectedMapper->SetInputData(selected);#endifselectedActor->SetMapper(selectedMapper);selectedActor->GetProperty()->EdgeVisibilityOn();selectedActor->GetProperty()->SetEdgeColor(1,0,0);selectedActor->GetProperty()->SetLineWidth(3);selectedActor->GetProperty()->SetPointSize(10);this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(selectedActor);}// Forward eventsvtkInteractorStyleTrackballCamera::OnLeftButtonDown();}vtkSmartPointer<vtkPolyData> Data;vtkSmartPointer<vtkDataSetMapper> selectedMapper;vtkSmartPointer<vtkActor> selectedActor;};vtkStandardNewMacro(MouseInteractorStyle);int main (int, char *[]){vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); vtkSmartPointer<vtkDoubleArray> scalar = vtkSmartPointer<vtkDoubleArray>::New();//用高程值(Z坐标值)填充,用于显示 //读取文件,将点云存储在vtkPoints对象中,并用高程值作为属性信息存储在vtkDoubleArray对象中 FILE * fRead; fRead = fopen("d:\\03.txt","r"); double pt[3]; int n=0; while (!feof(fRead)) { fscanf(fRead,"%lf %lf %lf",pt,pt+1,pt+2); points->InsertPoint(n,pt[0],pt[1],pt[2]); scalar->InsertNextTuple1(pt[2]); n++; } vtkSmartPointer<vtkCellArray> polyvertex = vtkSmartPointer<vtkCellArray>::New();//vtkCell的具体实现 polyvertex->SetNumberOfCells(n);//设置ID个数 int i = 0; for(i=0;i<n;i++)//建立拓扑关系 { vtkIdType Cell[1] = {i}; polyvertex->InsertNextCell(1,Cell);//第一个参数:cell由一个点组成;第二个参数:组成cell的pointID } vtkSmartPointer<vtkPolyData > grid = vtkSmartPointer<vtkPolyData>::New(); grid->SetPoints(points); grid->SetVerts(polyvertex);//设置建立vertex的cell array grid->GetPointData()->SetScalars(scalar); double p[3];grid->GetPoint(384456,p);std::cout << "Point 384456"<< " : (" << p[0] << " " << p[1] << " " << p[2] << ")" << std::endl;vtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();//mapper->SetInputConnection(planeSource->GetOutputPort());mapper->SetInput(grid);mapper->SetScalarRange(-2,20);vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();actor->GetProperty()->SetColor(0,1,0); //greenactor->SetMapper(mapper);vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->SetRenderWindow(renderWindow);renderWindowInteractor->Initialize();// Set the custom stype to use for interaction.vtkSmartPointer<MouseInteractorStyle> style =vtkSmartPointer<MouseInteractorStyle>::New();style->SetDefaultRenderer(renderer);style->Data = grid;renderWindowInteractor->SetInteractorStyle(style);renderer->AddActor(actor);renderer->ResetCamera();renderer->SetBackground(0,0,0); // BlackrenderWindow->Render();renderWindowInteractor->Start();return EXIT_SUCCESS;}
0 0
- VTK鼠标选点
- VTK鼠标交互方式
- vtk 鼠标操作
- vtk 各种不同的鼠标交互方式
- js百度地图-鼠标选点连线,最终生成多边形覆盖物
- VTK
- VTK
- vtk
- vtk
- vtk
- VTK
- 在vs环境中用c# + vtk怎么做鼠标交互
- 选点问题
- 区间选点
- NYOJ891 选点
- 贪心算法-区间选点
- 贪心之区间选点
- 区间选点问题【贪心】
- Python Linux下安装python 软件包
- [Java]collection类集中的Set接口
- Java Web 中使用ffmpeg实现视频转码、视频截图
- HDU_2448_Mining Station on the Sea(最短路 + 最小费用流)
- POJ 1836 Alignment (简单DP)
- VTK鼠标选点
- Intellij Idea 将java项目打包成jar
- UESTC 92 Journey(LCA或树剖)
- 快速排序
- POJ2777 Count Color 线段树区间更新
- 匿名
- FOJ有奖月赛-2015年10月(没有补完)
- leetcode 102 Binary Tree Level Order Traversal(难易度:Easy)
- Fatal signal 11问题的解决方法