views:

223

answers:

2

Hiya.

can anyone assist me in finding the proper formula for quad normalization ?

using c++ with opengl.

thank you!

+1  A: 

Assuming that you want the normal vector for a quad, this pseudo-code works

Vector3d vertex[4] = { ... }
Vector3d normal(0,0,0) ;

for (int i=0; i<4; i++)
{
    normal += cross (vertex[i], vertex[(i+1)%4]) ; // cross product
}
normalize (normal) ;
// normal is the unit normal to the quad

This gives you the formula n=A/|A|, where A = v0xv1 + v1xv2 + v2xv3 + v3xv0 and vi=vertex[i]). |A|/2 is also the area of the polygon. This can be generalized to arbitrary polygons, and will even give reasonable results for non-planar polygons, as long as they aren't too non-planar.

One reference is http://softsurfer.com/Archive/algorithm_0101/algorithm_0101.htm

If you know that the quad/polygon is planar, you only have to calculate the normal of the triangle formed by the first three vertices. This is A1/|A1|, where A1 = (v1-v0)x(v2-v0) = v0xv1 + v1xv2 + v2xv0.

If by "quad normalization" you meant something else, just ignore this answer.

EDIT: I found this related question: http://stackoverflow.com/questions/2350604/get-the-area-of-a-3d-surface

brainjam
shouldn't you subtract 2 of the vertices to get a vector? Taking the cross product of points isn't useful for normals.
cobbal
@cobbal, you're right: (v_1 - v_0) x (v_2 - v_0)
sisis
why the for loop and why the modulo?
sisis
@cobbal, you can do it with edge vectors like (v_1 - v_0) x (v_2 - v_0), but once you simplify it it ends up being something like `v0xv1 + v1xv2 + v2Xv0`. @sisis, the modulo give you the `v0` in the last term.
brainjam
I'm loading dxf files that are build from quads and drawing them on the screen, i want to find their normals so I'll have proper lighting
ufk
You're missing a ']' in "normal += cross (vertex[i], vertex[(i+1)%4) ;" I think it should be "normal += cross (vertex[i], vertex[(i+1)%4]) ;" ... ?
andand
Right on. Fixed. It would be nice if SO had a way of checking for balanced parentheses :)
brainjam
+1  A: 

Newell's method is usually your best choice for computing the normals of polygons which are nearly planar. It tends to be fairly robust to minor irregularities without being too expensive. See the Graphics Gems article. It's similar to that described above:

Vector3d normal(0,0,0) ;

for (int i=0; i<4; i++)
{
    int j = (i+1)%4;
    normal.x += (vertex[i].y - vertex[j].y)
               *(vertex[i].z + vertex[j].z);
    normal.y += (vertex[i].z - vertex[j].z)
               *(vertex[i].x + vertex[j].x);
    normal.z += (vertex[i].x - vertex[j].x)
               *(vertex[i].y + vertex[j].y);
}
normalize (normal) ;

It's probably not going to matter much for quads if they're reasonably well behaved, but I would certainly use it if you're dealing with more complex polygons.

MPG