013_BestMPRBaseVtk myVtkInteractorStyleImage交互样式

202 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

头图

BestMPRBaseVtk myVtkInteractorStyleImage交互样式

myVtkInteractorStyleImage继承自vtkInteractorStyleImage,将重写部分鼠标事件和将部分数据传输给外部,形成信号发送出去。


BestMPRBaseVtk myVtkInteractorStyleImage交互样式1 重写鼠标滚轮事件,实现鼠标滚轮切换Slice☞ 源码

关键字: StyleInteractorStyleInteractorvtk交互

1 重写鼠标滚轮事件,实现鼠标滚轮切换Slice

vtkInteractorStyleImage中没有找到对应的接口。vtkInteractorStyleImage继承自vtkInteractorStyleTrackballCamera,在vtkInteractorStyleTrackballCamera中有与鼠标滚轮相关的接口。如下:

   void OnMouseWheelForward() override;
   void OnMouseWheelBackward() override;

官方的代码实现如下,默认鼠标滚轮为放大缩小。

 //----------------------------------------------------------------------------
 void vtkInteractorStyleTrackballCamera::OnMouseWheelForward()
 {
   this->FindPokedRenderer(
     this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
   if (this->CurrentRenderer == nullptr)
   {
     return;
   }
 ​
   this->GrabFocus(this->EventCallbackCommand);
   this->StartDolly();
   double factor = this->MotionFactor * 0.2 * this->MouseWheelMotionFactor;
   this->Dolly(pow(1.1, factor));
   this->EndDolly();
   this->ReleaseFocus();
 }
 ​
 //----------------------------------------------------------------------------
 void vtkInteractorStyleTrackballCamera::OnMouseWheelBackward()
 {
   this->FindPokedRenderer(
     this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
   if (this->CurrentRenderer == nullptr)
   {
     return;
   }
 ​
   this->GrabFocus(this->EventCallbackCommand);
   this->StartDolly();
   double factor = this->MotionFactor * -0.2 * this->MouseWheelMotionFactor;
   this->Dolly(pow(1.1, factor));
   this->EndDolly();
   this->ReleaseFocus();
 }

改写以下:

 /**
  * @brief myVtkInteractorStyleImage::OnMouseWheelForward
  * 鼠标滚轮向前滚动
  * 切换图层显示
  */
 void myVtkInteractorStyleImage::OnMouseWheelForward()
 {
     int maxSlice = this->ImageViewer->getSliceMax();
     int currentSlice = this->ImageViewer->getSlice();
     if(currentSlice < maxSlice)
     {
         currentSlice += 1;
         this->ImageViewer->setSlice(currentSlice);
         this->ImageViewer->render();
         bppWidget->emitSliceChangedSignal(currentSlice);
     }
 }
 /**
  * @brief myVtkInteractorStyleImage::OnMouseWheelBackward
  * 鼠标滚轮向后滚动
  * 切换图层显示
  */
 void myVtkInteractorStyleImage::OnMouseWheelBackward()
 {
     int minSlice = this->ImageViewer->getSliceMin();
     int currentSlice = this->ImageViewer->getSlice();
     if(currentSlice > minSlice)
     {
         currentSlice -= 1;
         this->ImageViewer->setSlice(currentSlice);
         this->ImageViewer->render();
         bppWidget->emitSliceChangedSignal(currentSlice);
     }
 }

其他的事件也稍作了修改,可以看源码内容。

☞ 源码

源码链接:GitHub仓库自取

使用方法:☟☟☟

源码


博客签名2021