VTK/Tutorials/TriangleGeometryVertices: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
m (Write a file of triangle corners (+vertices) moved to Triangle - Geometry + Vertices: Changed structure of polydata examples)
No edit summary
Line 1: Line 1:
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.
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">
<source lang="cpp">
#include <iostream>
#include <vector>
#include "vtkCellArray.h"
#include "vtkCellArray.h"
#include "vtkSmartPointer.h"
#include "vtkPoints.h"
#include "vtkPoints.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkPolyData.h"
#include "vtkPolyData.h"


struct Point
int main()
{
{
double x,y,z;
  //Setup point coordinates
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
  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 );
  }


int main()
  //create a polydata object
{
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
//setup points
 
std::vector<Point> Coords;
  //set the points and vertices we created as the geometry and topology of the polydata
Coords.push_back(Point(-1.0, 1.0, 0.0));
  polydata->SetPoints ( Points );
Coords.push_back(Point(1.0, 1.0, 0.0));
  polydata->SetVerts ( Vertices );
Coords.push_back(Point(1.0, -1.0, 0.0));
vtkPoints* Points = vtkPoints::New();
vtkCellArray* Vertices = vtkCellArray::New();


for ( unsigned int i = 0; i < Coords.size(); ++i )
  //write the polydata to a file
{
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
vtkIdType pid[1];
  writer->SetFileName ( "TriangleVerts.vtp" );
Point P = Coords[i];
  writer->SetInput ( polydata );
pid[0] = Points->InsertNextPoint(P.x, P.y, P.z);
  writer->Write();
Vertices->InsertNextCell(1,pid);
}
vtkPolyData* polydata = vtkPolyData::New();


polydata->SetPoints(Points);
polydata->SetVerts(Vertices);
vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
writer->SetFileName("TriangleVerts.vtp");
writer->SetInput(polydata);
writer->Write();


}
}
</source>
==CMakeLists.txt==
<source lang="text">
cmake_minimum_required(VERSION 2.6)
PROJECT(TriangleVertices)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(TriangleVertices TriangleVertices.cxx)
TARGET_LINK_LIBRARIES(TriangleVertices vtkHybrid)
</source>
</source>

Revision as of 18:27, 22 October 2009

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() {

 //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" );
 writer->SetInput ( polydata );
 writer->Write();


}

</source>

CMakeLists.txt

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

PROJECT(TriangleVertices)

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

ADD_EXECUTABLE(TriangleVertices TriangleVertices.cxx) TARGET_LINK_LIBRARIES(TriangleVertices vtkHybrid)

</source>