[vtkusers] Set position of vtkImageSlice

David Doria daviddoria at gmail.com
Tue Nov 13 16:41:31 EST 2012


>
> 2012/11/13 David Lonie <david.lonie at kitware.com>
>
>> Sorry for the noise -- message got sent too early last time...
>>
>> Hi list,
>>
>> I'm writing a test for some new functionality that will generate
>> several vtkImageData instances and render them at different positions.
>>
>> I'm setting up a vtkImageSlice like this:
>>
>> vtkNew<vtkImageData> image1;
>> // Generate image...
>> vtkNew<vtkImageSliceMapper> mapper1;
>> mapper1->SetInputData(image1.GetPointer());
>> vtkNew<vtkImageSlice> actor1;
>> actor1->SetMapper(mapper1.GetPointer());
>> actor1->SetPosition(10, 10, 10);
>>
>> then adding the actor to the renderer as usual. The docs for
>> vtkImageSlice say:
>>
>> "Prop3D methods such as SetPosition() and RotateWXYZ() change the
>> position and orientation of the data with respect to VTK world
>> coordinates."
>>
>> Trouble is, no matter what I set the position of the actor to, the
>> image appears in the center of the scene. Is this expected behavior?
>> Is there a better class to use for this sort of thing?
>>
>> Thanks,
>>
>> Dave
>
>
If you want to do it with the "change the position of the actor" technique,
here is one way to make the view not update to always make the object look
centered. I get the camera before adding the actor, add the actor (which
changes the current camera, but not the one I deep copied previously), then
set the camera to the one that was saved.

(I just saw your reply - that is easier haha, but this might be good to
know about too :) ).

#include <vtkSphereSource.h>

#include <vtkCamera.h>

#include <vtkPolyData.h>

#include <vtkSmartPointer.h>

#include <vtkPolyDataMapper.h>

#include <vtkActor.h>

#include <vtkRenderWindow.h>

#include <vtkRenderer.h>

#include <vtkRenderWindowInteractor.h>


int main(int, char *[])

{

  // Create a sphere

  vtkSmartPointer<vtkSphereSource> sphereSource =

    vtkSmartPointer<vtkSphereSource>::New();

  sphereSource->SetCenter(0, 0, 0);

//  sphereSource->SetCenter(0, -0.1, 0);

  sphereSource->SetCenter(0.1, 0.1, 0);

  sphereSource->SetRadius(0.1);


  vtkSmartPointer<vtkPolyDataMapper> mapper =

    vtkSmartPointer<vtkPolyDataMapper>::New();

  mapper->SetInputConnection(sphereSource->GetOutputPort());


  vtkSmartPointer<vtkActor> actor =

    vtkSmartPointer<vtkActor>::New();

  actor->SetMapper(mapper);


  vtkSmartPointer<vtkRenderer> renderer =

    vtkSmartPointer<vtkRenderer>::New();

  vtkSmartPointer<vtkRenderWindow> renderWindow =

    vtkSmartPointer<vtkRenderWindow>::New();

  renderWindow->AddRenderer(renderer);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =

    vtkSmartPointer<vtkRenderWindowInteractor>::New();

  renderWindowInteractor->SetRenderWindow(renderWindow);


  vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();

  camera->DeepCopy(renderer->GetActiveCamera());


  renderer->AddActor(actor);


  renderer->SetActiveCamera(camera);


  renderWindowInteractor->Start();


  return EXIT_SUCCESS;

}



David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20121113/a4cd63bb/attachment.htm>


More information about the vtkusers mailing list