VTK/Examples/Cxx/Boneyard/GeometricObjects/WriteFile/Triangle

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search

This example creates a "self contained" triangle. This means the geometry is stored such that the triangle itself contains the geometry and can therefore be passed to a function and operated on without any other arrays present. Unfortunately, there is then no way to add this triangle to a polydata object, as the triangles for a polydata must simply be indexed to points in the polydatas point array.

Triangle.cxx

#include <vtkSmartPointer.h>
#include <vtkTriangle.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>

void PrintTriangle(vtkTriangle* Triangle);

int main(int, char *[])
{
  
  vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();
  triangle->GetPoints()->SetPoint(0, 1.0, 0.0, 0.0);
  triangle->GetPoints()->SetPoint(1, 0.0, 0.0, 0.0);
  triangle->GetPoints()->SetPoint(2, 0.0, 1.0, 0.0);
  
  PrintTriangle(triangle);
  
  return EXIT_SUCCESS;
}

void PrintTriangle(vtkTriangle* triangle)
{
  double p[3];
  for(unsigned int i = 0; i < 3; i++)
    {
    triangle->GetPoints()->GetPoint(i, p);
    cout << p[0] << " " << p[1] << " " << p[2] <<endl;
    }
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(Triangle)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(Triangle Triangle.cxx)

TARGET_LINK_LIBRARIES(Triangle vtkHybrid)