Raztou3D
Raztou3D

Reputation: 73

Patch Normals Algorithm

I'm trying hard to reproduce a MATLAB algorithm called "patchnormal" which first calculates the normal vectors of all faces, and after that calculates the vertex normals from the face normals weighted by the angles of the faces. (See illustration below)

There doesn't seem to be a free library available for 3D mesh in WPF C# oriented to such mathematical use. Or is there ?

So the question is : How do I compute this (red) vector for all my vertices? Can it be optimized to be used in real time simulation ?

PatchNormal Illustration Image
(source: hostingpics.net)

Upvotes: 7

Views: 608

Answers (1)

comingstorm
comingstorm

Reputation: 26117

You can compute the angle between two edges as follows:

given:  edge vectors E and F for a given face of your vertex,

E_normalized = normalize(E)
F_normalized = normalize(F)
cross_normal = cross(E_normalized, F_normalized)
sin_theta = length( cross_normal )
cos_theta = dot(E_normalized, F_normalized)

results:
    face normal = normalize(cross_normal)
    face angle theta = atan2(sin_theta, cos_theta)

Then, weight the normals accordingly:

total_vector = vec(0,0,0)
for(each face adjacent to a particular vertex):
    [compute normal and theta as above]
    total_vector += normal * theta
return normalize(total_vector)

To optimize for real-time, I would first profile to see what was actually slowing things down. I'd guess that computing atan2() several times per vertex might turn out to be expensive, but improving on that would really require finding some substitute for angle-weighted normals.

Upvotes: 2

Related Questions