Skip to content

TriangleArea

Repository source: TriangleArea

Question

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

Code

TriangleArea.cxx

#include <vtkCellArray.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkTriangle.h>

int main(int, char*[])
{
  // setup points (geometry)
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);

  // create a triangle on the three points in the polydata
  vtkNew<vtkTriangle> triangle1;
  triangle1->GetPointIds()->SetId(0, 0);
  triangle1->GetPointIds()->SetId(1, 1);
  triangle1->GetPointIds()->SetId(2, 2);

  vtkNew<vtkTriangle> triangle2;
  triangle2->GetPointIds()->SetId(0, 2);
  triangle2->GetPointIds()->SetId(1, 3);
  triangle2->GetPointIds()->SetId(2, 0);

  // add the triangles to the list of triangles
  vtkNew<vtkCellArray> triangles;
  triangles->InsertNextCell(triangle1);
  triangles->InsertNextCell(triangle2);

  // create a polydata object
  vtkNew<vtkPolyData> polydata;

  // add the geometry and topology to the polydata
  polydata->SetPoints(points);
  polydata->SetPolys(triangles);

  for (vtkIdType i = 0; i < polydata->GetNumberOfCells(); i++)
  {
    vtkCell* cell = polydata->GetCell(0);

    vtkTriangle* triangle = dynamic_cast<vtkTriangle*>(cell);
    double p0[3];
    double p1[3];
    double p2[3];
    triangle->GetPoints()->GetPoint(0, p0);
    std::cout << "p0: " << p0[0] << " " << p0[1] << " " << p0[2] << std::endl;
    triangle->GetPoints()->GetPoint(1, p1);
    std::cout << "p1: " << p1[0] << " " << p1[1] << " " << p1[2] << std::endl;
    triangle->GetPoints()->GetPoint(2, p2);
    std::cout << "p2: " << p2[0] << " " << p2[1] << " " << p2[2] << std::endl;

    double area = vtkTriangle::TriangleArea(p0, p1, p2);

    std::cout << "area of triangle " << i << ": " << area << std::endl;
  }
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TriangleArea)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
)

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

Download and Build TriangleArea

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

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

./TriangleArea

WINDOWS USERS

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