本次使用winform显示om3格式的点云文件,首先安装acitiviz
在设计器中拖入一个panel一个按钮
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;
using Kitware.VTK;
namespace pointcloud
{
public partial class Form1 : Form
{
private RenderWindowControl renderWindowControl1;
private HTuple hv_ObjectModel3D;
private HTuple hv_Status;
private vtkRenderer VTKrenderer;
private void Form1_Load(object sender, EventArgs e)
{
renderWindowControl1 = new RenderWindowControl();
renderWindowControl1.Dock = System.Windows.Forms.DockStyle.Fill;
renderWindowControl1.Location = new System.Drawing.Point(0, 0);
renderWindowControl1.Name = "aaa";
renderWindowControl1.Size = new System.Drawing.Size(100, 100);
renderWindowControl1.TabIndex = 0;
renderWindowControl1.TestText = null;
panel1.Controls.Add(renderWindowControl1);
VTKrenderer = renderWindowControl1.RenderWindow.GetRenderers().GetFirstRenderer();
VTKrenderer.SetBackground(0.1, 0.88, 0.3);
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
hv_ObjectModel3D = new HTuple();
hv_Status = new HTuple();
HTuple hv_x = new HTuple();
HTuple hv_y = new HTuple();
HTuple hv_z = new HTuple();
HTuple hv_num = new HTuple();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
openFileDialog.Title = "please choose an om3";
openFileDialog.Filter = "om3(*.om3)|*.om3";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var filePath = openFileDialog.FileName;
HOperatorSet.ReadObjectModel3d(filePath, "mm", new HTuple(), new HTuple(), out hv_ObjectModel3D, out hv_Status);
vtkPoints points = h2v_cloud(hv_ObjectModel3D);
show_cloud(points, 1, 0, 0, 2);
}
}
static vtkPoints h2v_cloud(HTuple hv_ObjectModel3D)
{
HTuple hv_x = new HTuple();
HTuple hv_y = new HTuple();
HTuple hv_z = new HTuple();
HTuple hv_num = new HTuple();
HOperatorSet.GetObjectModel3dParams(hv_ObjectModel3D, "point_coord_x", out hv_x);
HOperatorSet.GetObjectModel3dParams(hv_ObjectModel3D, "point_coord_y", out hv_y);
HOperatorSet.GetObjectModel3dParams(hv_ObjectModel3D, "point_coord_z", out hv_z);
HOperatorSet.GetObjectModel3dParams(hv_ObjectModel3D, "num_points", out hv_num);
int num = hv_num[0].I;
vtkPoints points = new vtkPoints();
for (int i = 1; i < num; i++)
{
points.InsertPoint(i, hv_x.DArr[i], hv_y.DArr[i], hv_z.DArr[i]);
}
return points;
}
public void show_cloud(vtkPoints points, double r = 1.0, double g = 1.0, double b = 1.0, float size = 4f)
{
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().SetPointSize(size);
actor.GetProperty().SetColor(r, g, b);
VTKrenderer.AddActor(actor);
}
}
}
运行之后可以鼠标左键拖拽移动视角,滚动滑轮调整大小,按住滑轮拖拽移动模型