VTK——angleWidget的回调函数设置

组件创建代码

//        qDebug() << __FUNCTION__ << QString::fromUtf8("模式");

        removeCellWidget(0);

        auto selector = getInteractor()->GetSelector();
        int *position = getInteractor()->GetEventPosition();
        selector->Pick(position[0], position[1], 0, getInteractor()->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
        double selected[3];
        selector->GetPickPosition(selected);

        std::copy(selected, selected + 3, startPointSelected);
//        angleWidgetHandle = vtkSmartPointer<vtkAngleWidget>::New();
        vtkAngleWidget* angleWidgetHandle = vtkAngleWidget::New();

        angleWidgetHandle->SetInteractor(getInteractor());


        // Create and configure representation
        vtkSmartPointer<vtkAngleRepresentation2D> representation = vtkSmartPointer<vtkAngleRepresentation2D>::New();
        angleWidgetHandle->SetRepresentation(representation);
        angleWidgetHandle->SetEnabled(true);
        angleWidgetHandle->CreateDefaultRepresentation();
        // Create and set ray properties (line color)
        vtkSmartPointer<vtkProperty2D> rayAttr = vtkSmartPointer<vtkProperty2D>::New();
        rayAttr->SetColor(1, 0, 0); // red
        representation->GetRay1()->GetProperty()->DeepCopy(rayAttr);
        representation->GetRay2()->GetProperty()->DeepCopy(rayAttr);

        // Create arc properties (line color)
        vtkSmartPointer<vtkProperty2D> arcAttr = vtkSmartPointer<vtkProperty2D>::New();
        arcAttr->SetColor(0, 0, 1); // blue
        representation->GetArc()->GetProperty()->DeepCopy(arcAttr);

        representation->SetCenterDisplayPosition(selected);

        // Render once
        getInteractor()->Render();

//        // Set initial position of angle information (top endpoint position)
        vtkNew<vtkAngleWidgetCallback> angleCB;
        angleWidgetHandle->AddObserver(vtkCommand::EndInteractionEvent, angleCB);
        // Enable the widget
        angleWidgetHandle->EnabledOn();

        currentPickingState = 0;

        // Render again
        getInteractor()->Render();
        double calculatedAngle = representation->GetAngle(); // Get angle



创建回调函数

回调函数用于在组件创建成功后完成发送,打印等操作。如果想要实时更新角度组件的端点位置、角度,可以在回调函数中设置。

class vtkAngleWidgetCallback : public vtkCommand
{
public:
    static vtkAngleWidgetCallback* New()
    {
        return new vtkAngleWidgetCallback;
    }

    virtual void Execute(vtkObject* caller, unsigned long, void*)
    {
        vtkAngleWidget* angleWidget = reinterpret_cast<vtkAngleWidget*>(caller);
        vtkAngleRepresentation2D* angleRepresentation = reinterpret_cast<vtkAngleRepresentation2D*>(angleWidget->GetRepresentation());

        // 获取三个端点的坐标
        double point1[3], point2[3], center[3];
        angleRepresentation->GetPoint1WorldPosition(point1);
        angleRepresentation->GetPoint2WorldPosition(point2);
        angleRepresentation->GetCenterWorldPosition(center);

        // 打印或使用这些坐标
        std::cout << "Point 1: " << point1[0] << ", " << point1[1] << ", " << point1[2] << std::endl;
        std::cout << "Point 2: " << point2[0] << ", " << point2[1] << ", " << point2[2] << std::endl;
        std::cout << "Center: " << center[0] << ", " << center[1] << ", " << center[2] << std::endl;
    }

    vtkAngleWidgetCallback() {}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值