需要帮助编写2D和3D之间的转换方法(Point3DToPoint和PointAndZToPoint3D) [英] Need help writing conversion methods between 2D and 3D (Point3DToPoint and PointAndZToPoint3D)
问题描述
我是新来的WPF 3D,所以我可能只是缺少明显的东西,但我怎么从3D转换为2D和(对于给定ž位置)从2D到3D?
I'm new to WPF 3D, so I may just be missing something obvious, but how do I convert from 3D to 2D and (for a given z location) from 2D to 3D?
具体而言,我需要两个转换方法:
Specifically, I need two conversion methods:
-
Point3DToPoint - 如果我有一个(X,Y,Z)在3D世界坐标,我怎么确定(X,Y)的投射二维表面上的坐标。方法签名:
公共点Point3DToPoint(三维点三维点)
Point3DToPoint - If I have an (x, y, z) coordinate in the 3D world, how do I determine the (x, y) coordinate on the projected 2D surface. Method signature:
public Point Point3DToPoint(Point3D point3D)
PointAndZToPoint3D - 如果我有一个(X,Y)的投射二维表面的和协调的3D世界中AZ的位置,我怎么确定(X,Y,Z)在3D世界坐标?方法签名:公共三维点PointAndZToPoint3D(点对点,双Z)
PointAndZToPoint3D - If I have an (x, y) coordinate on the projected 2D surface and a z location in the 3D world, how do I determine the (x, y, z) coordinate in the 3D world? Method signature: public Point3D PointAndZToPoint3D(Point point, double z)
我想在 2D坐标可以从 Viewport3D
的左上角测量的位置和三维坐标是位置相对原点(0,0,0)的3D世界。
I'd like the 2D coordinate to be the location measured from the upper-left corner of Viewport3D
and the 3D coordinate to be the location relative to the origin (0, 0, 0) of the 3D world.
注1:我发现这个<一href="http://stackoverflow.com/questions/724219/how-to-convert-a-3d-point-into-2d-perspective-projection">related问题,但它只是解决了从3D到2D的转换(而不是相反),我不知道,如果答案是最新的最新的。
Note 1: I found this related question, but it only addresses conversion from 3D to 2D (not the reverse), and I'm not sure if the answers are up-to-date.
注2:我目前使用.NET 3.5,但如果有在.NET 4.0中的改进将帮助我,请让我知道
Note 2: I'm currently using .NET 3.5, but if there are improvements in .NET 4.0 that would help me, please let me know.
推荐答案
查尔斯Petzold的3D库,可下载的这里下」,包含一个类 ViewportInfo
这两个静态方法:
Charles Petzold's 3D Library, which can be downloaded here under "The Petzold.Media3D library", contains a class ViewportInfo
with these two static methods:
-
公共静态点Point3DToPoint2D(Viewport3D视口,三维点点)
公共静态布尔Point2DToPoint3D(Viewport3D视,点,PTIN,出LineRange范围)
Point2DToPoint3D
不是精确匹配PointAndZToPoint3D()
,因为它返回(通过退出
参数)一个 LineRange
,而不是一个特定的点,但它只是恰巧, LineRange
有一个方法 PointFromZ(双zValue)
,它提供了那里的射线相交由Z = zValue $ C定义的平面上点$ C>。
Point2DToPoint3D
isn't an exact match for PointAndZToPoint3D()
because it returns (via an out
parameter) a LineRange
rather than a specific point, but it just so happens that LineRange
has a method PointFromZ(double zValue)
, which provides the point where the ray intersects the plane defined by z = zValue
.
code样品:</ STRONG>
Code Sample:
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 code
<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>
这篇关于需要帮助编写2D和3D之间的转换方法(Point3DToPoint和PointAndZToPoint3D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!