VTK/Examples/IO/ReadOBJ

From KitwarePublic

< VTK | Examples(Redirected from Read an OBJ File)
Jump to: navigation, search

This example demonstrates how to read an OBJ file.

(Need an example obj file for testing).

#include <vtkSmartPointer.h>
#include <vtkOBJReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCell.h>
#include <vtkCellArray.h>
#include <vtkIdList.h>
#include <vtkUnsignedCharArray.h>
#include <vtkPointData.h>
 
int main(int argc, char *argv[])
{
  //parse command line arguments
  if(argc != 2)
    {
    vtkstd::cout << "Required arguments: Filename" << vtkstd::endl;
    exit(-1);
    }
 
  vtkstd::string filename = argv[1];
  vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
  reader->SetFileName(filename.c_str());
  reader->Update();
 
  vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
  vtkIdType numberOfPoints = polydata->GetNumberOfPoints();
 
  double point[3];
  for(int i = 0; i < numberOfPoints; i++)
    {
    polydata->GetPoint(i, point);
    vtkstd::cout << "Point " << i << ": " << point[0] << " " << point[1] << " " << point[2] << vtkstd::endl;
    }
 
  //create triangle vertex lists
  vtkIdType numberOfPolys = polydata->GetNumberOfPolys();
  if(numberOfPolys > 0)
    {
    vtkSmartPointer<vtkCellArray> triangleCells = polydata->GetPolys();
    vtkIdType npts;
    vtkIdType *pts;
 
    while(triangleCells->GetNextCell(npts, pts))
      {
      vtkstd::cout << "Indices of triangle points: " << pts[0] << " " << pts[1] << " " << pts[2] << vtkstd::endl;
      }	
    }
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(ReadOBJFile)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(ReadOBJFile ReadOBJFile.cxx)
TARGET_LINK_LIBRARIES(ReadOBJFile vtkHybrid)
Personal tools