Marching Cubes

Visualising Scalar Fields 3D: Marching Cubes Part 1 : Contour Map

June 23, 2016

Tags: , , , , , , , , , , , , , ,

Previous : Visualising Scalar Fields : Marching Squares Part 1 : Contour Map


If you haven’t already, it’s worth taking a look through the Marching Squares posts for visualising scalar fields before jumping into these 3D versions. The basic principles are the same (I’ll assume knowledge of those principles in these posts), but they’re probably a bit easier to get to grips with. The 2D version of this contour example can be found here.

The code for the marching cubes examples can be found here. The examples in this post can be found in the Contour scene.

There’s a really great post on Marching Cubes by Paul Bourke, with all the case configurations – I’ve simply applied his work to the examples I used in the Marching Squares posts in Unity, so all credit for these posts to him.

Marching Cubes

Marching Cubes is the algorithm that Marching Squares (used in the 2D scalar field posts) was derived from. A lot of the work is therefore the same, but with a couple of changes. Firstly, we’re adding another dimension, which gives us a little more work to do in terms of assigning corner values and drawing the mesh.

Secondly, the number of configurations for each cube has increased. As before there are two possible states for each corner of the cube: inside the isolevel (equivalent of the term ‘threshold value’ used in the 2D posts), or outside the isolevel. This time, however, there are eight corners to consider instead of 4, so we have 256 different configurations instead of 16 (28 instead of 24). That’s unfortunately too many to display here, but I’ve created a CaseDemonstration scene to view them individually (there’s a tickbox in the App inspector that displays the grid cell outline, and you might have to move the view around in the Scene window to see the mesh correctly).

With the extra dimension, we’re now working with a cube that has the following corner indexing for determining the case index, going from the bottom to top and moving clockwise:

Screen Shot 2016-07-06 at 16.25.15

 

With those corner values assigned, we can work out the case configuration :


if (gridCell.VertexValue [0] < isolevel) caseIndex |= 1;
if (gridCell.VertexValue [1] < isolevel) caseIndex |= 2;
if (gridCell.VertexValue [2] < isolevel) caseIndex |= 4;
if (gridCell.VertexValue [3] < isolevel) caseIndex |= 8;
if (gridCell.VertexValue [4] < isolevel) caseIndex |= 16;
if (gridCell.VertexValue [5] < isolevel) caseIndex |= 32;
if (gridCell.VertexValue [6] < isolevel) caseIndex |= 64;
if (gridCell.VertexValue [7] < isolevel) caseIndex |= 128;

This then gives us the case index for the triangle table lookup and tells us which and the edge points of our cube that need to be added to our mesh vertices array.

There are 12 of these edge points (indexing is shown below).

Screen Shot 2016-07-06 at 16.27.27

As with marching squares, a lot of the values and edge points are shared between cubes, so I’ve done a little extra work to pass information between neighbours where possible.

Applying this to a 3D grid, we now get the following result for the cone example:

Screen Shot 2016-07-06 at 13.06.34

Screen Shot 2016-07-06 at 13.06.25

NB : The colours are applied based Mesh.colors, by adding a colour value for each vertex being sampled based on its y value.


Previous : Visualising Scalar Fields : Marching Squares Part 1 : Contour Map

0 likes

Author

Your email address will not be published.