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!