# Projecting a 2D ray to 3D space

This topic is 3409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

(as a coder, I communicate better in code, im not saying "gimmie code", its just literally easier for me to communicate this way) The following code translates from 3D space to 2D, perfectly.
   matrix = projection_matrix.mul(model_view_matrix);

// Transform the vertex
Vec4 result = new Vec4();
Vec4 input  = new Vec4(v.x, v.y, v.z, 1.0f);

result.x = Math3D.dot(matrix.getRow(0),input);
result.y = Math3D.dot(matrix.getRow(1),input);
result.z = Math3D.dot(matrix.getRow(2),input);
result.w = Math3D.dot(matrix.getRow(3),input);

double rhw = 1 / result.w;
double resultx = (1 + result.x * rhw) * viewport[2] / 2 + viewport[0];
double resulty = (1 - result.y * rhw) * viewport[3] / 2 + viewport[1];
double resultz = (result.z * rhw) * (depthrange[1] - depthrange[0]) + depthrange[0];

what I need is the inverse of that function, a converter from 2D space to 3D space, this is what I have currently but it does not do the job...
   matrix = projection_matrix.mul(model_view_matrix);
matrix.invert();

double resultx = ((2*(v.x-viewport[0])/viewport[2])-1)/v.w;
double resulty = ((2*(v.y-viewport[1])/viewport[3])+1)/v.w;
double resultz = ((v.z-depthrange[0])/(depthrange[1] - depthrange[0]))/v.w;

// Transform the vertex
Vec4 result = new Vec4();
Vec4 input  = new Vec4(v.x, v.y, v.z, 1.0f);

result.x = Math3D.dot(matrix.getRow(0),input);
result.y = Math3D.dot(matrix.getRow(1),input);
result.z = Math3D.dot(matrix.getRow(2),input);
result.w = Math3D.dot(matrix.getRow(3),input);

Any help in this regard would be appreciated

##### Share on other sites
1) You can't rely on a projection matrix being invertible.
2) There is no general way to recover the information lost when you reduce the rank of a vector.

For a simple example of why this is a problem, consider the vector <x,y,z> projected on the XY plane. The resulting vector is <x,y,0>.

So, given a vector <x,y,0>, find its three dimensional counterpart. The problem is that there are infinite solutions. The original z could be 5, 5000, or Π-8i. There is no longer any way to find out.

##### Share on other sites
You are technically correct, which is why I preserve the Z transformation from the first equation (I dont use it to display), but I am passing it back to second equation.

##### Share on other sites
Quote:
 Original post by GeddenYou are technically correct

The best kind of correct!

Quote:
 which is why I preserve the Z transformation from the first equation (I dont use it to display), but I am passing it back to second equation.

If you communicate best in code, then maybe you should communicate enough so I don't have to guess what problems you've worked around. The only thing that stands out about your implementation is that resultx, resulty, and resultz seem to do nothing in the second function.

Are they implemented somewhere that isn't shown?
If the function isn't doing what you expect, how is it failing?
Have you tried replacing the matrix and vector with simple things that you can verify by hand to see where they are failing?
If your matrix inversion fails, do you have any way to detect it or recover from the error?
What are your inputs, and what are your outputs?
If you are preserving information for restoration already, why not preserve the whole thing?
What problem are you actually trying to solve? Is this the best way to do it?

Johnny 5 needs input.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• ### Forum Statistics

• Total Topics
632160
• Total Posts
3004497

×
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.21878616511821747, "perplexity": 2622.165475439089}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-34/segments/1534221213666.61/warc/CC-MAIN-20180818114957-20180818134957-00211.warc.gz"}