Skip to content

PlateVibration

vtk-examples/Cxx/VisualizationAlgorithms/PlateVibration


Description

The motion of a vibrating beam is shown. The original undeformed outline is shown as a wireframe.

Other languages

See (Python)

Question

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

Code

PlateVibration.cxx

// Translated from vib.tcl

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVectorDot.h>
#include <vtkWarpVector.h>

#include <array>
#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  auto Scale = [](std::vector<double>& v, double scale) {
    std::transform(std::begin(v), std::end(v), std::begin(v),
                   [=](double const& n) { return n / scale; });
    return;
  };

  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " filename" << std::endl;
    std::cout << "where: filename is the file plate.vtk." << std::endl;
    std::cout
        << "Produces figure 6-14(a) Beam displacement from the VTK Textbook."
        << std::endl;
    return EXIT_FAILURE;
  }

  std::string fileName = argv[1];

  vtkNew<vtkNamedColors> colors;

  // Set the background color and plate color.
  std::array<unsigned char, 4> bar{{255, 160, 140}};
  colors->SetColor("PlateColor", bar.data());
  std::array<unsigned char, 4> bkg{{65, 99, 149}};
  colors->SetColor("BkgColor", bkg.data());

  // Read a vtk file.
  //
  vtkNew<vtkPolyDataReader> plate;
  plate->SetFileName(fileName.c_str());
  plate->Update();
  double bounds[6];
  plate->GetOutput()->GetBounds(bounds);
  plate->SetVectorsName("mode2");

  vtkNew<vtkPolyDataNormals> normals;
  normals->SetInputConnection(plate->GetOutputPort());
  vtkNew<vtkWarpVector> warp;
  warp->SetInputConnection(normals->GetOutputPort());
  warp->SetScaleFactor(0.5);
  vtkNew<vtkVectorDot> color;
  color->SetInputConnection(warp->GetOutputPort());
  vtkNew<vtkDataSetMapper> plateMapper;
  plateMapper->SetInputConnection(warp->GetOutputPort());
  vtkNew<vtkActor> plateActor;
  plateActor->SetMapper(plateMapper);
  plateActor->GetProperty()->SetColor(
      colors->GetColor3d("PlateColor").GetData());
  plateActor->RotateX(-90);

  // Create the outline.
  //
  vtkNew<vtkOutlineFilter> outline;
  outline->SetInputConnection(plate->GetOutputPort());
  vtkNew<vtkPolyDataMapper> outlineMapper;
  outlineMapper->SetInputConnection(outline->GetOutputPort());
  vtkNew<vtkActor> outlineActor;
  outlineActor->SetMapper(outlineMapper);
  outlineActor->RotateX(-90);
  outlineActor->GetProperty()->SetColor(colors->GetColor3d("White").GetData());

  // Create the RenderWindow, Renderer and both Actors.
  //
  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren);

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  // Add the actors to the renderer, set the background and size.
  //
  ren->AddActor(plateActor);
  ren->AddActor(outlineActor);
  renWin->SetSize(500, 500);
  renWin->SetWindowName("PlateVibration");

  // Render the image.
  renWin->Render();
  ren->SetBackground(colors->GetColor3d("BkgColor").GetData());
  // This closely matches the original illustration.
  ren->GetActiveCamera()->SetPosition(-3.7, 13, 15.5);
  ren->ResetCameraClippingRange();

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PlateVibration)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersGeneral
  FiltersModeling
  IOLegacy
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PlateVibration: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(PlateVibration MACOSX_BUNDLE PlateVibration.cxx )
  target_link_libraries(PlateVibration PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS PlateVibration
  MODULES ${VTK_LIBRARIES}
)

Download and Build PlateVibration

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

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

./PlateVibration

WINDOWS USERS

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