鼠标交互事件:点两个点形成一条线

通过鼠标点两个点,同时标记这两个点,形成一条线。(只有一条线,并不是每次点两个点都会新出现一条线)

这个版本是个初级版本,(后面我也实现了任意拖动一个点,直线会相应发生变化,以及两点在Z轴上的变动,线也会同步更新)。代码太多所以我也没有贴上来,有需要了我会发出来!

double point1[3]{ 0 };double point2[3]{ 0 };double point3[3]{ 0 };double p[3]{ 0 };
double WorldPointbegin[3]{ 0 };double WorldPointend[3]{ 0 };
int *clickPosbegin;int *clickPosend;int *click;
class customMouseInteractorStyle : public vtkInteractorStyleTrackballCamera{
public:static customMouseInteractorStyle* New();vtkTypeMacro(customMouseInteractorStyle, vtkInteractorStyleTrackballCamera);
protected:;
public:vtkPolyDataMapper *lineMapper = vtkPolyDataMapper::New();vtkSmartPointer<vtkActor> lineActor=vtkSmartPointer<vtkActor>::New();vtkSmartPointer<vtkLineSource> lineSource= vtkSmartPointer<vtkLineSource>::New();virtual void OnLeftButtonDown(){if(i==0){//打印鼠标左键像素位置clickPosbegin = this->Interactor->GetEventPosition();point1[0]=clickPosbegin[0];point1[1]=clickPosbegin[1];std::cout << "clickPosbegin: " << this->Interactor->GetEventPosition()[0]<< " " << this->Interactor->GetEventPosition()[1] << std::endl;//注册拾取点函数this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],this->Interactor->GetEventPosition()[1], 0,  // always zero.this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());//打印拾取点空间位置this->Interactor->GetPicker()->GetPickPosition(WorldPointbegin);std::cout << "WorldPointbegin :" << WorldPointbegin[0] << "," << WorldPointbegin[1] << "," << WorldPointbegin[2] << std::endl;//对拾取点进行标记sphereSourcebegin->Update();mapperbegin->SetInputConnection(sphereSourcebegin->GetOutputPort());actorbegin->SetMapper(mapperbegin);actorbegin->SetPosition(WorldPointbegin);actorbegin->SetScale(4);actorbegin->GetProperty()->SetColor(1.0, 0.0, 1.0);this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actorbegin);renderer->GetRenderWindow()->GetInteractor()->Initialize();renderer->GetRenderWindow()->GetInteractor()->Render();i=1;}else if(i==1){//打印鼠标左键像素位置clickPosend = this->Interactor->GetEventPosition();point2[0]=clickPosend[0];point2[1]=clickPosend[1];std::cout << "clickPosend: " << this->Interactor->GetEventPosition()[0]<< " " << this->Interactor->GetEventPosition()[1] << std::endl;//注册拾取点函数this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],this->Interactor->GetEventPosition()[1], 0,  // always zero.this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());//打印拾取点空间位置this->Interactor->GetPicker()->GetPickPosition(WorldPointend);std::cout << "WorldPointend : " << WorldPointend[0] << " " << WorldPointend[1] << " " << WorldPointend[2] << std::endl;//对拾取点进行标记sphereSourcend->Update();mapperend->SetInputConnection(sphereSourcend->GetOutputPort());actorend->SetMapper(mapperend);actorend->SetPosition(WorldPointend);actorend->SetScale(4);actorend->GetProperty()->SetColor(0.0, 1.0, 1.0);Point_in_Tube_function(pointsPolydata);lineSource->SetPoint1(WorldPointbegin);lineSource->SetPoint2(WorldPointend);//两点确定一条直线lineSource->Update();lineMapper->SetInputConnection(lineSource->GetOutputPort());lineMapper->ScalarVisibilityOff();lineActor->SetMapper(lineMapper);lineActor->GetProperty()->SetColor(0, 0, 0);//线的颜色设置lineActor->GetProperty()->SetOpacity(1);//0是透明/***这里可以添加图像与直线的交互过程**/this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actorend);this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(lineActor);i=0;renderer->GetRenderWindow()->GetInteractor()->Initialize();renderer->GetRenderWindow()->GetInteractor()->Render();}}{ vtkInteractorStyleTrackballCamera::OnLeftButtonUp();}};
vtkStandardNewMacro(customMouseInteractorStyle);

更新:这个是鼠标拖动begin 和end 中的任意一点,直线随即更新。可以看到其中有很多代码是重复的,我在实际用的时候重复的做成函数调用,因为放这函数多了显得乱,我就直接贴上来了。如果有问题,可以及时沟通!

其中:point1和point2始终是 begin和end 的鼠标点的二维坐标(后面转化为3维),通过判断point3与她们两个的位置关系,判断当前操作的点。

    virtual void OnMiddleButtonDown(){click = this->GetInteractor()->GetEventPosition();point3[0] = click[0];point3[1] = click[1];if(abs(point3[0]-point1[0])<50 && abs(point3[1]-point1[1])<50)beginOrend = 1;else if (abs(point3[0]-point2[0])<50 && abs(point3[1]-point2[1])<50)beginOrend = 2;elsebeginOrend = 0; }virtual void OnMiddleButtonUp(){if(beginOrend == 1){ //操作的是第一个点clickPosbegin = this->Interactor->GetEventPosition();point1[0]=clickPosbegin[0];point1[1]=clickPosbegin[1];std::cout << "clickPosbegin: " << this->Interactor->GetEventPosition()[0]<< " " << this->Interactor->GetEventPosition()[1] << std::endl;//注册拾取点函数this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],this->Interactor->GetEventPosition()[1], 0,  // always zero.this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());//打印拾取点空间位置this->Interactor->GetPicker()->GetPickPosition(WorldPointbegin);std::cout << "WorldPointbegin :" << WorldPointbegin[0] << "," << WorldPointbegin[1] << "," << WorldPointbegin[2] << std::endl;//对拾取点进行标记sphereSourcebegin->Update();mapperbegin->SetInputConnection(sphereSourcebegin->GetOutputPort());actorbegin->SetMapper(mapperbegin);actorbegin->SetPosition(WorldPointbegin);actorbegin->SetScale(4);actorbegin->GetProperty()->SetColor(1.0, 0.0, 1.0);lineSource->SetPoint1(WorldPointbegin);lineSource->SetPoint2(WorldPointend);lineSource->Update();lineMapper->SetInputConnection(lineSource->GetOutputPort());lineMapper->ScalarVisibilityOff();lineActor->SetMapper(lineMapper);lineActor->GetProperty()->SetColor(0, 0, 0);lineActor->GetProperty()->SetOpacity(1);this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actorbegin);this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(lineActor);renderer->GetRenderWindow()->GetInteractor()->Initialize();renderer->GetRenderWindow()->GetInteractor()->Render();}else if (beginOrend == 2) //操作的是第二个点{//打印鼠标左键像素位置clickPosend = this->Interactor->GetEventPosition();point2[0]=clickPosend[0];point2[1]=clickPosend[1];std::cout << "clickPosend: " << this->Interactor->GetEventPosition()[0]<< " " << this->Interactor->GetEventPosition()[1] << std::endl;//注册拾取点函数this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],this->Interactor->GetEventPosition()[1], 0,  // always zero.this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());//打印拾取点空间位置this->Interactor->GetPicker()->GetPickPosition(WorldPointend);std::cout << "WorldPointend : " << WorldPointend[0] << " " << WorldPointend[1] << " " << WorldPointend[2] << std::endl;//对拾取点进行标记sphereSourcend->Update();mapperend->SetInputConnection(sphereSourcend->GetOutputPort());actorend->SetMapper(mapperend);actorend->SetPosition(WorldPointend);actorend->SetScale(4);actorend->GetProperty()->SetColor(0.0, 1.0, 1.0);Point_in_Tube_function(pointsPolydata);lineSource->SetPoint1(WorldPointbegin);lineSource->SetPoint2(WorldPointend);//两点确定一条直线lineSource->Update();lineMapper->SetInputConnection(lineSource->GetOutputPort());lineMapper->ScalarVisibilityOff();lineActor->SetMapper(lineMapper);lineActor->GetProperty()->SetColor(0, 0, 0);//线的颜色设置lineActor->GetProperty()->SetOpacity(1);//0是透明this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actorend);this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(lineActor);renderer->GetRenderWindow()->GetInteractor()->Initialize();renderer->GetRenderWindow()->GetInteractor()->Render();}else {beginOrend = 0;}}

VTK 鼠标画线(点移动线可同步更新)相关推荐

  1. IOS之Label画一条删除线

    IOS之Label画一条删除线 例如上面的价格100 如何对100添加删除线. 我们需要自定义一个Label,继承于UILabel系统类.在xib上对控件添加自定义Label类.LJCenterLab ...

  2. canvas 画一条波浪线 进度条

    前言: 记得刚接触Web的时候, 老板需要画一个有波浪线的进度球: 那个时候无赖不熟悉canvas; 所以这个功能没有满足; 今天刚好有时间来满足下心愿 第一步 我们先实现一个波浪  这里核心就用到了 ...

  3. 【GraphVisual】画节点与线以及移动节点线随着移动

    画节点和线似乎是这个程序最基础的部分,看一下效果图: 节点目前是从左边拖出来的图片,然后组成一个QGraphicsPixmapItem,线是按下键盘i键后,光标切换为十字,在两个节点处连线.关于画节点 ...

  4. vsto画箱体图 箱线图 xlboxwhisker

    vsto画箱体图 箱线图 xlboxwhisker using Excel=Microsoft.Office.Interop.Excel; Excel.Chart lChart = null; //1 ...

  5. 使用ggplot2画 点图、箱线图、小提琴图、蜂窝图、云雨图

    使用ggplot2画 点图.箱线图.小提琴图.蜂窝图.云雨图 加载包 library(tidyverse) library(cowplot) library(ggrepel) library(ggsc ...

  6. LISP道路中线_如何画两条样条线间中心线之三:CAD插件/Lisp编程法

    前面有介绍过两种CAD画两条样条线的中心线的方法,分别是用三维曲面提取中心线和画辅助取中心线,这两种方法.其实还有更简单的方法,就是用插件,感兴趣的可以自己找有相关功能的插件来使用. 这里提供一个. ...

  7. MATLAB 画矢量场和轨线(可用来确定微分方程平衡点)

    MATLAB 画矢量场和轨线(可用来确定微分方程平衡点) 本方法可用于确定二元一阶微分方程平衡点 MATLAB 画矢量场和轨线 MATLAB 画矢量场和轨线(可用来确定微分方程平衡点) 确定参数 绘图 ...

  8. lisp语言画阿基米德线_120种UG表达式曲线画法(阿基米德螺旋线、数学方程式)...

    在UG中利用[规律曲线]|[根据方程]绘制各种方程曲线: 1.极坐标(或柱坐标r,θ,z)与直角坐标系(x,y,z)的转换关系: x=r*cos(θ):y=r*sin(θ):z=z 2.球坐标系(r, ...

  9. python画资本市场线_资本市场线

    资本市场线(Capital Market Line,简称CML) [编辑] 什么是资本市场线 资本市场线是指表明有效组合的期望收益率和标准差之间的一种简单的线性关系的一条射线.它是沿着投资组合的有效边 ...

最新文章

  1. 国产Linux发行版再添一员,操作界面不输苹果!
  2. 应用程序基础知识:activity和intent——Android开发秘籍
  3. excel处置4000行数据卡_【Excel技巧】在Excel中奇偶行提取和奇偶行筛选方法
  4. 利用cache特性检测Android模拟器
  5. 你可以不知道原因,但是,我们不能停止努力。httplook抓取路由配置界面信息...
  6. php mysql 查询数据出现连接重置_php使用mysql和mysqli连接查询数据
  7. LeetCode 473. 火柴拼正方形
  8. stm32中.bss和.data段是在哪里初始化的
  9. SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别 及动态查询中的标识符函数QUOTENAME
  10. 棋盘问题(深度搜索)
  11. 深度系统文件服务器,深度系统镜像文件
  12. abb机器人离线编程软件叫做_滨州abb机器人离线编程软件
  13. 38、nginx的upstream目前支持的5种方式的分配
  14. 30岁以前该做什么,30岁以后该做什么!!很有道理
  15. linux 系统基本设置
  16. pcb 受潮_硬盘SATA接口断裂及PCB板受潮_希捷 Barracuda 3TB 7200转 64MB_固态硬盘评测-中关村在线...
  17. 打造高安全数字基础设施:中国电子云服务关键行业的宣言
  18. 如何对项目的研发团队年终绩效考核?
  19. Oracle VM VirtualBox上配置新的Ubuntu20.04
  20. WiFi、蓝牙以及双WiFi流程

热门文章

  1. 放大电路的分析方法(以共射放大电路为例、交流通路、直流通路、三极管等效电路及其如何等效的)
  2. ASP.NET中的配置文件
  3. 有道打赏视频/支付已对接/自带资源
  4. STM32程序烧录---TTL
  5. 2023最新连锁店软件排名,国内十大连锁店管理软件新鲜出炉!
  6. TC275——05ASCLIN-UART
  7. pixhawk开发环境linux,[Pixhawk/PX4]开发环境搭建(Ubuntu) 遇到一些问题
  8. 程序员技术开发委托合同模板 私活模板
  9. 阿里开源组件库--ant design of vue的安装和使用
  10. mysql占用服务器cpu过高的原因以及解决办法