2
votes

My problem:

How can I take two 3D points and lock them to a single axis? For instance, so that both their z-axes are 0.

What I'm trying to do:

I have a set of 3D coordinates in a scene, representing a a box with a pyramid on it. I also have a camera, represented by another 3D coordinate. I subtract the camera coordinate from the scene coordinate and normalize it, returning a vector that points to the camera. I then do ray-plane intersection with a plane that is behind the camera point.

O + tD

Where O (origin) is the camera position, D is the direction from the scene point to the camera and t is time it takes for the ray to intersect the plane from the camera point.

If that doesn't make sense, here's a crude drawing:

Crude drawing

I've searched far and wide, and as far as I can tell, this is called using a "pinhole camera".

The problem is not my camera rotation, I've eliminated that. The trouble is in translating the intersection point to barycentric (uv) coordinates.

The translation on the x-axis looks like this:

uaxis.x = -a_PlaneNormal.y;
uaxis.y = a_PlaneNormal.x;
uaxis.z = a_PlaneNormal.z;

point vaxis = uaxis.CopyCrossProduct(a_PlaneNormal);

point2d.x = intersection.DotProduct(uaxis);
point2d.y = intersection.DotProduct(vaxis);

return point2d; 

While the translation on the z-axis looks like this:

uaxis.x = -a_PlaneNormal.z;
uaxis.y = a_PlaneNormal.y;
uaxis.z = a_PlaneNormal.x;

point vaxis = uaxis.CopyCrossProduct(a_PlaneNormal);

point2d.x = intersection.DotProduct(uaxis);
point2d.y = intersection.DotProduct(vaxis);

return point2d; 

My question is: how can I turn a ray plane intersection point to barycentric coordinates on both the x and the z axis?

1
I don't understand what you mean in your question under "My Problem". What does it mean to lock 2 points into an axis? Are you looking for a transform which will align the line through the points along an axis? Also, barycentric coordinates only make sense for triangles or possibly other convex polygons. Do you just mean parametric coordinates in the plane perpendicular to the aforementioned axis?Victor Liu
Yes, I am looking for a transform which will align the points along an axis. I'm not very familiar with the mathematical terms, but what I mean by "barycentric coordinates" is the x- and y- offset of the intersection point compared to to plane origin point. A plane is defined as a point in space and a normal and is infinite for all intents and purposes. If I can lock the two points to a single axis I can simply do point2d = Vec3(intersection.x - origin.x, intersection.y - origin.y, 0). The second method solves the same problem.knight666

1 Answers

3
votes

The usual formula for points (p) on a line, starting at (p0) with vector direction (v) is:

p = p0 + t*v

The criterion for a point (p) on a plane containing (p1) and with normal (n) is:

(p - p1).n = 0

So, plug&chug:

(p0 + t*v - p1).n = (p0-p1).n + t*(v.n) = 0

   ->  t = (p1-p0).n / v.n
   ->  p = p0 + ((p1-p0).n / v.n)*v

To check:

(p - p1).n = (p0-p1).n + ((p1-p0).n / v.n)*(v.n)
                   = (p0-p1).n + (p1-p0).n
                   = 0

If you want to fix the Z coordinate at a particular value, you need to choose a normal along the Z axis (which will define a plane parallel to XY plane).

Then, you have:

n = (0,0,1)

   ->  p = p0 + ((p1.z-p0.z)/v.z) * v
   ->  x and y offsets from p0 = ((p1.z-p0.z)/v.z) * (v.x,v.y)

Finally, if you're trying to build a virtual "camera" for 3D computer graphics, the standard way to do this kind of thing is homogeneous coordinates. Ultimately, working with homogeneous coordinates is simpler (and usually faster) than the kind of ad hoc 3D vector algebra I have written above.