Jump to content

  • Log In with Google      Sign In   
  • Create Account

Trying to write a smoothing algorithm for meshes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 carkey   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 September 2013 - 10:14 AM

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.



Sponsor:

#2 TiagoCosta   Crossbones+   -  Reputation: 2199

Like
1Likes
Like

Posted 13 September 2013 - 11:39 AM

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...


Edited by TiagoCosta, 17 September 2013 - 08:05 AM.


#3 LorenzoGatti   Crossbones+   -  Reputation: 2703

Like
1Likes
Like

Posted 14 September 2013 - 08:31 AM

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?

Produci, consuma, crepa

#4 carkey   Members   -  Reputation: 106

Like
0Likes
Like

Posted 16 September 2013 - 03:16 AM

@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.



#5 carkey   Members   -  Reputation: 106

Like
0Likes
Like

Posted 16 September 2013 - 07:18 AM


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.



#6 TiagoCosta   Crossbones+   -  Reputation: 2199

Like
0Likes
Like

Posted 16 September 2013 - 10:29 AM

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--|
 

Edited by TiagoCosta, 16 September 2013 - 10:29 AM.


#7 carkey   Members   -  Reputation: 106

Like
0Likes
Like

Posted 16 September 2013 - 01:14 PM

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


Edited by carkey, 16 September 2013 - 01:15 PM.


#8 TiagoCosta   Crossbones+   -  Reputation: 2199

Like
0Likes
Like

Posted 16 September 2013 - 05:21 PM

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.



#9 carkey   Members   -  Reputation: 106

Like
0Likes
Like

Posted 17 September 2013 - 03:41 AM

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 :)



#10 TiagoCosta   Crossbones+   -  Reputation: 2199

Like
0Likes
Like

Posted 17 September 2013 - 08:06 AM

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.



#11 carkey   Members   -  Reputation: 106

Like
0Likes
Like

Posted 17 September 2013 - 09:45 AM

 

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.

 

 

Ah okay cool thanks, I'll give this another go because this makes sense to me now :)

 

Thanks for all the help and really quick responses!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS