0

I am trying to implement marching cubes in C#, but I've come to a part where I don't understand the algorithm and I don't how to implement it.

int Polygonise(GRIDCELL grid, double isolevel, TRIANGLE *triangles)

The third argument I don't really understand. I know it's a pointer, but later on in the algo, when you set the triangles it appears as though the triangles variable is an array of the TRIANGLE struct:

int ntriang = 0;

for (int i=0; triTable[cubeindex,i]!=-1; i+=3) {
    triangles[ntriang].p[i  ] = vertlist[triTable[cubeindex,i  ]];
    triangles[ntriang].p[i+1] = vertlist[triTable[cubeindex,i+1]];
    triangles[ntriang].p[i+2] = vertlist[triTable[cubeindex,i+2]];
    ntriang++;
}

Notice the triangles[ntriang]. That doesn't make sense because before we set triangles to TRIANGLE *triangles. I also don't understand why It's a pointer.

Daniel Node.js
  • 6,734
  • 9
  • 35
  • 57
  • 4
    Why doesn't `triangles[ntriang]` make sense? It simply uses `ntriang` to index into the array pointed to by `triangles`. The rest of the expression assigns something to the member `p` of the struct at the `ntriang-th` position. – pmr Jul 18 '12 at 12:38

2 Answers2

2

The caller of Polygonize expects *triangles point to an allocated array long enough to contain all the triangles. The equivalent in c# can be a TRIANGLE[] or a List<TRIANGLE>()

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
1

It looks like this function takes the GRID of voxels/cells and outputs the triangles. It is a pointer since you will get a list of triangles.

Chefire
  • 139
  • 1
  • 7