Bounding Box Of A Point Set

Synopsis

Get the bounding box of a PointSet

Results

Output::

bounds: [0, 0.1, 0, 0.1, 0, 0] center: [0.05, 0.05, 0] diagonal length squared: 0.02

Code

C++

#include "itkPoint.h"
#include "itkPointSet.h"
#include "itkBoundingBox.h"

int
main(int, char *[])
{
  using CoordType = float;
  constexpr unsigned int Dimension = 3;

  using PointSetType = itk::PointSet<CoordType, Dimension>;

  using PointIdentifier = PointSetType::PointIdentifier;
  using PointType = PointSetType::PointType;
  using PointsContainerPointer = PointSetType::PointsContainerPointer;

  PointSetType::Pointer  pointSet = PointSetType::New();
  PointsContainerPointer points = pointSet->GetPoints();

  // Create points
  PointType p0, p1, p2;

  p0[0] = 0.0;
  p0[1] = 0.0;
  p0[2] = 0.0;
  p1[0] = 0.1;
  p1[1] = 0.0;
  p1[2] = 0.0;
  p2[0] = 0.0;
  p2[1] = 0.1;
  p2[2] = 0.0;

  points->InsertElement(0, p0);
  points->InsertElement(1, p1);
  points->InsertElement(2, p2);

  using BoundingBoxType = itk::BoundingBox<PointIdentifier, Dimension, CoordType>;

  BoundingBoxType::Pointer boundingBox = BoundingBoxType::New();
  boundingBox->SetPoints(points);
  boundingBox->ComputeBoundingBox();

  std::cout << "bounds: " << boundingBox->GetBounds() << std::endl;
  std::cout << "center: " << boundingBox->GetCenter() << std::endl;
  std::cout << "diagonal length squared: " << boundingBox->GetDiagonalLength2() << std::endl;

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import platform

Dimension = 3
CoordType = itk.ctype('float')
# Windows requires unsigned long long for 64-bit identifiers
if platform.system() == 'Windows':
    ElementIdentifierType = itk.ctype('unsigned long long')
else:
    ElementIdentifierType = itk.ctype('unsigned long')


PointSetType = itk.PointSet[CoordType, Dimension]

pointSet = PointSetType.New()
points = pointSet.GetPoints()

# Create points
p0 = itk.Point[CoordType, Dimension]()
p1 = itk.Point[CoordType, Dimension]()
p2 = itk.Point[CoordType, Dimension]()

p0[0] = 0.0
p0[1] = 0.0
p0[2] = 0.0
p1[0] = 0.1
p1[1] = 0.0
p1[2] = 0.0
p2[0] = 0.0
p2[1] = 0.1
p2[2] = 0.0

points.InsertElement(0, p0)
points.InsertElement(1, p1)
points.InsertElement(2, p2)

VecContType = itk.VectorContainer[ElementIdentifierType,
                                  itk.Point[CoordType, Dimension]]
BoundingBoxType = itk.BoundingBox[ElementIdentifierType,
                                  Dimension, CoordType, VecContType]

boundingBox = BoundingBoxType.New()
boundingBox.SetPoints(points)
boundingBox.ComputeBoundingBox()

print("bounds: " + str(boundingBox.GetBounds()))
print("center: " + str(boundingBox.GetCenter()))
print("diagonal length squared: " + str(boundingBox.GetDiagonalLength2()))

Classes demonstrated