前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >显示PCL点云数据

显示PCL点云数据

原创
作者头像
秦建辉
修改2024-08-27 21:46:49
2310
修改2024-08-27 21:46:49
代码语言:xml
复制
<Window x:Class="FirstSolver.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="16"
        TextOptions.TextFormattingMode="Ideal" 
        TextOptions.TextRenderingMode="ClearType"       
        Background="{DynamicResource MaterialDesignPaper}"
        FontFamily="Microsoft YaHei Light"
        Name="RootWindow" Title="PCL点云数据" WindowState="Maximized" WindowStartupLocation="CenterScreen">
    <materialDesign:DialogHost Identifier="RootDialog" DialogTheme="Inherit">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <TextBox Grid.Column="0" Style="{StaticResource MaterialDesignFilledTextBox}" materialDesign:HintAssist.Hint="点云数据文件" Name="TextBoxPclPath" IsReadOnly="True"/>
                <Button Grid.Column="1" Margin="4" Style="{StaticResource MaterialDesignIconButton}" Click="ButtonSelectFile">
                    <Button.Content>
                        <materialDesign:PackIcon Kind="FolderOpenOutline"/>
                    </Button.Content>
                </Button>
                <Button Grid.Column="2" Margin="4" Style="{StaticResource MaterialDesignIconButton}" Click="ButtonShowPcl">
                    <Button.Content>
                        <materialDesign:PackIcon Kind="HumanRunFast"/>
                    </Button.Content>
                </Button>
            </Grid>

            <WindowsFormsHost Grid.Row="1">
                <vtk:RenderWindowControl x:Name="VtkFormControl1"/>
            </WindowsFormsHost>
        </Grid>
    </materialDesign:DialogHost>
</Window>
代码语言:cs
复制
using Kitware.VTK;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;

namespace FirstSolver
{
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 对话框宿主标识符
        /// </summary>
        public const string DialogHostIdentifier = "RootDialog";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonSelectFile(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "PCD|*.pcd",
                DereferenceLinks = true
            };

            if (dlg.ShowDialog() == true)
            {
                TextBoxPclPath.Text = dlg.FileName;
            }
        }

        private void ButtonShowPcl(object sender, RoutedEventArgs e)
        {
            try
            {
                string path = TextBoxPclPath.Text;
                if (!File.Exists(path)) return;

                vtkPoints points = ReadPcd(path);
                vtkPolyVertex polyVertex = vtkPolyVertex.New(); // 多顶点

                long number = points.GetNumberOfPoints();
                polyVertex.GetPointIds().SetNumberOfIds(number);
                for (long i = 0; i < number; i++)
                {
                    polyVertex.GetPointIds().SetId(i, i);
                }
                vtkUnstructuredGrid grid = vtkUnstructuredGrid.New(); // 网格
                grid.SetPoints(points);
                grid.InsertNextCell(polyVertex.GetCellType(), polyVertex.GetPointIds());
                vtkDataSetMapper mapper = vtkDataSetMapper.New();
                mapper.SetInputData(grid);

                // 演员
                vtkActor actor = vtkActor.New();
                actor.SetMapper(mapper);
                actor.GetProperty().SetColor(1, 0, 0);

                // WinForm控件
                vtkRenderWindow renderWindow = VtkFormControl1.RenderWindow;
                vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
                renderer.RemoveAllViewProps();

                renderer.AddActor(actor);
                renderer.SetBackground(0, 0, 0);
                renderer.ResetCamera();
                renderWindow.Render();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "异常", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private vtkPoints ReadPcd(string pcd)
        {
            vtkPoints points = vtkPoints.New();
            using (StreamReader sr = new StreamReader(pcd, Encoding.ASCII))
            {
                for (int i = 0; i < 11; i++) sr.ReadLine();

                for (int i = 0; !sr.EndOfStream; i++)
                {
                    double[] point = sr.ReadLine().Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries).Select(x => double.Parse(x)).ToArray();
                    points.InsertNextPoint(point[0], point[1], point[2]);
                }
            }
            return points;
        }
    }
}
ism_train_michael.pcd
ism_train_michael.pcd

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档