bastijn
bastijn

Reputation: 5953

vtkImageData from 3rd party structure

I have a volume stored as slices in c# memory. The slices may not be consecutive in memory. I want to import this data and create a vtkImageData object.

The first way I found is to use a vtkImageImporter, but this importer only accepts a single void pointer as data input it seems. Since my slices may not be consecutive in memory, I cannot hand a single pointer to my slice data.

A second option is to create the vtkImageData from scratch and use vtkImageData->GetScalarPointer()" to get a pointer to its data. Than fill this using a loop. This is quite costly (although memcpy could speed things up a bit). I could also combine the copy approach with the vtkImageImport ofcourse.

Are these my only options, or is there a better way to get the data into a vtk object? I want to be sure there is no other option before I take the copy approach (performance heavy), or modify the low level storage of my slices so they become consecutive in memory.

Upvotes: 0

Views: 1214

Answers (1)

El Marce
El Marce

Reputation: 3344

I'm not too familiar with VTK for C# (ActiViz). In C++ is a good approach and rather fast one to use vtkImageData->GetScalarPointer() and manually copy your slices. It will increase your speed storing all memory first as you said, perhaps you want to do it this more robust way (change the numbers):

vtkImageData * img = vtkImageData::New();
  img->SetExtent(0, 255, 0, 255, 0, 9);
  img->SetSpacing(sx , sy, sz);
  img->SetOrigin(ox, oy, oz);
  img->SetNumberOfScalarComponents(1);
  img->SetScalarTypeToFloat();
  img->AllocateScalars();

Then is not to hard do something like:

  float *  fp = static_cast<float *>(img->GetScalarPointer());

  for ( int i = 0; i < 256* 256* 10; i ++) {
      fp[i] = mydata[i]
  }

Another fancier option is to create your own vtkImporter basing the code in the vtkImageImport.

Upvotes: 2

Related Questions