I first created a volumetric mesh. Then extract the surface mesh using vtkGeometryFilter and use vtkPolyDataToImageStencil to generate the 3D image. I have got the image dimension as (200, 200, 320). But now I want to increase the height and width of the 3D image to (512, 512, 320). The image dimension array is calculated using the bounds of the ellipsoid. The ellipsoid should be located at the center of the 3D image volume like the image created from the below code.
//read all the data from the file
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName(ref_volumetric_mesh_path.c_str());
reader->Update();
vtkSmartPointer geometryFilter =
vtkSmartPointer::New();
geometryFilter->SetInputData(reader->GetOutput());
geometryFilter->Update();
vtkSmartPointer pd = geometryFilter->GetOutput();
// Mesh voxelization code
vtkSmartPointer whiteImage =
vtkSmartPointer::New();
double bounds[6];
pd->GetBounds(bounds);
double spacing[3]; // desired volume spacing
spacing[0] = 0.5;
spacing[1] = 0.5;
spacing[2] = 0.5;
whiteImage->SetSpacing(spacing);
// compute dimensions
int dim[3];
for (int i = 0; i < 3; i++)
{
dim[i] = static_cast(ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i]));
}
whiteImage->SetDimensions(dim);
whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);
double origin[3];
origin[0] = bounds[0] + spacing[0] / 2;
origin[1] = bounds[2] + spacing[1] / 2;
origin[2] = bounds[4] + spacing[2] / 2;
whiteImage->SetOrigin(origin);
whiteImage->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
// fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = whiteImage->GetNumberOfPoints();
for (vtkIdType i = 0; i < count; ++i)
{
whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
}
// polygonal data --> image stencil:
vtkSmartPointer pol2stenc =
vtkSmartPointer::New();
pol2stenc->SetInputData(pd);
pol2stenc->SetOutputOrigin(origin);
pol2stenc->SetOutputSpacing(spacing);
pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());
pol2stenc->Update();
// cut the corresponding white image and set the background:
vtkSmartPointer imgstenc =
vtkSmartPointer::New();
imgstenc->SetInputData(whiteImage);
imgstenc->SetStencilConnection(pol2stenc->GetOutputPort());
imgstenc->ReverseStencilOff();
imgstenc->SetBackgroundValue(outval);
imgstenc->Update();
vtkSmartPointer writer =
vtkSmartPointer::New();
writer->SetFileName(ref_volumetric_mesh_voxelized_path.c_str());
writer->SetInputData(imgstenc->GetOutput());
writer->Write();