使用
wpf,C#,Kitware.VTK
实现
有两个点,画出这两点之间的一条直线
- 第一次实现时,画面只显示两个点,没有显示直线,如下代码
private void DrawGlueWidth()
{
List<double[]> Points = new List<double[]>();
Points.Add(new double[] { 100, 100, 100 });
Points.Add(new double[] { 50, 50, 50 });
// Create a points array
vtkPoints points = vtkPoints.New();
// Create a cell array to store the lines
vtkCellArray lines = vtkCellArray.New();
vtkCellArray ps = vtkCellArray.New();
for (int i = 0; i < Points.Count; i++)
{
points.InsertNextPoint(Points[i][0], Points[i][1], Points[i][2]);
if (i == 0 || i % 2 == 0)
{
// Create a line between the points
vtkLine line = vtkLine.New();
line.GetPointIds().SetId(0, i);
line.GetPointIds().SetId(1, i + 1);
lines.InsertNextCell(line);
}
}
vtkLine line = vtkLine.New();
line.GetPointIds().SetId(0, 0);
line.GetPointIds().SetId(1, 1);
lines.InsertNextCell(line);
// Create a polyline data structure
vtkPolyData polyData = vtkPolyData.New();
polyData.SetPoints(points);
polyData.SetLines(lines);
// Create a mapper and actor
vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
glyphFilter.SetInputConnection(polyData.GetProducerPort());
vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(glyphFilter.GetOutputPort());
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
actor.GetProperty().SetPointSize(5);
actor.GetProperty().SetLineStipplePattern(15);
actor.GetProperty().SetLineWidth(15);
// Add the actor to the renderer
vtkRenderer renderer = renderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
renderControl.RenderWindow.Render();
- 第二次更改后, 可以了,如下代码
private void DrawGlueWidth()
{
List<double[]> Points = new List<double[]>();
Points.Add(new double[] { 58.9000015258789, -11.6255941390991, 56.7488250732422 });
Points.Add(new double[] { 63.7000007629395, -11.7111196517944, 56.80859375 });
// Create a points array
vtkPoints points = vtkPoints.New();
// Create a cell array to store the lines
vtkCellArray lines = vtkCellArray.New();
for (int i = 0; i < Points.Count; i++)
{
points.InsertNextPoint(Points[i][0], Points[i][1], Points[i][2]);
if (i == 0 || i % 2 == 0)
{
// Create a line between the points
vtkLine line = vtkLine.New();
line.GetPointIds().SetId(0, i);
line.GetPointIds().SetId(1, i + 1);
lines.InsertNextCell(line);
}
}
// 创建一个PolyData对象来存储点和线
vtkPolyData polyData = vtkPolyData.New();
polyData.SetPoints(points);
polyData.SetLines(lines);
// 创建一个Mapper和Actor来渲染点和线
vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(polyData.GetProducerPort());
vtkActor actor = vtkActor.New();
actor.SetMapper(mapper);
actor.GetProperty().SetColor(0, 0, 1);
actor.GetProperty().SetLineWidth(2);
// Add the actor to the renderer
vtkRenderer renderer = renderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
renderControl.RenderWindow.Render();
}