VTK/ExamplesBoneyard/Cxx/PolyData/AddLineToPolydata

From KitwarePublic
Jump to navigationJump to search

This example shows how to add a line to a polydata.

AddLineToPolydata.cxx

#include <vtkSmartPointer.h>
#include <vtkCellArray.h>
#include <vtkPoints.h>
#include <vtkLine.h>
#include <vtkPolyData.h>
#include <vtkXMLPolyDataWriter.h>

int main(int, char *[])
{
  // Create two points
  double p0[3];
  p0[0] = 1;
  p0[1] = 0;
  p0[2] = 0;
		
  double p1[3];
  p1[0] = 0;
  p1[1] = 1;
  p1[2] = 0;
		
  // Add the two poitns to a vtkPoints object
  vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::New();
  pts->InsertNextPoint(p0);
  pts->InsertNextPoint(p1);
	
  // Create a line between the two points
  vtkSmartPointer<vtkLine> line = 
    vtkSmartPointer<vtkLine>::New();
  line->GetPointIds()->SetId(0,0); //the SetId(A,B) call is the following: 
  //A = the id of the point relative to the line - this can only be 0 or 1. 
  //B = the index into the vtkPoints object of the point that you would like to set the Ath point to.
  line->GetPointIds()->SetId(1,1);
	
  // Create a cell array to store the line in
  vtkSmartPointer<vtkCellArray> lines = 
    vtkSmartPointer<vtkCellArray>::New();
  lines->InsertNextCell(line);

  // Create a polydata to store everything in
  vtkSmartPointer<vtkPolyData> pdata = 
    vtkSmartPointer<vtkPolyData>::New();

  // Add the points to the dataset
  pdata->SetPoints(pts);

  // Add the lines to the dataset
  pdata->SetLines(lines);

  // Write the file
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetInput(pdata);
  writer->SetFileName("Line.vtp");
  writer->Write();

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(AddLineToPolydata)

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

add_executable(AddLineToPolydata MACOSX_BUNDLE AddLineToPolydata.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(AddLineToPolydata ${VTK_LIBRARIES})
else()
  target_link_libraries(AddLineToPolydata vtkHybrid vtkWidgets)
endif()

Download and Build AddLineToPolydata

Click here to download AddLineToPolydata. and its CMakeLists.txt file.

Once the tarball AddLineToPolydata.tar has been downloaded and extracted,

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

./AddLineToPolydata

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

Check that this is sufficiently different from the Line geometric object example.