Message 1 of 3
Reverse project 2D point to 3D Point

Not applicable
05-22-2017
11:03 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to convert 2D XY point to 3D X,Y,Z points based on the selected viewport. I can assume the 3rd coordinate value as 0. I have written a function and it is working somewhat. My current issue is that my solution depends on the size of the selected viewport which I need to avoid.
My final target is, based on the selected viewport and input point, I need to move a plane so that if I render from the same viewport I get the input value as the top left point of the plane.
I am not sure what I am doing is actually right or wrong? To check I will add my function here.
Point3 point_2d_to_3d(Point2 point2d) { Box2f safe_frame = safeframe();//calculate safe frame float width_ratio = safe_frame.getW() / render_width(); float height_ratio = safe_frame.getH() / render_height(); float X_float = point2d.x * width_ratio; float Y_float = point2d.y * height_ratio; X_float += safe_frame.getX(); Y_float += safe_frame.getY(); /* converting viewport screen space point to world space*/ IPoint2 s_point( (X_float, Y_float ); float focal_d = viewport.GetFocalDist(); Point3 point_3d = viewport.MapScreenToView(s_point,focal_d); if(viewport.IsPerspView()) { point_3d.x = point_3d.x * -1; point_3d.y = point_3d.y * -1; } /* Applying camera rotation and translation to the world point */ point_3d = inverse_camera_matrix.PointTransform(point_3d); { /* Setting one of the axis to 0 based on view direction */ Matrix3 aTM, coordSysTM; viewport.GetAffineTM(aTM); coordSysTM = Inverse(aTM); Point3 viewDir = coordSysTM.GetRow(2); if(almost_equal(fabsf(viewDir.x), 1.0f)) { point_3d.x = 0.0f; } if(almost_equal(fabsf(viewDir.y), 1.0f)) { point_3d.y = 0.0f; } if(almost_equal(fabsf(viewDir.z), 1.0f)) { point_3d.z = 0.0f; } } return point_3d; }
Any help is very much appreciated even a maxscript will be very much helpful.