Skip to content

ReadAllUnstructuredGridTypes

vtk-examples/Cxx/IO/ReadAllUnstructuredGridTypes


Description

The example reads a vtkUnstructuredGrid with vtkXMLUnstructuredGridReader if the extension is .vtu or the legacy vtkUnstructuredGridReader if the extension is .vtk. If a file is not present, the example creates a vtkUnstructuredGrid by passing a vtkSphere through vtkAppendFilter.

For a description of the VTK File Formats see VTKFileFormats.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ReadAllUnstructuredGridTypes.cxx

#include <vtkAppendFilter.h>
#include <vtkSphereSource.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkXMLUnstructuredGridReader.h>

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>

#include <algorithm>
#include <array>
#include <string>

namespace {
vtkSmartPointer<vtkUnstructuredGrid>
ReadUnstructuredGrid(std::string const& fileName);
}

int main(int argc, char* argv[])
{
  // Vis Pipeline
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> renderer;

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(640, 480);
  renderWindow->AddRenderer(renderer);

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
  renderer->UseHiddenLineRemovalOn();

  std::cout << "Loading: " << argv[1] << std::endl;
  auto unstructuredGrid = ReadUnstructuredGrid(std::string(argv[1]));

  // Visualize
  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputData(unstructuredGrid);
  mapper->ScalarVisibilityOff();

  vtkNew<vtkProperty> backProp;
  backProp->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
  backProp->SetSpecular(.6);
  backProp->SetSpecularPower(30);

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->SetBackfaceProperty(backProp);
  actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Tomato").GetData());
  actor->GetProperty()->SetSpecular(.3);
  actor->GetProperty()->SetSpecularPower(30);
  actor->GetProperty()->EdgeVisibilityOn();
  renderer->AddActor(actor);
  renderer->GetActiveCamera()->Azimuth(45);
  renderer->GetActiveCamera()->Elevation(45);
  renderer->ResetCamera();
  renderWindow->SetWindowName("ReadAllUnstructuredGridTypes");
  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

namespace {
vtkSmartPointer<vtkUnstructuredGrid>
ReadUnstructuredGrid(std::string const& fileName)
{
  vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid;
  std::string extension = "";
  if (fileName.find_last_of(".") != std::string::npos)
  {
    extension = fileName.substr(fileName.find_last_of("."));
  }

  // Drop the case of the extension
  std::transform(extension.begin(), extension.end(), extension.begin(),
                 ::tolower);

  if (extension == ".vtu")
  {
    vtkNew<vtkXMLUnstructuredGridReader> reader;
    reader->SetFileName(fileName.c_str());
    reader->Update();
    unstructuredGrid = reader->GetOutput();
  }
  else if (extension == ".vtk")
  {
    vtkNew<vtkUnstructuredGridReader> reader;
    reader->SetFileName(fileName.c_str());
    reader->Update();
    unstructuredGrid = reader->GetOutput();
  }
  else
  {
    vtkNew<vtkSphereSource> source;
    source->Update();
    vtkNew<vtkAppendFilter> appendFilter;
    appendFilter->AddInputData(source->GetOutput());
    appendFilter->Update();
    unstructuredGrid = appendFilter->GetOutput();
  }

  return unstructuredGrid;
}

} // namespace

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ReadAllUnstructuredGridTypes)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersCore
  vtkFiltersSources
  vtkIOLegacy
  vtkIOXML
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ReadAllUnstructuredGridTypes: ${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(ReadAllUnstructuredGridTypes MACOSX_BUNDLE ReadAllUnstructuredGridTypes.cxx )
  target_link_libraries(ReadAllUnstructuredGridTypes PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ReadAllUnstructuredGridTypes MACOSX_BUNDLE ReadAllUnstructuredGridTypes.cxx )
  target_link_libraries(ReadAllUnstructuredGridTypes PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ReadAllUnstructuredGridTypes
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ReadAllUnstructuredGridTypes

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

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

./ReadAllUnstructuredGridTypes

WINDOWS USERS

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