StripFran

VTKExamples/Cxx/Rendering/StripFran


Description

Triangle strip examples. Left, structured triangle mesh consisting of 134 strips each of 390 triangles. Right, Unstructured triangle mesh consisting of 2227 strips of average length 3.94, longest strip 101 triangles. Images are generated by displaying every other triangle strip.

Other Languages

See (Python)

Question

If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

StripFran.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDecimatePro.h>
#include <vtkMaskPolyData.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStripper.h>

int main (int argc, char *argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: " << argv[0] << " fran_cut.vtk" << std::endl;
    return EXIT_FAILURE;
  }
// Create the RenderWindow, Renderer and both Actors
//
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  vtkSmartPointer<vtkRenderer> renderer1 =
    vtkSmartPointer<vtkRenderer>::New();
  renderer1->SetViewport( 0., 0., 0.5, 1. );

  vtkSmartPointer<vtkRenderer> renderer2 =
    vtkSmartPointer<vtkRenderer>::New();
  renderer2->SetViewport( 0.5, 0., 1., 1. );

  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer1);
  renderWindow->AddRenderer(renderer2);

  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);

// create a cyberware source
//
  vtkSmartPointer<vtkPolyDataReader> cyber =
    vtkSmartPointer<vtkPolyDataReader>::New();
  cyber->SetFileName(argv[1]);

  vtkSmartPointer<vtkDecimatePro> deci =
    vtkSmartPointer<vtkDecimatePro>::New();
  deci->SetInputConnection(cyber->GetOutputPort());
  deci->SetTargetReduction(0.7);
  deci->PreserveTopologyOn();

  vtkSmartPointer<vtkPolyDataNormals> normals =
    vtkSmartPointer<vtkPolyDataNormals>::New();
  normals->SetInputConnection(deci->GetOutputPort());

  vtkSmartPointer<vtkMaskPolyData> mask =
    vtkSmartPointer<vtkMaskPolyData>::New();
  mask->SetInputConnection(deci->GetOutputPort());
  mask->SetOnRatio(2);

  vtkSmartPointer<vtkPolyDataMapper> cyberMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  cyberMapper->SetInputConnection(mask->GetOutputPort());

  vtkSmartPointer<vtkActor> cyberActor =
    vtkSmartPointer<vtkActor>::New();
  cyberActor->SetMapper(cyberMapper);
  cyberActor->GetProperty()->SetColor(colors->GetColor3d("Flesh").GetData());

  vtkSmartPointer<vtkStripper> stripper =
    vtkSmartPointer<vtkStripper>::New();
  stripper->SetInputConnection(cyber->GetOutputPort());

  vtkSmartPointer<vtkMaskPolyData> stripperMask =
    vtkSmartPointer<vtkMaskPolyData>::New();
  stripperMask->SetInputConnection(stripper->GetOutputPort());
  stripperMask->SetOnRatio(2);

  vtkSmartPointer<vtkPolyDataMapper> stripperMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  stripperMapper->SetInputConnection(stripperMask->GetOutputPort());

  vtkSmartPointer<vtkActor> stripperActor =
    vtkSmartPointer<vtkActor>::New();
  stripperActor->SetMapper(stripperMapper);
  stripperActor->GetProperty()->SetColor(colors->GetColor3d("Flesh").GetData());

// Add the actors to the renderer, set the background and size
//
  renderer1->AddActor(stripperActor);
  renderer2->AddActor(cyberActor);
  renderer1->SetBackground(colors->GetColor3d("Wheat").GetData());
  renderer2->SetBackground(colors->GetColor3d("Papaya_Whip").GetData());
  renderWindow->SetSize(1024, 640);

// render the image
//
  vtkSmartPointer<vtkCamera> cam1 =
    vtkSmartPointer<vtkCamera>::New();
  cam1->SetFocalPoint(0, 0, 0);
  cam1->SetPosition(1, 0, 0);
  cam1->SetViewUp(0, 1, 0);
  renderer1->SetActiveCamera(cam1);
  renderer2->SetActiveCamera(cam1);
  renderer1->ResetCamera();
  cam1->Azimuth(30);
  cam1->Elevation(30);
  cam1->Dolly(1.4);
  renderer1->ResetCameraClippingRange();

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(StripFran)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkFiltersCore
  vtkIOLegacy
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping StripFran: ${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(StripFran MACOSX_BUNDLE StripFran.cxx )
  target_link_libraries(StripFran PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(StripFran MACOSX_BUNDLE StripFran.cxx )
  target_link_libraries(StripFran PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS StripFran
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build StripFran

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

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

./StripFran

WINDOWS USERS

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