# How to find the gradient for a given discrete 3D mesh?

I have a 3D mesh that is looking like this:

ie I have a set of triangles in a 3D space, and they are all linked by their edge. I have to compute the gradient associated with this field, at each edge of the mesh. For a continuous function it would be the derivative along the X and Y axis, but in this case I can't do this. How can I compute this? I'm not even sure how I can define the (approximate) gradient in this situation.

thanks

• Is there a function $f(\mathbf{x}), x \in \mathbb{R}^3$, whose gradient $\nabla f$ should be evaluated at all vertices/edges of your mesh? Or is f only defined on the mesh? For me "gradient associated with this field" is unclear in your question. Do you have any further information on the problem? – ArminB May 9 '13 at 17:01
• @ArminB : the mesh is an approximation of z = sin(x+y) . I have to compare the gradient of this mesh to the gradient of the exact function. So for my question I need to compute the gradient just based on this approximate mesh – lezebulon May 9 '13 at 17:16

I think you can find more inspiration if you express your problem differently:

If you keep in mind that "the gradient of a function at a point is perpendicular to the level set of the function at that point" - ie that for a function defining a surface, the gradient will be normal to this surface (Q: Why is the gradient normal?) - then all you want to do with your mesh is to compute its vertices' unit normals.

Using this wording, you should find plenty of methods to do so. For instance, here is a simple algorithm described by Perlin here:

1. Initialize all the vertex normals to the null vector. In the next step, you're going to gradually accumulate values into these vectors.

2. Loop through all the faces of your mesh. For each face, consider all pairs of successive edges around the face. For example, if your face has vertices A,B,C,D, in that order, then you would consider the four pairs of successive edges:

• A→B and B→C
• B→C and C→D
• C→D and D→A
• D→A and A→B

For each pair of successive edges $e_1$ and $e_2$, take the cross product $e_1 × e_2$. Add the resulting vector to the normal of every vertex that adjoins the face (for example, in this case you would add to the normals of vertices A,B,C,D).

3. Loop through all the vertices, and normalize the length of the normal vector of each one.

Bye!