Skip to content

RectilinearGridToTetrahedra

vtk-examples/Cxx/RectilinearGrid/RectilinearGridToTetrahedra

Other languages

See (Java)

Question

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

Code

RectilinearGridToTetrahedra.cxx

#include <vtkDoubleArray.h>
#include <vtkNew.h>
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridToTetrahedra.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>

int main(int, char*[])
{
  // Create a grid
  vtkNew<vtkRectilinearGrid> grid;

  grid->SetDimensions(5, 4, 3);

  vtkNew<vtkDoubleArray> xArray;
  xArray->InsertNextValue(0.0);
  xArray->InsertNextValue(1.0);
  xArray->InsertNextValue(2.0);
  xArray->InsertNextValue(3.0);
  xArray->InsertNextValue(4.0);

  vtkNew<vtkDoubleArray> yArray;
  yArray->InsertNextValue(0.0);
  yArray->InsertNextValue(1.0);
  yArray->InsertNextValue(2.0);
  yArray->InsertNextValue(3.0);

  vtkNew<vtkDoubleArray> zArray;
  zArray->InsertNextValue(0.0);
  zArray->InsertNextValue(1.0);
  zArray->InsertNextValue(2.0);

  grid->SetXCoordinates(xArray);
  grid->SetYCoordinates(yArray);
  grid->SetZCoordinates(zArray);

  vtkNew<vtkRectilinearGridToTetrahedra> rectilinearGridToTetrahedra;
  rectilinearGridToTetrahedra->SetInputData(grid);
  rectilinearGridToTetrahedra->Update();

  vtkNew<vtkXMLUnstructuredGridWriter> writer;
  writer->SetFileName("output.vtu");
  writer->SetInputConnection(rectilinearGridToTetrahedra->GetOutputPort());
  writer->Write();

  std::cout << "There are "
            << rectilinearGridToTetrahedra->GetOutput()->GetNumberOfCells()
            << " cells." << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(RectilinearGridToTetrahedra)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersGeneral
  vtkIOXML
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping RectilinearGridToTetrahedra: ${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(RectilinearGridToTetrahedra MACOSX_BUNDLE RectilinearGridToTetrahedra.cxx )
  target_link_libraries(RectilinearGridToTetrahedra PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(RectilinearGridToTetrahedra MACOSX_BUNDLE RectilinearGridToTetrahedra.cxx )
  target_link_libraries(RectilinearGridToTetrahedra PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS RectilinearGridToTetrahedra
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build RectilinearGridToTetrahedra

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

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

./RectilinearGridToTetrahedra

WINDOWS USERS

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