c# - 需要帮助编写 2D 和 3D 之间的转换方法(Point3DToPoint 和 PointAndZToPoint3D)

标签 c# wpf 3d

我是 WPF 3D 的新手,所以我可能只是遗漏了一些明显的东西,但是我如何从 3D 转换为 2D 以及(对于给定的 z 位置)从 2D 转换为 3D?

具体来说,我需要两种转换方式:

  • Point3DToPoint - 如果我在 3D 世界中有一个 (x, y, z) 坐标,我如何确定投影的 2D 表面上的 (x, y) 坐标。方法签名:public Point Point3DToPoint(Point3D point3D)

  • PointAndZToPoint3D - 如果我在投影的 2D 表面上有一个 (x, y) 坐标并且在 3D 世界中有一个 z 位置,我如何确定3D 世界中的 (x, y, z) 坐标?方法签名:public Point3D PointAndZToPoint3D(Point point, double z)

我希望2D 坐标 是从Viewport3D 的左上角测量的位置,3D 坐标 是相对于 3D 世界原点 (0, 0, 0) 的位置。

注1:我找到这个related question ,但它只涉及从 3D 到 2D 的转换(不是相反),我不确定答案是否是最新的。

注意 2:我目前使用的是 .NET 3.5,但如果 .NET 4.0 中有改进可以帮助我,请告诉我。

最佳答案

Charles Petzold 的 3D 库,可以下载 here在“The Petzold.Media3D library”下,包含一个类 ViewportInfo 和这两个静态方法:

  • public static Point Point3DToPoint2D(Viewport3D viewport, Point3d point)

  • public static bool Point2DToPoint3D(Viewport3D viewport, Point, ptIn, out LineRange range)

Point2DToPoint3DPointAndZToPoint3D() 不完全匹配,因为它返回(通过 out 参数)LineRange 而不是特定的点,但恰好 LineRange 有一个方法 PointFromZ(double zValue),它提供了光线与定义的平面相交的点通过 z = zValue

代码示例:

using System.Windows;
using System.Windows.Input;
using Petzold.Media3D;

namespace _3DTester
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        /* This MouseDown event handler prints:
          (1) the current position of the mouse
          (2) the 3D mouse location on the ground plane (z = 0)
          (3) the 2D mouse location converted from the 3D location */

        private void Window_MouseDown(object sender, MouseEventArgs e)
        {
            var range = new LineRange();
            var isValid = ViewportInfo.Point2DtoPoint3D(Viewport, e.GetPosition(Viewport), out range);
            if (!isValid)
                MouseLabel.Content = "(no data)";
            else
            {
                var point3D = range.PointFromZ(0);
                var point = ViewportInfo.Point3DtoPoint2D(Viewport, point3D);
                MouseLabel.Content = e.GetPosition(Viewport) + "\n" + point3D + "\n" + point;
            }
        }
    }
}

XAML 代码

<Window
    x:Class="_3DTester.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="300"
    MouseDown="Window_MouseDown">
    <Grid>
        <Viewport3D Name="Viewport">
            <Viewport3D.Camera>
                <PerspectiveCamera
                    Position="0,0,30"
                    LookDirection="0,0,-1" 
                    UpDirection="0,1,0" />
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <Model3DGroup>
                        <DirectionalLight Color="White" Direction="1,-1,-1" />
                        <GeometryModel3D>
                            <GeometryModel3D.Geometry>
                                <MeshGeometry3D
                                    Positions="0,0,10 -5,-5,0 -5,5,0 5,5,0 5,-5,0"
                                    TriangleIndices="2 1 0  2 0 3  4 3 0  1 4 0" />
                            </GeometryModel3D.Geometry>
                            <GeometryModel3D.Material>
                                <DiffuseMaterial Brush="Red" />
                            </GeometryModel3D.Material>
                        </GeometryModel3D>
                    </Model3DGroup>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>
        <Label Name="MouseLabel" Content="(no data)" />
    </Grid>
</Window>

关于c# - 需要帮助编写 2D 和 3D 之间的转换方法(Point3DToPoint 和 PointAndZToPoint3D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740213/

相关文章:

c# - NRefactory 缺少 dll

javascript - 设置 window.location 后继续执行 javascript 代码以调用 Controller 中的 get 方法吗?

c# - 在 C# 中格式化 Excel 中多行的最快方法

c++ - 开放世界 3d 环境的最佳 map 格式

c# - 如何将对象列表转换为二级字典?

c# - WPF中,XAML是否可以获取当前渲染背景的颜色?

WPF窗口样式在运行时不起作用

c# - 如何在两个或多个 View 中共享单个Viewmodel?

python - 在 python 中旋转 3d 对象

android - 使用动画绘制形状或字母。2D 或 3D 或任何简单的程序?