将混合编程之一,二生成的dll文件找到,并新建winform项目
工具栏,通过NuGet包搜索vtk,找到Activiz.NET.X64,下载安装
(直接拖动RenderWindowControl控件报错,解决方案见我的博客:(73条消息) ActiViz(VTK的C#库)C#64位VTK如何在Visual Studio里创建RenderWindowControl控件_chaonian16的博客-CSDN博客)
将 PCLdll.dll,sharpvtkdll.dll文件分别放入winform项目中的bin目录下,在解决方案中右击引用,添加引用,找到c#库类文件sharpvtkdll.dll
在打开按钮写入如下代码:
private void button1_Click(object sender, EventArgs e)
{
//打开点云文件
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择点云文件";
ofd.InitialDirectory = @"C:\Users\17116\Desktop";
ofd.Filter = "点云文件|*.ply";
ofd.ShowDialog();
url = ofd.FileName;
Class1 test = new Class1();
//string url = @"C:/Users/17116/Desktop/couldfile/text_1000.ply";
//获取点云大小
//int size = Size(url);
int size = test.PcdSize(url);
//创建X,Y,Z数组,用来储存,X,Y,Z坐标
double[] arr_X = new double[size];
double[] arr_Y = new double[size];
double[] arr_Z = new double[size];
//调用dll加载点云函数
//loadPCDFile(url, arr_X, arr_Y, arr_Z);
test.LoadFile(url, ref arr_X,ref arr_Y,ref arr_Z);
//将获取到的X,Y,Z点云数据存入vtkPoint对像
vtkPoints points = vtkPoints.New();
for (int i = 0; i < arr_X.Length; i++)
{
points.InsertNextPoint(arr_X[i], arr_Y[i], arr_Z[i]);
}
vtkPolyData polydata = vtkPolyData.New();
polydata.SetPoints(points);
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(255, 255, 255); // 设置“角色”颜色[RGB]
// Create components of the rendering subsystem
//
vtkRenderer ren1 = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer();
// 新建渲染器
vtkRenderer render1 = vtkRenderer.New();
vtkRenderWindow renWin = renderWindowControl1.RenderWindow;
// 将“角色Actor”添加到“渲染器Renderer”并渲染
renWin.AddRenderer(render1);
render1.AddActor(actor);
// 设置Viewport窗口
render1.SetViewport(0.0, 0.0, 1.0, 1.0);
// 设置背景色
render1.SetBackground(0.0, 0.0, 0.0);
// Add the actors to the renderer, set the window size
// 将“角色Actor”添加到“渲染器Renderer”并渲染
//ren1.AddViewProp(actor); // 渲染器添加角色
//renWin.SetSize(250, 250); // 设置渲染窗口大小[无效语句]
//renWin.Render();
//设置"相机Camera"
//vtkCamera camera = ren1.GetActiveCamera();
//camera.Zoom(1.5);
}
在分割按钮写入如下代码:
private void button2_Click(object sender, EventArgs e)
{
Class1 test = new Class1();
int size = test.PcdSize(url);
float[] arr_X = new float[size];
float[] arr_Y = new float[size];
float[] arr_Z = new float[size];
//分割算法
//PassThoughPCDFile(url, arr_X, arr_Y, arr_Z);
test.PassThoughPCD(url, ref arr_X, ref arr_Y, ref arr_Z);
vtkPoints points = vtkPoints.New();
for (int i = 0; i < arr_X.Length; i++)
{
points.InsertNextPoint(arr_X[i], arr_Y[i], arr_Z[i]);
}
vtkPolyData polydata = vtkPolyData.New();
polydata.SetPoints(points);
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(255, 255, 0);
// Create components of the rendering subsystem
//
vtkRenderer ren1 = renderWindowControl2.RenderWindow.GetRenderers().GetFirstRenderer();
vtkRenderWindow renWin = renderWindowControl2.RenderWindow;
// Add the actors to the renderer, set the window size
//
ren1.AddViewProp(actor);
renWin.SetSize(250, 250);
renWin.Render();
}
在显示点云大小按钮下写入如下代码:
private void button3_Click(object sender, EventArgs e)
{
//显示点云大小
Class1 test = new Class1();
int size = test.PcdSize(url);
textBox1.Text = size.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
//显示点云大小
Class1 test = new Class1();
int size = test.PcdSize(url);
textBox1.Text = size.ToString();
}
右击项目,属性改成64位
注意:项目框架和库类框架一致
运行后结果如下: