| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageHelper.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:38 $ |
| 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 |
|
#ifndef __itkImageHelper_h |
| 18 |
|
#define __itkImageHelper_h |
| 19 |
|
|
| 20 |
|
#include "itkConceptChecking.h" |
| 21 |
|
|
| 22 |
|
namespace itk |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
/** \class ImageHelper |
| 26 |
|
* \brief Fast Index/Offset computation |
| 27 |
|
* |
| 28 |
|
* These helper methods use recursive templates to unroll the loops |
| 29 |
|
* of simple calculations. The resulting speed improvement varies from |
| 30 |
|
* compiler to compiler. Some gcc compilers with debug turned on |
| 31 |
|
* exhibit slight speed increases, but most compilers see |
| 32 |
|
* improvement. The ComputeOffset performance improvement is |
| 33 |
|
* impressive. For example, the Windows VS7.0 compiler shows almost a |
| 34 |
|
* factor of 2 speed improvement with the recursive templates. Usually |
| 35 |
|
* recursive templates use partial specialization to terminate |
| 36 |
|
* loops. Here we use a technique used by Brad King in the itk Concept |
| 37 |
|
* Checking code. |
| 38 |
|
* |
| 39 |
|
* \note This work is part of the National Alliance for Medical Image |
| 40 |
|
* Computing (NAMIC), funded by the National Institutes of Health |
| 41 |
|
* through the NIH Roadmap for Medical Research, Grant U54 EB005149. |
| 42 |
|
* Information on the National Centers for Biomedical Computing |
| 43 |
|
* can be obtained from http://nihroadmap.nih.gov/bioinformatics. |
| 44 |
|
* |
| 45 |
|
*/ |
| 46 |
|
|
| 47 |
|
// Forward reference ImageBase |
| 48 |
|
template < |
| 49 |
|
unsigned int NImageDimension |
| 50 |
IND |
**> class ImageBase; |
| 51 |
|
|
| 52 |
|
template <unsigned int NImageDimension, unsigned int NLoop> |
| 53 |
|
class ImageHelper |
| 54 |
|
{ |
| 55 |
|
public: |
| 56 |
|
typedef ImageBase<NImageDimension> ImageType; |
| 57 |
|
typedef typename ImageType::IndexType IndexType; |
| 58 |
|
typedef typename ImageType::OffsetType OffsetType; |
| 59 |
|
typedef typename ImageType::IndexValueType IndexValueType; |
| 60 |
|
typedef typename ImageType::OffsetValueType OffsetValueType; |
| 61 |
|
typedef Concept::Detail::UniqueType_bool<false> UniqueTypeBoolFalse; |
| 62 |
|
typedef Concept::Detail::UniqueType_bool<true> UniqueTypeBoolTrue; |
| 63 |
|
|
| 64 |
|
/** ComputeIndex with recursive templates */ |
| 65 |
|
inline static void ComputeIndex(const IndexType &bufferedRegionIndex, |
| 66 |
|
OffsetValueType offset, |
| 67 |
|
const OffsetValueType offsetTable[], |
| 68 |
|
IndexType &index) |
| 69 |
|
{ |
| 70 |
IND |
******ImageHelper<NImageDimension,NLoop-1>:: |
| 71 |
IND |
******ComputeIndexInner(bufferedRegionIndex, |
| 72 |
|
offset, |
| 73 |
|
offsetTable, |
| 74 |
|
index, |
| 75 |
|
Concept::Detail::UniqueType_bool<(NLoop==1)>()); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
inline static void ComputeIndexInner(const IndexType &bufferedRegionIndex, |
| 79 |
|
OffsetValueType &offset, |
| 80 |
|
const OffsetValueType offsetTable[], |
| 81 |
|
IndexType &index, |
| 82 |
|
const UniqueTypeBoolFalse& ) |
| 83 |
IND |
**{ |
| 84 |
|
index[NLoop] = static_cast<IndexValueType>(offset / offsetTable[NLoop]); |
| 85 |
|
offset = offset - (index[NLoop] * offsetTable[NLoop]); |
| 86 |
|
index[NLoop] = index[NLoop] + bufferedRegionIndex[NLoop]; |
| 87 |
|
ImageHelper<NImageDimension, NLoop-1>:: |
| 88 |
IND |
******ComputeIndexInner(bufferedRegionIndex, |
| 89 |
|
offset, |
| 90 |
|
offsetTable, |
| 91 |
|
index, |
| 92 |
|
Concept::Detail::UniqueType_bool<(NLoop==1)>()); |
| 93 |
|
|
| 94 |
IND |
**} |
| 95 |
|
|
| 96 |
|
inline static void ComputeIndexInner(const IndexType &bufferedRegionIndex, |
| 97 |
|
OffsetValueType &offset, |
| 98 |
|
const OffsetValueType [], |
| 99 |
|
IndexType &index, |
| 100 |
|
const UniqueTypeBoolTrue& ) |
| 101 |
IND |
**{ |
| 102 |
|
// Do last |
| 103 |
|
index[0] = bufferedRegionIndex[0] + static_cast<IndexValueType>(offset); |
| 104 |
IND |
**} |
| 105 |
|
|
| 106 |
|
// ComputeOffset |
| 107 |
|
// |
| 108 |
|
inline static void ComputeOffset(const IndexType &bufferedRegionIndex, |
| 109 |
|
const IndexType &index, |
| 110 |
|
const OffsetValueType offsetTable[], |
| 111 |
|
OffsetValueType &offset) |
| 112 |
|
{ |
| 113 |
IND |
******ImageHelper<NImageDimension,NLoop-1>:: |
| 114 |
IND |
********ComputeOffsetInner(bufferedRegionIndex, |
| 115 |
|
index, |
| 116 |
|
offsetTable, |
| 117 |
|
offset, |
| 118 |
|
Concept::Detail::UniqueType_bool<(NLoop==1)>()); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
LEN |
inline static void ComputeOffsetInner(const IndexType &bufferedRegionIndex, |
| 122 |
|
const IndexType &index, |
| 123 |
|
const OffsetValueType offsetTable[], |
| 124 |
|
OffsetValueType &offset, |
| 125 |
|
const UniqueTypeBoolFalse& ) |
| 126 |
IND |
**{ |
| 127 |
LEN |
offset = offset + (index[NLoop] - bufferedRegionIndex[NLoop])*offsetTable[NLoop]; |
| 128 |
LEN,IND |
// std::cout << " offset += (index[" << NLoop << "] - bufferedRegionIndex[" << NLoop << "])*offsetTable[" << NLoop << "];" << std::endl; |
| 129 |
|
ImageHelper<NImageDimension, NLoop-1>:: |
| 130 |
IND |
******ComputeOffsetInner(bufferedRegionIndex, |
| 131 |
|
index, |
| 132 |
|
offsetTable, |
| 133 |
|
offset, |
| 134 |
|
Concept::Detail::UniqueType_bool<(NLoop==1)>()); |
| 135 |
|
|
| 136 |
IND |
**} |
| 137 |
|
|
| 138 |
|
inline static void ComputeOffsetInner(const IndexType &bufferedRegionIndex, |
| 139 |
|
const IndexType &index, |
| 140 |
|
const OffsetValueType [], |
| 141 |
|
OffsetValueType &offset, |
| 142 |
|
const UniqueTypeBoolTrue& ) |
| 143 |
IND |
**{ |
| 144 |
|
// Do last |
| 145 |
|
offset = offset + index[0] - bufferedRegionIndex[0]; |
| 146 |
LEN,IND |
// std::cout << " offset += (index[0] - bufferedRegionIndex[0]);" << std::endl; |
| 147 |
IND |
**} |
| 148 |
|
|
| 149 |
|
}; |
| 150 |
|
} // end namespace itk |
| 151 |
|
|
| 152 |
|
#endif |
| 153 |
|
|
| 154 |
EOF |
|