ideasman42
ideasman42

Reputation: 47968

Efficient method of extracting a point from a 3D plane

I've come across this method of extracting a location from a 3D plane.

I've tested this and it works correctly, but for such a primitive operation, I was wondering if there was a, more efficient method in common practice.

void position_from_plane(float r_co[3], const float p[4])
{
    const float p_len_sq = p[0]*p[0] + p[1]*p[1] + p[2]*p[2];
    const float d = (-p[3] / p_len_sq) - 1.0f;

    r_co[0] = p[0] + p[0]*d;
    r_co[1] = p[1] + p[1]*d;
    r_co[2] = p[2] + p[2]*d;
}

Note: this simply offsets the plane's direction component to find a point on a plane, it could of course return a point somewhere else on the plane and still be a correct answer, offsetting the planes orientation is just the most direct way to find this point.

Upvotes: 0

Views: 47

Answers (1)

MvG
MvG

Reputation: 60858

Your code looks good. You can shorten it somewhat to this:

void position_from_plane(float r_co[3], const float p[4])
{
    const float d = -p[3] / (p[0]*p[0] + p[1]*p[1] + p[2]*p[2]);
    r_co[0] = p[0]*d;
    r_co[1] = p[1]*d;
    r_co[2] = p[2]*d;
}

You could get slightly shorter code if you were to intersect one of the coordinate axes with your plane. But you'd pay for that by needing a case distinction, which I'd rather avoid. Unless you can guarantee that the plane won't be parallel to one of the coordinate axes, that is.

Upvotes: 1

Related Questions