0

I have a problem, I need to do that (a code to know X,Y,Z coordinates when I click on a Viewport3D that shows me a 3D image obtained with Kinect):

        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);
        }

But when I run the code it gives me that exception on MathUtils.cs:

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

I was trying to change the DependencyObject type to Visual3D, but I don't know how to do it.

Can anyone help me to fix the error and do the code works?

Thanks a lot!

  • The input parameter `visual` should be a Visual3D that is already existing somewhere on your application, not a newly created object. Please note also that `viewport3DVisual` is an output parameter of the TryTransformTo2DAncestor method, and therefore does not need to be initialized, like you do with VisualTreeHelper.GetParent(...). – Clemens Feb 24 '15 at 11:58
  • if I use graphicsViewport instead of viewport3DVisual it gives me an error before debug. – Prod Feb 24 '15 at 12:02
  • where this Visual3D parameter could be? do you know? – Prod Feb 24 '15 at 12:03
  • I search in all my solution and I don't have any "Visual3D" parameter, can I create one? – Prod Feb 24 '15 at 12:04
  • Sorry, I can't tell you more. It's entirely unclear what you are doing, and why you would call the TryTransformTo2DAncestor method at all, when you do not even know which Visual3D to pass as input. – Clemens Feb 24 '15 at 13:22
  • all I want to do is to have X,Y,Z coordinates of the mouse click on the viewPort3D, and I don't know what I have to pass on that function – Prod Feb 24 '15 at 13:30
  • You still haven't explained why would you use that method at all. There should be a reason, right? As an alternative, you might just use `VisualTreeHelper.HitTest(viewport3D, position)`, and cast the result to `RayHitTestResult`. Then you would get a Point3D from the result's `PointHit` property. Please take the time to read the online documentation of these classes. – Clemens Feb 24 '15 at 13:44
  • I try to do it, but I recieve a "null" value and I try this other method that I see on the internet, I can put the other code on my question if you want to see it... it could help – Prod Feb 24 '15 at 13:50

0 Answers0