2

I am trying to understand how the marching cubes algorithm works.

Source: http://paulbourke.net/geometry/polygonise/

What i don't understand is how do you calculate the "GRIDCELL" values. To be exact the

 double val[8];

part is not clear for me what it actually supposed to contain.

typedef struct {
   XYZ p[8];
   double val[8];
} GRIDCELL;

As i understand XYZ p[8]; are the vertex coordinates for the output cube. But what val[8]; is?

Playdome.io
  • 3,137
  • 2
  • 17
  • 34

3 Answers3

3

The marching cubes algorithm is -- as explained in the linked description -- an algorithm to build a polygonal representation from sampled data. The

double val[8];

are the samples for the 8 vertices of the cube. So they are not computed they are measurements from e.g. MRI scans. So the algorithm is the other way around: take a set of measured numbers and construct a surface representation for visualization from it.

rpress
  • 462
  • 3
  • 6
  • Sorry, but i still don't understand. Can you please give me an example if i have a single cube what would be the samples? – Playdome.io Jan 11 '15 at 02:53
  • You would most likely read them from a file or receive them from a network connection or some other source. – rpress Jan 11 '15 at 03:05
2

After further reading and research the explanation is quite simple.

First off all:

  • A voxel represents a value on a regular grid in three-dimensional space.

This value simply represents the so called "isosurface". Or in other words the density of the space.

double val[8];

To simplify: Basically this should be a value between -1.0f to 0.0f. Where -1.0f means solid and 0.0f empty space.

For ISO values a perlin/simplex noise can be used for example.

Playdome.io
  • 3,137
  • 2
  • 17
  • 34
  • For perlin/simplex noise as source there are other algorithms than the marching cubes algorithm. If the noise is 2D you can use some height field technique, for example, or use the cubic interpolation used in perlin noise to construct surfaces and tesselate them into triangles for visualization. – rpress Jan 12 '15 at 11:38
2

Te val is the level of "charge" for each vertex of the cell, it depends of the tipe of shape that you want to creae. f.e.: if you want to made a ball you can sample the values with the formula:

 for (int l = 0; l < 8; ++l){
           float distance = sqrtf(pow(cell.p[l].x - chargepos.x, 2.0) + pow(cell.p[l].y - chargepos.y, 2.0) + pow(cell.p[l].z - chargepos.z, 2.0));

    cell.val[l] = chargevalue /pow(distance, 2.0);}
Custodius
  • 63
  • 10