template
Offset 是一个模板类,用于表示多维偏移,即 (i,j,k,…)。Offset是在空间维度上模板化的。
ITK假设size (bounds)的第一个元素是移动最快的索引。
为了效率,Offset 没有定义默认构造函数、复制构造函数或 operator=。我们依靠编译器来提供高效的按位复制。
Offset 是一个“聚合”类。它的数据是公开的 (m_InternalArray),允许快速方便的实例化/分配。
允许/建议使用以下语法来指定聚合类型:
Offset<3> var{{ 256, 256, 20 }}; // 避免使用Offset<3> var = {{ 256, 256, 20 }}
需要双大括号 {{ 和 }} 以防止 gcc -Wall(可能还有其他编译器)抱怨括号内的初始化程序。
作为旨在提供最高性能特征的聚合类型,此类不适合继承,因此将此结构设置为 final。
(1)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("")
# index: [5, 5]
# offset: [1, 1]
# index + offset: [6, 6]
#
# index: [5, 5]
# offset: [-1, 1]
# index + offset: [4, 6]
(2)C++代码
#include "itkIndex.h" #include "itkOffset.h" #includeint main(int, char *[]) { constexpr unsigned int Dimension = 2; itk::Index index; index.Fill(5); itk::Offset 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; }
参考目录
https://itk.org/ITKExamples/src/Core/Common/AddOffsetToIndex/documentation.html



