栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Cocos2d-x

C#+VTK显示点云

Cocos2d-x 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#+VTK显示点云

PS:第一次处理点云,记录一下。

基础环境:VS(我用的2019 ,之前用2012离线版报错了)
话不多说,先上效果图:

效果肯定没有CloudCompare好,但是能处理就行了,这个视窗按住鼠标中键是平移,左键拖动是旋转,设计有点反人类

具体实现步骤如下
第一步:
新建一个Win From窗体,用于放置控件。
(这个我就不放图了)
第二步:
通过Nuget包管理工具下载VTK的库

安装完在项目的引用里会多两个DLL

第三步:
当有了这两个DLL后,去工具箱的控件里找renderWindowControl控件,把它拖到winfrom 桌面,再放一个button 按钮,以方便验证程式

最后调用库里的IO类,去读写点文件,并显示出来,源码如下:
private void ReadPlainText()
{
// Path to vtk data must be set as an environment variable
// VTK_DATA_ROOT = “C:VTKvtkdata-5.8.0”
vtkTesting test = vtkTesting.New();
string root = test.GetDataRoot();
//string filePath = System.IO.Path.Combine(root, @“Datateapot.xyz”);
string filePath = System.IO.Path.Combine(@“test2.xyz”);
FileStream fs = null;
StreamReader sr = null;
String sLineBuffer;
String[] sXYZ;
char[] chDelimiter = new char[] { ’ ', ‘t’, ‘;’ };
double[] xyz = new double[3];
vtkPoints points = vtkPoints.New();
int cnt = 0;

        try
        {
            // in case file must be open in another application too use "FileShare.ReadWrite"
            fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            sr = new StreamReader(fs);
            while (!sr.EndOfStream)
            {
                sLineBuffer = sr.ReadLine();
                cnt++;
                sXYZ = sLineBuffer.Split(chDelimiter, StringSplitOptions.RemoveEmptyEntries);
                if (sXYZ == null || sXYZ.Length < 3)
                {
                    MessageBox.Show("data seems to be in wrong format at line " + cnt, "Format Exception", MessageBoxButtons.OK);
                    return;
                }
                xyz[0] = double.Parse(sXYZ[0], CultureInfo.InvariantCulture);
                xyz[1] = double.Parse(sXYZ[1], CultureInfo.InvariantCulture);
                xyz[2] = double.Parse(sXYZ[2], CultureInfo.InvariantCulture);
                points.InsertNextPoint(xyz[0], xyz[1], xyz[2]);
            }
            vtkPolyData polydata = vtkPolyData.New();
            polydata.SetPoints(points);
            vtkVertexGlyphFilter glyphFilter = vtkVertexGlyphFilter.New();
            glyphFilter.SetInputConnection(polydata.GetProducerPort());

            // Visualize
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
            mapper.SetInputConnection(glyphFilter.GetOutputPort());

            vtkActor actor = vtkActor.New();
            actor.SetMapper(mapper);
            actor.GetProperty().SetPointSize(4);
            actor.GetProperty().SetColor(1, 0.5, 0);
            // get a reference to the renderwindow of our renderWindowControl1
            vtkRenderWindow renderWindow = renderWindowControl1.RenderWindow;
            // renderer
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
            // set background color
            renderer.SetBackground(0.2, 0.3, 0.4);
            // add our actor to the renderer
            renderer.AddActor(actor);
        }
        catch (IOException ex)
        {
            MessageBox.Show(ex.Message, "IOException", MessageBoxButtons.OK);
        }
        finally
        {
            if (sr != null)
            {
                sr.Close();
                sr.Dispose();
                sr = null;
            }
        }
    }

PS:大家也可以去官方原网站学习,里面也有C#的范例
官方地址:https://kitware.github.io/vtk-examples/site/CSharp/IO/ReadPLY/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967787.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号