Skip to content


# BoxClipUnstructuredGrid

VTKEx/Cxx/Visualization/BoxClipUnstructuredGrid

### Description

This example uses the data hexa.vtk.

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

BoxClipUnstructuredGrid.cxx

    #include <vtkSmartPointer.h>
    #include <vtkBoxClipDataSet.h>
    #include <vtkLookupTable.h>

    #include <vtkUnstructuredGridReader.h>
    #include <vtkUnstructuredGrid.h>

    #include <vtkDataSetMapper.h>
    #include <vtkCamera.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>

    #include <vtkNamedColors.h>
    #include <vtkColor.h>

    int main(int argc, char *argv[])
    {
      if (argc < 2)
      {
        std::cout << "Usage: " << argv[0] << " file.vtk" << endl;
        return EXIT_FAILURE;
      }

      auto colors =
        vtkSmartPointer<vtkNamedColors>::New();
      vtkColor3d backgroundColor = colors->GetColor3d("Silver");

      // Read the data
      auto reader =
        vtkSmartPointer<vtkUnstructuredGridReader>::New();
      reader->SetFileName (argv[1]);
      reader->Update();

      double bounds[6];
      reader->GetOutput()->GetBounds(bounds);

      double range[2];
      reader->GetOutput()->GetScalarRange(range);

      double minBoxPoint[3];
      double maxBoxPoint[3];
      minBoxPoint[0] = (bounds[1] - bounds[0]) / 2.0 + bounds[0];
      minBoxPoint[1] = (bounds[3] - bounds[2]) / 2.0 + bounds[2];
      minBoxPoint[2] = (bounds[5] - bounds[4]) / 2.0 + bounds[4];
      maxBoxPoint[0] = bounds[1];
      maxBoxPoint[1] = bounds[3];
      maxBoxPoint[2] = bounds[5];

      auto boxClip =
        vtkSmartPointer<vtkBoxClipDataSet>::New();
      boxClip->SetInputConnection (reader->GetOutputPort());
      boxClip->GenerateClippedOutputOn();

      const double minusx[] = { -1.0, -0.5, 0.0 };
      const double minusy[] = { 0.0, -1.0, 0.0 };
      const double minusz[] = { 0.0, 0.0, -1.0 };
      const double plusx[] = { 1.0, 0.0, 0.0 };
      const double plusy[] = { 0.0, 1.0, 0.0 };
      const double plusz[] = { 0.0, 0.0, 1.0 };
      boxClip->SetBoxClip(minusx, minBoxPoint,
                          minusy, minBoxPoint,
                          minusz, minBoxPoint,
                          plusx, maxBoxPoint,
                          plusy, maxBoxPoint,
                          plusz, maxBoxPoint);

      // Define a lut
      auto lut1 =
        vtkSmartPointer<vtkLookupTable>::New();
      lut1->SetHueRange (.667, 0);

      auto mapperIn =
      vtkSmartPointer<vtkDataSetMapper>::New();
      mapperIn->SetInputConnection(boxClip->GetOutputPort());
      mapperIn->SetScalarRange(reader->GetOutput()->GetScalarRange());
      mapperIn->SetLookupTable(lut1);
      mapperIn->SetColorModeToMapScalars();

      auto actorIn =
        vtkSmartPointer<vtkActor>::New();
      actorIn->SetMapper(mapperIn);

      auto mapperOut =
        vtkSmartPointer<vtkDataSetMapper>::New();
      mapperOut->SetInputConnection(boxClip->GetOutputPort(1));
      mapperOut->SetScalarRange(reader->GetOutput()->GetScalarRange());
      mapperOut->SetLookupTable(lut1);
      mapperOut->SetColorModeToMapScalars();

      auto actorOut =
        vtkSmartPointer<vtkActor>::New();
      actorOut->SetMapper(mapperOut);
      actorOut->AddPosition(-.5 * (maxBoxPoint[0] - minBoxPoint[0]),
                            -.5 * (maxBoxPoint[1] - minBoxPoint[1]),
                            -.5 * (maxBoxPoint[2] - minBoxPoint[2]));

      // Create a renderer, render window, and interactor
      auto renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->SetBackground(backgroundColor.GetData());
      renderer->UseHiddenLineRemovalOn();

      auto renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      renderWindow->SetSize(640, 480);

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

      // Add the actor to the scene
      renderer->AddActor(actorIn);
      renderer->AddActor(actorOut);

      // Generate an interesting view
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Azimuth(120);
      renderer->GetActiveCamera()->Elevation(30);
      renderer->GetActiveCamera()->Dolly(1.0);
      renderer->ResetCameraClippingRange();

      // Render and interact
      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(BoxClipUnstructuredGrid)

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

### Download and Build BoxClipUnstructuredGrid

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

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

    ./BoxClipUnstructuredGrid

WINDOWS USERS

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