Add Offset To Index#

Synopsis#

Add an offset to a pixel index

Results#

Output:
index: [5, 5]
offset: [1, 1]
index + offset: [6, 6]
index: [5, 5]
offset: [-1, 1]
index + offset: [4, 6]

Code#

Python#

#!/usr/bin/env python

import sys
import itk

from distutils.version import StrictVersion as VS

if VS(itk.Version.GetITKVersion()) < VS("4.9.0"):
    print("ITK 4.9.0 is required.")
    sys.exit(1)

Dimension = 2

index = itk.Index[Dimension]()
index.Fill(5)

offset = itk.Offset[Dimension]()
offset.Fill(1)

newIndex = index + offset

print("index: " + str([int(index[0]), int(index[1])]))
print("offset: " + str([int(offset[0]), int(offset[1])]))
print("index + offset: " + str([int(newIndex[0]), int(newIndex[1])]))
print("")

offset[0] = -1
newIndex = index + offset

print("index: " + str([int(index[0]), int(index[1])]))
print("offset: " + str([int(offset[0]), int(offset[1])]))
print("index + offset: " + str([int(newIndex[0]), int(newIndex[1])]))
print("")

C++#

#include "itkIndex.h"
#include "itkOffset.h"

#include <iostream>

int
main()
{
  constexpr unsigned int Dimension = 2;

  itk::Index<Dimension> index;
  index.Fill(5);

  itk::Offset<Dimension> offset;
  offset.Fill(1);

  std::cout << "index: " << index << std::endl;
  std::cout << "offset: " << offset << std::endl;
  std::cout << "index + offset: " << index + offset << std::endl;
  std::cout << std::endl;

  offset[0] = -1;

  std::cout << "index: " << index << std::endl;
  std::cout << "offset: " << offset << std::endl;
  std::cout << "index + offset: " << index + offset << std::endl;
  std::cout << std::endl;

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<unsigned int VDimension = 2>
struct Offset

Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.

Offset is a templated class to represent a multi-dimensional offset, i.e. (i,j,k,…). Offset is templated over the dimension of the space. ITK assumes the first element of a size (bounds) is the fastest moving index.

For efficiency, Offset does not define a default constructor, a copy constructor, or an operator=. We rely on the compiler to provide efficient bitwise copies.

Offset is an “aggregate” class. Its data is public (m_InternalArray) allowing for fast and convenient instantiations/assignments.

The following syntax for assigning an aggregate type like this is allowed/suggested:

Offset<3> var{{ 256, 256, 20 }}; // Also prevent narrowing conversions Offset<3> var = {{ 256, 256, 20 }};

The doubled braces {{ and }} are required to prevent gcc -Wall (and perhaps other compilers) from complaining about a partly bracketed initializer.

As an aggregate type that is intended to provide highest performance characteristics, this class is not appropriate to inherit from, so setting this struct as final.

See

Index

ITK Sphinx Examples:

See itk::Offset for additional documentation.
template<unsigned int VDimension = 2>
struct Index

Represent a n-dimensional index in a n-dimensional image.

Index is a templated class to represent a multi-dimensional index, i.e. (i,j,k,…). Index is templated over the dimension of the index. ITK assumes the first element of an index is the fastest moving index.

For efficiency sake, Index does not define a default constructor, a copy constructor, or an operator=. We rely on the compiler to provide efficient bitwise copies.

Index is an “aggregate” class. Its data is public (m_InternalArray) allowing for fast and convenient instantiations/assignments.

The following syntax for assigning an aggregate type like this is allowed/suggested:

Index<3> var{{ 256, 256, 20 }}; // Also prevent narrowing conversions Index<3> var = {{ 256, 256, 20 }};

The doubled braces {{ and }} are required to prevent gcc -Wall (and perhaps other compilers) from complaining about a partly bracketed initializer.

As an aggregate type that is intended to provide highest performance characteristics, this class is not appropriate to inherit from, so setting this struct as final.

ITK Sphinx Examples:

See itk::Index for additional documentation.