基于vtk绘制简单图形元素
1、生成简单模型
利用vtk的典型流程是:读取或生成一些数据,过滤(filter)它,渲染(render)它,然后与它交互。
有两种基本的获取数据的方式。这些数据可能是存在文件中,需要读入vtk系统,数据也可能有一些方法生成(通过算法或数学表达)。获取数据用于渲染管线初始化的过程称作源对象(source object),用于生成数据的源对象称为过程对象(procedural object),用于读取数据的对象称为读取器对象(reader object)。
比如vtkCylinderSource源对象,用于生成圆柱的多边形表示,它的输出是圆柱(cylinder),可以作为多边形数据映射器的输入。比如vtkSphereSource源对象,用于生成球面的多边形表示,它的输出是球(sphere),可以作为多边形映射器的输入,在输入之前可以进行收缩操作vtkShrinkPolyData,收缩后可以看见球面的三角形表示。
1.1//画点
private void DrawPoint()
{
// Create the geometry of the points (the coordinate)
vtkPoints points = vtkPoints.New();
double[,] p = new double[,]
{
{1.0, 2.0, 3.0},
{3.0, 1.0, 2.0},
{2.0, 3.0, 1.0},
{1.0, 3.0, 3.0}
};
// Create topology of the points (a vertex per point)
vtkCellArray vertices = vtkCellArray.New();
int nPts = 4;
int[] ids = new int[nPts];
for(int i = 0; i<nPts; i++)
ids[i] = (int)points.InsertNextPoint(p[i, 0], p[i, 1], p[i, 2]);
int size = Marshal.SizeOf(typeof(int)) * nPts;
IntPtr pIds = Marshal.AllocHGlobal(size);
Marshal.Copy(ids, 0, pIds, nPts);
vertices.InsertNextCell(nPts, pIds);
Marshal.FreeHGlobal(pIds);
// Create a polydata object
vtkPolyData pointPoly = vtkPolyData.New();
// Set the points and vertices we created as the geometry and topology of the polydata
pointPoly.SetPoints(points);
pointPoly.SetVerts(vertices);
// Visualize
vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
mapper.SetInputData(pointPoly);
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetPointSize(10);
vtkRenderWindow renderWindow = myRenderWindowControl.RenderWindow;
vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
renderer.SetBackground(0.3, 0.2, 0.1);
renderer.AddActor(actor);
}
1.2//画三角形
private void DrawTriangle()
{
//创建点数据
vtkPoints points = vtkPoints.New();
points.InsertNextPoint(1.0, 0.0, 0.0);
points.InsertNextPoint(0.0, 1.0, 0.0);
points.InsertNextPoint(0.0, 0.0, 0.0);
//每两个坐标之间分别创建一条线
//SetId()的第一个参数是线段的端点ID,第二参数是连接的的点的ID
vtkLine line0 = vtkLine.New();
line0.GetPointIds().SetId(0, 0);
line0.GetPointIds().SetId(1, 1);
vtkLine line1 = vtkLine.New();
line1.GetPointIds().SetId(0, 1);
line1.GetPointIds().SetId(1, 2);
vtkLine line2 = vtkLine.New();
line2.GetPointIds().SetId(0, 2);
line2.GetPointIds().SetId(1, 0);
//创建单元数组,用于存储以上创建的线段
vtkCellArray lines = vtkCellArray.New();
lines.InsertNextCell(line0);
lines.InsertNextCell(line1);
lines.InsertNextCell(line2);
//将点和线加入数据集中,前者定义数据集的几何结构,后者定义拓扑结构
//创建vtkPolyData类型的数据,是一种数据集
vtkPolyData polyData = vtkPolyData.New();
//将创建的点数据加入vtkPolyData数据里
polyData.SetPoints(points); //点数据定义了polydata数据集的几何结构。
polyData.SetLines(lines); //定义拓扑结构
//显示数据
vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
mapper.SetInputData(polyData);
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1.0, 0.0, 0.0);
vtkRenderWindow renWin = myRenderWindowControl.RenderWindow;
vtkRenderer renderer = renWin.GetRenderers().GetFirstRenderer();
renderer.SetBackground(1.0, 1.0, 1.0);
renderer.AddActor(actor);
}
1.3//画圆柱
private void DrawCylinder()
{
vtkCylinderSource cylinderSource = vtkCylinderSource.New();
//cylinderSource.SetHeight(3.0);
//cylinderSource.SetRadius(0.1);
cylinderSource.SetResolution(8);
vtkPolyDataMapper cylinderMapper = vtkPolyDataMapper.New();
cylinderMapper.SetInputConnection(cylinderSource.GetOutputPort());
vtkActor cylinderActor = vtkActor.New();
cylinderActor.SetMapper(cylinderMapper);
//cylinderActor.GetProperty().SetColor(1.0, 0.0, 0.0);
cylinderActor.GetProperty().SetColor((float)Color.Tomato.R/256, (float)Color.Tomato.G/256, (float)Color.Tomato.B/256);
cylinderActor.RotateX(30.0);
cylinderActor.RotateY(-45.0);
vtkRenderWindow renWin = renderWindowControl1.RenderWindow;
vtkRenderer renderer = renWin.GetRenderers().GetFirstRenderer();
renderer.SetBackground(1.0, 1.0, 1.0);
renderer.AddActor(cylinderActor);
}
1.4//画球体
private void DrawSphere(double radius)
{
vtkSphereSource sphereSource = vtkSphereSource.New();
sphereSource.SetThetaResolution(8);
sphereSource.SetPhiResolution(16);
sphereSource.SetRadius(radius);
vtkShrinkPolyData shrink = vtkShrinkPolyData.New();
shrink.SetInputConnection(sphereSource.GetOutputPort());
shrink.SetShrinkFactor(0.8);
vtkPolyDataMapper sphereMapper = vtkPolyDataMapper.New();
//sphereMapper.SetInputConnection(sphereSource.GetOutputPort());
sphereMapper.SetInputConnection(shrink.GetOutputPort());
vtkActor sphereActor = vtkActor.New();
sphereActor.SetMapper(sphereMapper);
sphereActor.GetProperty().SetColor(1, 0, 0);
vtkRenderer sphereRender = vtkRenderer.New();
vtkRenderWindow renWin = renderWindowControl1.RenderWindow;
renWin.AddRenderer(sphereRender);
sphereRender.AddActor(sphereActor);
sphereRender.SetBackground(0.0, 0.0, 1.0);
}
1.5//画组合体
private void DrawAssembly()
{
//Create four parts: a top level assembly and three primitives
vtkSphereSource sphereSource = vtkSphereSource.New();
vtkPolyDataMapper sphereMapper = vtkPolyDataMapper.New();
sphereMapper.SetInputConnection(sphereSource.GetOutputPort());
vtkActor sphereActor = vtkActor.New();
sphereActor.SetMapper(sphereMapper);
sphereActor.SetOrigin(2, 1, 3);
sphereActor.RotateY(6);
sphereActor.SetPosition(2.25, 0, 0);
sphereActor.GetProperty().SetColor(1, 0, 1);
vtkCubeSource cubeSource = vtkCubeSource.New();
vtkPolyDataMapper cubeMapper = vtkPolyDataMapper.New();
cubeMapper.SetInputConnection(cubeSource.GetOutputPort());
vtkActor cubeActor = vtkActor.New();
cubeActor.SetMapper(cubeMapper);
cubeActor.SetPosition(0, 2.25, 0);
cubeActor.GetProperty().SetColor(0, 0, 1);
vtkConeSource coneSource = vtkConeSource.New();
vtkPolyDataMapper coneMapper = vtkPolyDataMapper.New();
coneMapper.SetInputConnection(coneSource.GetOutputPort());
vtkActor coneActor = vtkActor.New();
coneActor.SetMapper(coneMapper);
coneActor.SetPosition(0, 0, 2.25);
coneActor.GetProperty().SetColor(0, 1, 0);
vtkCylinderSource cylinderSource = vtkCylinderSource.New();
vtkPolyDataMapper cylinderMapper = vtkPolyDataMapper.New();
cylinderMapper.SetInputConnection(cylinderSource.GetOutputPort());
vtkActor cylinderActor = vtkActor.New();
cylinderActor.SetMapper(cylinderMapper);
//cylinderActor.SetPosition(0, 0, 0);
cylinderActor.GetProperty().SetColor(1, 0, 0);
vtkAssembly assembly = vtkAssembly.New();
assembly.AddPart(cylinderActor);
assembly.AddPart(sphereActor);
assembly.AddPart(cubeActor);
assembly.AddPart(coneActor);
assembly.SetOrigin(5, 10, 5);
assembly.AddPosition(5, 0, 0);
assembly.RotateX(15);
vtkRenderer renderer = vtkRenderer.New();
vtkRenderWindow renWin = myRenderWindowControl.RenderWindow;
renWin.AddRenderer(renderer);
renderer.AddActor(assembly);
renderer.AddActor(coneActor);
}
1.6//体数据提取等制图
private void DrawVisQuad()
{
//# This example demonstrates the use of the contour filter, and the use of
//# the vtkSampleFunction to generate a volume of data samples from an
//# implicit function.
//# VTK supports implicit functions of the form f(x,y,z)=constant. These
//# functions can represent things spheres, cones, etc. Here we use a
//# general form for a quadric to create an elliptical data field.
vtkQuadric quadricFunction = vtkQuadric.New();
quadricFunction.SetCoefficients(0.5, 1, 0.2, 0, 0.1, 0, 0, 0.2, 0, 0);
//# vtkSampleFunction samples an implicit function over the x-y-z range
//# specified (here it defaults to -1,1 in the x,y,z directions).
vtkSampleFunction sample = vtkSampleFunction.New();
sample.SetSampleDimensions(30, 30, 30);
sample.SetImplicitFunction(quadricFunction);
//# Create five surfaces F(x,y,z) = constant between range specified. The
//# GenerateValues() method creates n isocontour values between the range
//# specified.
vtkContourFilter contourFilter = vtkContourFilter.New();
contourFilter.SetInputConnection(sample.GetOutputPort());
contourFilter.GenerateValues(10, 0.0, 1.2);
vtkPolyDataMapper contMapper = vtkPolyDataMapper.New();
contMapper.SetInputConnection(contourFilter.GetOutputPort());
contMapper.SetScalarRange(0.0, 1.2);
vtkActor conActor = vtkActor.New();
conActor.SetMapper(contMapper);
//We'll put a simple outline around the data
vtkOutlineFilter outline = vtkOutlineFilter.New();
outline.SetInputConnection(sample.GetOutputPort());
vtkPolyDataMapper outlineMapper = vtkPolyDataMapper.New();
outlineMapper.SetInputConnection(outline.GetOutputPort());
vtkActor outlineActor = vtkActor.New();
outlineActor.SetMapper(outlineMapper);
outlineActor.GetProperty().SetColor(0, 0, 0);
//The usual rendering stuff
vtkRenderer ren = vtkRenderer.New();
vtkRenderWindow renWin = myRenderWindowControl.RenderWindow;
//vtkRenderWindow renWin = vtkRenderWindow.New();
renWin.AddRenderer(ren);
//vtkRenderWindowInteractor iren = vtkRenderWindowInteractor.New();
//iren.SetRenderWindow(renWin);
ren.SetBackground(1, 1, 1);
ren.AddActor(conActor);
ren.AddActor(outlineActor);
//iren.Initialize();
//renWin.Render();
//iren.Start();
}
1.7//生成非结构网格
private void DrawBuildUGrid()
{
//# This example shows how to manually construct unstructured grids
//# using C#. Unstructured grids require explicit point and cell
//# representations, so every point and cell must be created, and then
//# added to the vtkUnstructuredGrid instance.
//# Create several unstructured grids each containing a cell of a
//# different type.
//create voxel
vtkPoints voxelPoints = vtkPoints.New();
voxelPoints.SetNumberOfPoints(8);
voxelPoints.InsertPoint(0, 0, 0, 0);
voxelPoints.InsertPoint(1, 1, 0, 0);
voxelPoints.InsertPoint(2, 0, 1, 0);
voxelPoints.InsertPoint(3, 1, 1, 0);
voxelPoints.InsertPoint(4, 0, 0, 1);
voxelPoints.InsertPoint(5, 1, 0, 1);
voxelPoints.InsertPoint(6, 0, 1, 1);
voxelPoints.InsertPoint(7, 1, 1, 1);
vtkVoxel aVoxel = vtkVoxel.New();
aVoxel.GetPointIds().SetId(0, 0);
aVoxel.GetPointIds().SetId(1, 1);
aVoxel.GetPointIds().SetId(2, 2);
aVoxel.GetPointIds().SetId(3, 3);
aVoxel.GetPointIds().SetId(4, 4);
aVoxel.GetPointIds().SetId(5, 5);
aVoxel.GetPointIds().SetId(6, 6);
aVoxel.GetPointIds().SetId(7, 7);
vtkUnstructuredGrid aVoxelGrid = vtkUnstructuredGrid.New();
aVoxelGrid.Allocate(1, 1);
aVoxelGrid.InsertNextCell(aVoxel.GetCellType(), aVoxel.GetPointIds());
aVoxelGrid.SetPoints(voxelPoints);
vtkDataSetMapper aVoxelMapper = vtkDataSetMapper.New();
aVoxelMapper.SetInputData(aVoxelGrid);
vtkActor aVoxelActor = vtkActor.New();
aVoxelActor.SetMapper(aVoxelMapper);
aVoxelActor.GetProperty().SetDiffuseColor(1, 0, 0);
//create Hexahedron
vtkPoints hexahedronPoints = new vtkPoints();
hexahedronPoints.SetNumberOfPoints(8);
hexahedronPoints.InsertPoint(0, 0, 0, 0);
hexahedronPoints.InsertPoint(1, 1, 0, 0);
hexahedronPoints.InsertPoint(2, 1, 1, 0);
hexahedronPoints.InsertPoint(3, 0, 1, 0);
hexahedronPoints.InsertPoint(4, 0, 0, 1);
hexahedronPoints.InsertPoint(5, 1, 0, 1);
hexahedronPoints.InsertPoint(6, 1, 1, 1);
hexahedronPoints.InsertPoint(7, 0, 1, 1);
vtkHexahedron aHexahedron = new vtkHexahedron();
aHexahedron.GetPointIds().SetId(0, 0);
aHexahedron.GetPointIds().SetId(1, 1);
aHexahedron.GetPointIds().SetId(2, 2);
aHexahedron.GetPointIds().SetId(3, 3);
aHexahedron.GetPointIds().SetId(4, 4);
aHexahedron.GetPointIds().SetId(5, 5);
aHexahedron.GetPointIds().SetId(6, 6);
aHexahedron.GetPointIds().SetId(7, 7);
vtkUnstructuredGrid aHexahedronGrid = new vtkUnstructuredGrid();
aHexahedronGrid.Allocate(1, 1);
aHexahedronGrid.InsertNextCell(aHexahedron.GetCellType(), aHexahedron.GetPointIds());
aHexahedronGrid.SetPoints(hexahedronPoints);
vtkDataSetMapper aHexahedronMapper = new vtkDataSetMapper();
aHexahedronMapper.SetInputData(aHexahedronGrid);
vtkActor aHexahedronActor = new vtkActor();
aHexahedronActor.SetMapper(aHexahedronMapper);
aHexahedronActor.AddPosition(2, 0, 0);
aHexahedronActor.GetProperty().SetDiffuseColor(1, 1, 0);
vtkRenderer render = vtkRenderer.New();
vtkRenderWindow renWin = myRenderWindowControl.RenderWindow;
renWin.AddRenderer(render);
render.SetBackground(0, 0, 1);
render.AddActor(aVoxelActor);
render.AddActor(aHexahedronActor);
}