Hi guys,

I have a collection of vertices that are roughly on the same plane, I want them to actually be on the same plane.

I thought I would loop through the vertices and compute an average normal and an average position and use them to define the plane. The plane normal looks correct but when I project the vertices they are a good way off from their original positions.

Here is the (pseudo) code:

float3 avg_pos = (0, 0, 0); float3 avg_nrm = (0, 0, 0); for (i = 0; i < num_vertices; i++){ // avg position avg_pos += vertex_array[i]; // create normal (no array bounds check/wrap-around in this example) // I know I am skipping triangles this way but it's a good enough sample v1 = vertex_array[i]; v2 = vertex_array[i + 1]; v3 = vertex_array[i + 2]; v12 = v2 - v1; v13 = v3 - v1; nrm = cross(v12, v13); avg_nrm += normalize(nrm); } // divide avg_pos by count avg_pos /= num_vertices // normalize avg_normal (it becomes the plane normal) plane_nrm = normalize(avg_nrm); // now project for (i = 0; i < num_vertices; i++){ d = dot(vertex_array[i], plane_nrm); pos = vertex_array[i] - (d * plane_n); // add in average position ? pos += avg_pos; }

The vertices do end up on the average plane which is nice but, like i wrote, the positions are noticably off. I'd like as little displacement as possible. Maybe its correct and it's just not a good technique to do what I want ?

Thanks a lot.