Variable Length Vector#

Synopsis#

This computes a variable length vector.

Results#

Output:

[1, 2]
1 2
VectorToVariableLengthVector()
variableLengthVector: [1, 2]
VariableLengthVectorToVector()
fixedLengthVector: [1, 2]

Code#

C++#

#include <itkVariableLengthVector.h>
#include <itkVector.h>

void
VectorToVariableLengthVector();
void
VariableLengthVectorToVector();

int
main()
{
  using VectorType = itk::VariableLengthVector<double>;
  VectorType v;
  v.SetSize(2);
  v[0] = 1;
  v[1] = 2;
  std::cout << v << std::endl;

  for (unsigned int i = 0; i < v.Size(); ++i)
  {
    std::cout << v[i] << " ";
  }
  std::cout << std::endl;

  VectorToVariableLengthVector();
  VariableLengthVectorToVector();
  return EXIT_SUCCESS;
}

void
VectorToVariableLengthVector()
{
  std::cout << "VectorToVariableLengthVector()" << std::endl;

  using FixedVectorType = itk::Vector<double, 2>;
  FixedVectorType fixedLengthVector;
  fixedLengthVector[0] = 1;
  fixedLengthVector[1] = 2;

  using VariableVectorType = itk::VariableLengthVector<double>;
  VariableVectorType variableLengthVector;

  // This works
  variableLengthVector.SetSize(fixedLengthVector.Size());
  variableLengthVector.SetData(fixedLengthVector.GetDataPointer());

  // This crashes with both true and false
  // variableLengthVector.SetData(fixedLengthVector.GetDataPointer(), 2, true);

  std::cout << "variableLengthVector: " << variableLengthVector << std::endl;
}

void
VariableLengthVectorToVector()
{
  std::cout << "VariableLengthVectorToVector()" << std::endl;
  using VariableVectorType = itk::VariableLengthVector<double>;
  VariableVectorType variableLengthVector;
  variableLengthVector.SetSize(2);

  variableLengthVector[0] = 1;
  variableLengthVector[1] = 2;

  using FixedVectorType = itk::Vector<double, 2>;
  FixedVectorType fixedLengthVector;

  for (unsigned int i = 0; i < FixedVectorType::GetVectorDimension(); ++i)
  {
    fixedLengthVector[i] = variableLengthVector[i];
  }

  // This function doesn't exist!
  // fixedLengthVector.SetData(variableLengthVector.GetDataPointer());
  std::cout << "fixedLengthVector: " << fixedLengthVector << std::endl;
}

Classes demonstrated#

template<typename TValue>
class VariableLengthVector

Represents an array whose length can be defined at run-time.

This class is templated over the data type. This data-type is meant to be a scalar, such as float, double etc…

Note

ITK itself provides several classes that can serve as Arrays.

  • FixedArray - Compile time fixed length arrays that’s intended to represent an enumerated collection of n entities.

  • Array - Run time resizeable array that is intended to hold a collection of n entities

  • Vector - Compile time fixed length array that is intended to hold a collection of n data types. A vector usually has a mathematical meaning. It should only be used when mathematical operations such as addition, multiplication by a scalar, product etc make sense.

  • VariableLengthVector - Run time array that is intended to hold a collection of scalar data types. Again, it should be used only when mathematical operations on it are relevant. If not, use an Array.

  • Point - Represents the spatial coordinates of a spatial location. Operators on Point reflect geometrical concepts.

For the reasons listed above, you cannot instantiate

VariableLengthVector< bool > 
.

Design Considerations: We do not derive from vnl_vector to avoid being limited by the explicit template instantiations of vnl_vector and other hacks that vnl folks have been forced to use.

Note

This work is part of the National Alliance for Medical Image Computing (NAMIC), funded by the National Institutes of Health through the NIH Roadmap for Medical Research, Grant U54 EB005149.

See

CovariantVector

See

SymmetricSecondRankTensor

See

RGBPixel

See

DiffusionTensor3D

ITK Sphinx Examples:

Invariant

If m_LetArrayManageMemory is true, m_Data is deletable (whether it’s null or pointing to something with no elements. i.e. m_NumElements may be 0 and yet m_Data may be not null.)

Policies

The following Policies will be used by itk::VariableLengthVector::SetSize

See itk::VariableLengthVector for additional documentation.