| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageRegionMultidimensionalSplitter.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:39 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef _itkImageRegionMultidimensionalSplitter_txx |
| 18 |
DEF |
#define _itkImageRegionMultidimensionalSplitter_txx |
| 19 |
|
#include "itkImageRegionMultidimensionalSplitter.h" |
| 20 |
|
#include <math.h> |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
/** |
| 26 |
|
* |
| 27 |
|
*/ |
| 28 |
|
template <unsigned int VImageDimension> |
| 29 |
|
unsigned int |
| 30 |
|
ImageRegionMultidimensionalSplitter<VImageDimension> |
| 31 |
|
::GetNumberOfSplits(const RegionType ®ion, unsigned int requestedNumber) |
| 32 |
|
{ |
| 33 |
|
const SizeType ®ionSize = region.GetSize(); |
| 34 |
|
|
| 35 |
|
// requested number of splits per dimension |
| 36 |
|
double splitsPerDimension = |
| 37 |
IND |
****::ceil( pow( (double) requestedNumber, 1.0/(double) VImageDimension)); |
| 38 |
|
|
| 39 |
|
// if a given dimension has fewer pixels that splitsPerDimension, then |
| 40 |
|
// only split number of pixels times |
| 41 |
|
unsigned int i, numPieces; |
| 42 |
|
numPieces = 1; |
| 43 |
|
for (i=0; i < VImageDimension; ++i) |
| 44 |
|
{ |
| 45 |
|
if (regionSize[i] < splitsPerDimension) |
| 46 |
|
{ |
| 47 |
|
numPieces *= regionSize[i]; |
| 48 |
|
} |
| 49 |
|
else |
| 50 |
|
{ |
| 51 |
|
numPieces *= (unsigned int) splitsPerDimension; |
| 52 |
|
} |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
return numPieces; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
/** |
| 60 |
|
* |
| 61 |
|
*/ |
| 62 |
|
template <unsigned int VImageDimension> |
| 63 |
|
ImageRegion<VImageDimension> |
| 64 |
|
ImageRegionMultidimensionalSplitter<VImageDimension> |
| 65 |
|
::GetSplit(unsigned int i, unsigned int numberOfPieces, |
| 66 |
|
const RegionType ®ion) |
| 67 |
|
{ |
| 68 |
|
RegionType splitRegion; |
| 69 |
|
IndexType splitIndex; |
| 70 |
|
SizeType splitSize, regionSize; |
| 71 |
|
|
| 72 |
|
// Initialize the splitRegion to the requested region |
| 73 |
|
splitRegion = region; |
| 74 |
|
splitIndex = splitRegion.GetIndex(); |
| 75 |
|
splitSize = splitRegion.GetSize(); |
| 76 |
|
|
| 77 |
|
regionSize = region.GetSize(); |
| 78 |
|
|
| 79 |
|
// requested number of splits per dimension |
| 80 |
|
double splitsPerDimension = |
| 81 |
IND |
****::ceil( pow( (double) numberOfPieces, 1.0/(double) VImageDimension)); |
| 82 |
|
|
| 83 |
|
// if a given dimension has fewer pixels that splitsPerDimension, then |
| 84 |
|
// only split number of pixels times |
| 85 |
|
unsigned int splits[VImageDimension], pixelsPerSplit[VImageDimension]; |
| 86 |
|
unsigned int j, numPieces; |
| 87 |
|
unsigned int ijk[VImageDimension]; |
| 88 |
|
unsigned int offsetTable[VImageDimension]; |
| 89 |
|
numPieces = 1; |
| 90 |
|
for (j=0; j < VImageDimension; ++j) |
| 91 |
|
{ |
| 92 |
|
offsetTable[j] = numPieces; |
| 93 |
|
if (regionSize[j] < splitsPerDimension) |
| 94 |
|
{ |
| 95 |
|
splits[j] = regionSize[j]; |
| 96 |
|
pixelsPerSplit[j] = 1; |
| 97 |
|
numPieces *= regionSize[j]; |
| 98 |
|
} |
| 99 |
|
else |
| 100 |
|
{ |
| 101 |
|
splits[j] = (unsigned int) splitsPerDimension; |
| 102 |
|
pixelsPerSplit[j] = (unsigned int) ::ceil(regionSize[j] |
| 103 |
|
/ (double) splits[j]); |
| 104 |
|
numPieces *= (unsigned int) splitsPerDimension; |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
// determine which split we are in |
| 109 |
|
unsigned int offset = i; |
| 110 |
|
for (j=VImageDimension-1; j > 0; j--) |
| 111 |
|
{ |
| 112 |
|
ijk[j] = offset / offsetTable[j]; |
| 113 |
|
offset -= (ijk[j] * offsetTable[j]); |
| 114 |
|
} |
| 115 |
|
ijk[0] = offset; |
| 116 |
|
|
| 117 |
|
// compute the split |
| 118 |
|
for (j=0; j < VImageDimension; j++) |
| 119 |
|
{ |
| 120 |
|
splitIndex[j] += ijk[j]*pixelsPerSplit[j]; |
| 121 |
|
if (ijk[j] < splits[j] - 1) |
| 122 |
|
{ |
| 123 |
|
splitSize[j] = pixelsPerSplit[j]; |
| 124 |
|
} |
| 125 |
|
else |
| 126 |
|
{ |
| 127 |
|
// this dimension is falling off the edge of the image |
| 128 |
|
splitSize[j] = splitSize[j] - ijk[j]*pixelsPerSplit[j]; |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
// set the split region ivars |
| 133 |
|
splitRegion.SetIndex( splitIndex ); |
| 134 |
|
splitRegion.SetSize( splitSize ); |
| 135 |
|
|
| 136 |
|
itkDebugMacro(" Split Piece: " << std::endl << splitRegion ); |
| 137 |
|
|
| 138 |
|
return splitRegion; |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
/** |
| 144 |
|
* |
| 145 |
|
*/ |
| 146 |
|
template <unsigned int VImageDimension> |
| 147 |
|
void |
| 148 |
|
ImageRegionMultidimensionalSplitter<VImageDimension> |
| 149 |
|
::PrintSelf(std::ostream& os, Indent indent) const |
| 150 |
|
{ |
| 151 |
|
Superclass::PrintSelf(os,indent); |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
|
| 155 |
|
} // end namespace itk |
| 156 |
|
|
| 157 |
|
#endif |
| 158 |
|
|