Reverse project 2D point to 3D Point

Reverse project 2D point to 3D Point

Anonymous
Not applicable
652 Views
2 Replies
Message 1 of 3

Reverse project 2D point to 3D Point

Anonymous
Not applicable

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.

 

0 Likes
653 Views
2 Replies
Replies (2)
Message 2 of 3

denisT.MaxDoctor
Advisor
Advisor

there is no a single 3D point that corresponds to a 2D screen point. it might any point on the ray that goes from camera view through the screen point.

 

there is mapScreenToWorldRay mxs method that gives you this ray. 

0 Likes
Message 3 of 3

Anonymous
Not applicable

Thank you for the reply. As mentioned in the question, I understood there is no single point and I can accept the value of depth as 0. Thanks for the API. I will check it.

0 Likes