Distance Between Points#

Synopsis#

Compute the distance between two 3D points. This can easily be extended to ND by changing the constant Dimension.

Results#

Output:

Dist: 1.73205
Dist2: 3

Code#

C++#

#include "itkPoint.h"
#include "itkMath.h"

#include <iostream>

int
main()
{
  constexpr unsigned int Dimension = 3;
  using CoordType = double;

  using PointType = itk::Point<CoordType, Dimension>;

  PointType p0;
  p0[0] = 0.0;
  p0[1] = 0.0;
  p0[2] = 0.0;

  PointType p1;
  p1[0] = 1.0;
  p1[1] = 1.0;
  p1[2] = 1.0;

  PointType::RealType dist = p0.EuclideanDistanceTo(p1);
  std::cout << "Dist: " << dist << std::endl;

  if (dist != p1.EuclideanDistanceTo(p0))
  {
    std::cerr << "p0.EuclideanDistanceTo(p1) != p1.EuclideanDistanceTo(p0)" << std::endl;
    return EXIT_FAILURE;
  }

  if (p1.EuclideanDistanceTo(p1) != 0.)
  {
    std::cerr << "p1.EuclideanDistanceTo(p1) != 0." << std::endl;
    return EXIT_FAILURE;
  }

  PointType::RealType dist2 = p0.SquaredEuclideanDistanceTo(p1);
  std::cout << "Dist2: " << dist2 << std::endl;

  if (itk::Math::abs(dist2 - dist * dist) < itk::Math::eps)
  {
    std::cerr << "dist2 != dist * dist" << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TCoordinate, unsigned int VPointDimension = 3>
class Point : public itk::FixedArray<TCoordinate, 3>

A templated class holding a geometric point in n-Dimensional space.

Point is a templated class that holds a set of coordinates (components). Point can be used as the data type held at each pixel in an Image or at each vertex of an Mesh. The template parameter T can be any data type that behaves like a primitive (or atomic) data type (int, short, float, complex). The VPointDimension defines the number of components in the point array.

See also

Image

See also

Mesh

See also

Vector

See also

CovariantVector

See also

Matrix

ITK Sphinx Examples:

Subclassed by itk::ContinuousIndex< TInterpolatorPrecisionType, InputImageDimension >, itk::ContinuousIndex< SpacePrecisionType, VDimension >, itk::ContinuousIndex< typename DisplacementFieldPointType::CoordinateType, ImageDimension >, itk::ContinuousIndex< typename PointType::ValueType, ImageDimension >

See itk::Point for additional documentation.