VTK/Tutorials/TriangleGeometryOnly: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
This example writes the coordinates of the corners of a square to a vtp file. There is geometry (points), but there is no topology (vertices), so if you open this file in Paraview, you will not see anything. You can "glyph" the points to see them, but generally some type of topology exists. We will see topology in the next example.
<source lang="cpp">
<source lang="cpp">
void TestWritePointSquare(const std::string &OutputFile)
#include <iostream>
#include <vector>
 
#include "vtkCellArray.h"
#include "vtkPoints.h"
#include "vtkXMLPolyDataWriter.h"
#include "vtkPolyData.h"
 
struct Point
{
double x,y,z;
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
};
 
int main()
{
{
//setup points
//setup points
std::vector<Point> Points;
std::vector<Point> Coords;
Points.push_back(Point(-1, 1, 0));
Coords.push_back(Point(-1.0, 1.0, 0.0));
Points.push_back(Point(1, 1, 0));
Coords.push_back(Point(1.0, 1.0, 0.0));
Points.push_back(Point(1, -1, 0));
Coords.push_back(Point(1.0, -1.0, 0.0));
Points.push_back(Point(-1, -1, 0));
Coords.push_back(Point(-1.0, -1.0, 0.0));
vtkPoints* Points = vtkPoints::New();
 
for (unsigned int i = 0; i < Coords.size(); ++i )
vtkSmartPointer<vtkPoints> points3D = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkCellArray> Vertices = vtkSmartPointer<vtkCellArray>::New();
 
for ( unsigned int i = 0; i < Points.size(); ++i )
{
{
vtkIdType pid[1];
Point P = Coords[i];
Point P = Points[i];
Points->InsertNextPoint(P.x, P.y, P.z);
pid[0] = points3D->InsertNextPoint(P.x, P.y, P.z);
Vertices->InsertNextCell(1,pid);
}
}
vtkPolyData* polydata = vtkPolyData::New();


vtkSmartPointer<vtkPolyData> polydata = vtkPolyData::New();
polydata->SetPoints(Points);
 
polydata->SetPoints(points3D);
polydata->SetVerts(Vertices);
vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
writer->SetFileName(OutputFile.c_str());
writer->SetFileName("Square.vtp");
writer->SetInput(polydata);
writer->SetInput(polydata);
writer->Write();
writer->Write();
}
 
</source>
}</source>

Revision as of 14:46, 18 June 2009

This example writes the coordinates of the corners of a square to a vtp file. There is geometry (points), but there is no topology (vertices), so if you open this file in Paraview, you will not see anything. You can "glyph" the points to see them, but generally some type of topology exists. We will see topology in the next example.

<source lang="cpp">

  1. include <iostream>
  2. include <vector>
  1. include "vtkCellArray.h"
  2. include "vtkPoints.h"
  3. include "vtkXMLPolyDataWriter.h"
  4. include "vtkPolyData.h"

struct Point { double x,y,z; Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {} };

int main() { //setup points std::vector<Point> Coords; Coords.push_back(Point(-1.0, 1.0, 0.0)); Coords.push_back(Point(1.0, 1.0, 0.0)); Coords.push_back(Point(1.0, -1.0, 0.0)); Coords.push_back(Point(-1.0, -1.0, 0.0));

vtkPoints* Points = vtkPoints::New();

for (unsigned int i = 0; i < Coords.size(); ++i ) { Point P = Coords[i]; Points->InsertNextPoint(P.x, P.y, P.z); }

vtkPolyData* polydata = vtkPolyData::New();

polydata->SetPoints(Points);

vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New(); writer->SetFileName("Square.vtp"); writer->SetInput(polydata); writer->Write();

}</source>