1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkImageIORegion_h
#define itkImageIORegion_h
#include <algorithm>
#include "itkIntTypes.h"
#include "itkObjectFactory.h"
#include "itkImageRegion.h"
namespace itk
{
/** \class ImageIORegion
* \brief An ImageIORegion represents a structured region of data.
*
* ImageIORegion is an class that represents some structured portion or
* piece of an Image. The ImageIORegion is represented with an index and
* a size in each of the n-dimensions of the image. (The index is the
* corner of the image, the size is the lengths of the image in each of
* the topological directions.) ImageIORegion is not templated over
* dimension, but uses dynamic arrays instead.
*
* The first pixel of an image always have a Zero index. Therefore the
* index values of ImageIORegion may not directly correspond to those
* of ImageRegion. When translation between the two is performed one
* much consider the largest possible region who has a non-zero
* starting index for the image.
*
* \sa Region
* \sa ImageRegion
* \sa Index
* \sa Size
* \sa MeshRegion
* \ingroup ITKCommon
*/
class ITKCommon_EXPORT ImageIORegion : public Region
{
public:
/** Standard class type aliases. */
using Self = ImageIORegion;
using Superclass = Region;
/** these types correspond to those of itk::Size, itk::Offset and itk::Index
*/
using SizeValueType = itk::SizeValueType;
using IndexValueType = itk::IndexValueType;
using OffsetValueType = itk::OffsetValueType;
/** Index type alias support An index is used to access pixel values. */
using IndexType = std::vector<IndexValueType>;
/** Size type alias support A size is used to define region bounds. */
using SizeType = std::vector<SizeValueType>;
/** Region type taken from the superclass */
using RegionType = Superclass::RegionEnum;
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(ImageIORegion);
/** Dimension of the image available at run time. */
unsigned int
GetImageDimension() const;
/** Dimension of the region to be written. This differs from the
* the image dimension and is calculated at run-time by examining
* the size of the image in each coordinate direction. */
unsigned int
GetRegionDimension() const;
/** Return the region type. Images are described with structured regions. */
RegionType
GetRegionType() const override;
/** Constructor. ImageIORegion is a lightweight object that is not reference
* counted, so the constructor is public. */
ImageIORegion(unsigned int dimension);
/** Constructor. ImageIORegion is a lightweight object that is not reference
* counted, so the constructor is public. Default dimension is 2. */
ImageIORegion() = default;
/** Destructor. ImageIORegion is a lightweight object that is not reference
* counted, so the destructor is public. */
~ImageIORegion() override;
/** Copy constructor. ImageIORegion is a lightweight object that is not
* reference counted, so the copy constructor is public. */
ImageIORegion(const Self &) = default;
/** Move-constructor. Fast and non-throwing. */
ImageIORegion(Self &&) = default;
/** Copy-assignment operator. ImageIORegion is a lightweight object that is not reference
* counted, so operator= is public. */
Self &
operator=(const Self & region);
/** Move-assignment operator. Fast and non-throwing. */
Self &
operator=(Self &&) = default;
/** Set the index defining the corner of the region. */
void
SetIndex(const IndexType & index);
/** Get index defining the corner of the region. */
const IndexType &
GetIndex() const;
IndexType &
GetModifiableIndex();
/** Set the size of the region. This plus the index determines the
* rectangular shape, or extent, of the region. */
void
SetSize(const SizeType & size);
/** Get the size of the region. */
const SizeType &
GetSize() const;
SizeType &
GetModifiableSize();
/** Convenience methods to get the size of the image in a particular
* coordinate direction i. Do not try to access image sizes beyond the
* the ImageDimension. */
SizeValueType
GetSize(unsigned long i) const;
IndexValueType
GetIndex(unsigned long i) const;
void
SetSize(const unsigned long i, SizeValueType size);
void
SetIndex(const unsigned long i, IndexValueType idx);
/** Compare two regions. */
bool
operator==(const Self & region) const;
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);
/** Test if an index is inside */
bool
IsInside(const IndexType & index) const;
/** Test if a region (the argument) is completely inside of this region */
bool
IsInside(const Self & region) const;
/** Get the number of pixels contained in this region. This just
* multiplies the size components. */
SizeValueType
GetNumberOfPixels() const;
protected:
/** Methods invoked by Print() to print information about the object
* including superclasses. Typically not called by the user (use Print()
* instead) but used in the hierarchical print process to combine the
* output of several classes. */
void
PrintSelf(std::ostream & os, Indent indent) const override;
private:
unsigned int m_ImageDimension{ 2 };
IndexType m_Index{ IndexType(2) };
SizeType m_Size{ SizeType(2) };
};
// Declare operator<<
extern ITKCommon_EXPORT std::ostream &
operator<<(std::ostream & os, const ImageIORegion & region);
/** \class ImageIORegionAdaptor
* \brief Helper class for converting ImageRegions into ImageIORegions and back.
*
* \ingroup ITKCommon
*/
template <unsigned int VDimension>
class ImageIORegionAdaptor
{
public:
using ImageRegionType = ImageRegion<VDimension>;
using ImageIORegionType = ImageIORegion;
using ImageSizeType = typename ImageRegionType::SizeType;
using ImageIndexType = typename ImageRegionType::IndexType;
static void
Convert(const ImageRegionType & inImageRegion,
ImageIORegionType & outIORegion,
const ImageIndexType & largestRegionIndex)
{
//
// The ImageRegion and ImageIORegion objects may have different dimensions.
// Here we only copy the common dimensions between the two. If the
// ImageRegion
// has more dimensions than the ImageIORegion, then the defaults of the
// ImageRegion
// will take care of the remaining codimension. If the ImageRegion has less
// dimensions
// than the ImageIORegion, then the remaining IO dimensions are simply
// ignored.
//
const unsigned int ioDimension = outIORegion.GetImageDimension();
const unsigned int imageDimension = VDimension;
const unsigned int minDimension = std::min(ioDimension, imageDimension);
const ImageSizeType & size = inImageRegion.GetSize();
const ImageIndexType & index = inImageRegion.GetIndex();
for (unsigned int i = 0; i < minDimension; ++i)
{
outIORegion.SetSize(i, size[i]);
outIORegion.SetIndex(i, index[i] - largestRegionIndex[i]);
}
//
// Fill in the remaining codimension (if any) with default values
//
for (unsigned int k = minDimension; k < ioDimension; ++k)
{
outIORegion.SetSize(k, 1); // Note that default size in IO is 1 not 0
outIORegion.SetIndex(k, 0);
}
}
static void
Convert(const ImageIORegionType & inIORegion,
ImageRegionType & outImageRegion,
const ImageIndexType & largestRegionIndex)
{
ImageSizeType size;
ImageIndexType index;
size.Fill(1); // initialize with default values
index.Fill(0);
//
// The ImageRegion and ImageIORegion objects may have different dimensions.
// Here we only copy the common dimensions between the two. If the
// ImageRegion
// has more dimensions than the ImageIORegion, then the defaults of the
// ImageRegion
// will take care of the remaining codimension. If the ImageRegion has less
// dimensions
// than the ImageIORegion, then the remaining IO dimensions are simply
// ignored.
//
const unsigned int ioDimension = inIORegion.GetImageDimension();
const unsigned int imageDimension = VDimension;
const unsigned int minDimension = std::min(ioDimension, imageDimension);
for (unsigned int i = 0; i < minDimension; ++i)
{
size[i] = inIORegion.GetSize(i);
index[i] = inIORegion.GetIndex(i) + largestRegionIndex[i];
}
outImageRegion.SetSize(size);
outImageRegion.SetIndex(index);
}
};
} // end namespace itk
#endif
|