Skip to content

Tetrahedron

vtk-examples/Cxx/GeometricObjects/Tetrahedron


Description

Tetrahedron. The tetrahedron is a primary three-dimensional cell. The tetrahedron is defined by a list of four nonplanar points. The tetrahedron has six edges and four triangular faces.

Other languages

See (Python), (CSharp)

Question

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

Code

Tetrahedron.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTetra.h>
#include <vtkUnstructuredGrid.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0, 0, 0);
  points->InsertNextPoint(1, 0, 0);
  points->InsertNextPoint(1, 1, 0);
  points->InsertNextPoint(0, 1, 1);

  points->InsertNextPoint(2, 2, 2);
  points->InsertNextPoint(3, 2, 2);
  points->InsertNextPoint(3, 3, 2);
  points->InsertNextPoint(2, 3, 3);

  // Method 1
  vtkNew<vtkUnstructuredGrid> unstructuredGrid1;
  unstructuredGrid1->SetPoints(points);

  vtkIdType ptIds[] = {0, 1, 2, 3};
  unstructuredGrid1->InsertNextCell(VTK_TETRA, 4, ptIds);

  // Method 2
  vtkNew<vtkUnstructuredGrid> unstructuredGrid2;
  unstructuredGrid2->SetPoints(points);

  vtkNew<vtkTetra> tetra;

  tetra->GetPointIds()->SetId(0, 4);
  tetra->GetPointIds()->SetId(1, 5);
  tetra->GetPointIds()->SetId(2, 6);
  tetra->GetPointIds()->SetId(3, 7);

  vtkNew<vtkCellArray> cellArray;
  cellArray->InsertNextCell(tetra);
  unstructuredGrid2->SetCells(VTK_TETRA, cellArray);

  // Create a mapper and actor
  vtkNew<vtkDataSetMapper> mapper1;
  mapper1->SetInputData(unstructuredGrid1);

  vtkNew<vtkActor> actor1;
  actor1->SetMapper(mapper1);
  actor1->GetProperty()->SetColor(colors->GetColor3d("Cyan").GetData());

  // Create a mapper and actor
  vtkNew<vtkDataSetMapper> mapper2;
  mapper2->SetInputData(unstructuredGrid2);

  vtkNew<vtkActor> actor2;
  actor2->SetMapper(mapper2);
  actor2->GetProperty()->SetColor(colors->GetColor3d("Yellow").GetData());

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetWindowName("Tetrahedron");
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actor to the scene
  renderer->AddActor(actor1);
  renderer->AddActor(actor2);
  renderer->SetBackground(colors->GetColor3d("DarkGreen").GetData());
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(-10);
  renderer->GetActiveCamera()->Elevation(-20);

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Tetrahedron)

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

if (NOT VTK_FOUND)
  message(FATAL_ERROR "Tetrahedron: 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(Tetrahedron MACOSX_BUNDLE Tetrahedron.cxx )
  target_link_libraries(Tetrahedron PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS Tetrahedron
  MODULES ${VTK_LIBRARIES}
)

Download and Build Tetrahedron

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

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

./Tetrahedron

WINDOWS USERS

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