Halcon与Winform联合(四)

本文介绍了如何在WindowsForm应用中使用HalconDotNet和VTK库加载Om3格式的点云文件,并实现通过鼠标和滑轮对3D模型进行交互操作。
摘要由CSDN通过智能技术生成

本次使用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);
        }
    }
}

运行之后可以鼠标左键拖拽移动视角,滚动滑轮调整大小,按住滑轮拖拽移动模型
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值