Covariant Vector Norm#

Synopsis#

Compute the norm of a CovariantVector and normalize it.

Results#

Output:

v: [1, 2, 3]
vnorm: 3.74166
vnorm2: 14
v: [0.267261, 0.534522, 0.801784]
u: [0.267261, 0.534522, 0.801784]

Code#

C++#

#include "itkCovariantVector.h"

int
main()
{
  using VectorType = itk::CovariantVector<double, 3>;
  VectorType v;
  v[0] = 1.0;
  v[1] = 2.0;
  v[2] = 3.0;

  std::cout << "v: " << v << std::endl;

  // norm
  VectorType::RealValueType vnorm = v.GetNorm();
  std::cout << "vnorm: " << vnorm << std::endl;

  VectorType::RealValueType vnorm2 = v.GetSquaredNorm();
  std::cout << "vnorm2: " << vnorm2 << std::endl;

  VectorType u = v;

  // normalization
  v.Normalize();
  std::cout << "v: " << v << std::endl;

  // another way to normalize
  if (vnorm != 0.)
  {
    for (unsigned int i = 0; i < u.GetNumberOfComponents(); ++i)
    {
      u[i] /= vnorm;
    }
  }

  std::cout << "u: " << u << std::endl;

  if ((u - v).GetNorm() != 0.)
  {
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename T, unsigned int NVectorDimension = 3>
class CovariantVector : public itk::FixedArray<T, NVectorDimension>

A templated class holding a n-Dimensional covariant vector.

CovariantVector is a templated class that holds a single vector (i.e., an array of values). CovariantVector 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 NVectorDimension defines the number of components in the vector array.

CovariantVector is not a dynamically extendible array like std::vector. It is intended to be used like a mathematical vector.

If you wish a simpler pixel types, you can use Scalar, which represents a single data value at a pixel. There is also the more complex type ScalarCovariantVector, which supports (for a given pixel) a single scalar value plus an array of vector values. (The scalar and vectors can be of different data type.)

CovariantVector is the type that should be used for representing normals to surfaces and gradients of functions. AffineTransform transform covariant vectors different than vectors.

See

Image

See

Mesh

See

Point

See

Vector

See

Matrix

ITK Sphinx Examples:

See itk::CovariantVector for additional documentation.