We can use VTK observer-command mechanism to listen to slider value changing event and override our responding method void Execute(vtkObject *caller, unsigned long, void*) to change target item’s size.
It’s easy to implement with the subclass of vtkCommand and vtkSliderWidget. The following example shows how to do it.
I add a slider bar in render window and create a cone above the slider. Make the default scale value equal to 1 on the slider, then we can move the tool and change the size of the cone.

class vtkSliderCallback : public vtkCommand
{
public:
    static vtkSliderCallback *New()
    { return new vtkSliderCallback; }
    void Execute(vtkObject *caller, unsigned long, void*) override
    {
        vtkSliderWidget *sliderWidget =
          reinterpret_cast(caller);
        double scaleValue = static_cast(sliderWidget->GetRepresentation())->GetValue();
        cout << "scaleValue: " << scaleValue << endl;
        vtkTransform *trans = vtkTransform::New();
        trans->Scale( scaleValue, scaleValue, scaleValue );
        actor->SetUserTransform( trans );
    }
    vtkSliderCallback():actor(nullptr) {}
    vtkActor *actor;
};

int main()
{
    vtkSmartPointer coneSource =
            vtkSmartPointer::New();

    vtkSmartPointer mapper =
            vtkSmartPointer::New();
    mapper->SetInputConnection( coneSource->GetOutputPort() );

    vtkSmartPointer actor =
            vtkSmartPointer::New();
    actor->SetMapper( mapper );

    vtkSmartPointer sliderRep =
        vtkSmartPointer::New();
    sliderRep->SetValue( 1 );
    sliderRep->GetPoint1Coordinate()->SetCoordinateSystemToWorld();
    sliderRep->GetPoint1Coordinate()->SetValue(0,0,0);
    sliderRep->GetPoint2Coordinate()->SetCoordinateSystemToWorld();
    sliderRep->GetPoint2Coordinate()->SetValue(2,0,0);
    sliderRep->SetSliderLength(0.075);
    sliderRep->SetSliderWidth(0.05);
    sliderRep->SetEndCapLength(0.05);

    double *bounds = sliderRep->GetBounds();
    actor->SetPosition( (bounds[0] + bounds[1])/2,
            (bounds[2] + bounds[3])/2*7,
            (bounds[4] + bounds[5])/2 );

    vtkSmartPointer sliderWidget =
        vtkSmartPointer::New();
    sliderWidget->GetEventTranslator()->SetTranslation(vtkCommand::RightButtonPressEvent,
                                                       vtkWidgetEvent::Select);
    sliderWidget->GetEventTranslator()->SetTranslation(vtkCommand::RightButtonReleaseEvent,
                                                       vtkWidgetEvent::EndSelect);

    vtkSmartPointer callback =
      vtkSmartPointer::New();
    callback->actor = actor;
    sliderWidget->AddObserver(vtkCommand::InteractionEvent,callback);

    vtkSmartPointer renderer =
            vtkSmartPointer::New();
    renderer->AddActor( actor );

    vtkSmartPointer renderWindow =
            vtkSmartPointer::New();
    renderWindow->AddRenderer( renderer );
    vtkSmartPointer iren =
        vtkSmartPointer::New();
    iren->SetRenderWindow( renderWindow );

    sliderWidget->SetInteractor(iren);
    sliderWidget->SetRepresentation(sliderRep);
    sliderWidget->SetAnimationModeToAnimate();
    sliderWidget->EnabledOn();

    iren->Start();
    return 0;
}




0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page