[vtkusers] fix position of vtkAxesActor

Bill Lorensen bill.lorensen at gmail.com
Sun Sep 19 11:07:34 EDT 2010


The widget only exists during the scope of displayAxes unless another
object increases the ref count of the widget's pointer. In your case,
the ref count of widget.GetPointer() remains at 1 throughout
displayAxes. When displayAxes returns, widget is destroyed along with
widget's pointer. On the other hand, axes' pointer ref count is
increment by 1 with the statement:
 widget->SetOrientationMarker( axes );
so even though the SmartPointer axes is deleted, the axes.GetPointer()
is not destroyed on return.

One option is to pass in a reference to the widget.

See below:

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkAxesActor.h>
#include <vtkPropAssembly.h>
#include <vtkSmartPointer.h>


void displayAxes(vtkSmartPointer<vtkOrientationMarkerWidget> &widget,
                 vtkRenderWindowInteractor *renderWindowInteractor)
{
    vtkSmartPointer<vtkAxesActor> axes =
        vtkSmartPointer<vtkAxesActor>::New();

    widget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
    widget->SetOrientationMarker( axes );
    widget->SetInteractor( renderWindowInteractor );
    widget->SetViewport( 0.0, 0.0, 0.4, 0.4 );
    widget->SetEnabled( 1 );
    widget->InteractiveOn();
}



int main (int, char *[])
{
    vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetCenter(0.0, 0.0, 0.0);
    sphereSource->SetRadius(1.0);
    sphereSource->Update();

    vtkPolyData* polydata = sphereSource->GetOutput();

    // Create a mapper
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInput(polydata);

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

    // A renderer and render window
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    // An interactor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Add the actors to the scene
    renderer->AddActor(actor);
    renderer->SetBackground(.2, .3, .4);

    vtkSmartPointer<vtkOrientationMarkerWidget> widget =
        vtkSmartPointer<vtkOrientationMarkerWidget>::New();
    displayAxes(widget, renderWindowInteractor);

    // Render the scene
    renderer->ResetCamera();
    renderWindow->Render();

    // Begin mouse interaction
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}



More information about the vtkusers mailing list