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.