Skip to content

多Actor绑定选中事件

xxx

1 交互只有: 放大、移动、沿着z轴旋转
2 增加选中回调
3 增加部分模型隐藏

在官方案例基础上改的 https://kitware.github.io/vtk-examples/site/Cxx/Picking/HighlightPickedActor/

TrackballCamera 记录下所有的actor,每次鼠标单机后调用 vtkPropPicker 判断是否选中某一个模型。

class MouseInteractorHighLightActor : public vtkInteractorStyleTrackballCamera
{
public:
    vtkTypeMacro(MouseInteractorHighLightActor, vtkInteractorStyleTrackballCamera);
    static MouseInteractorHighLightActor *New();
    MouseInteractorHighLightActor();
    ~MouseInteractorHighLightActor() override;
    void OnLeftButtonDown() override;
    void OnMouseMove() override;
    void Rotate() override;
    QList<vtkActor *> actors_;

private:
    vtkActor *LastPickedActor;
    vtkProperty *LastPickedProperty;
};


void MouseInteractorHighLightActor::OnLeftButtonDown()
{
    vtkNew<vtkNamedColors> colors;
    int *clickPos = GetInteractor()->GetEventPosition();
    vtkNew<vtkPropPicker> picker;
    picker->Pick(clickPos[0], clickPos[1], 0, GetDefaultRenderer());
    if (LastPickedActor) {
        LastPickedActor->GetProperty()->DeepCopy(LastPickedProperty);
    }
    LastPickedActor = picker->GetActor();
    if (LastPickedActor) {
        LastPickedProperty->DeepCopy(LastPickedActor->GetProperty());
        LastPickedActor->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());
        LastPickedActor->GetProperty()->SetOpacity(1.0);
    }
    int i = 0;
    bool select = false;
    foreach (auto actor, actors_) {
        if (actor == LastPickedActor) {
            select = true;
            InvokeEvent(vtkCommand::UserEvent + 1, &i);
        }
        i++;
    }
    if (!select) {
        i = -1;
        InvokeEvent(vtkCommand::UserEvent + 1, &i);
    }

    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}


foreach (auto var, filename) {
    vtkNew<vtkSTLReader> reader;
    reader->SetFileName(var.toLocal8Bit().data());
    vtkNew<vtkSphereSource> source;
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(reader->GetOutputPort());
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetSpecularColor(colors->GetColor3d("White").GetData());
    actor->GetProperty()->SetSpecularPower(30.0);
    renderer->AddActor(actor);
    style->actors_ << actor;
}

注意:vtkPropPicker 和 选中后发出 InvokeEvent 都是耗时操作,如果外边处理事情很多可能造成**widget**闪烁。实际使用的时候我是在**vtkPropPicker**后**Render**一下。

void MouseInteractorHighLightActor::OnLeftButtonDown()
{
    int *clickPos = GetInteractor()->GetEventPosition();
    picker_->Pick(clickPos[0], clickPos[1], 0, GetDefaultRenderer());
    GetDefaultRenderer()->Render();
    if (last_picked_actor_) {
        last_picked_actor_->GetProperty()->DeepCopy(last_picked_property_);
    }
    last_picked_actor_ = picker_->GetActor();
    if (last_picked_actor_) {
        last_picked_property_->DeepCopy(last_picked_actor_->GetProperty());
        last_picked_actor_->GetProperty()->SetColor(colors_->GetColor3d("Red").GetData());
    }
    int i = 0;
    bool select = false;
    foreach (auto actor, actors_) {
        if (actor == last_picked_actor_) {
            select = true;
            InvokeEvent(vtkCommand::UserEvent + 1, &i);
        }
        i++;
    }
    if (!select) {
        i = -1;
        InvokeEvent(vtkCommand::UserEvent + 1, &i);
    }
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}