Skip to content

DataBounds

vtk-examples/Cxx/PolyData/DataBounds

Description

This examples gets the bounds of a dataset. Note that Update() must be applied to create the output of the filter.

Question

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

Code

DataBounds.cxx

#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>

int main(int argc, char* argv[])
{
  vtkSmartPointer<vtkPolyData> polyData;

  if (argc > 1)
  {
    vtkNew<vtkXMLPolyDataReader> reader;
    reader->SetFileName(argv[1]);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else
  {
    vtkNew<vtkSphereSource> modelSource;
    modelSource->Update();
    polyData = modelSource->GetOutput();
  }

  double bounds[6];
  polyData->GetBounds(bounds);

  std::cout << "xmin: " << bounds[0] << " "
            << "xmax: " << bounds[1] << std::endl
            << "ymin: " << bounds[2] << " "
            << "ymax: " << bounds[3] << std::endl
            << "zmin: " << bounds[4] << " "
            << "zmax: " << bounds[5] << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DataBounds)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkIOXML
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping DataBounds: ${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(DataBounds MACOSX_BUNDLE DataBounds.cxx )
  target_link_libraries(DataBounds PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(DataBounds MACOSX_BUNDLE DataBounds.cxx )
  target_link_libraries(DataBounds PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS DataBounds
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build DataBounds

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

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

./DataBounds

WINDOWS USERS

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