| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
LEN |
Module: $RCSfile: itkFloodFilledSpatialFunctionConditionalConstIterator.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:35 $ |
| 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 _itkFloodFilledSpatialFunctionConditionalConstIterator_txx |
| 18 |
DEF |
#define _itkFloodFilledSpatialFunctionConditionalConstIterator_txx |
| 19 |
|
|
| 20 |
|
#include "itkFloodFilledSpatialFunctionConditionalConstIterator.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
template<class TImage, class TFunction> |
| 26 |
|
FloodFilledSpatialFunctionConditionalConstIterator<TImage, TFunction> |
| 27 |
|
::FloodFilledSpatialFunctionConditionalConstIterator(const ImageType *imagePtr, |
| 28 |
|
FunctionType *fnPtr, |
| 29 |
LEN |
IndexType startIndex): Superclass(imagePtr, fnPtr, startIndex) |
| 30 |
|
{ |
| 31 |
|
// The default inclusion strategy is "center" |
| 32 |
|
this->SetCenterInclusionStrategy(); |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
template<class TImage, class TFunction> |
| 36 |
|
FloodFilledSpatialFunctionConditionalConstIterator<TImage, TFunction> |
| 37 |
|
::FloodFilledSpatialFunctionConditionalConstIterator(const ImageType *imagePtr, |
| 38 |
LEN |
FunctionType *fnPtr): Superclass(imagePtr, fnPtr) |
| 39 |
|
{ |
| 40 |
|
// The default inclusion strategy is "center" |
| 41 |
|
this->SetCenterInclusionStrategy(); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
template<class TImage, class TFunction> |
| 45 |
|
bool |
| 46 |
|
FloodFilledSpatialFunctionConditionalConstIterator<TImage, TFunction> |
| 47 |
|
::IsPixelIncluded(const IndexType & index) const |
| 48 |
|
{ |
| 49 |
|
// This temp var is used in all cases |
| 50 |
|
FunctionInputType position; |
| 51 |
|
|
| 52 |
|
switch( m_InclusionStrategy ) { |
| 53 |
|
|
| 54 |
IND |
**// Origin |
| 55 |
IND |
**case 0: |
| 56 |
IND |
**{ |
| 57 |
IND |
**// Get the physical location of this index |
| 58 |
IND |
**this->m_Image->TransformIndexToPhysicalPoint(index, position); |
| 59 |
|
|
| 60 |
IND |
**// Evaluate the function at this point |
| 61 |
IND |
**return this->GetFunction()->Evaluate(position); |
| 62 |
IND |
**} |
| 63 |
IND |
**break; |
| 64 |
|
|
| 65 |
IND |
**// Center |
| 66 |
IND |
**case 1: |
| 67 |
IND |
**{ |
| 68 |
IND |
**// The center of the pixel is the index provided in the function |
| 69 |
IND |
**// call converted to a continuous index with an offset of 0.5 |
| 70 |
IND |
**// along each dimension |
| 71 |
IND |
**ContinuousIndex<double, TImage::ImageDimension> contIndex; |
| 72 |
|
|
| 73 |
IND |
**for(unsigned int i = 0; i < TImage::ImageDimension; i ++ ) |
| 74 |
|
{ |
| 75 |
|
contIndex[i] = (double)index[i] + 0.5; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
// Get the physical location of this index |
| 79 |
|
this->m_Image->TransformContinuousIndexToPhysicalPoint(contIndex, position); |
| 80 |
|
|
| 81 |
IND |
**// Evaluate the function at this point |
| 82 |
IND |
**return this->GetFunction()->Evaluate(position); |
| 83 |
IND |
**} |
| 84 |
IND |
**break; |
| 85 |
|
|
| 86 |
IND |
**// Complete |
| 87 |
IND |
**case 2: |
| 88 |
IND |
**{ |
| 89 |
IND |
**// This is unfortunately a little complicated... |
| 90 |
IND |
**// We want to examine whether or not all of the corners of this pixel |
| 91 |
IND |
**// are within the spatial function. For a pixel at (0,0) with a spacing |
| 92 |
IND |
**// of (1,1), this involves checking the following pixels: |
| 93 |
IND |
**// (0,0) (0,1) (1,0) (1,1) |
| 94 |
IND |
**// In other words, all possible permutations of adding either 0 or 1 to |
| 95 |
IND |
**// the index of the pixel of interest. For an index of dimension n, |
| 96 |
IND |
**// there are 2^n indices that need to be tested. |
| 97 |
IND |
**// The simplest way to implement this is by counting in binary fashion |
| 98 |
IND |
**// and adding the value of the appropriate binary digit to the corresponding |
| 99 |
IND |
**// index location |
| 100 |
IND |
**// Since I've chosen to implement this algorithm with an unsigned int counter, |
| 101 |
IND |
**// it will only behave correctly for images with dimensions <= 16. |
| 102 |
|
// However, given that the number of function inclusion tests is 2^n as well, |
| 103 |
|
// it seems unlikely that anyone would want to use this for images larger than |
| 104 |
|
// 3 or 4 dimensions, most likely only 3. Cases 0 or 1 provide a constant time |
| 105 |
|
// means of determining index inclusion. |
| 106 |
|
|
| 107 |
|
// To reiterate... DO NOT use this on images higher than 16D |
| 108 |
|
unsigned int counter; |
| 109 |
|
unsigned int counterCopy; |
| 110 |
|
unsigned int dim = TImage::ImageDimension; |
| 111 |
|
unsigned int numReps = static_cast<unsigned int>( pow( |
| 112 |
LEN |
static_cast<double>( 2.0 ), |
| 113 |
LEN,IND |
******************************************************static_cast<double>( dim ) ) ); |
| 114 |
|
|
| 115 |
IND |
**IndexType tempIndex; |
| 116 |
|
|
| 117 |
IND |
**// First we loop over the binary counter |
| 118 |
IND |
**for(counter = 0; counter < numReps; counter++) |
| 119 |
|
{ |
| 120 |
|
// Next we use the binary values in the counter to form |
| 121 |
|
// an index to look at |
| 122 |
|
for(unsigned int i = 0; i < dim; i++) |
| 123 |
|
{ |
| 124 |
|
counterCopy = counter; |
| 125 |
|
tempIndex[i] = index[i] + static_cast<int>( (counterCopy >> i) & 0x0001 ); |
| 126 |
IND |
******} |
| 127 |
|
|
| 128 |
IND |
****// Now that we've built an index, we can test it |
| 129 |
IND |
****// Get the physical location of this index |
| 130 |
IND |
****this->m_Image->TransformIndexToPhysicalPoint(tempIndex, position); |
| 131 |
|
|
| 132 |
IND |
****// Evaluate the function at this index, if it's false |
| 133 |
IND |
****// then the AND of all function dimensions is false, |
| 134 |
IND |
****// and hence it's not included |
| 135 |
IND |
****if( !(this->GetFunction()->Evaluate(position)) ) |
| 136 |
IND |
******return false; |
| 137 |
IND |
****} |
| 138 |
|
|
| 139 |
IND |
**// If we reach this point, we've tested all dimensions and none |
| 140 |
IND |
**// were outside the function, therefore the pixel is inside |
| 141 |
IND |
**return true; |
| 142 |
IND |
**} |
| 143 |
IND |
**break; |
| 144 |
|
|
| 145 |
IND |
**// Intersect |
| 146 |
IND |
**case 3: |
| 147 |
IND |
**{ |
| 148 |
IND |
**// The notes for the previous case apply here as well |
| 149 |
IND |
**// The only difference is that we return true if any of the |
| 150 |
IND |
**// generated indices are true |
| 151 |
|
|
| 152 |
IND |
**// To reiterate... DO NOT use this on images higher than 16D |
| 153 |
IND |
**unsigned int counter; |
| 154 |
IND |
**unsigned int counterCopy; |
| 155 |
IND |
**unsigned int dim = TImage::ImageDimension; |
| 156 |
IND |
**unsigned int numReps = static_cast<unsigned int>( pow( |
| 157 |
LEN |
static_cast<double>( 2.0 ), |
| 158 |
LEN,IND |
******************************************************static_cast<double>( dim ) ) ); |
| 159 |
IND |
**IndexType tempIndex; |
| 160 |
|
|
| 161 |
IND |
**// First we loop over the binary counter |
| 162 |
IND |
**for(counter = 0; counter < numReps; counter++) |
| 163 |
|
{ |
| 164 |
|
// Next we use the binary values in the counter to form |
| 165 |
|
// an index to look at |
| 166 |
|
for(unsigned int i = 0; i < dim; i++) |
| 167 |
|
{ |
| 168 |
|
counterCopy = counter; |
| 169 |
|
tempIndex[i] = index[i] + static_cast<int>( (counterCopy >> i) & 0x0001); |
| 170 |
IND |
******} |
| 171 |
|
|
| 172 |
IND |
****// Now that we've built an index, we can test it |
| 173 |
IND |
****// Get the physical location of this index |
| 174 |
IND |
****this->m_Image->TransformIndexToPhysicalPoint(tempIndex, position); |
| 175 |
|
|
| 176 |
IND |
****// Evaluate the function at this index, if it's true |
| 177 |
IND |
****// then the OR of all function dimensions is true, |
| 178 |
IND |
****// and hence it's included |
| 179 |
IND |
****if( this->m_Function->Evaluate(position) ) |
| 180 |
IND |
******return true; |
| 181 |
IND |
****} |
| 182 |
|
|
| 183 |
IND |
**// If we reach this point, we've tested all dimensions and none |
| 184 |
IND |
**// were inside the function, therefore the pixel is outside |
| 185 |
IND |
**return false; |
| 186 |
IND |
**} |
| 187 |
IND |
**} // end switch inclusion strategy |
| 188 |
|
|
| 189 |
|
// Somehow me managed to exit the switch statement without returning |
| 190 |
|
// To be safe, we'll say that the pixel is not inside |
| 191 |
|
return false; |
| 192 |
|
|
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
} // end namespace itk |
| 196 |
|
|
| 197 |
|
#endif |
| 198 |
|
|