0

我有一个问题,我需要这样做(当我单击 Viewport3D 时知道 X、Y、Z 坐标的代码,它显示了使用 Kinect 获得的 3D 图像):

        bool success;
        Viewport3DVisual viewport3DVisual = VisualTreeHelper.GetParent(GraphicsViewport) as Viewport3DVisual;
        DependencyObject visual = new DependencyObject();
        Matrix3D screenTransform = MathUtils.TryTransformTo2DAncestor(visual, out viewport3DVisual, out success);

        if (success)
        {
            Point3D scenePoint = new Point3D(mouseLocation.X, mouseLocation.Y, 0);
            Point3D screenPoint = screenTransform.Transform(scenePoint);
            MessageBox.Show("screenPoint: " + screenPoint.X + " " + screenPoint.Y + " " + screenPoint.Z);
        }

        if (screenTransform.HasInverse)
        {
            Matrix3D reverseTransform = screenTransform;
            reverseTransform.Invert();
            Point3D pointOnScreen = new Point3D(mouseLocation.X, mouseLocation.Y, 1); // you need to choose the z-depth
            Point3D pointInWorld = reverseTransform.Transform(pointOnScreen);
            MessageBox.Show("pointInWorld: " + pointInWorld.X + " " + pointInWorld.Y + " " + pointInWorld.Z);
        }

但是当我运行代码时,它给了我 MathUtils.cs 上的异常:

if (!(visual is Visual3D))
        {
            throw new ArgumentException("Must be of type Visual3D.", "visual");
        }

我试图将 DependencyObject 类型更改为 Visual3D,但我不知道该怎么做。

任何人都可以帮助我修复错误并使代码有效吗?

非常感谢!

4

0 回答 0