# OBJImporter

VTKExamples/Cxx/IO/OBJImporter

### Code

OBJImporter.cxx

    #include <vtkSmartPointer.h>

    #include <vtkOBJImporter.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkPolyDataNormals.h>
    #include <vtkCleanPolyData.h>
    #include <vtkPolyData.h>
    #include <vtkTexture.h>
    #include <vtkNamedColors.h>

    int main (int argc, char *argv[])
    {
      if (argc < 4)
      {
        std::cout << "Usage: " << argv[0]
                  << " objfile mtlfile texturepath"
                  << std::endl;
        return EXIT_FAILURE;
      }
      vtkSmartPointer<vtkOBJImporter> importer =
        vtkSmartPointer<vtkOBJImporter>::New();
      importer->SetFileName(argv[1]);
      importer->SetFileNameMTL(argv[2]);
      importer->SetTexturePath(argv[3]);

      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();

      renderer->SetBackground2 (colors->GetColor3d("Silver").GetData());
      renderer->SetBackground (colors->GetColor3d("Gold").GetData());
      renderer->GradientBackgroundOn();
      renWin->AddRenderer(renderer);
      renderer->UseHiddenLineRemovalOn();
      renWin->AddRenderer(renderer);
      renWin->SetSize(640, 480);

      iren->SetRenderWindow(renWin);
      importer->SetRenderWindow(renWin);
      importer->Update();

      vtkSmartPointer<vtkActorCollection> actors =
        vtkSmartPointer<vtkActorCollection>::New();
      actors = renderer->GetActors();
      actors->InitTraversal();
      std::cout << "There are " << actors->GetNumberOfItems() << " actors" << std::endl;

      for (vtkIdType a = 0; a < actors->GetNumberOfItems(); ++a)
      {
        std::cout << importer->GetOutputDescription(a) << std::endl;

        vtkActor * actor = actors->GetNextActor();

        // OBJImporter turns texture interpolation off
        if (actor->GetTexture())
        {
          std::cout << "Hastexture\n";
          actor->GetTexture()->InterpolateOn();
        }

        vtkPolyData *pd = dynamic_cast<vtkPolyData*>(actor->GetMapper()->GetInput());
        vtkSmartPointer<vtkCleanPolyData> clean =
          vtkSmartPointer<vtkCleanPolyData>::New();
        clean->SetInputData(pd);
        clean->Update();

        vtkSmartPointer<vtkPolyDataNormals> normals =
          vtkSmartPointer<vtkPolyDataNormals>::New();
        normals->SetInputData(pd);
        normals->SplittingOff();
        normals->ConsistencyOn();
        normals->Update();
        vtkPolyDataMapper *mapper = dynamic_cast<vtkPolyDataMapper*>(actor->GetMapper());
        mapper->SetInputData(normals->GetOutput());
        mapper->SetInputData(pd);
      }
      renWin->Render();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(OBJImporter)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersCore
      vtkIOImport
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping OBJImporter: ${VTK_NOT_FOUND_MESSAGE}")
      return ()
    endif()
    message (STATUS "VTK_VERSION: ${VTK_VERSION}")
    if (VTK_VERSION VERSION_LESS "8.90.0")
      # old system
      include(${VTK_USE_FILE})
      add_executable(OBJImporter MACOSX_BUNDLE OBJImporter.cxx )
      target_link_libraries(OBJImporter PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(OBJImporter MACOSX_BUNDLE OBJImporter.cxx )
      target_link_libraries(OBJImporter PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS OBJImporter
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build OBJImporter

Click here to download OBJImporter and its CMakeLists.txt file. Once the tarball OBJImporter.tar has been downloaded and extracted,

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

    ./OBJImporter

WINDOWS USERS

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