原文转自:
http://www.paraview.org/Wiki/VTK/ExamplesBoneyard/Cxx/PolyData/AddLineToPolydata
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; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) PROJECT(AddLineToPolydata) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) add_executable(AddLineToPolydata MACOSX_BUNDLE AddLineToPolydata) if(VTK_LIBRARIES) target_link_libraries(AddLineToPolydata ${VTK_LIBRARIES}) else() target_link_libraries(AddLineToPolydata vtkHybrid) endif()