converting vector on plane to 2d

Started by
3 comments, last by utkua 17 years, 6 months ago
Hi, Is there a quick way to convert vectors on the same plane to 2d coordinates? I have thought to select an arbitrary 2 vector on plane and cross product difference of them(v1) with the plane normal and find another vector(v2) and project vectors on plane to v1 and v2 to get 2d coordinates, but I this does not seem the best way to do that...
Advertisement
Quote:Original post by utkua
but I this does not seem the best way to do that...


It seems to me like it's the most straightforward way. And those should be preferred - from my experience those are the only fully correct ways implementing math algorithms.

About finding this "arbitrary 2 vector on plane" - I'm doing it like this:

// any orthonormal vector to @v0.xvec orthonormalTo(const vec& v0){  // Who's less paralel?  if ( fabs(v0.z) < fabs(v0.y) ) {    // First shot - right.    xvec xvFirst(0,0,1);    // gramm-schmidt (with predicted dot value).    const float dotS0 = v0.z;    xvFirst -= v0 * dotS0;    return norm(xvFirst);  } else {    // Second shot - up.    xvec xvSec(0,1,0);    // gramm-schmidt (with predicted dot value).    const float dotF0 = v0.y;    xvSec -= v0 * dotF0;    return norm(xvSec);  };};// gives two orthonormal vertices, matching given.void createPlaneBase(const xvec& xvPlaneNormal, vec& vOut1, vec& vOut2){  const xvec xvFirst = orthonormalTo(xvPlaneNormal);  vOut1 = xvFirst;  vOut2 = cross(xvFirst, xvPlaneNormal);};
If you already have two vectors, then no need to find two more. Just let one of the vectors represent the "x-axis" for the plane, and you can calculate the coordinates (x1,y1) and (x2,y2) for each vector:
float x1 = normalize(firstVector); // Returns the length before normalizationfloat y1 = 0.0f;float x2 = dot(secondVector, firstVector);float y2 = length(secondVector - x2 * firstVector);
Zipster's method is clean and tidy, but if you're working on accelerated hardware (or even if you're not), you may prefer to take the slightly more intuitive approach of transforming the plane onto a axial hyperplane.
Given any plane in 3-space, there exists a unique rotation that will transform the barycentric coordinates to projected global coordinates, effectively rotating the plane onto the xy, xz, or yz plane (whichever you choose). To convert any coordinate in your domain, you need only multiply by a constant matrix and discard the unused (invariant zero) coordinate. This has the bonus of being conformal, so geometry works as you'd expect.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Thanks!
Deffer: Your code lead me various optimizations, there are nice tips.
Zipster: Thats really a good point, I only need to calculate the distance to the ray I have on the plane :)
Admiral: I sensed only normal info must be enough to map to 2d, but could not express it. Your method is great.

This topic is closed to new replies.

Advertisement