标签:OLE pre tst nal fill idt text Dimension ast
vtkSmartPointer<vtkImageData> polyDataToImageData(vtkSmartPointer<vtkPolyData> polydata)
{
vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
double bounds[6];
polydata->GetBounds(bounds);
double spacing[3];// desired volume spacing
spacing[0] = 0.3;
spacing[1] = 0.3;
spacing[2] = 0.3;
imageData->SetSpacing(spacing);
//compute dimensions
int dim[3];
for(int i = 0;i < 3;++i){
dim[i] = static_cast<int>(ceil((bounds[2*i+1]-bounds[2*i])/spacingVal));
}
imageData->SetDimensions(dim);
imageData->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;
imageData->SetOrigin(origin);
imageData->AllocateScalars(VTK_UNSIGNED_CHAR,1);
//fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = imageData->GetNumberOfPoints();
for(vtkIdType i = 0;i < count;++i){
imageData->GetPointData()->GetScalars()->SetTuple1(i,inval);
}
//polygonal data --> image stencil:
vtkSmartPointer<vtkPolyDataToImageStencil> pdtoImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
pdtoImageStencil->SetInputData(polydata);
pdtoImageStencil->SetOutputOrigin(origin);
pdtoImageStencil->SetOutputSpacing(spacingVal,spacingVal,spacingVal);
pdtoImageStencil->SetOutputWholeExtent(imageData->GetExtent());
pdtoImageStencil->Update();
//cut the corresponding white image and set the background:
vtkSmartPointer<vtkImageStencil> imageStencil = vtkSmartPointer<vtkImageStencil>::New();
imageStencil->SetInputData(imageData);
imageStencil->SetStencilConnection(pdtoImageStencil->GetOutputPort());
imageStencil->ReverseStencilOff();
imageStencil->SetBackgroundValue(outval);
imageStencil->Update();
imageData->DeepCopy(imageStencil->GetOutput());
return imageData;
}
vtkPolyData 如何转换 vtkImageData ?
标签:OLE pre tst nal fill idt text Dimension ast
原文地址:https://www.cnblogs.com/cheungxiongwei/p/12694284.html