ReadAllUnstructuredGridTypes
VTKEx/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 simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
ReadAllUnstructuredGridTypes.cxx
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkSphereSource.h>
#include <vtkAppendFilter.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <string>
#include <algorithm>
#include <array>
namespace
{
vtkSmartPointer<vtkUnstructuredGrid> ReadUnstructuredGrid(std::string const& fileName);
}
int main (int argc, char *argv[])
{
// Vis Pipeline
auto colors = vtkSmartPointer<vtkNamedColors>::New();
auto renderer = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640, 480);
renderWindow->AddRenderer(renderer);
auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
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
auto mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(unstructuredGrid);
mapper->ScalarVisibilityOff();
auto backProp = vtkSmartPointer<vtkProperty>::New();
backProp->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
backProp->SetSpecular(.6);
backProp->SetSpecularPower(30);
auto actor = vtkSmartPointer<vtkActor>::New();
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->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")
{
auto reader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
reader->SetFileName (fileName.c_str());
reader->Update();
unstructuredGrid = reader->GetOutput();
}
else if (extension == ".vtk")
{
auto reader = vtkSmartPointer<vtkUnstructuredGridReader>::New();
reader->SetFileName (fileName.c_str());
reader->Update();
unstructuredGrid = reader->GetOutput();
}
else
{
auto source = vtkSmartPointer<vtkSphereSource>::New();
source->Update();
auto appendFilter =
vtkSmartPointer<vtkAppendFilter>::New();
appendFilter->AddInputData(source->GetOutput());
appendFilter->Update();
unstructuredGrid = appendFilter->GetOutput();
}
return unstructuredGrid;
}
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ReadAllUnstructuredGridTypes)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersCore
vtkvtkFiltersSources
vtkvtkIOLegacy
vtkvtkIOXML
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 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.