3

How is the marching cubes triangle table generated? I know, that i can just use a precalculated one, but i am interested in how the tables can be generated.

Seems like the original authors generated it by triangulating everything on paper, but is there a method to generate it in code?

The edge table is straight forward, but I do not see if there is an easy method to get a triangle table, even when disregarding the ambiguous cases.

allo
  • 3,955
  • 8
  • 40
  • 71

3 Answers3

3

Here is one method:

Start with the vertices and edge points. Now, if one vertex is on, then put three edge points adjacent to that vertex. Do this for every vertex.

Now, take note of the edges that connect the vertices that are on. Remove the edge points that lie on them.

Make use of those edge points to make a triangle array.

This way, you can make a triangle table by using a for loop that loops 256 times, and in fact, you might not even need one, using this method.

In order to test it:

Try using the method I told you. It works. Just one thing though, I got confused when I was testing it because of how it was drawn. Anyway, enjoy! :)

EDIT : This way you can use any way to render the triangles. People use different triangle tables. Using this method, you don't have to figure out what is the numbering for the edge points. You can use this your way so that you don't have to waste time figuring out.

allo
  • 3,955
  • 8
  • 40
  • 71
Kino Bacaltos
  • 373
  • 1
  • 2
  • 16
  • I am not sure if this fully answers my question. You describe the step to get from the distance function at vertices to the edge points, which is part of the algorithm. Afterwards the found edge points need to be connected by triangles (as seen in your image). Now I want to know the *algorithm used to generate the triangle table*, which maps edge combinations to triangles like the one http://paulbourke.net/geometry/polygonise/](here). Can you elaborate on this part? – allo Aug 21 '18 at 13:12
  • Well, this can be used to generate it. Iterate though the corner points, and use the method. – Kino Bacaltos Aug 21 '18 at 13:45
  • mm. ok i know this is too late but look at this. You can number all of the edges with a valid point then take this pattern `0, 1, 2, 1, 3, 2, 2, 3, 4, 3, 5, 4, 4, 5, 6, 5, 7, 6` where each number represents the point or edge number. Then there you have it.. But what makes a valid point? If a point only gets calculated once, then it is a valid point. If at least two, then it is invalid. – Kino Bacaltos Apr 09 '19 at 12:51
  • I am still not so sure, if this really answers my question. I am looking for the algorithm to generate the 16 cases. When I now have a look at the generated points, I could for example generate an invalid triangulation with intersecting triangles for case 9. One could think, you only need to triangulate the point set of the points on the edges, but they are not in all cases planar, so a simple 2D triangulation won't work. And I do not get how your pattern works at all. Maybe you want to add it with some more explanation to your answer. – allo Apr 10 '19 at 08:33
  • When you got some nice method, a bit of pseudo code in the answer would help to get the idea how you're generating the triangle list. I have an input like the indices of the active edges and the vertex positions on the edges (if they matter) and want to get as output a list of 1-4 triangles as 3-tuple of active edges, that defines a triangle. Consistent orientation would be plus, but I think I can figure out the part myself, once I got the idea how to generate the triangle table. – allo Apr 10 '19 at 08:37
  • 1
    m8 is it ok if i just make another answer to this question (For now I'm editing it) – Kino Bacaltos Apr 12 '19 at 14:39
  • Yes, multiple answers (with different content) are acceptable here. And when you really have a nice explanation of the algorithm to generate the different triangle sets, I think you may get quite a few upvotes. All solutions I found so far are "just use the table" or "When we look at all possible combinations and write them down", but no automatic algorithms. – allo Apr 14 '19 at 10:53
  • 1
    I could give you another solution tomorrow. I'm still working on exams. – Kino Bacaltos May 08 '19 at 12:08
1

Here is the original explanation of the algorithm: http://paulbourke.net/geometry/polygonise/

abenci
  • 8,422
  • 19
  • 69
  • 134
-1

Another Solution (DONT LOOK AT THIS UNTIL I TELL YOU TO DO SO) (OR ELSE YOU'LL SEE MY UNFINISHED MESS BECAUSE I HAVE NO TIME TO FINISH IT)

I won't really be citing code.

The Marching Cube Triangle List starts from a cube.

If you have a cube, or can make one, I would advice you to use one, just for convenience. Neatly number its corners and edges using a pen or a marker. Please don't count them irregularly. Make the counting "programmable".

A cube has 8 corners. Let's imaging each corner as a light bulb. So the triangle list generates triangles for every combination of on and off light bulbs. That is, 256 combinations. ( (2 bulb conditions)^(8 bulbs) = 256 combinations) But how is a triangle list generated?

For every combination, we have a unique cube. Let's start with a combination like this

 /7.on-----8.off
/ |           /|
3.off----4.on/ |
| 5.on ------| 6.off
| /          | /
|/           |/
1.on------2.off

We can make a list of corners or bulbs. bulb 1, bulb 2, bulb 3 - bulb 8 And then tell if they're on or off on, off, off... off The way you number your points really depends on what you want. Though I would recommend an organized way. There are 40,320 combinations of point orders. For this example I'm using the order I'm used to.

We could turn this list of bulb states into a binary number. Off means 0, on means 1. 10011010 in binary = 154th index of table

Probably you know all of this :P

2. Create a 12-element array of edge points. For the newbies (no offense rlly): edge points -> points lying on the edge.

int[] edgePoints = new int[12] {0,0,0,0,0,0,0,0,0,0,0,0} //12 elements

Then we enable the edge points. For every corner point, enable its adjacent edge points. If this iteration is done, check if a point has been enabled more than 1 time. If it has, disable it. If it hasn't been enabled, it's disabled.

Now you have your valid edge points. Store in an array or list. Now let's make the triangles.

Here are some arrays we can use.

List<int> validVertexIndices = new List<int>(); //add the valids
List<int> usedVertices = new List<int>();
List<int> oldVertices = new List<int>();
List<int[]> usedTris = new List<int[]>();

Let's start by making one triangle. The program doesn't really know how to connect the dots. (to make triangles) So, let's start with a random point. (Best start with the lowest index)

usedVertices.Add(your random point index);

Then check the nearest point to the starting point. To do this, iterate though all of the points except the one started with. Check the distance between the starting point and every other point. (assuming the x,y,z position of each point is a perfect multiple of 0.5) (example 0, 0.5, 1.0)

If the distance between the starting point and another point is the shortest among all the other distances, then those two are the first two points of the first triangle.

usedVertices.Add(That second point to be used);

Then get the nearest point to the second point, that isn't the first or the second point. That's the third point.

usedVertices.Add(The third point);

And then you can register the triangle.

usedTris.Add(usedVertices.ToArray());
oldVertices.Add(usedVertices);
usedVertices = new List<int>();

Now for the other triangles. There are a lot of rules. You could simply repeat the process for the first triangle, except this time, you'd have to reset the iteration and change a point if it goes against the rules. You can change the first point to the next point that isn't used. Then the second point and third point if it fails.

The three main rules are: 1. Triangles shouldn't be the same as the previous triangles. 2. Triangles shouldn't intersect. There are intersection algorithms out there but I can't put them here as they are too long. 3. Check if it is covering enabled corner points. - Get the average distance of all adjacent enabled corner points and see if it is parallel and has the same direction with the normal of the current triangle.

It's complicated and might not be successful but you've got a set of rules already.

Keep on doing it until you have reached n(n-1)/2 times. You are then guranteed to have triangles. (where n = number of enabled edge points);

Then, for every triangle, create a clone so that its normal is reversed.

Then there you have it. Might not be like much so it's better to just find a source.

lol also, please do not make and train a neural network to make one xD that is just torture.

Kino Bacaltos
  • 373
  • 1
  • 2
  • 16