| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkNeighborhood.txx.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:42 $ |
| 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 _itkNeighborhood_txx |
| 18 |
DEF |
#define _itkNeighborhood_txx |
| 19 |
|
namespace itk { |
| 20 |
|
|
| 21 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 22 |
|
void |
| 23 |
|
Neighborhood<TPixel, VDimension, TContainer> |
| 24 |
|
::ComputeNeighborhoodStrideTable() |
| 25 |
|
{ |
| 26 |
|
unsigned stride, accum; |
| 27 |
|
|
| 28 |
|
for (unsigned int dim = 0; dim < VDimension; ++dim) |
| 29 |
|
{ |
| 30 |
|
stride = 0; |
| 31 |
|
accum = 1; |
| 32 |
|
|
| 33 |
|
for (unsigned int i=0; i<VDimension; ++i) |
| 34 |
|
{ |
| 35 |
|
if (i == dim) stride = accum; |
| 36 |
|
accum *= m_Size[i]; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
m_StrideTable[dim] = stride; |
| 40 |
|
} |
| 41 |
|
} |
| 42 |
|
|
| 43 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 44 |
|
void Neighborhood<TPixel, VDimension, TContainer> |
| 45 |
|
::ComputeNeighborhoodOffsetTable() |
| 46 |
|
{ |
| 47 |
|
m_OffsetTable.clear(); |
| 48 |
|
m_OffsetTable.reserve(this->Size()); |
| 49 |
|
OffsetType o; |
| 50 |
|
unsigned int i, j; |
| 51 |
|
for (j = 0; j < VDimension; j++) |
| 52 |
|
{ |
| 53 |
|
o[j] = -(static_cast<long>(this->GetRadius(j))); |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
for (i = 0; i < this->Size(); ++i) |
| 57 |
|
{ |
| 58 |
|
m_OffsetTable.push_back(o); |
| 59 |
|
for (j= 0; j< VDimension; j++) |
| 60 |
|
{ |
| 61 |
|
o[j] = o[j] + 1; |
| 62 |
|
if (o[j] > static_cast<long>(this->GetRadius(j))) |
| 63 |
|
{ |
| 64 |
|
o[j] = -(static_cast<long>(this->GetRadius(j))); |
| 65 |
|
} |
| 66 |
|
else break; |
| 67 |
|
} |
| 68 |
|
} |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 72 |
|
void |
| 73 |
|
Neighborhood<TPixel, VDimension, TContainer> |
| 74 |
|
::SetRadius(const unsigned long s) |
| 75 |
|
{ |
| 76 |
|
SizeType k; |
| 77 |
|
for (unsigned int i = 0; i< VDimension; i++) |
| 78 |
|
{ |
| 79 |
|
k[i] = s; |
| 80 |
|
} |
| 81 |
|
this->SetRadius(k); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 85 |
|
void |
| 86 |
|
Neighborhood<TPixel, VDimension, TContainer> |
| 87 |
|
::SetRadius(const SizeType &r) |
| 88 |
|
{ |
| 89 |
|
memcpy(m_Radius.m_Size, r.m_Size, sizeof(const unsigned long)*VDimension); |
| 90 |
|
this->SetSize(); |
| 91 |
|
|
| 92 |
|
unsigned int cumul=1; |
| 93 |
|
for (unsigned int i = 0; i<VDimension; i++) |
| 94 |
|
{ |
| 95 |
|
cumul*=m_Size[i]; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
this->Allocate(cumul); |
| 99 |
|
this->ComputeNeighborhoodStrideTable(); |
| 100 |
|
this->ComputeNeighborhoodOffsetTable(); |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 104 |
|
Neighborhood<TPixel, VDimension, TContainer> |
| 105 |
|
::Neighborhood(const Self &other) |
| 106 |
|
{ |
| 107 |
|
m_Radius = other.m_Radius; |
| 108 |
|
m_Size = other.m_Size; |
| 109 |
|
m_DataBuffer = other.m_DataBuffer; |
| 110 |
LEN |
::memcpy(m_StrideTable, other.m_StrideTable, sizeof(unsigned int) * VDimension); |
| 111 |
|
m_OffsetTable = other.m_OffsetTable; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 116 |
|
Neighborhood<TPixel, VDimension, TContainer> & |
| 117 |
|
Neighborhood<TPixel, VDimension, TContainer> |
| 118 |
|
::operator=(const Self &other) |
| 119 |
|
{ |
| 120 |
|
m_Radius = other.m_Radius; |
| 121 |
|
m_Size = other.m_Size; |
| 122 |
|
m_DataBuffer = other.m_DataBuffer; |
| 123 |
LEN |
::memcpy(m_StrideTable, other.m_StrideTable, sizeof(unsigned int) * VDimension); |
| 124 |
|
m_OffsetTable = other.m_OffsetTable; |
| 125 |
|
return *this; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
|
| 129 |
EML |
|
| 130 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 131 |
|
std::slice Neighborhood<TPixel, VDimension, TContainer> |
| 132 |
|
::GetSlice(unsigned int d) const |
| 133 |
|
{ |
| 134 |
|
unsigned int n = this->Size()/2; |
| 135 |
|
n = n - ( static_cast<unsigned>(this->GetStride(d)) |
| 136 |
IND |
************* (static_cast<unsigned>(this->GetSize()[d] / 2) ) ); |
| 137 |
|
|
| 138 |
|
return std::slice(static_cast<size_t>(n), |
| 139 |
IND |
********************static_cast<size_t>(this->GetSize()[d]), |
| 140 |
IND |
********************static_cast<size_t>(this->GetStride(d)) ); |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 144 |
|
unsigned int Neighborhood<TPixel, VDimension, TContainer> |
| 145 |
|
::GetNeighborhoodIndex(const OffsetType &o) const |
| 146 |
|
{ |
| 147 |
|
unsigned int idx = (this->Size()/2); |
| 148 |
|
for (unsigned i = 0; i < VDimension; ++i) |
| 149 |
|
{ idx+=o[i] * static_cast<long>(m_StrideTable[i]); } |
| 150 |
|
return idx; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
template<class TPixel, unsigned int VDimension, class TContainer> |
| 155 |
|
void Neighborhood<TPixel, VDimension, TContainer> |
| 156 |
|
::PrintSelf(std::ostream &os, Indent indent) const |
| 157 |
|
{ |
| 158 |
|
unsigned int i; |
| 159 |
|
|
| 160 |
|
os << indent << "m_Size: [ "; |
| 161 |
|
for (i=0; i<VDimension; ++i) os << m_Size[i] << " "; |
| 162 |
|
os << "]" << std::endl; |
| 163 |
|
|
| 164 |
|
os << indent << "m_Radius: [ "; |
| 165 |
|
for (i=0; i<VDimension; ++i) os << m_Radius[i] << " "; |
| 166 |
|
os << "]" << std::endl; |
| 167 |
|
|
| 168 |
|
os << indent << "m_StrideTable: [ "; |
| 169 |
|
for (i=0; i<VDimension; ++i) os << m_StrideTable[i] << " "; |
| 170 |
|
os << "]" << std::endl; |
| 171 |
|
|
| 172 |
|
os << indent << "m_OffsetTable: [ "; |
| 173 |
|
for (i=0; i< m_OffsetTable.size(); ++i) os << m_OffsetTable[i] << " "; |
| 174 |
|
os << "]" << std::endl; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
} // namespace itk |
| 178 |
|
|
| 179 |
|
#endif |
| 180 |
|
|