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<vtkSliderWidget*>(caller);
        double scaleValue = static_cast<vtkSliderRepresentation *>(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<vtkConeSource> coneSource =
            vtkSmartPointer<vtkConeSource>::New();

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

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

    vtkSmartPointer<vtkSliderRepresentation3D> sliderRep =
        vtkSmartPointer<vtkSliderRepresentation3D>::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<vtkSliderWidget> sliderWidget =
        vtkSmartPointer<vtkSliderWidget>::New();
    sliderWidget->GetEventTranslator()->SetTranslation(vtkCommand::RightButtonPressEvent,
                                                       vtkWidgetEvent::Select);
    sliderWidget->GetEventTranslator()->SetTranslation(vtkCommand::RightButtonReleaseEvent,
                                                       vtkWidgetEvent::EndSelect);

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

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

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

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

    iren->Start();
    return 0;
}




0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] You can use the key ‘i’ to enable or disable the vtkSliderWidget object defaultly. Here is a post about how to use vtkSliderWidget to control other model, Control The Size Of Model By VtkSliderWidget. […]

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
1
0
Would love your thoughts, please comment.x
()
x