Construct a plane from 3 points

Started by
7 comments, last by _Sauce_ 14 years, 2 months ago
I'm attempting to construct a plane from 3 points for my raytracer, and wanted to check that my code is correct. I can construct a plane from a normal and a distance along that normal to the origin, which renders fine, however when constructing a plane from 3 points, nothing is rendered at all. I'm guessing I end up with an invalid plane, so I wanted someone to check over my code and tell me if I'm still sane.
RTPlane::RTPlane(const Vector3f &A, const Vector3f &B, const Vector3f &C, const RTMaterial &material)
{
	mMaterial = material;

	Vector3f v1 = B - A;
	Vector3f v2 = C - A;

	mNormal = (v1.cross(v2)).normalise();

	mDistance = mNormal.dot(A);
}
The points are wound in a clockwise order.
Advertisement
The only thing I can find is that the distance calculation is incorrect. You should not normalize before the dot product.
Sleep is for the weak, or at least that is what I tell myself every morning.
Thanks for the advice, but it doesn't seem to be making any difference.

A = (-1, -1, 0)B = (1, -1, 0)C = (1, 1, 0)mDistance = 0mNormal = (0, 0, 4) //(0, 0, 1) normalised


As a sanity check I've got another plane with the same coords in reverse winding order...

A = (1, 1, 0)B = (-1, -1, 0)C = (1, -1, 0)mDistance = 0mNormal = (0, 0, 4)


Are these values correct? If that's not the problem then I'm beginning to wonder whether or not my ray-plane intersection algorithm fails horribly on any plane that isn't facing straight up or down (something which I have yet to test :S)

edit: ray-plane intersection definitely performs as expected.
Nevermind, all appears to be good now. I'll test it a bit more and come back later if I have any further trouble. Thanks jpmcmu :)
Then length of the normal is irrelevant as long as you satisfy the equation Ax + By + Cz + D = 0, which will always be the case so long as you calculate the distance after the normal. By convention though normals are unit length, which is where the normalization comes in. You should not, however, modify the normal after the distance is calculated or else your plane definition will be incorrect. If you rearrange the equation as Ax + By + Cz = -D, then you can see that if the normal (A,B,C) changes, D must change accordingly to maintain equality.

Long story short, if you modify the normal you must recalculate the distance to maintain correctness. Everything else isn't mathematically necessary, including unit normal length.
So I should be performing a normalisation before calculating the distance afterall?
Quote:Original post by _Sauce_
So I should be performing a normalisation before calculating the distance afterall?
If you're going to normalize, you should normalize before calculating the distance. (As for whether or not you *need* to normalize the plane normal, that depends on what you'll be doing with the plane exactly.)
Quote:Original post by _Sauce_
So I should be performing a normalisation before calculating the distance afterall?

Absolutely. Remember that another definition of the dot product is:

A·B = |A||B|cos(Θ)

If one of your input vectors has a large magnitude, then your dot product would also be quite large if the vectors were relative parallel. After the normalization, you still have this gigantic distance simply because one of the input vectors had a large magnitude and has nothing to do with the new normal.
Quote:Original post by jyk
Quote:Original post by _Sauce_
So I should be performing a normalisation before calculating the distance afterall?
If you're going to normalize, you should normalize before calculating the distance. (As for whether or not you *need* to normalize the plane normal, that depends on what you'll be doing with the plane exactly.)

I'll be using the normals for lighting, so yes it's important :)

This topic is closed to new replies.

Advertisement