QT& VTK交互风格

时间:2014-12-30 18:44:43

标签: c++ qt vtk qvtkwidget

我使用VTK作为qvtkwidget来在QT中显示3D对象。 我知道我可以通过自己修改的交互方式来改变其交互方式。 我想通过右键单击并左键单击来旋转和移动对象。 并尝试在头文件中创建修改后的交互样式类,但在修改的交互样式中使用其他类中声明的函数存在一些困难。这应该很简单。 有谁知道这个?

  boxWidget.at(boxWidget.count()-1)->SetInput(reader->GetOutput());
        boxWidget.at(boxWidget.count()-1)->SetScalingEnabled(0); //turn this on to be able to resize the model
        boxWidget.at(boxWidget.count()-1)->SetRotationEnabled(0);
        boxWidget.at(boxWidget.count()-1)->SetInteractor(this->ui->qvtkWidget->GetInteractor());
        boxWidget.at(boxWidget.count()-1)->GetInteractor()->SetInteractorStyle(MyStyle::New());  /

BoxWidget是我试图应用'MyStyle'的对象 以下是MyStyle类

class MyStyle : public vtkInteractorStyleTrackballCamera , public MainWindow
{
private:
unsigned int NumberOfClicks;
int PreviousPosition[2];
int ResetPixelDistance;
public:
static MyStyle *New();

vtkTypeMacro(MyStyle, vtkInteractorStyleTrackballCamera);

PeterStyle() : NumberOfClicks(0) , ResetPixelDistance(5)
{
    this->PreviousPosition[0] = 0;
    this->PreviousPosition[1] = 0;
}

virtual void OnLeftButtonDown()
{
    qDebug() << "My Style Mouse Left Clicked";
    //std::cout << "Pressed left mouse button." << std::endl;
    this->NumberOfClicks++;
    //std::cout << "NumberOfClicks = " << this->NumberOfClicks << std::endl;
    int pickPosition[2];
    this->GetInteractor()->GetEventPosition(pickPosition);

    int xdist = pickPosition[0] - this->PreviousPosition[0];
    int ydist = pickPosition[1] - this->PreviousPosition[1];

    this->PreviousPosition[0] = pickPosition[0];
    this->PreviousPosition[1] = pickPosition[1];

    int moveDistance = (int)sqrt((double)(xdist*xdist + ydist*ydist));

    // Reset numClicks - If mouse moved further than resetPixelDistance
    if(moveDistance > this->ResetPixelDistance)
    {
        this->NumberOfClicks = 1;
    }


    if(this->NumberOfClicks == 2)
    {
        vtkSmartPointer<vtkCamera> camera =
            vtkSmartPointer<vtkCamera>::New();
        camera->SetPosition(140.0, 155.0, 590.0);
        camera->SetFocalPoint(140.0, 155.0, 0.0);
        camera->SetClippingRange(590.0, 600.0);

        this->GetCurrentRenderer()->SetActiveCamera(camera);
        qDebug() << "Double clicked.";
        this->NumberOfClicks = 0;
    }
    // forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}

我不知道用leftclick移动对象需要改变什么。

1 个答案:

答案 0 :(得分:0)

如果您只想 来移动带有左键的对象,则不需要为自己的交互方式创建子类,vtk已经实现了vtkInteractorStyleTrackballActor允许通过鼠标控制actor而不与相机交互的样式。然后,您可以覆盖它以执行任何操作。