Skip to content

RibbonFilter

Repository source: RibbonFilter


Description

This example demonstrates how to draw a flat surface (a ribbon) along a line.

Other languages

See (Java)

Question

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

Code

RibbonFilter.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkRibbonFilter.h>

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

  // Spiral parameters.
  unsigned int nV = 256; // No. of vertices
  double rS = 2;         // Spiral radius
  unsigned int nCyc = 3; // No. of helical cycles
  double h = 10;         // Height

  unsigned int i;

  // Create points and cells for a helix.
  vtkNew<vtkPoints> points;
  for (i = 0; i < nV; i++)
  {
    // Helical coordinates.
    auto vX = rS * cos(2 * vtkMath::Pi() * nCyc * i / (nV - 1));
    auto vY = rS * sin(2 * vtkMath::Pi() * nCyc * i / (nV - 1));
    auto vZ = h * i / nV;
    points->InsertPoint(i, vX, vY, vZ);
  }

  vtkNew<vtkCellArray> lines;
  lines->InsertNextCell(nV);
  for (i = 0; i < nV; i++)
  {
    lines->InsertCellPoint(i);
  }

  vtkNew<vtkPolyData> polyData;
  polyData->SetPoints(points);
  polyData->SetLines(lines);

  // Create a mapper and actor.
  vtkNew<vtkPolyDataMapper> lineMapper;
  lineMapper->SetInputData(polyData);

  vtkNew<vtkActor> lineActor;
  lineActor->SetMapper(lineMapper);
  lineActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
  lineActor->GetProperty()->SetLineWidth(3);

  // Create a ribbon around the line.
  vtkNew<vtkRibbonFilter> ribbonFilter;
  ribbonFilter->SetInputData(polyData);
  ribbonFilter->SetWidth(.4);

  // Create a mapper and actor.
  vtkNew<vtkPolyDataMapper> ribbonMapper;
  ribbonMapper->SetInputConnection(ribbonFilter->GetOutputPort());

  vtkNew<vtkActor> ribbonActor;
  ribbonActor->SetMapper(ribbonMapper);
  ribbonActor->GetProperty()->SetColor(
      colors->GetColor3d("AliceBlue").GetData());

  // Create a renderer, render window, and interactor.
  vtkNew<vtkRenderer> renderer;

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->SetWindowName("RibbonFilter");

  // Add the actor to the scene.
  renderer->AddActor(ribbonActor);
  renderer->AddActor(lineActor);

  // Render and interact.
  renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

  // Generate an interesting view.
  renderer->GetActiveCamera()->Azimuth(40);
  renderer->GetActiveCamera()->Elevation(30);
  renderer->ResetCamera();
  renderer->ResetCameraClippingRange();

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(RibbonFilter)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersModeling
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "RibbonFilter: 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(RibbonFilter MACOSX_BUNDLE RibbonFilter.cxx )
  target_link_libraries(RibbonFilter PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS RibbonFilter
  MODULES ${VTK_LIBRARIES}
)

Download and Build RibbonFilter

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

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

./RibbonFilter

WINDOWS USERS

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