Skip to content


# MeshQuality

VTKEx/Cxx/PolyData/MeshQuality

### Description

This example uses one of many selectable methods to determine the quality of a mesh. In this case, we have selected to use the area of the triangles. We show how to retrieve the quality metric computed on each triangle after the process is completed.

Other languages

See (Java)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

MeshQuality.cxx

    #include <vtkPolyData.h>
    #include <vtkPointData.h>
    #include <vtkCellData.h>
    #include <vtkDoubleArray.h>
    #include <vtkDataSet.h>
    #include <vtkMeshQuality.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>
    #include <vtkTriangleFilter.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>

    int main(int, char *[])
    {
      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->Update();

      vtkSmartPointer<vtkTriangleFilter> triangleFilter =
        vtkSmartPointer<vtkTriangleFilter>::New();
      triangleFilter->SetInputConnection(sphereSource->GetOutputPort());
      triangleFilter->Update();

      vtkPolyData* mesh = triangleFilter->GetOutput();
      std::cout << "There are " << mesh->GetNumberOfCells() << " cells." << std::endl;

      vtkSmartPointer<vtkMeshQuality> qualityFilter =
        vtkSmartPointer<vtkMeshQuality>::New();
      qualityFilter->SetInputData(mesh);
      qualityFilter->SetTriangleQualityMeasureToArea();
      qualityFilter->Update();

      vtkSmartPointer<vtkDoubleArray> qualityArray =
        dynamic_cast<vtkDoubleArray*>(qualityFilter->GetOutput()->GetCellData()->GetArray("Quality"));

      std::cout << "There are " << qualityArray->GetNumberOfTuples() << " values." << std::endl;

      for(vtkIdType i = 0; i < qualityArray->GetNumberOfTuples(); i++)
      {
        double val = qualityArray->GetValue(i);
        std::cout << "value " << i << " : " << val << std::endl;
      }

      vtkSmartPointer<vtkPolyData> polydata =
        vtkSmartPointer<vtkPolyData>::New();
      polydata->ShallowCopy(qualityFilter->GetOutput());

      // Visualize
      vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      mapper->SetInputData(polydata);
      mapper->SetScalarRange(polydata->GetScalarRange());

      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      renderWindowInteractor->SetRenderWindow(renderWindow);

      renderer->AddActor(actor);
      renderer->SetBackground(.3, .6, .3); // Background color green

      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(MeshQuality)

    find_package(VTK COMPONENTS
      vtkvtkCommonCore
      vtkvtkCommonDataModel
      vtkvtkFiltersCore
      vtkvtkFiltersSources
      vtkvtkFiltersVerdict
      vtkvtkInteractionStyle
      vtkvtkRenderingContextOpenGL2
      vtkvtkRenderingCore
      vtkvtkRenderingFreeType
      vtkvtkRenderingGL2PSOpenGL2
      vtkvtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping MeshQuality: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(MeshQuality MACOSX_BUNDLE MeshQuality.cxx )
      target_link_libraries(MeshQuality PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(MeshQuality MACOSX_BUNDLE MeshQuality.cxx )
      target_link_libraries(MeshQuality PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS MeshQuality
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build MeshQuality

Click here to download MeshQuality and its CMakeLists.txt file. Once the tarball MeshQuality.tar has been downloaded and extracted,

    cd MeshQuality/build

If VTK is installed:

    cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

    cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

    make

and run it:

    ./MeshQuality

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.