VTK/Tutorials/TriangleGeometryVertices: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 3: Line 3:
==TriangleVertices.cxx==
==TriangleVertices.cxx==
<source lang="cpp">
<source lang="cpp">
#include "vtkCellArray.h"
#include <vtkCellArray.h>
#include "vtkSmartPointer.h"
#include <vtkSmartPointer.h>
#include "vtkPoints.h"
#include <vtkPoints.h>
#include "vtkXMLPolyDataWriter.h"
#include <vtkXMLPolyDataWriter.h>
#include "vtkPolyData.h"
#include <vtkPolyData.h>


int main()
int main(int argc, char *argv[])
{
{
   //Setup point coordinates
   //Setup point coordinates
Line 19: Line 19:
   //cells to the polydata, and the cells only contain 1 element, so they are, by definition,
   //cells to the polydata, and the cells only contain 1 element, so they are, by definition,
   //0-D topology (vertices).
   //0-D topology (vertices).
   vtkSmartPointer<vtkPoints> Points = vtkSmartPointer<vtkPoints>::New();
   vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
   vtkSmartPointer<vtkCellArray> Vertices = vtkSmartPointer<vtkCellArray>::New();
   vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();


   for ( unsigned int i = 0; i < 3; ++i )
   for ( unsigned int i = 0; i < 3; ++i )
  {
    {
     //Declare a variable to store the index of the point that gets added. This behaves just like an unsigned int.
     //Declare a variable to store the index of the point that gets added. This behaves just like an unsigned int.
     vtkIdType pid[1];
     vtkIdType pid[1];
      
      
     //Add a point to the polydata and save its index, which we will use to create the vertex on that point.
     //Add a point to the polydata and save its index, which we will use to create the vertex on that point.
     pid[0] = Points->InsertNextPoint ( X[i], Y[i], Z[i] );
     pid[0] = points->InsertNextPoint ( X[i], Y[i], Z[i] );
      
      
     //create a vertex cell on the point that was just added.
     //create a vertex cell on the point that was just added.
     Vertices->InsertNextCell ( 1,pid );
     vertices->InsertNextCell ( 1,pid );
  }
    }


   //create a polydata object
   //create a polydata object
Line 38: Line 38:


   //set the points and vertices we created as the geometry and topology of the polydata
   //set the points and vertices we created as the geometry and topology of the polydata
   polydata->SetPoints ( Points );
   polydata->SetPoints ( points );
   polydata->SetVerts ( Vertices );
   polydata->SetVerts ( vertices );


   //write the polydata to a file
   //write the polydata to a file
   vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
   vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
   writer->SetFileName ( "TriangleVerts.vtp" );
   writer->SetFileName ( "TriangleVerts.vtp" );
#if VTK_MAJOR_VERSION <= 5
   writer->SetInput ( polydata );
   writer->SetInput ( polydata );
#else
  writer->SetInputData ( polydata );
#endif
   writer->Write();
   writer->Write();


 
  return EXIT_SUCCESS;
}
}


Line 53: Line 57:


==CMakeLists.txt==
==CMakeLists.txt==
<source lang="text">
<source lang="cmake">
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 2.6)


PROJECT(TriangleVertices)
project(TriangleVertices)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})


ADD_EXECUTABLE(TriangleVertices TriangleVertices.cxx)
find_package(VTK REQUIRED)
TARGET_LINK_LIBRARIES(TriangleVertices vtkHybrid)
include(${VTK_USE_FILE})


add_executable(TriangleVertices TriangleVertices.cxx)
if(VTK_LIBRARIES)
  target_link_libraries(TriangleVertices ${VTK_LIBRARIES})
else()
  target_link_libraries(TriangleVertices vtkHybrid)
endif()
</source>
</source>

Latest revision as of 19:18, 25 April 2014

This example writes the coordinates of the corners of a triangle to a vtp file. There is now geometry (points) as well as topology (vertices). If you open this file in Paraview, you will now see the points immediately.

TriangleVertices.cxx

<source lang="cpp">

  1. include <vtkCellArray.h>
  2. include <vtkSmartPointer.h>
  3. include <vtkPoints.h>
  4. include <vtkXMLPolyDataWriter.h>
  5. include <vtkPolyData.h>

int main(int argc, char *argv[]) {

 //Setup point coordinates
 double X[3] = {1.0, 0.0, 0.0};
 double Y[3] = {0.0, 0.0, 1.0};
 double Z[3] = {0.0, 0.0, 0.0};
 //Create points and add a vertex at each point. Really what you are doing is adding
 //cells to the polydata, and the cells only contain 1 element, so they are, by definition,
 //0-D topology (vertices).
 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
 vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
 for ( unsigned int i = 0; i < 3; ++i )
   {
   //Declare a variable to store the index of the point that gets added. This behaves just like an unsigned int.
   vtkIdType pid[1];
   
   //Add a point to the polydata and save its index, which we will use to create the vertex on that point.
   pid[0] = points->InsertNextPoint ( X[i], Y[i], Z[i] );
   
   //create a vertex cell on the point that was just added.
   vertices->InsertNextCell ( 1,pid );
   }
 //create a polydata object
 vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
 //set the points and vertices we created as the geometry and topology of the polydata
 polydata->SetPoints ( points );
 polydata->SetVerts ( vertices );
 //write the polydata to a file
 vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
 writer->SetFileName ( "TriangleVerts.vtp" );
  1. if VTK_MAJOR_VERSION <= 5
 writer->SetInput ( polydata );
  1. else
 writer->SetInputData ( polydata );
  1. endif
 writer->Write();
 return EXIT_SUCCESS;

}

</source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

project(TriangleVertices)

find_package(VTK REQUIRED) include(${VTK_USE_FILE})

add_executable(TriangleVertices TriangleVertices.cxx) if(VTK_LIBRARIES)

 target_link_libraries(TriangleVertices ${VTK_LIBRARIES})

else()

 target_link_libraries(TriangleVertices vtkHybrid)

endif() </source>