Matthew Lebo28574

I have a canvas control which can have an adjustable width and height. I then add points to this canvas based on mouse x and y coordinates . I'd like to take these x and y points and convert them from the 2d canvas and make them points in a Viewport3D control. (I don't care about z as I'm simply setting it to 1). I have a camera setup in my Viewport3D with a specific LookDirection, Position, and FOV. There must be some calculation to take these 2d points / cords and convert them to my 3d scene, but I cant' figure it out. Any help would be greatly appreciated.

Thanks
Matthew

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Laurent Bugnion

Hi,

You need to check Daniel Lehebauer's blog. He got some helper classes for this kind of math.

http://blogs.msdn.com/danlehen/default.aspx

HTH,
Laurent

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Ales Sturala

I suppose in WPF are no classes, properties or methods doing what you need. You have to implement converting 2D values to 3D by yourself. You have to compute 3D position accroding to size of Viewport3D and Z coordinate of that object.

PS: Maybe Daniel L. did something similar.

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Szymon Kobalczyk

You can use the 3D Tools library created by Daniel Lehenbauer. You can get it here http://www.codeplex.com/3DTools

Besides other goodies it contains a MathUtils helper class that performs the calculations you are looking for.

For example to convert a point in 3D space to screen coordinates you would do the following:

Code Snippet

Viewport3DVisual viewport;

bool success;

Matrix3D screenTransform = MathUtils.TryTransformTo2DAncestor(myVisual, out viewport, out success);

if (success)

{

Point3D scenePoint = new Point3D(1, 1, 0);

Point3D screenPoint = screenTransform.Transform(scenePoint);

...

}

The X and Y coordinates of screenPoint will be expressed as relative to viewport area. This allows for very easy positioning of 2D content over 3D objects.

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Matthew Lebo

I don't see how this will convert my 2d cords to 3d cords. It looks as though it will convert 3d to 2d. How can I reverse this process

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Jordan Parker - MSFT

A 2D point maps to a ray in 3D space so there are an infinite number of 3D points. What you need to do is use the inverse of that 3D -> 2D matrix and pick an aribrary Z for your 2D point. That should determine where along that ray you are.

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Matthew Lebo

But thats where I am confused. I don't know how to invert the 3d to 2d process. Any suggestions on where to start

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Szymon Kobalczyk

As Jordan said above, to project from 2D to 3D space you need to invert the transformation. To continue my previous example you should write:

Code Snippet

if (screenTransform.HasInverse)

{

Matrix3D reverseTransform = screenTransform;

reverseTransform.Invert();

Point3D pointOnScreen = new Point(30,20,1); // you need to choose the z-depth

Point3D pointInWorld = reverseTransform.Transform(pointOnScreen);

}

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Matthew Lebo

I've tried to implement this but I am not sure what goes into the visual parameter of the TryTransformTo2DAncestor() method. Right now I am simply passing a model3dvisual which is described in my xaml (this is obviously incorrect). It appears that the screenTransform.HasInverse() method is always returning false. I guess the real question is how can i take the code samples you have given and make it use my already existing Viewport3D defined in my XAML.

Below is my code taken from your snippets above. I am basically passing in an x and y point that i wish to display in a Viewport3D control already defined in my xaml

Point3D Convert2DPoint(Point pointToConvert)
{
Viewport3DVisual viewport;
Matrix3D screenTransform =
MathUtils.TryTransformTo2DAncestor(mv, out viewport, out success);

Point3D pointInWorld = new Point3D();
if (screenTransform.HasInverse)
{
Matrix3D reverseTransform = screenTransform;
reverseTransform.Invert();

Point3D pointOnScreen = new
Point3D(pointToConvert.X,pointToConvert.Y, 1);

pointInWorld =
reverseTransform.Transform(pointOnScreen);
}
return pointInWorld;
}

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Jordan Parker - MSFT

"Right now I am simply passing a model3dvisual which is described in my xaml (this is obviously incorrect)."

What do you mean Using your root ModelVisual3D should work.

What are you transforms like The only one that might not be invertible is the camera projection but you can fix that by changing your camera parameters.

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Matthew Lebo

This is my xaml. In it is the ModelVisual3D that i am passing into TryTransformTo2DAncestor() as mv, what am i doing wrong here

<Viewport3D x:Name="Designer3D" ClipToBounds="True">
<Viewport3D.Camera>
<PerspectiveCamera x:Name="Camera1"
FarPlaneDistance="1000"
LookDirection="0,0,-9"
UpDirection="0,1,0"
NearPlaneDistance="1"
Position="0,0,36"
FieldOfView="40" />
</Viewport3D.Camera>
<ModelVisual3D x:Name="mv">
<ModelVisual3D.Content>
<DirectionalLight
Color="White"
Direction="-2,-3,-10" />
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Matthew Lebo

Anyone who can please continue helping me with this problem

Thank you!

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Szymon Kobalczyk

Hello,

I'm not sure why this doesn't work but I suspect that the TryTransformTo2DAncestor method needs some real mesh in order to work properly. Since you apply it on a ligh that doesn't have any real visual representation and specific coordinates this method would fail. I would suggest you put at least some plane in your scene to serve as canvas for other objects. I guess you can even make it transparent and it should still work.

Hope this helps and sorry for the delay.

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Jordan Parker - MSFT

Sorry for the delay, I had to step through things and debug.

My guess is you're calling the function before layout gets a chance to happen. If layout doesn't happen, the Viewport3DVisual's Viewport property will be an empty rect and the method will fail because we can't get the width and height. The Loaded event fires after layout has happened and soon before rendering is going to happen for the first time. I did this and it worked for me (using your code and Xaml):

Code Snippet

public Window1()

{

InitializeComponent();

}

...

{

Convert2DPoint(new Point(10, 5));

}

Don't forget to check the value of "success" in Convert2DPoint as well.

### Re: Windows Presentation Foundation (WPF) 2d Mouse Coordinates to 3d

Matthew Lebo

Well it wasn't a loading issue, but at some point in my code i was setting the visibility of my ViewPort3D to Collapsed before I would do the calculation on point conversion. How LAME am I

Thank you so much for solving my issue and helping me figure out where I went wrong as well as taking the time. Everything works perfectly now.