# Error in Marching Squares/Cubes implementation?

**URL:** https://discourse.vtk.org/t/error-in-marching-squares-cubes-implementation/13317
**Category:** Web
**Tags:** proposal
**Created:** [February 14, 2024, 5:17pm UTC](https://discourse.vtk.org/t/error-in-marching-squares-cubes-implementation/13317 "2024-02-14T17:17:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Neral](https://discourse.vtk.org/user_avatar/discourse.vtk.org/neral/32/8468_2.png) [@Neral](https://discourse.vtk.org/u/Neral)
#### Post date: [February 14, 2024, 5:17pm UTC](https://discourse.vtk.org/t/error-in-marching-squares-cubes-implementation/13317/1 "2024-02-14T17:17:03Z")

</div>

Hi,

As I am trying to implement the quadric decimation algorithm and I found the following while manipulating meshes computed with ImageMarchingCubes: sometimes, triangles generated might be empty, this is the consequence of how the parameter t is computed:

In file [Filters/General/ImageMarchingCubes/index.js](https://github.com/Kitware/vtk-js/blob/master/Sources/Filters/General/ImageMarchingCubes/index.js)

```auto
const t =
    (cVal - voxelScalars[edgeVerts[0]]) /
    (voxelScalars[edgeVerts[1]] - voxelScalars[edgeVerts[0]]);

```

If we have voxelScalars[edgeVerts[0]] === cVal, the position of the point along the axis of edgeVerts will be computed at one of the ends. If the same thing happens on the 3 axis, the generated triangle will be have its three points at same coordinates and it will be empty (which can lead to different kinds of problem).

When looking at the code in C++, the condition on cVal threshold seems slightly different:

In file [Filters/General/ImageMarchingCubes/index.js](https://github.com/Kitware/vtk-js/blob/master/Sources/Filters/General/ImageMarchingCubes/index.js)

```auto
if (voxelScalars[VERT_MAP[idx]] >= cVal) {
    index |= CASE_MASK[idx]; // eslint-disable-line no-bitwise
}

```

In file [Filters/General/vtkImageMarchingCubes.cxx](https://github.com/Kitware/VTK/blob/master/Filters/General/vtkImageMarchingCubes.cxx)

```auto
if ((double)(ptr[0]) > value)
{
    cubeIndex += 1;
}

```

Replacing “\>= cVal” with “\> cVal” corrects the issue as there is no more risk of dealing with empty triangles.

What I can’t understand is that the C++ implementations uses “\>=” in Filters/Core/vtkMarchingCubes.cxx but “\>” in Filters/General/vtkImageMarchingCubes.cxx

Also, when creating the new polydata and updating the output, I think the following line needs to be added:  
In file [Filters/General/ImageMarchingCubes/index.js](https://github.com/Kitware/vtk-js/blob/master/Sources/Filters/General/ImageMarchingCubes/index.js), after

```auto
const polydata = vtkPolyData.newInstance();
polydata.getPoints().setData(new Float32Array(pBuffer), 3);
polydata.getPolys().setData(new Uint32Array(tBuffer));

```

polydata.getPolys().setNumberOfComponents(4);

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [February 15, 2024, 11:38am UTC](https://discourse.vtk.org/t/error-in-marching-squares-cubes-implementation/13317/2 "2024-02-15T11:38:53Z")

</div>

> What I can’t understand is that the C++ implementations uses “\>=” in Filters/Core/vtkMarchingCubes.cxx but “\>” in Filters/General/vtkImageMarchingCubes.cxx

My guess is that the implementation of vtkImageMarchingCubes is incorrect. Please submit an issue / MR so we can clean this up.
