Retrieve 2D co-ordinate from a 3D point on a 3D plane

微笑、不失礼 提交于 2019-12-06 06:17:36

Since you have a 2D image on that plane, you apparently want to match its coordinate system. To do so, determine the unit vectors of the picture. That is, take the 3d coordinates B for the picture position (x,0) for any x>0, and subtract from that the 3d coordinates A for the origin (0,0) of the picture. The resulting vector B − A will describe the positive x direction of your image. Do the same for the y direction. Then normalize both these vectors. This means dividing them by their length, sqrt(x²+y²+z²), but D3Dx has a function D3DXVec3Normalize for this. Let's call the resulting 3d vectors X and Y. To compute the x and y coordinate of any 3D point p, simply subtract the origin A from p, i.e. compute the vector p − A. Then compute the dot product between the result and the unit vectors X and Y. This will give you two numbers: the desired coordinates. This is because the dot product can be used to compute an orthogonal projection.

Translating this into D3Dx, it should look somewhat like the following. As I have never used it, this might have mistakes.

D3DXVECTOR3 *p;                  // input point
D3DXVECTOR3 a, b, c, ab, ac, ap; // helper vectors
FLOAT x, y;                      // output coordinates
imagePosTo3D(&a, 0, 0);          // a = origin of image
imagePosTo3D(&b, 1, 0);          // b = anywhere on positive x axis, perhaps a corner
imagePosTo3D(&c, 0, 1);          // c = anywhere on positive y axis, perhaps a corner
D3DXVec3Subtract(&ab, &b, &a);   // ab = b - a
D3DXVec3Subtract(&ac, &c, &a);   // ac = c - a
D3DXVec3Normalize(&ab, &ab);     // ab = ab / |ab|
D3DXVec3Normalize(&ac, &ac);     // ac = ac / |ac|
// the above has to be done once for the image, the code below for every p
D3DXVec3Subtract(&ap, p, &a);    // ap = p - a
x = D3DXVec3Dot(&ab, &ap);       // x = ab∙ap
y = D3DXVec3Dot(&ac, &ap);       // y = ac∙ap
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!