直接上的代码:
package vtkdemoeasy;
import java.util.Random;
import vtk.vtkColorTransferFunction;
import vtk.vtkFloatArray;
import vtk.vtkGPUVolumeRayCastMapper;
import vtk.vtkImageData;
import vtk.vtkInformation;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkNativeLibrary;
import vtk.vtkPiecewiseFunction;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkVolume;
import vtk.vtkVolumeProperty;
public class Imagedata {
/**
* @param args
*/
static {
if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
if (!lib.IsLoaded()) {
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
public static void creatImageData() {
int min_gray = 4;
int max_gray = 200;
double transparency = 0.03;
int dx = 5;
int dy =5;
int dz = 5;
int x = 100;
int y = 100;
int z = 50;
//-----_建立图像数据-------
vtkImageData img=new vtkImageData();
vtkInformation info=new vtkInformation();
//vtkInformation info2=img.GetInformation();
//此处也可以从img里面获取information,如
//果information为空,则会创建一个新的,相当于调用New之后再SetInformation
img.SetDimensions(x, y, z);
img.SetSpacing(dx, dy, dz);
img.SetOrigin(0, 0, 0);
img.SetScalarType(8, info);//img->SetScalarType(VTK_UNSIGNED_CHAR, info);
img.SetNumberOfScalarComponents(1, info);
img.AllocateScalars(info);
int size=img.GetScalarSize();
int type=img.GetScalarType();
System.out.println(size);
System.out.println(type);
float temp, max = 0;
float[] tempfloat = new float[x * y * z];
vtkFloatArray pointsScalars= new vtkFloatArray();
for (int i = 0; i<x * y * z; i++)
{
Random ran=new Random();
temp=ran.nextFloat();
System.out.println(temp);
if (max < temp)
max = temp;
tempfloat[i]=temp;
}
for (int i = 0; i<x * y * z; i++) {
pointsScalars.SetNumberOfTuples(x * y * z);//此行可有可无
pointsScalars.InsertValue(i,(tempfloat[i] / max * 255));//第1个参数是points点的Id,第2个参数是该点的属性值
}
img.GetPointData().SetScalars(pointsScalars);
vtkGPUVolumeRayCastMapper volumeMapper =new vtkGPUVolumeRayCastMapper();
volumeMapper.SetInputData(img);
vtkVolumeProperty volumeProperty=new vtkVolumeProperty();
volumeProperty.SetInterpolationTypeToLinear();
//volumeProperty.ShadeOn(); //打开或者关闭阴影测试
volumeProperty.SetAmbient(0.4);
volumeProperty.SetDiffuse(0.6);
volumeProperty.SetSpecular(0.2);
vtkPiecewiseFunction compositeOpacity =new vtkPiecewiseFunction();
compositeOpacity.ClampingOn();
compositeOpacity.AddPoint(min_gray, 0);
compositeOpacity.AddPoint(max_gray, transparency);
volumeProperty.SetScalarOpacity(compositeOpacity); //设置不透明度传输函数
vtkColorTransferFunction color=new vtkColorTransferFunction();
color.AddRGBPoint(0.0, 0.0, 0.0, 0.0);
color.AddRGBPoint(36.0, 1.0, 0.0, 0.0);
color.AddRGBPoint(72.0, 1.0, 1.0, 0.0);
color.AddRGBPoint(108.0, 0.0, 1.0, 0.0);
color.AddRGBPoint(144.0, 0.0, 1.0, 1.0);
color.AddRGBPoint(180.0, 0.0, 0.0, 1.0);
color.AddRGBPoint(216.0, 1.0, 0.0, 1.0);
color.AddRGBPoint(255.0, 1.0, 1.0, 1.0);
volumeProperty.SetColor(color);
vtkVolume volume = new vtkVolume();
volume.SetMapper(volumeMapper);
volume.SetProperty(volumeProperty);
vtkRenderer ren =new vtkRenderer();
ren.SetBackground(1.0, 1.0, 1.0);
ren.AddVolume(volume);
//ren.ResetCamera();
//创建显示窗口
vtkRenderWindow renWin= new vtkRenderWindow();
renWin.AddRenderer(ren);
vtkRenderWindowInteractor iren= new vtkRenderWindowInteractor();
vtkInteractorStyleTrackballCamera style= new vtkInteractorStyleTrackballCamera();
iren.SetInteractorStyle(style);
iren.SetRenderWindow(renWin);
renWin.SetSize(700,700);
renWin.Render();
iren.Start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
creatImageData();
}
}