显示立方体和3D点云

2024-08-28 10:03:18 浏览数 (2)

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

            <StackPanel Grid.Row="0" Orientation="Horizontal">
                <Button Name="ButtonShowCube" Style="{StaticResource MaterialDesignFlatButton}" Content="显示立方体" Click="ButtonShowCube_Click"/>
                <Button Name="ButtonShowPcl" Style="{StaticResource MaterialDesignFlatButton}" Content="显示3D点云" Click="ButtonShowPcl_Click"/>
            </StackPanel>

            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <WindowsFormsHost Grid.Column="0" Margin="0,0,4,0">
                    <vtk:RenderWindowControl x:Name="VtkFormControl1"/>
                </WindowsFormsHost>

                <WindowsFormsHost Grid.Column="1" Margin="4,0,0,0">
                    <vtk:RenderWindowControl x:Name="VtkFormControl2"/>
                </WindowsFormsHost>
            </Grid>
        </Grid>
    </materialDesign:DialogHost>
</Window>
代码语言:cs复制
using Kitware.VTK;
using System.Windows;

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

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonShowCube_Click(object sender, RoutedEventArgs e)
        {
            // 立方体
            vtkCubeSource cube = vtkCubeSource.New();
            vtkPolyDataMapper cubeMapper = vtkPolyDataMapper.New();
            cubeMapper.SetInputConnection(cube.GetOutputPort());

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

            // WinForm控件
            vtkRenderWindow renderWindow = VtkFormControl1.RenderWindow;
            vtkRenderer renderer = renderWindow.GetRenderers().GetFirstRenderer();
            renderer.RemoveAllViewProps();
            renderer.AddActor(actor);
            renderer.SetBackground(0.1, 0.2, 0.4);
            renderer.ResetCamera();
            renderWindow.Render();
        }

        private void ButtonShowPcl_Click(object sender, RoutedEventArgs e)
        {
            // 创建点云数据
            vtkPoints points = vtkPoints.New();
            points.InsertNextPoint(0.0, 0.0, 0.0);
            points.InsertNextPoint(1.0, 1.0, 1.0);
            points.InsertNextPoint(-1.0, -1.0, -1.0);

            // 创建PolyData对象
            vtkPolyData polyData = vtkPolyData.New();
            polyData.SetPoints(points);

            // 将点渲染为球体
            vtkSphereSource sphereSource = vtkSphereSource.New();
            sphereSource.SetRadius(0.1); // 设置球体半径

            vtkGlyph3D glyph3D = vtkGlyph3D.New(); // 为输入数据的每一个点生成符号
            glyph3D.SetInputData(polyData);
            glyph3D.SetSourceConnection(sphereSource.GetOutputPort());

            // 创建mapper和actor
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New();
            mapper.SetInputConnection(glyph3D.GetOutputPort());

            vtkActor actor = vtkActor.New();
            actor.SetMapper(mapper);

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

            renderer.AddActor(actor);
            renderer.SetBackground(0.1, 0.2, 0.4);
            renderer.ResetCamera();
            renderWindow.Render();
        }
    }
}
vtk

0 人点赞