Skip to content

PineRootDecimation

Repository source: PineRootDecimation


Description

Demonstrates how to apply the decimation filter to get a reduced data size and then the connectivity filter to remove noisy isosurfaces. The data is from the root system of a pine tree.

To count the number of triangles in the polydata we do the following:

C++

We use a lambda function

auto NumberofTriangles = [](auto *pd)

Python

We just implement:

def NumberOfTriangles(pd):

within the scope of the calling function.

Info

See Figure 9-52 in Chapter 9 in the VTK Textbook.

Other languages

See (Python), (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

PineRootDecimation.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkConnectivityFilter.h>
#include <vtkDataSetMapper.h>
#include <vtkDecimatePro.h>
#include <vtkMCubesReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#ifdef VTK_CELL_ARRAY_V2
#include <vtkCellArrayIterator.h>
#endif // VTK_CELL_ARRAY_V2

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " filename" << std::endl;
    std::cout << "where: filename is pine_root.tri." << std::endl;
    return EXIT_FAILURE;
  }

  /**
   * Count the triangles in the polydata.
   * @param pd: vtkPolyData.
   * @return The number of triangles.
   */
  auto NumberofTriangles = [](vtkPolyData* pd) {
    vtkCellArray* cells = pd->GetPolys();
    vtkIdType npts = 0;
    auto numOfTriangles = 0;
#ifdef VTK_CELL_ARRAY_V2

    // Newer versions of vtkCellArray prefer local iterators:
    auto cellIter = vtk::TakeSmartPointer(cells->NewIterator());
    for (cellIter->GoToFirstCell(); !cellIter->IsDoneWithTraversal();
         cellIter->GoToNextCell())
    {
      auto cell = cellIter->GetCurrentCell();
      if (cell == nullptr)
      {
        break;
      }
      // If a cell has three points it is a triangle.
      if (cell->GetNumberOfIds() == 3)
      {
        numOfTriangles++;
      }
    }

#else  // VTK_CELL_ARRAY_V2

    // Older implementations of vtkCellArray use internal iterator APIs (not
    // thread safe):
    vtkIdType* pts;
    for (auto i = 0; i < pd->GetNumberOfPolys(); ++i)
    {
      int c = cells->GetNextCell(npts, pts);
      if (c == 0)
      {
        break;
      }
      // If a cell has three points it is a triangle.
      if (npts == 3)
      {
        numOfTriangles++;
      }
    }
#endif // VTK_CELL_ARRAY_V2

    return numOfTriangles;
  };
  ///**
  //* Count the triangles in the polydata.
  //* @param pd: vtkPolyData.
  //* @return The number of triangles.
  //*/
  // auto NumberofTriangles = [](vtkPolyData* pd) {
  //  vtkCellArray* cells = pd->GetPolys();
  //  vtkIdType npts = 0;
  //  vtkIdType* pts;
  //  auto numOfTriangles = 0;
  //  for (auto i = 0; i < pd->GetNumberOfPolys(); ++i)
  //  {
  //    int c = cells->GetNextCell(npts, pts);
  //    if (c == 0)
  //    {
  //      break;
  //    }
  //    // If a cell has three points it is a triangle.
  //    if (npts == 3)
  //    {
  //      numOfTriangles++;
  //    }
  //  }
  //  return numOfTriangles;
  //};

  std::string fileName = argv[1];

  vtkNew<vtkNamedColors> colors;

  // Create the pipeline.
  vtkNew<vtkMCubesReader> reader;
  reader->SetFileName(fileName.c_str());
  reader->FlipNormalsOff();
  reader->Update();
  std::cout << "Before Decimation there are:  "
            << NumberofTriangles(reader->GetOutput()) << " triangles."
            << std::endl;

  vtkNew<vtkDecimatePro> deci;
  deci->SetInputConnection(reader->GetOutputPort());
  deci->SetTargetReduction(0.9);
  deci->SetAbsoluteError(0.0005);
  deci->SetFeatureAngle(30);
  deci->SetErrorIsAbsolute(1);
  deci->AccumulateErrorOn();
  // deci->SplittingOff();
  deci->Update();
  std::cout << "After Decimation there are:   "
            << NumberofTriangles(deci->GetOutput()) << " triangles."
            << std::endl;

  vtkNew<vtkConnectivityFilter> connect;
  connect->SetInputConnection(deci->GetOutputPort());
  connect->SetExtractionModeToLargestRegion();
  connect->Update();
  // Note the use of dynamic_cast<vtkPolyData*> here.
  std::cout << "After Connectivity there are: "
            << NumberofTriangles(
                   dynamic_cast<vtkPolyData*>(connect->GetOutput()))
            << " triangles." << std::endl;

  vtkNew<vtkDataSetMapper> isoMapper;
  isoMapper->SetInputConnection(connect->GetOutputPort());
  isoMapper->ScalarVisibilityOff();
  vtkNew<vtkActor> isoActor;
  isoActor->SetMapper(isoMapper);
  isoActor->GetProperty()->SetColor(colors->GetColor3d("raw_sienna").GetData());

  // Get an outline of the data set for context.
  vtkNew<vtkOutlineFilter> outline;
  outline->SetInputConnection(reader->GetOutputPort());
  vtkNew<vtkPolyDataMapper> outlineMapper;
  outlineMapper->SetInputConnection(outline->GetOutputPort());
  vtkNew<vtkActor> outlineActor;
  outlineActor->SetMapper(outlineMapper);
  outlineActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

  // Create the Renderer, RenderWindow and RenderWindowInteractor.
  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren);
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  // Add the actors to the renderer, set the background and size.
  ren->AddActor(outlineActor);
  ren->AddActor(isoActor);
  // renWin->SetSize(750, 750);
  renWin->SetSize(512, 512);
  renWin->SetWindowName("PineRootDecimation");

  ren->SetBackground(colors->GetColor3d("SlateGray").GetData());

  vtkCamera* cam = ren->GetActiveCamera();
  cam->SetFocalPoint(40.6018, 37.2813, 50.1953);
  cam->SetPosition(40.6018, -280.533, 47.0172);
  cam->ComputeViewPlaneNormal();
  cam->SetClippingRange(26.1073, 1305.36);
  cam->SetViewAngle(20.9219);
  cam->SetViewUp(0.0, 0.0, 1.0);

  iren->Initialize();
  renWin->Render();
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PineRootDecimation)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersModeling
  IOGeometry
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PineRootDecimation: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(PineRootDecimation MACOSX_BUNDLE PineRootDecimation.cxx )
  target_link_libraries(PineRootDecimation PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS PineRootDecimation
  MODULES ${VTK_LIBRARIES}
)

Download and Build PineRootDecimation

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

cd PineRootDecimation/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:

./PineRootDecimation

WINDOWS USERS

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