医站点医维基

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3043|回复: 9

[VTK] VTK例子24-vtkCellPicker

[复制链接]

336

主题

410

回帖

2806

积分

管理员

积分
2806

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2015-12-6 12:33:41 | 显示全部楼层 |阅读模式
本帖最后由 medimage 于 2015-12-6 12:46 编辑

vtkCellPicker
vtkObject与vtkCommand共同完成消息的相应。一旦vtkObject的某一事件触发,
vtkCommand即会执行其Execute成员函数,该函数内部会调用一个用户自定义的回调函数,
这就给用户提供了的控制能力。
  vtkInteractorStyleTrackballCamera负责响应键盘与鼠标的事件。该例中,
vtkInteractorStyleTrackballCamera与vtkCallbackCommand共同完成了对鼠标点击的响应。
一旦点击事件触发,vtkCallbackCommand调用Execute(),该函数调用自定义的回调函数PickerInteractionCallback(),
该回调函数内部调用vtkCellPicker:pick(),而该函数触发了vtkCommand::EndPickEvent事件,PickCommand::Execute
作出响应,输出坐标



This example demonstrates cell picking using vtkCellPicker.  It displays
the results of picking using a vtkTextMapper.

converted from TCL
#include "stdafx.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkLODActor.h"
#include "vtkConeSource.h"
#include "vtkGlyph3D.h"
#include "vtkCellPicker.h"
#include "vtkTextMapper.h"
#include "vtkActor2D.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTextProperty.h"
#include "vtkCallbackCommand.h"
#include "vtkCamera.h"

int MouseMotion;
vtkRenderer *ren1;
vtkRenderWindow *renWin;
vtkRenderWindowInteractor *iren;
vtkCellPicker *picker;
vtkActor2D *textActor;
vtkTextMapper *textMapper;
class PickCommand : public vtkCommand
{
public:
   static PickCommand *New() { return new PickCommand; }
   void Delete() { delete this; }
   virtual void Execute(vtkObject *caller, unsigned long l, void *callData)
   {
       if (picker->GetCellId() < 0 )
       {
           textActor->VisibilityOff();
       }
       else
       {
           double selpt[3];
           picker->GetSelectionPoint(selpt);
           double x = selpt[0];
           double y = selpt[1];
           double pickPos[3];
           picker->GetPickPosition( pickPos );
           double xp = pickPos[0];
           double yp = pickPos[1];
           double zp = pickPos[2];
           char text[120];
           sprintf( text, "(%5.5f,  %5.5f,  %5.5f)", xp, yp, zp );
           textMapper->SetInput( text );
           textActor->SetPosition(x, y);
           textActor->VisibilityOn();
       }
       renWin->Render();
   }
};
void PickerInteractionCallback( vtkObject* vtkNotUsed(object),
                                      unsigned long event,
                                      void* clientdata,
                                      void* vtkNotUsed(calldata) )
{
   vtkInteractorStyleTrackballCamera * style =
(vtkInteractorStyleTrackballCamera*)clientdata;
   switch( event )
   {
   case vtkCommand::LeftButtonPressEvent:
       MouseMotion = 0;
       style->OnLeftButtonDown();
       break;
   case vtkCommand::LeftButtonReleaseEvent:
       if (MouseMotion == 0)
       {
           int *pick = iren->GetEventPosition();
           picker->Pick((double)pick[0], (double)pick[1], 0.0, ren1);
       }
       style->OnLeftButtonUp();
       break;
   case vtkCommand::MouseMoveEvent:
       MouseMotion = 1;
       style->OnMouseMove();
       break;
   }
}
void main(int argc, char* argv)
{
   MouseMotion = 0;
   vtkSphereSource *sphere = vtkSphereSource::New();
   vtkPolyDataMapper *sphereMapper = vtkPolyDataMapper::New();
   sphereMapper->SetInput(sphere->GetOutput());
   sphereMapper->GlobalImmediateModeRenderingOn();
   vtkLODActor *sphereActor = vtkLODActor::New();
   sphereActor->SetMapper(sphereMapper);
// create the spikes by glyphing the sphere with a cone.  Create the mapper
// and actor for the glyphs.
   vtkConeSource *cone = vtkConeSource::New();
   vtkGlyph3D *glyph = vtkGlyph3D::New();
   glyph->SetInput(sphere->GetOutput());
   glyph->SetSource(cone->GetOutput());
   glyph->SetVectorModeToUseNormal();
   glyph->SetScaleModeToScaleByVector();
   glyph->SetScaleFactor(0.25);
   vtkPolyDataMapper *spikeMapper = vtkPolyDataMapper::New();
   spikeMapper->SetInput(glyph->GetOutput());
   vtkLODActor *spikeActor = vtkLODActor::New();
   spikeActor->SetMapper(spikeMapper);
// Create a cell picker.
   PickCommand* pickObserver = PickCommand::New();
   picker = vtkCellPicker::New();
   picker->AddObserver( vtkCommand::EndPickEvent, pickObserver );
// Create a text mapper and actor to display the results of picking.
   textMapper = vtkTextMapper::New();
   vtkTextProperty *tprop = textMapper->GetTextProperty();
   tprop->SetFontFamilyToArial();
   tprop->SetFontSize(12);
   tprop->BoldOn();
//    tprop->ShadowOn();
   tprop->SetColor(1, 0, 0);
   textActor = vtkActor2D::New();
   textActor->VisibilityOff();
   textActor->SetMapper(textMapper);
// Create the Renderer, RenderWindow, and RenderWindowInteractor


   vtkInteractorStyleTrackballCamera *style =
vtkInteractorStyleTrackballCamera::New();
   vtkCallbackCommand * pickerCommand = vtkCallbackCommand::New();
   pickerCommand->SetClientData(style);
   pickerCommand->SetCallback(PickerInteractionCallback);
   style->AddObserver(vtkCommand::LeftButtonPressEvent, pickerCommand);
   style->AddObserver(vtkCommand::MouseMoveEvent, pickerCommand);
   style->AddObserver(vtkCommand::LeftButtonReleaseEvent, pickerCommand);
   ren1 = vtkRenderer::New();
   renWin = vtkRenderWindow::New();
   renWin->AddRenderer(ren1);
   iren = vtkRenderWindowInteractor::New();
   iren->SetRenderWindow(renWin);
   iren->SetInteractorStyle(style);
   iren->SetPicker(picker);
// Add the actors to the renderer, set the background and size


   ren1->AddActor2D(textActor);
   ren1->AddActor(sphereActor);
   ren1->AddActor(spikeActor);
   ren1->SetBackground(1, 1, 1);
   renWin->SetSize(300, 300);
// Get the camera and zoom in closer to the image.
   vtkCamera *cam1 = ren1->GetActiveCamera();
   cam1->Zoom(1.4);
   iren->Initialize();
   iren->Start();
   picker->RemoveObserver( pickObserver );
   sphere->Delete();
   sphereMapper->Delete();
   sphereActor->Delete();
   cone->Delete();
   glyph->Delete();
   spikeMapper->Delete();
   spikeActor->Delete();
   picker->Delete();
   textMapper->Delete();
   textActor->Delete();
   pickerCommand->Delete();
   style->Delete();
   ren1->Delete();
   renWin->Delete();
   pickObserver->Delete();
//iren->Delete();
}
24.png
回复

使用道具 举报

47

主题

416

回帖

1214

积分

版主

积分
1214

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2015-12-6 22:27:23 | 显示全部楼层
很不错噢
回复

使用道具 举报

109

主题

23

回帖

3550

积分

管理员

积分
3550
发表于 2015-12-8 23:43:17 手机频道 | 显示全部楼层
谢谢分享!

来自: 微社区
回复

使用道具 举报

0

主题

412

回帖

1035

积分

版主

积分
1035

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-11 12:34:38 | 显示全部楼层
一直在摸索,还是楼主厉害!
回复

使用道具 举报

149

主题

462

回帖

1611

积分

版主

积分
1611

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-12 18:37:16 | 显示全部楼层
这个东西一定要看看
回复

使用道具 举报

37

主题

424

回帖

1169

积分

版主

积分
1169

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-16 20:33:29 | 显示全部楼层
好好好好哈
回复

使用道具 举报

149

主题

462

回帖

1611

积分

版主

积分
1611

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-21 09:06:05 | 显示全部楼层
回复看下,!!
回复

使用道具 举报

119

主题

457

回帖

1524

积分

版主

积分
1524

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-22 06:35:57 | 显示全部楼层
大哥谢了
回复

使用道具 举报

15

主题

451

回帖

1164

积分

版主

积分
1164

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-22 21:04:40 | 显示全部楼层
回复

使用道具 举报

135

主题

407

回帖

1461

积分

版主

积分
1461

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-24 03:04:54 | 显示全部楼层
谢谢啊版主
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|关于我们|医维基|网站地图|Archiver|手机版|医疗之家 ( 沪ICP备2023001278号-1 )  

GMT+8, 2024-5-2 14:46 , Processed in 0.216324 second(s), 32 queries .

Designed by Medical BBS

快速回复 返回顶部 返回列表