# How to decimate a 3D box mesh generated by GMSH?

**URL:** https://discourse.vtk.org/t/how-to-decimate-a-3d-box-mesh-generated-by-gmsh/2699
**Category:** Support
**Created:** [February 25, 2020, 2:19pm UTC](https://discourse.vtk.org/t/how-to-decimate-a-3d-box-mesh-generated-by-gmsh/2699 "2020-02-25T14:19:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![duy\_duc](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/d/258eb7/32.png) [@duy\_duc](https://discourse.vtk.org/u/duy_duc)
#### Post date: [February 25, 2020, 2:19pm UTC](https://discourse.vtk.org/t/how-to-decimate-a-3d-box-mesh-generated-by-gmsh/2699/1 "2020-02-25T14:19:01Z")

</div>

Hi everyone

I am new to VTK.  
I want to decimate a box meshed with tetrahedra and generated by GMSH. I follow the example for Python [here](https://lorensen.github.io/VTKExamples/site/Python/Meshes/Decimation/).  
However, my decimated mesh only contain boundaries of the box and 2D triangle meshes.

Could you please help me to point out what is wrong in my code?

Thank you very much

```
from vtk import *

def unstructured_grid_to_polydata(unstructured_grid):
   # Convert unstructured grid to polydata
   geo_filter = vtk.vtkGeometryFilter()
   geo_filter.SetInputData(unstructured_grid)
   geo_filter.Update()
   polydata = geo_filter.GetOutput()
   return polydata 

def decimate_mesh(mesh_name):
    print('=== decimate ===')
    print('mesh_name:', mesh_name)
    head_name, tail_name = mesh_name.split('.')

    # Reader reads mesh file
    reader = vtkUnstructuredGridReader()
    reader.SetFileName(mesh_name)
    reader.Update()

    # convert unstructured grid to polydata
    inputPoly = unstructured_grid_to_polydata(reader.GetOutput())

    print("Before decimation\n"
          "-----------------\n"
          "There are " + str(inputPoly.GetNumberOfPoints()) + " points.\n"
          "There are " + str(inputPoly.GetNumberOfPolys()) + " polygons.\n")

    # decimate
    decimate = vtkDecimatePro()
    decimate.SetInputData(inputPoly)
    decimate.SetTargetReduction(.10)
    decimate.Update()

    # decimate Polydata
    decimatedPoly = vtkPolyData()
    decimatedPoly.ShallowCopy(decimate.GetOutput())

    print("After decimation \n"
          "-----------------\n"
          "There are " + str(decimatedPoly.GetNumberOfPoints()) + " points.\n"
          "There are " + str(decimatedPoly.GetNumberOfPolys()) + " polygons.\n")

    # export decimated mesh (does not work)
    #writer = vtkUnstructuredGridWriter()
    #writer.SetInputData(decimate.GetOutput())
    #writer.SetFileName("{}_decimated.vtk".format(head_name))
    #writer.Write()

    #save .vtk polydata
    writer = vtk.vtkPolyDataWriter()
    writer.SetFileTypeToASCII()
    writer.SetInputData(decimate.GetOutput()) 
    writer.SetFileName("{}_decimated.vtk".format(head_name))
    writer.Write()

    #STL writer
    stlWriter = vtkSTLWriter()
    stlWriter.SetFileName("{}_decimated.stl".format(head_name))
    stlWriter.SetFileTypeToBinary()
    stlWriter.SetInputConnection(decimate.GetOutputPort())
    stlWriter.Write()

    return

if __name__ == " __main__":
    load_path = 'Geometry/'
    mesh_name = 'box.vtk'
    decimate_mesh(load_path + mesh_name)

```

 ![box](https://discourse.vtk.org/uploads/default/original/2X/6/6c35d8938aebebe3a4394f8682a836657638f4e3.png) ![box_decimated](https://discourse.vtk.org/uploads/default/original/2X/0/005c5021bc90a1f5cd5591cd109c1685b13aa0c7.png)

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [February 25, 2020, 8:00pm UTC](https://discourse.vtk.org/t/how-to-decimate-a-3d-box-mesh-generated-by-gmsh/2699/2 "2020-02-25T20:00:39Z")

</div>

The decimation algorithms (e.g. `vtkDecimatePro`) only work on `vtkPolyData` meshes (2D surface geometries - so the bounding surface of your volumetric mesh).

As far as I am aware, there aren’t any decimation algorithms for 3D geometries in VTK ( **to my knowledge** ).

@akaszynski might have a 3D version of the ACVD algorithm that he might be willing (😀) to upload in [PyACVD](https://github.com/pyvista/pyacvd) which works with [PyVsita](https://docs.pyvista.org) (a Python wrapper for VTK). So maybe keep an eye on PyACVD…

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [February 25, 2020, 8:03pm UTC](https://discourse.vtk.org/t/how-to-decimate-a-3d-box-mesh-generated-by-gmsh/2699/3 "2020-02-25T20:03:41Z")

</div>

If you open an issue in [PyACVD](https://github.com/pyvista/pyacvd) then you might be likely to see that 3D algorthm added 😉

---

<div class="post-metadata">

### Author: ![akaszynski](https://discourse.vtk.org/user_avatar/discourse.vtk.org/akaszynski/32/10751_2.png) [@akaszynski](https://discourse.vtk.org/u/akaszynski)
#### Post date: [February 25, 2020, 8:24pm UTC](https://discourse.vtk.org/t/how-to-decimate-a-3d-box-mesh-generated-by-gmsh/2699/4 "2020-02-25T20:24:04Z")

</div>

It’s a 3D clustering algorithm that works fairly well, but I’ve only used it internally. If there’s interest on GitHub I’ll add it.
