Skip to content

BoundingBox

vtk-examples/Cxx/Utilities/BoundingBox

Description

Seealso

Outline and CubeAxesActor2D

Question

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

Code

BoundingBox.cxx

#include <vtkBoundingBox.h>

int main(int, char*[])
{
  double p0[3] = {0, 0, 0};
  double p1[3] = {0.5, 0.5, 0.5};
  double p2[3] = {1.0, 1.0, 1.0};

  vtkBoundingBox boundingBox;

  boundingBox.AddPoint(p0);

  double bounds[6];

  boundingBox.GetBounds(bounds);
  std::cout << "Bounds: " << bounds[0] << ", " << bounds[1] << ", " << bounds[2]
            << ", " << bounds[3] << ", " << bounds[4] << ", " << bounds[5]
            << std::endl;

  // After adding this point, the box gets bigger
  boundingBox.AddPoint(p2);

  boundingBox.GetBounds(bounds);
  std::cout << "Bounds: " << bounds[0] << ", " << bounds[1] << ", " << bounds[2]
            << ", " << bounds[3] << ", " << bounds[4] << ", " << bounds[5]
            << std::endl;

  // After adding this point, the box size does not change as the point is
  // already inside the box
  boundingBox.AddPoint(p1);

  boundingBox.GetBounds(bounds);
  std::cout << "Bounds: " << bounds[0] << ", " << bounds[1] << ", " << bounds[2]
            << ", " << bounds[3] << ", " << bounds[4] << ", " << bounds[5]
            << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(BoundingBox)

find_package(VTK COMPONENTS 
)

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

Download and Build BoundingBox

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

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

./BoundingBox

WINDOWS USERS

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