Trying to write a smoothing algorithm for meshes

Started by
9 comments, last by carkey 10 years, 7 months ago

Hi all,

I'm working on a small project and I've got to a point where I want to be able to smooth the meshes I've got.

I've had a quick google around and found something called Laplacian Smoothing http://en.wikipedia.org/wiki/Laplacian_smoothing but I'm not really sure how it works.

Does anyone have any good resources of what smoothing algorithms are out there, their pros/cons etc?

Thanks for your time.

Advertisement

I've never tried to do mesh smoothing so I'll just try to explain how to implement Laplacian smoothing based on your link:

Basically you have to calculate the average position of each vertex and its neighbors, adding them all and dividing by the number of adjacent vertices + 1 (the original vertex itself) . You can use the index buffer to find the neighbors.

(I'm assuming you want to apply the algorithm to a triangle list mesh with index buffer)


for(uint i = 0; i < vertices.size(); i++)
{
    uint x = 1;
    std::vector used;
 
    smoothedVertices[i] += vertices[i];
 
    used.push_back(vertices[i]);
 
    for(uint j = 0; j < indices.size(); j++)
    {
        if(indices[j] != i)
            continue;
 
        //add new vertices from this triangle
        uint startTriangleIndex = j - j%3;
 
        for(uint k = 0; k < 3; k++)
        {
            if(!used.contains(vertices[startTriangleIndex+k])) //Dont add the same vertex more than once
            {
                smoothedVertices[i] += vertices[startTriangleIndex+k];
                used.push_back(vertices[startTriangleIndex+k]);
                x++;
            }
      }
 
      smoothedVertices[i] /= x;
}

The code above should work, unless you have duplicate vertices...

Consider the requirements of your smoothing:

  • What do you consider smooth or not smooth enough?
  • Do you have sharp edges that should be preserved?
  • Do you need to compute sensible texture coordinates to keep the old textures without distortion? This usually means preserving angles (a conformal mapping).
  • Do you care about preserving the volume, area or length of parts of your geometry? For example, lengths determines whether an object can pass through a hole, areas determine the size of shadow and occlusion cones, and volume determines mass and buoyancy in physical simulations.
  • Would you like to preserve mesh structure exactly because it's important and/or to allow the use of simpler algorithms?
  • If not, do you need to collapse redundant or noisy geometry, add vertices to get more rounded shapes and a more regular subdivision, or both?
  • Do you want to limit the distance between the original and smoothed surfaces, or other error measures?

Omae Wa Mou Shindeiru

@TiagoCosta - Thanks for the explanation, that makes sense to me now, I'll have a go at implementing it soon!

@LorenzoGatti - It is a very basic project I am working on and so the answer is no to most of those questions:

1) I basically want to experiment with different 'amounts' of smoothing, so going as far as smoothing a Cube into a near-sphere to smoothing it only slightly.

2) Not really, as I say in (1) I just want to experiment

3) No, I do not use textures.

4) No

5) No, this is to save the mesh to file so I don't need to preserve the original mesh.

6) I assume I do if I am smoothing it? There is possibly some noisy geom. so it would be nice to be able to smooth this.

7) No.

Thanks for your help so far, hopefully those answers will give you more of an idea of what I want to do :)

Thanks.


You can use the index buffer to find the neighbors.

Hi TiagoCosta,

I was just implementing this and I was wondering about the above assertion. Is this the case? Isn't it possible for vertices I to be surrounded by j, k and l but their indicies are 10, 15, 44, 67? Depending on how the mesh has been created?

Everything else makes sense to me, just not this bit about using the indicies to know the neighbors.

Thanks for your time.

Maybe neighbors is the wrong word for it... By neighbors I meant adjacent vertices (if vertex V1 belongs to triangle A and vertices V2/V3 also belong to triangle A, V2/V3 are adjacent to V1). Since triangle lists are organized like this:


 
Indices -    0 1 2 3 4 5 6 7 8
Triangles - |--A--|--B--|--C--|
 

Ah okay, I see what you mean.

So if I had a 2 quad (4 triangle) grid like this:


a - b - c
|\  |\  |
| \ | \ |
|  \|  \|
d - e - f

?

?

?

?

In a triangle list the vertex list would be: {a, b, c, d, e, f}

And the indices list would be: {(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)}

Even though indices 1 and 5 (if they are both talking about vertex 'e') are exactly the same?

I think my mesh is constructed differently so that there would be no redundant indices and instead of {(0, 1, 2), (3, 4, 5), ... } it is {(0, 1, 2), (0, 3, 1), ...}

Does that make sense? Because triangles 1 and 2 share the vertices 'a' and 'e', they both use the same index? Or am I thinking about this all wrong? (Is that what you call a 'triangle strip'?)

If I do currently have my mesh setup the wrong way, is there a way to do the algorithm on this sort of mesh or should I think about converting it into a triangle list?

Thanks again for all the help so far smile.png

So if I had a 2 quad (4 triangle) grid like this:


a - b - c
|\  |\  |
| \ | \ |
|  \|  \|
d - e - f

?

In a triangle list the vertex list would be: {a, b, c, d, e, f}

And the indices list would be: {(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)}

(0,1,2) isn't a triangle.

The indices list would be {(0,1,4), (0,4,3), (1,2,5), (1,5,4)}

Ex: Since vertex b belongs to 3 triangles some of vertices on those triangles are the same.

Triangles that vertex b is part of:

a-b-e

b-c-f

b-f-e

As you can see vertices f and e appear twice so you have to keep a list of vertices already used to prevent using a vertex twice.

The example above demonstrates the case where there are duplicate vertex indices in the indices list but no duplicate vertices in the vertex list.

Ah okay, I see what you mean now.

The only thing I'm not quite understanding now is that the k for-loop uses the condition k < 2, should this be k < 3? Because say for vertex 0, j = 0, startTriangleIndex = 0, and then the loop only runs k = 0 and k = 1. k = 0 will not be used because that is our current vertex and it's already been added to the used vector and so then we'll only be averaging with k = 1 (vertices[0 + 1]), but what about vertices[2]?

Thanks for all the help so far, and hopefully I'll have some results soon to show :)

The only thing I'm not quite understanding now is that the k for-loop uses the condition k < 2, should this be k < 3?

Yes it should. I fixed my first reply.

This topic is closed to new replies.

Advertisement