VTK-C++版本AnnotatePick例子

原例子用了复杂的图形,我这边改成简单的cylinder,注意点击图形后再按p进入pick模式就可以显示鼠标点pick的信息了。如果不点击图形,会先报错”no current renderer on the interactor style.“,不过忽略后之后依然正常显示pick信息。

//pick.cpp
#include "Pick.h"
#include "vtkCellPicker.h"
#include "vtkCommand.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include "vtkActor2D.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "Cylinder.h"
#include "vtkInteractorObserver.h"
#include "vtkCallbackCommand.h"

vtkActor2D* textActor;
vtkTextMapper* textMapper;
vtkRenderWindow* renWin;

class AnnotatePick : public vtkCommand
{
public:
    static AnnotatePick* New()
    {
        return new AnnotatePick();
    }
    virtual void Execute(vtkObject* caller, unsigned long eventID, void* callData)
    {
        //vtkCellPicker* picker = reinterpret_cast<vtkCellPicker*>(caller);
        vtkCellPicker* picker = vtkCellPicker::SafeDownCast(caller);

        if (!picker)
        {
            return;
        }

        if (picker->GetCellId() < 0)
        {
            textActor->VisibilityOff();
        }
        else
        {
            double* selPt = picker->GetSelectionPoint();
            double x = selPt[0];
            double y = selPt[1];
            auto pickPos = picker->GetPickPosition();
            double xp = pickPos[0];
            double yp = pickPos[1];
            double zp = pickPos[2];
            char s[256];
            sprintf_s(s, sizeof(s), "xp:%f, yp:%f, zp:%f", xp, yp, zp);
            textMapper->SetInput(s);
            textActor->SetPosition(x, y);
            textActor->VisibilityOn();
        }

        renWin->Render();
    }
};

Pick::Pick()
{
    //picker
    vtkCellPicker* picker = vtkCellPicker::New();
    AnnotatePick* annotatePick = AnnotatePick::New();
    picker->AddObserver(vtkCommand::EndPickEvent, annotatePick);
    annotatePick->Delete();
    //文字显示设置
    textMapper = vtkTextMapper::New();
    auto tprop = textMapper->GetTextProperty();
    tprop->SetFontFamilyToArial();
    tprop->SetFontSize(10);
    tprop->BoldOn();
    tprop->ShadowOn();
    tprop->SetColor(1, 0, 0);
    textActor = vtkActor2D::New();
    textActor->VisibilityOff();
    textActor->SetMapper(textMapper);
    //窗口相关设置
    vtkRenderer* ren = vtkRenderer::New();
    renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren);
    vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);
    iren->SetPicker(picker);
    //
    auto cylinderActor = Cylinder::getActor();
    ren->AddActor2D(textActor);//注意是AddActor2D
    ren->AddActor(cylinderActor);//两个Actor要加到一个renderer里否则文字不显示
    renWin->Render();
    //
    picker->Pick(85, 126, 0, ren);//模拟pick
    iren->Start();
}
//Cylinder.cpp
#include "Cylinder.h"

vtkActor* Cylinder::getActor()
{
    vtkCylinderSource* cylinder = vtkCylinderSource::New();
    vtkPolyDataMapper* cylinderMapper = vtkPolyDataMapper::New();
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
    vtkActor* cylinderActor = vtkActor::New();
    cylinderActor->SetMapper(cylinderMapper);
    return cylinderActor;
}
#include "Pick.h"

VTK_MODULE_INIT(vtkRenderingFreeType);

int main()
{
    Pick c;
}

头文件很简单我就不再赘述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mrbone11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值