Skip to content


# OBBDicer

VTKEx/Cxx/Meshes/OBBDicer

### Description

The vtkOBBDicer filter breaks up an input mesh into a number of pieces. The resulting mesh contains scalar point data that can be used to extract the individual pieces with a filter like vtkThresholdFilter. This examples displays the output of vtkOBBDicer with a different color for each piece.

The first argument is a filename for a vtkPolyData reader. If not specified, then a vtkSphereSource generates the vtkPolyData. The second argument is the number of pieces and is optional. The default is 4.

The example was run with these arguments:

       OBBDicer Armadill0 20

Other languages

See (CSharp)

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

OBBDicer.cxx

    #include <vtkSmartPointer.h>

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkLookupTable.h>
    #include <vtkNamedColors.h>
    #include <vtkOBBDicer.h>
    #include <vtkOutlineCornerFilter.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>

    #include <vtkBYUReader.h>
    #include <vtkOBJReader.h>
    #include <vtkPLYReader.h>
    #include <vtkPolyDataReader.h>
    #include <vtkSTLReader.h>
    #include <vtkXMLPolyDataReader.h>
    #include <vtkSphereSource.h>
    #include <vtksys/SystemTools.hxx>

    #include <random>

    namespace
    {
    vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName);
    }

    int main(int argc, char *argv[])
    {
      int pieces = 4;
      if (argc > 2)
      {
        pieces = std::atoi(argv[2]);
      }

      auto inputPolyData = ReadPolyData(argc > 1 ? argv[1] : "");;

      // Create pipeline
      auto dicer =
        vtkSmartPointer<vtkOBBDicer>::New();
      dicer->SetInputData(inputPolyData);
      dicer->SetNumberOfPieces(pieces);
      dicer->SetDiceModeToSpecifiedNumberOfPieces();
      dicer->Update();

      int numberOfRegions = dicer->GetNumberOfActualPieces();

      // Fill in a few known colors, the rest will be generated if needed
      auto colors =
        vtkSmartPointer<vtkNamedColors>::New();

      auto lut =
        vtkSmartPointer<vtkLookupTable>::New();
      lut->SetNumberOfTableValues(std::max(numberOfRegions, 10));
      lut->Build();
      lut->SetTableValue(0, colors->GetColor4d("Gold").GetData());
      lut->SetTableValue(1, colors->GetColor4d("Banana").GetData());
      lut->SetTableValue(2, colors->GetColor4d("Tomato").GetData());
      lut->SetTableValue(3, colors->GetColor4d("Wheat").GetData());
      lut->SetTableValue(4, colors->GetColor4d("Lavender").GetData());
      lut->SetTableValue(5, colors->GetColor4d("Flesh").GetData());
      lut->SetTableValue(6, colors->GetColor4d("Raspberry").GetData());
      lut->SetTableValue(7, colors->GetColor4d("Salmon").GetData());
      lut->SetTableValue(8, colors->GetColor4d("Mint").GetData());
      lut->SetTableValue(9, colors->GetColor4d("Peacock").GetData());

      // If the number of regions os larger than the number of specified colors,
      // generate some random colors.
      if (numberOfRegions > 9)
      {
        std::mt19937 mt(4355412); //Standard mersenne_twister_engine
        std::uniform_real_distribution<double> distribution(.6, 1.0);
        for (auto i = 10; i < numberOfRegions; ++i)
        {
          lut->SetTableValue(i, distribution(mt), distribution(mt), distribution(mt), 1.0);
        }
      }

      auto inputMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      inputMapper->SetInputConnection(dicer->GetOutputPort());
      inputMapper->SetScalarRange(0, dicer->GetNumberOfActualPieces());
      inputMapper->SetLookupTable(lut);

      std::cout << "Asked for: "
                << dicer->GetNumberOfPieces() << " pieces, got: "
                << dicer->GetNumberOfActualPieces() << std::endl;

      auto inputActor =
        vtkSmartPointer<vtkActor>::New();
      inputActor->SetMapper(inputMapper);
      inputActor->GetProperty()->SetInterpolationToFlat();

      auto outline =
        vtkSmartPointer<vtkOutlineCornerFilter>::New();
      outline->SetInputData(inputPolyData);

      auto outlineMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      outlineMapper->SetInputConnection(outline->GetOutputPort());

      auto outlineActor =
        vtkSmartPointer<vtkActor>::New();
      outlineActor->SetMapper(outlineMapper);
      outlineActor->GetProperty()->SetColor(0, 0, 0);

      auto renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->UseHiddenLineRemovalOn();

      auto renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

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

      // Add the actors to the renderer, set the background and size
      renderer->AddActor(outlineActor);
      renderer->AddActor(inputActor);
      renderer->SetBackground(.2, .3, .4);
      renderer->GetActiveCamera()->Azimuth(150);
      renderer->GetActiveCamera()->Elevation(15);
      renderer->ResetCamera();

      // Render the image
      renderWindow->Render();

      interactor->Start();

      return EXIT_SUCCESS;
    }
    namespace
    {
    vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName)
    {
      vtkSmartPointer<vtkPolyData> polyData;
      std::string extension =
          vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
      if (extension == ".ply")
      {
        auto reader = vtkSmartPointer<vtkPLYReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".vtp")
      {
        auto reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".obj")
      {
        auto reader = vtkSmartPointer<vtkOBJReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".stl")
      {
        auto reader = vtkSmartPointer<vtkSTLReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".vtk")
      {
        auto reader = vtkSmartPointer<vtkPolyDataReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else if (extension == ".g")
      {
        auto reader = vtkSmartPointer<vtkBYUReader>::New();
        reader->SetGeometryFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
      }
      else
      {
        auto source = vtkSmartPointer<vtkSphereSource>::New();
        source->SetPhiResolution(25);
        source->SetThetaResolution(25);
        source->Update();
        polyData = source->GetOutput();
      }
      return polyData;
    }
    } // namespace

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(OBBDicer)

    find_package(VTK COMPONENTS
      vtkvtkCommonColor
      vtkvtkCommonCore
      vtkvtkFiltersGeneral
      vtkvtkFiltersSources
      vtkvtkIOGeometry
      vtkvtkIOLegacy
      vtkvtkIOPLY
      vtkvtkIOXML
      vtkvtkInteractionStyle
      vtkvtkRenderingContextOpenGL2
      vtkvtkRenderingCore
      vtkvtkRenderingFreeType
      vtkvtkRenderingGL2PSOpenGL2
      vtkvtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping OBBDicer: ${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(OBBDicer MACOSX_BUNDLE OBBDicer.cxx )
      target_link_libraries(OBBDicer PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(OBBDicer MACOSX_BUNDLE OBBDicer.cxx )
      target_link_libraries(OBBDicer PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS OBBDicer
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build OBBDicer

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

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

    ./OBBDicer

WINDOWS USERS

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