代码语言: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;
}
}
}