[vtkusers] Problem with vtkPolyDataToImageStencil and vtkImageStencil

Andreas Kuhn kuhnan at ee.ethz.ch
Tue Mar 9 16:28:53 EST 2004


Hi,

I'm using VTK 4.2 on a Windows XP PC with Visual Studio 6.0.

I'm trying to use vtkPolyDataToImageStencil and vtkImageStencil to
extract vtkImageData out of vtkPolyData. But even when I try to convert
a simple sphere into image data, the result gets wrong.
I posted a picture to
http://people.ee.ethz.ch/~kuhnan/public/imageData.jpg. It contains the
slices of the resulting vtkImageData and you can see that on the first
and the last slice, there is a white line instead of a point.
Below I attached the code which I am using to convert the vtkPolyData to
vtkImageData.
Does someone know what I am doing wrong here?

I appreciate every help.
Andreas

--------------------------------------
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkImageStencil.h"
#include "vtkPolyDataToImageStencil.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkMarchingCubes.h"


int main( int argc, char *argv[] )
{

  // create sphere geometry
  vtkSphereSource *sphere = vtkSphereSource::New();
    sphere->SetPhiResolution(12);
    sphere->SetThetaResolution(12);
    sphere->SetCenter(5, 5, 5);
    sphere->SetRadius(3);

  // build empty image
  vtkImageData* image = vtkImageData::New();
    image->SetOrigin(0,0,0);
    image->SetSpacing(0.2,0.2,0.2);
    image->SetDimensions(50,50,50);
    image->SetScalarTypeToUnsignedChar();
    image->AllocateScalars();
    image->Update();
    for (int n=0; n<image->GetNumberOfPoints(); n++){
      image->GetPointData()->GetScalars()->SetTuple1(n, 127);
    }

  vtkPolyDataToImageStencil* PolyDataToStencil =
vtkPolyDataToImageStencil::New();
    PolyDataToStencil->SetInput (sphere->GetOutput());

  vtkImageStencil* stencil = vtkImageStencil::New();
    stencil->SetInput(image);
    stencil->SetStencil(PolyDataToStencil->GetOutput());
    stencil->ReverseStencilOff();
    stencil->SetBackgroundValue(0);
    stencil->Update();

  vtkMarchingCubes *marching = vtkMarchingCubes::New();
  	marching->SetInput(stencil->GetOutput());
    marching->ComputeScalarsOff();
  	marching->SetValue(0,50);

  // map to graphics library
  vtkPolyDataMapper *map = vtkPolyDataMapper::New();
    map->ScalarVisibilityOn();
    map->SetScalarModeToUsePointData();
    map->UseLookupTableScalarRangeOn();
		map->SetColorModeToMapScalars();
    map->SetInput(marching->GetOutput());

  // actor coordinates geometry, properties, transformation
  vtkActor *aSphere = vtkActor::New();
    aSphere->SetMapper(map);

  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer(ren1);

  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  // add the actor to the scene
  ren1->AddActor(aSphere);
  ren1->SetBackground(1,1,1); // Background color white

  renWin->Render();

  iren->Start();

  return 0;
}




More information about the vtkusers mailing list