12. VTK上选取点(VTK7版本+VTK9版本)

这个专栏是用于记录我在学习VTK过程中的一些心得体会。参考的资料主要有以下三个:

1. 张晓东 罗火灵《VTK图形图像开发进阶》
2. https://examples.vtk.org/site/
3. 沈子恒 《VTK 三维数据渲染进阶》

遇到的一个大问题就是由于版本更新,这些资料中很多代码无法正常运行,需要进行一定的修改,所以这个专栏会记录下来我修改后的程序代码,以便于我之后温习。也希望能给和我有同样困扰的小伙伴们一些帮助。

我使用的版本:VTK9 + VS2022

VTK上选取点、选取区域这两个例子我主要是参考了官网的程序代码examples.vtk.org/site/Cxx/Picking/CellPicking/  。

这个在我正在做的医学图像项目VS2022联合Qt5开发学习8(QT5.12.3联合VTK7在VS2022上开发医学图像项目3——医学图像可视化)-CSDN博客上有用到,所以在这里记录一下。

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);#include <vtkActor.h>
#include <vtkCellPicker.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractSelection.h>
#include <vtkIdTypeArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkSmartPointer.h>
#include <vtkTriangleFilter.h>
#include <vtkUnstructuredGrid.h>namespace {// 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 selectionvtkNew<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);} // namespaceint main(int, char* [])
{vtkNew<vtkNamedColors> colors;vtkNew<vtkPlaneSource> planeSource;planeSource->Update();vtkNew<vtkTriangleFilter> triangleFilter;triangleFilter->SetInputConnection(planeSource->GetOutputPort());triangleFilter->Update();vtkNew<vtkPolyDataMapper> mapper;mapper->SetInputConnection(triangleFilter->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 = triangleFilter->GetOutput();renderWindowInteractor->SetInteractorStyle(style);renderer->AddActor(actor);renderer->ResetCamera();renderer->SetBackground(colors->GetColor3d("PaleTurquoise").GetData());renderWindow->Render();renderWindowInteractor->Start();return EXIT_SUCCESS;
}

运行结果

VTK上选取点

因为我的医学图像项目VS2022联合Qt5开发学习8(QT5.12.3联合VTK7在VS2022上开发医学图像项目3——医学图像可视化)-CSDN博客用的是VTK7,所以我又修改了一下代码,让它能在VTK7下正常运行。修改的主要地方是用 .GetPointer() 方法获取指向对象的指针,并将其传递给需要原始指针的函数或方法。

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);#include <vtkActor.h>
#include <vtkCellPicker.h>
#include <vtkDataSetMapper.h>
#include <vtkExtractSelection.h>
#include <vtkIdTypeArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkSmartPointer.h>
#include <vtkTriangleFilter.h>
#include <vtkUnstructuredGrid.h>namespace {// 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.GetPointer()); // 使用 .GetPointer() 获取指针vtkNew<vtkSelection> selection;selection->AddNode(selectionNode.GetPointer());vtkNew<vtkExtractSelection> extractSelection;extractSelection->SetInputData(0, this->Data.GetPointer());extractSelection->SetInputData(1, selection.GetPointer());extractSelection->Update();// In selectionvtkNew<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.GetPointer());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);} // namespaceint main(int, char* [])
{vtkNew<vtkNamedColors> colors;vtkNew<vtkPlaneSource> planeSource;planeSource->Update();vtkNew<vtkTriangleFilter> triangleFilter;triangleFilter->SetInputConnection(planeSource->GetOutputPort());triangleFilter->Update();vtkNew<vtkPolyDataMapper> mapper;mapper->SetInputConnection(triangleFilter->GetOutputPort());vtkMapper* mapperRawPtr = mapper.GetPointer();  // 获取指针vtkNew<vtkActor> actor;vtkProp* actorRawPtr = actor.GetPointer();actor->GetProperty()->SetColor(colors->GetColor3d("SeaGreen").GetData());actor->SetMapper(mapperRawPtr);vtkNew<vtkRenderer> renderer;vtkRenderer* rendererRawPtr = renderer.GetPointer();  // 获取指针vtkNew<vtkRenderWindow> renderWindow;renderWindow->AddRenderer(rendererRawPtr);renderWindow->SetWindowName("CellPicking");vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;vtkRenderWindow* renderWindowRawPtr = renderWindow.GetPointer();renderWindowInteractor->SetRenderWindow(renderWindowRawPtr);renderWindowInteractor->Initialize();// Set the custom stype to use for interaction.vtkNew<MouseInteractorStyle> style;vtkInteractorObserver* styleRawPtr = style.GetPointer();style->SetDefaultRenderer(rendererRawPtr);style->Data = triangleFilter->GetOutput();renderWindowInteractor->SetInteractorStyle(styleRawPtr);renderer->AddActor(actorRawPtr);renderer->ResetCamera();renderer->SetBackground(colors->GetColor3d("PaleTurquoise").GetData());renderWindow->Render();renderWindowInteractor->Start();return EXIT_SUCCESS;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/612427.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

“Tab“ 的新型可穿戴人工智能项链

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

少儿编程 2023年12月中国电子学会图形化编程等级考试Scratch编程三级真题解析(判断题)

2023年12月scratch编程等级考试三级真题 判断题 19、下列两段程序的运行效果相同 答案:对 考点分析:考查积木综合使用,重点考查循环积木的使用;左边属于有条件的循环,由变量的值控制,当变量值大于50时,循环停止,而变量始终为零,不满足条件,所以一直循环,和右边的…

DOM操作怎样添加、移除、移动、复制、创建和查找节点

DOM&#xff08;Document Object Model&#xff09;操作是JavaScript中用于操作HTML或XML文档的一种方式。下面是一些基本的DOM操作&#xff1a; 添加节点 使用appendChild()方法可以将一个节点添加到现有节点的子节点列表的末尾。 let newNode document.createElement(“p”…

Go语言的sync.Pool如何使用?使用场景具体有哪些?

sync.Pool 是 Go 标准库中提供的一个对象池&#xff08;Object Pool&#xff09;的实现。对象池是一种用于缓存和复用对象的机制&#xff0c;可以在一定程度上减轻内存分配的开销。sync.Pool 专门用于管理临时对象&#xff0c;适用于一些需要频繁创建和销毁的短暂对象&#xff…

Golang 泛型

前言 泛型是在Go 1.18版本中引入的&#xff0c;它允许编写可以在多种数据类型上工作的函数和数据类型。这样做可以增加代码的复用性并减少重复 使用 类型参数&#xff08;Type Parameters&#xff09;&#xff1a; 你可以在函数或类型定义上声明类型参数&#xff0c;使其具有…

白嫖aws创建Joplin server服务器

网上有很多的Joplin服务器的搭建教程&#xff0c;但是基本都是抄来抄去&#xff0c;对初学者实在是太不友好了。 话不多说&#xff0c;说干就干&#xff0c;自己从头找资料搭了一个&#xff0c;这可能是全网最好的Joplin服务器搭建教程了。 aws服务器 aws的服务器还是很香的&…

企业用WhatsApp营销的好处有哪些?

1.建立良好的客户关系 WhatsApp是全球用户喜爱的即时通信软件&#xff0c;使用WhatsApp与客户沟通&#xff0c;可拉进企业和客户双方的距离。使用WhatsApp会话和消息推送功能&#xff0c;企业和用户可实时开展消息对话&#xff0c;及时解决客户咨询与疑虑&#xff0c;构建便捷…

移动通信原理与关键技术学习之信道编解码(5)

先回顾调制的过程&#xff1a;调制就是对信号源的信息进行处理加到载波上&#xff0c;使其变为适合于信道传输的形式的过程&#xff0c;就是使载波随信号而改变的技术。 1.什么是IQ调制&#xff1f; 答&#xff1a;将数据分为两路&#xff0c;分别进行载波调制&#xff0c;两…

PIG框架学习2——资源服务器的配置详解

一、前言 1、pig资源服务器的配置 Spring Security oauth2相关的依赖是在pigx-common-security模块中引入的&#xff0c;其他模块需要进行token鉴权的&#xff0c;需要在微服务中引入pigx-common-security模块的依赖&#xff0c;从而间接引入相关的Spring security oauth2依赖…

39 C++ 模版中的参数如果 是 vector,list等集合类型如何处理呢?

在前面写的例子中&#xff0c;模版参数一般都是 int&#xff0c;或者一个类Teacher&#xff0c;假设我们现在有个需求&#xff1a;模版的参数要是vector&#xff0c;list这种结合类型应该怎么写呢&#xff1f; //当模版中的类型是 vector &#xff0c;list 等集合类型的时候的处…

【LeetCode】27. 移除元素(简单)——代码随想录算法训练营第1天

题目描述 给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素&#xff0c;并返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 元素的顺序可以改变。你不需要考虑数组中超出…

R语言【base】——sample():随机取样和排列

Package base version 4.2.0 Description sample() 使用替换或不替换从参数【x】的元素中获取指定大小的样本。 Usage sample(x, size, replace FALSE, prob NULL)sample.int(n, size n, replace FALSE, prob NULL,useHash (n > 1e07 && !replace &&…

20240110在ubuntu20.04下重启samba服务

20240110在ubuntu20.04下重启samba服务 百度搜索&#xff1a;samba restart https://www.python100.com/html/78028.html 重启samba命令详解 更新&#xff1a;2023-05-17 16:04 一、重启samba命令 重启samba可以使用以下命令&#xff1a; /etc/init.d/smb restart 或者 syste…

中国智造闪耀CES | 木牛科技在美国CES展亮相多领域毫米波雷达尖端方案

素有全球科技潮流“风向标”之称的2024国际消费类电子产品展&#xff08;CES&#xff09;&#xff0c;于1月9-12日在美国拉斯维加斯会议中心举办。CES是全球最大的消费电子和消费技术展览会之一&#xff0c;汇集了世界各地优秀的消费电子和科技公司&#xff0c;带着最好的产品来…

uniapp项目怎么删除顶部导航栏

uniapp去掉顶部导航的方法&#xff1a; 1、去掉所有导航栏 "globalStyle": { "navigationBarTextStyle": "white", "navigationBarTitleText": "uni-app", "navigationBarBackgroundColor": "#007AFF"…

Apache ActiveMQ RCE CNVD-2023-69477 CVE-2023-46604

漏洞简介 Apache ActiveMQ官方发布新版本&#xff0c;修复了一个远程代码执行漏洞&#xff0c;攻击者可构造恶意请求通过Apache ActiveMQ的61616端口发送恶意数据导致远程代码执行&#xff0c;从而完全控制Apache ActiveMQ服务器。 影响版本 Apache ActiveMQ 5.18.0 before 5.1…

Windows系统下python版本Open3D-0.18.0 的快速安装与使用

目录 一、安装Anaconda3二、安装open3d三、测试代码四、结果展示五、测试数据 Windows系统下python版本Open3D-0.18.0 的快速安装与使用由CSDN点云侠原创&#xff0c;爬虫自重。如果你不是在点云侠的博客中看到该文章&#xff0c;那么此处便是不要脸的爬虫。 一、安装Anaconda…

Hive基础知识(八):Hive对数据库的增删改查操作

1. 创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment]#注释 [LOCATION hdfs_path]#指定当前库的hdfs目录 [WITH DBPROPERTIES (property_nameproperty_value,...)]; #备注创建作者和创建时间 1&#xff09;创建一个数据库&#xff0c;数据…

【机器学习300问】2、机器学习分为哪几类?

一、监督学习 监督学习&#xff08;Supervised Learning&#xff09;是机器学习和人工智能中的一种算法学习训练方式。它利用有标签的数据&#xff08;通常称为训练数据&#xff09;作为输入&#xff0c;训练一个模型来学习输入和输出之间的关系。模型学习后可以用于预测新的、…

Linux 常用进阶指令

我是南城余&#xff01;阿里云开发者平台专家博士证书获得者&#xff01; 欢迎关注我的博客&#xff01;一同成长&#xff01; 一名从事运维开发的worker&#xff0c;记录分享学习。 专注于AI&#xff0c;运维开发&#xff0c;windows Linux 系统领域的分享&#xff01; 其他…