[vtkusers] No output when rendering from raw image data

Camp Nou campnou at gmail.com
Thu Nov 12 01:30:08 EST 2009


I write a simple program using vtk to render volume from raw image data, 
i get a empty show-window.

Here's my code

int main()
{
   unsigned int data_length = WIDTH*DEPTH*LENGTH;
   unsigned char* raw_data = (unsigned char*)malloc(data_length);

   FILE* fp;

   if( !(fp = fopen("source.dat","rb")) ){
       printf("Open raw data failed.\n");
       EXIT_FAILURE;
   }

   if( fread(raw_data,sizeof(unsigned char),data_length,fp)!=data_length ){
       printf("Read raw data from file failed.\n");
       EXIT_FAILURE;
   }
   fclose(fp);
 
   vtkImageData* vol_data = vtkImageData::New();
   float sample_width = WIDTH_L / WIDTH*10;
   float sample_depth = DEPTH_L / DEPTH*10;
   float sample_length = LENGTH_L /LENGTH*10;

   vol_data->SetNumberOfScalarComponents(1);
   vol_data->SetDimensions(WIDTH,DEPTH,LENGTH);
   vol_data->SetSpacing(sample_width,sample_depth,sample_length);
   vol_data->SetScalarTypeToUnsignedChar();
   vol_data->SetOrigin(0,0,0);
   vol_data->AllocateScalars();

   memcpy(vol_data->GetScalarPointer(), raw_data, 
data_length*sizeof(unsigned char));
   free(raw_data);

   vtkVolumeMapper* mapper = vtkFixedPointVolumeRayCastMapper::New();
   mapper->SetInput(vol_data);
   mapper->SetBlendModeToComposite();

   vtkPiecewiseFunction* piecewise = vtkPiecewiseFunction::New();

   piecewise->AddPoint(0,0);
   piecewise->AddPoint(127,0.05);
   piecewise->AddPoint(253,1.0);

   vtkColorTransferFunction* color = vtkColorTransferFunction::New();
   color->AddRGBPoint(127,0.3,0.3,0.3);
   color->AddRGBPoint(253,1.0,1.0,1.0);

   vtkVolumeProperty* vol_prop = vtkVolumeProperty::New();
   vol_prop->SetColor(color);
   vol_prop->SetScalarOpacity(piecewise);
   vol_prop->SetInterpolationTypeToLinear();

   vtkVolume* vol = vtkVolume::New();
   vol->SetMapper(mapper);
   vol->SetProperty(vol_prop);

   vtkRenderer* ren = vtkRenderer::New();
   ren->AddActor(vol);
   ren->ResetCamera();
   ren->SetBackground(1, 1, 1);

   vtkRenderWindow *ren_win = vtkRenderWindow::New();
   ren_win->AddRenderer(ren);

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

   ren_win->Render();

   iren->Initialize();
   iren->Start();

   vol_data->Delete();
   mapper->Delete();
   piecewise->Delete();
   color->Delete();
   vol_prop->Delete();
   vol->Delete();
   ren->Delete();
   ren_win->Delete();
   iren->Delete();

   EXIT_SUCCESS;

}



More information about the vtkusers mailing list