转载来源:http://blog.163.com/adslxk@126/blog/static/100610671200911169232935/
最近无聊,琢磨出了如何通过已有的内存三维体数据场进行三维重建的vtk实现。以抛砖引玉:
const int dim=50;
void main()
{
//手动创建一个内存体数据
unsigned short da
for (int i=0;i<dim;i++)
for (int j=0;j<dim;j++)
for (int k=0;k<dim;k++)
da
//根据内存数据da
vtkUnsignedShortArray *array=vtkUnsignedShortArray::New();
array->SetVoidArray(da
//设置三维数据场格式
vtkImageData *imageData=vtkImageData::New();
imageData->GetPointData()->SetScalars(array);
imageData->SetDimensions(dim, dim, dim);//三维数据场的x、y、z方向上的体素个数
imageData->SetScalarTypeToUnsignedShort();//数据类型需要和三维数据场的实际类型一致
imageData->SetSpacing(1,1,1);//每个体素的大小
imageData->SetOrigin(0,0,0);
vtkPiecewiseFunction* opacityTransferFunction=vtkPiecewiseFunction::New();
opacityTransferFunction->AddPoint(1000,1.0);
opacityTransferFunction->ClampingOff();
vtkColorTransferFunction *colorTransferFunction=vtkColorTransferFunction::New(); colorTransferFunction->AddRGBPoint(1000.0, 0.8, 0.8, 0.8);
vtkVolumeProperty * volumeProperty=vtkVolumeProperty::New();
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->SetInterpolationTypeToLinear();
vtkVolumeRayCastCompositeFunction* compositeFunction=vtkVolumeRayCastCompositeFunction::New();
vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
volumeMapper->SetVolumeRayCastFunction(compositeFunction);
volumeMapper->SetInput(imageData);
vtkVolume* volume=vtkVolume::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
vtkRenderer* ren=vtkRenderer::New();
ren->AddVolume(volume);
ren->SetBackground(0,0,1);
vtkRenderWindow* renwin=vtkRenderWindow::New();
renwin->AddRenderer(ren);
vtkRenderWindowInteractor* iren=vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renwin);
renwin->Render();
iren->Start();
}