Skip to content

IndividualVRML

vtk-examples/Cxx/IO/IndividualVRML


Description

This example shows how to obtain each object of a scene and get it's initial transformation. The selected actor is represented in wireframe. To run this example:

IndividualVRML filename actorname

The .wrl file must contain a Shape with a DEF name.

Question

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

Code

IndividualVRML.cxx

#include <vtkAxesActor.h>
#include <vtkCamera.h>
#include <vtkDataSet.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTransform.h>
#include <vtkVRMLImporter.h>

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

  if (argc != 3)
  {
    std::cout << "Required arguments: Filename Actorname e.g. teapot.wrl teapot"
              << std::endl;
    return EXIT_FAILURE;
  }

  std::string filename = argv[1];
  std::cout << "Showing " << argv[2] << " from " << filename << std::endl;

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("IndividualVRML");

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

  // VRML Import
  vtkNew<vtkVRMLImporter> importer;
  importer->SetFileName(filename.c_str());
  importer->Read();
  importer->SetRenderWindow(renderWindow);
  importer->Update();

  // ----------------------------------------------------------
  vtkObject* defActor = importer->GetVRMLDEFObject(argv[2]);
  if (defActor == NULL)
  {
    std::cout << "Cannot locate actor " << argv[2] << " in " << filename
              << std::endl;
    importer->Print(std::cout);
    return EXIT_FAILURE;
  }

  vtkActor* actor = static_cast<vtkActor*>(defActor);
  double color[3] = {0.89, 0.81, 0.34};
  actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
  actor->GetProperty()->SetRepresentationToWireframe();

  vtkNew<vtkTransform> transform;

  transform->Translate(actor->GetCenter()[0], actor->GetCenter()[1],
                       actor->GetCenter()[2]);
  // axes
  vtkNew<vtkAxesActor> axes;

  double l[3];
  l[0] = (actor->GetBounds()[1] - actor->GetBounds()[0]) * 1.5;
  l[1] = (actor->GetBounds()[3] - actor->GetBounds()[2]) * 1.5;
  l[2] = (actor->GetBounds()[5] - actor->GetBounds()[4]) * 1.5;

  axes->SetTotalLength(l);
  axes->SetUserTransform(transform);
  renderer->AddActor(axes);
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
  renderWindow->Render();

  renderer->GetActiveCamera()->SetPosition(-14.8296, 18.1304, 12.3352);
  renderer->GetActiveCamera()->SetFocalPoint(2.09905, 0.0832915, 2.47961);
  renderer->GetActiveCamera()->SetViewUp(0.262918, -0.260671, 0.928937);
  renderer->GetActiveCamera()->SetDistance(26.6348);
  renderer->ResetCameraClippingRange();

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(IndividualVRML)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkCommonTransforms
  vtkIOImport
  vtkInteractionStyle
  vtkRenderingAnnotation
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

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

Download and Build IndividualVRML

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

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

./IndividualVRML

WINDOWS USERS

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