[vtkusers] Vol.cxx example + vtkImageWriter + vtkImageReader

Paul McGuinness mcguinpg at maths.tcd.ie
Wed Jul 23 12:01:40 EDT 2003


Hi vtk users,

I am creating a sphere using vtkImageData and then using vtkContourFilter.
This is the simple example Vol.cxx that can be found from the vtk website.
The problem I am having is to write the vtkImageData using vtkImageWriter
to files, and then reading these files back using vtkImageReader and then
contouring the data. I don't obtain the sphere I originally got but a
cubic shape that looks like a sponge.
Can someone please help me understand what I am doing wrong and how I can
fix this.
I have attached the code below so you can see what I am talking about.

Many thanks,
Paul


#include "vtkImageWriter.h"
#include "vtkImageReader.h"
#include "vtkPointData.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkStructuredPoints.h"
#include "vtkFloatArray.h"
#include "vtkContourFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkImageCast.h"

main ()
{

  float sp, z, y, x, s;
int i, j, k,  kOffset, jOffset, offset,h;

  vtkImageData *vol = vtkImageData::New();
  vol->SetDimensions(26,26,26);
  vol->SetOrigin(-0.5,-0.5,-0.5);
  sp =1.0/25.0;
  vol->SetSpacing(sp,sp,sp);

  vtkFloatArray *scalars =vtkFloatArray::New();
                     for(k=0;k<26;k++)
                     {
                     z = -0.5 + k*sp;
                     kOffset = k*26*26;

                     for(j=0; j<26; j++)
                     {
                     y = -0.5 +j*sp;
                     jOffset = j*26;

                     for(i=0;i<26;i++)
                     {
                     x = -0.5 + i*sp;
                     s = x*x + y*y + z*z - (0.4*0.4);
                     offset = i + jOffset + kOffset;
                     scalars->InsertTuple1(offset,s);
                     }
                     }
                     }

  vol->GetPointData()->SetScalars(scalars);

//------------------ Write Data ---------------------------
vtkImageWriter *writer =   vtkImageWriter::New();
  writer->SetInput(vol);
  writer->SetFilePrefix("voldata");
  writer->Write();

//----------------- Read Data ---------------------------
vtkImageReader *reader  = vtkImageReader::New();
   reader->SetDataExtent(0,25, 0, 25, 0, 25);
   reader->SetFilePrefix("voldata");
   reader->SetDataSpacing(sp, sp, sp);
   reader->SetDataOrigin(-0.5,-0.5,-0.5);



  vtkContourFilter *contour = vtkContourFilter::New();
//    contour->SetInput(vol);            //original
      contour->SetInput(reader->GetOutput());
      contour->SetValue(0,0);

  vtkPolyDataMapper *volMapper = vtkPolyDataMapper::New();
      volMapper->SetInput(contour->GetOutput());
      volMapper->ScalarVisibilityOff();
  vtkActor *volActor = vtkActor::New();
      volActor->SetMapper(volMapper);



  vtkRenderer *renderer = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(renderer);
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);


  renderer->AddActor(volActor);
      renderer->SetBackground(1,1,1);
  renWin->SetSize(200,200);

  // interact with data

  renWin->Render();
  iren->Start();

  // Clean up
  renderer->Delete();
  renWin->Delete();
  iren->Delete();
  vol->Delete();
  scalars->Delete();
  contour->Delete();
  volMapper->Delete();
  volActor->Delete();
}







More information about the vtkusers mailing list