vtk vtkPolyData 转 vtkImageData



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.5;
		spacing[1] = 0.5;
		spacing[2] = 0.5;
		imageData->SetSpacing(spacing);

		// compute dimensions
		int dim[3];
		for (int i = 0; i < 3; i++)
		{
			dim[i] = static_cast<int>(ceil((bounds[i * 2 + 1] - bounds[i * 2]) / spacing[i]));
		}
		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);

#if VTK_MAJOR_VERSION <= 5
		whiteImage->SetScalarTypeToUnsignedChar();
		whiteImage->AllocateScalars();
#else
		imageData->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
#endif
		// 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(spacing);
		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;
	}
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页