# The surface created from Marching Cube and the mesh created from it is not smooth enough

**URL:** <https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444>\
**Category:** Beginner Questions\
**Created:** [November 26, 2019, 3:30pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444 "2019-11-26T15:30:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [November 26, 2019, 3:30pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/1 "2019-11-26T15:30:12Z")

</div>

Hi,

I am still in the process of learning ITK/VTK and struggling for a few days to create a mesh with three types (triangle, tetrahedron, and Simplex) of cells from the binary mask volume. I feel that I am doing some mistakes.

What I did is that

- I created the surface by following the procedure in [this link](https://lorensen.github.io/VTKExamples/site/Cxx/Modelling/MarchingCubes/) using marching cubes and setting the parameters as follows:

and saved the surface in a `vtkPolyData`

- I created the triangle mesh by following the commands in [this link](https://github.com/InsightSoftwareConsortium/ITKApps/blob/master/Auxiliary/vtk/vtkPolyDataToITKMesh.cxx).

My question is two-folded:

1. The surface created using Marching Cube does not look smooth, and the mesh edges and cells also look the same (see the figures). Is this a usual representation of triangle mesh and marching cube? Or should I change any parameter?

**Marching cube surface**

![marchingcube](https://discourse.itk.org/uploads/default/original/2X/d/db3b06825817dca9809adb845fb54fd424bfa902.png)

**Mesh surface**  
 ![mesh](https://discourse.itk.org/uploads/default/original/2X/5/5664c70e0f6bfc836502e7cfbb5b2571708caac0.png)  
and the mesh structure types that I have defined:

```
#ifndef vtkFloatingPointType
#define vtkFloatingPointType double
#endif

const unsigned int PointDimension = 3;
const unsigned int MaxCellDimension = 3;

typedef itk::DefaultStaticMeshTraits<
        vtkFloatingPointType,
        PointDimension,
        MaxCellDimension,
        vtkFloatingPointType,
        vtkFloatingPointType > MeshTraits;

typedef itk::Mesh<
        vtkFloatingPointType,
        PointDimension,
        MeshTraits > floatMeshType;

```

1. What changes can I carry out in [this code](https://github.com/InsightSoftwareConsortium/ITKApps/blob/master/Auxiliary/vtk/vtkPolyDataToITKMesh.cxx) to have a tetrahedron mesh?

Thank you

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 26, 2019, 4:22pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/2 "2019-11-26T16:22:23Z")

</div>

For smooth surface, turn on `Module_Cuberille` when building ITK and use [`itk::CuberilleImageToMeshFilter`](https://itk.org/Doxygen/html/classitk_1_1CuberilleImageToMeshFilter.html) instead of `vtkMarchingCubes`.

For tetrahedal mesh generation, take a look at this [message](https://itk.org/pipermail/insight-users/2009-January/028531.html). The main class is [here](http://svn.na-mic.org/NAMICSandBox/trunk/TetrahedralMeshGeneration/BinaryMaskTo3DAdaptiveMeshFilter/Code/).

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [November 30, 2019, 2:26pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/3 "2019-11-30T14:26:49Z")

</div>

@dzenanz Hi Sir, Thanks for introducing `CuberilleImageToMeshFilter`. I have attached two meshes: 1)CuberilleMesh 2)MarchingCubeMesh, which have been created triangle mesh by using two methods.  
[meshes.zip](https://discourse.itk.org/uploads/short-url/ooS3Tx4rAWCgZXUdJir0Li4s6en.zip) (2.3 MB)

```
using CuberFilterType=itk::CuberilleImageToMeshFilter<ImageType ,floatMeshType >;

CuberFilterType::Pointer cuberFilter=CuberFilterType::New();
cuberFilter->SetInput(image);

   try
        {
            cuberFilter->Update();
        }
        catch (itk::ExceptionObject & error)
        {
            std::cerr << "Error: " << error << std::endl;
            return EXIT_FAILURE;
        }

floatMeshType::Pointer cuberMesh = cuberFilter->GetOutput();

```

As you can view the meshes, there is not that much difference between the two meshes in terms of smoothness. I am wondering should I change any parameter for the `cuberFilter` ? Or if I am doing some mistakes somewhere.

The famous bunny mesh looks smooth and the size of triangles are big. As attached here:  
[bunny\_mesh.zip](https://discourse.itk.org/uploads/short-url/uucdXcnuQQBVOr8WE40MD7s2jHT.zip) (1.1 MB)

How the size and the number of triangle (or other cell types) in a mesh can affect the mesh processing?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [November 30, 2019, 5:02pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/4 "2019-11-30T17:02:21Z")

</div>

Probably your input mesh is highly anisotropic. Resampling the image to have cubic voxels before segmentation will fix the issue. See some more details in [this discussion](https://discourse.slicer.org/t/fetal-lung-volume-calculation/578/5?u=lassoan).

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [December 2, 2019, 4:37pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/5 "2019-12-02T16:37:58Z")

</div>

For a smoother mesh, use a b-spline interpolator on the original volume data, not the thresholded volume, as described in the [Cuberille Insight Journal article](https://www.insight-journal.org/browse/publication/740).

---

<div class="post-metadata">

**Author:** ![Sara\_Caffe](https://discourse.itk.org/user_avatar/discourse.itk.org/sara_caffe/32/974_2.png) [@Sara\_Caffe](https://discourse.itk.org/u/Sara_Caffe)\
**Post date:** [December 19, 2019, 8:22am UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/6 "2019-12-19T08:22:46Z")

</div>

@matt.mccormick Thanks a lot Sir. Really appreciate it. This is the thing that needed it. May I ask one more question? What filter type should be used to view the created mesh by Cuberille? Should I convert it to UnstrcuturedGrid in Vtk and visualize using Vtk or can be visualized in ITK?

Thank you

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [December 20, 2019, 1:28am UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/7 "2019-12-20T01:28:15Z")

</div>

Hi @Sara_Caffe,

You can save the mesh with [`itk::MeshFileWriter`](https://itk.org/Doxygen/html/classitk_1_1MeshFileWriter.html), which can save the mesh as a `vtkPolyData` as a`.vtk` or other mesh file formats. The files can be read as `vtkPolyData` with VTK and rendered – this is the data structure VTK uses for rendering. `vtkUnstructuredGrid` can also be used, but it is converted to `vtkPolyData` for rendering.

Another option is to visualize [in Jupyter with _itkwidgets_](https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/master?filepath=examples/Mesh.ipynb).

Or, visualize the files with [itk-vtk-viewer](https://kitware.github.io/itk-vtk-viewer/index.html), e.g.

https://kitware.github.io/itk-vtk-viewer/app/?fileToLoad=https://data.kitware.com/api/v1/file/5d23b917877dfcc902def289/download/Bunny.vtp

---

<div class="post-metadata">

**Author:** ![mihail.isakov](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@mihail.isakov](https://discourse.itk.org/u/mihail.isakov)\
**Post date:** [December 20, 2019, 5:53pm UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/8 "2019-12-20T17:53:04Z")

</div>

BTW, i used in the past this pure VTK example, eliminates staircase artifacts completely with default parameters, produces very smooth meshes. Just for completeness.

[https://lorensen.github.io/VTKExamples/site/Cxx/Medical/GenerateModelsFromLabels/](https://lorensen.github.io/VTKExamples/site/Cxx/Medical/GenerateModelsFromLabels/)

---

<div class="post-metadata">

**Author:** ![RISHI\_J](https://discourse.itk.org/user_avatar/discourse.itk.org/rishi_j/32/2195_2.png) [@RISHI\_J](https://discourse.itk.org/u/RISHI_J)\
**Post date:** [July 27, 2021, 8:28am UTC](https://discourse.itk.org/t/the-surface-created-from-marching-cube-and-the-mesh-created-from-it-is-not-smooth-enough/2444/9 "2021-07-27T08:28:26Z")

</div>

Hi can you send me your code I am facing similar issue @Sara_Caffe
