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::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() {}
};

你可能感兴趣的:(VTK学习,c++,qt)