00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
#ifndef __itkNeighborhood_h
00018
#define __itkNeighborhood_h
00019
00020
#include <iostream>
00021
#include "itkNeighborhoodAllocator.h"
00022
#include "itkSize.h"
00023
#include "itkIndent.h"
00024
#include "itkSliceIterator.h"
00025
#include "vnl/vnl_vector.h"
00026
#include "itkOffset.h"
00027
#include <vector>
00028
00029
namespace itk {
00030
00052
template<
class TPixel,
unsigned int VDimension = 2,
00053
class TAllocator = NeighborhoodAllocator<TPixel> >
00054 class ITK_EXPORT Neighborhood
00055 {
00056
public:
00058 typedef Neighborhood
Self;
00059
00061 typedef TAllocator
AllocatorType;
00062
00064
itkStaticConstMacro(NeighborhoodDimension,
unsigned int, VDimension);
00065
00067 typedef TPixel
PixelType;
00068
00072 typedef typename AllocatorType::iterator
Iterator;
00073 typedef typename AllocatorType::const_iterator
ConstIterator;
00074
00076 typedef Size<VDimension> SizeType;
00077 typedef typename SizeType::SizeValueType
SizeValueType;
00078
00080 typedef Size<VDimension> RadiusType;
00081
00083 typedef Offset<VDimension> OffsetType;
00084
00086 typedef SliceIterator<TPixel, Self> SliceIteratorType;
00087
00089 Neighborhood() { m_Radius.Fill(0); m_Size.Fill(0); }
00090
00092 virtual ~Neighborhood() {}
00093
00095 Neighborhood(
const Self& other);
00096
00098 Self &operator=(
const Self& other);
00099
00101
bool
00102 operator==(
const Self& other)
const
00103
{
00104
return (m_Radius == other.
m_Radius &&
00105 m_Size == other.
m_Size &&
00106 m_DataBuffer == other.
m_DataBuffer);
00107 }
00108
00110 bool operator!=(
const Self& other)
const
00111
{
00112
return (m_Radius != other.
m_Radius ||
00113 m_Size != other.
m_Size ||
00114 m_DataBuffer != other.
m_DataBuffer);
00115 }
00116
00118 const SizeType GetRadius()
const
00119
{
return m_Radius; }
00120
00123 unsigned long GetRadius(
const unsigned long n)
const
00124
{
return m_Radius[n]; }
00125
00128 unsigned long GetSize(
const unsigned long n)
const
00129
{
return m_Size[n]; }
00130
00132 SizeType GetSize()
const
00133
{
return m_Size; }
00134
00138 unsigned GetStride(
const unsigned axis)
const
00139
{
return m_StrideTable[axis]; }
00140
00142 Iterator End()
00143 {
return m_DataBuffer.end(); }
00144 Iterator Begin()
00145 {
return m_DataBuffer.begin(); }
00146 ConstIterator End()
const
00147
{
return m_DataBuffer.end(); }
00148 ConstIterator Begin()
const
00149
{
return m_DataBuffer.begin(); }
00150
00152
unsigned int Size()
const
00153 {
return m_DataBuffer.size(); }
00154
00156 TPixel &operator[](
unsigned int i)
00157 {
return m_DataBuffer[i]; }
00158
const TPixel &operator[](
unsigned int i)
const
00159 {
return m_DataBuffer[i]; }
00160 TPixel &GetElement(
unsigned int i)
00161 {
return m_DataBuffer[i]; }
00162
00164 TPixel GetCenterValue()
const
00165
{
return (this->operator[]((this->Size())>>1)); }
00166
00169
void SetRadius(
const SizeType &);
00170
00173
void SetRadius(
const unsigned long *rad)
00174 {
00175 SizeType s;
00176 memcpy(s.
m_Size, rad,
sizeof(
unsigned long) * VDimension);
00177 this->SetRadius(s);
00178 }
00179
00183
void SetRadius(
const unsigned long);
00184
00186
void Print(std::ostream& os)
const
00187
{ this->
PrintSelf(os,
Indent(0)); }
00188
00190 AllocatorType &GetBufferReference()
00191 {
return m_DataBuffer; }
00192
const AllocatorType &GetBufferReference()
const
00193 {
return m_DataBuffer; }
00194
00196 TPixel &operator[](
const OffsetType &o)
00197 {
return this->operator[](this->GetNeighborhoodIndex(o)); }
00198
const TPixel &operator[](
const OffsetType &o)
const
00199
{
return this->operator[](this->GetNeighborhoodIndex(o)); }
00200
00203
virtual OffsetType GetOffset(
unsigned int i)
const
00204
{
return m_OffsetTable[i]; }
00205
00206
virtual unsigned int GetNeighborhoodIndex(
const OffsetType &) const;
00207
00208 unsigned int GetCenterNeighborhoodIndex()
const
00209
{
return static_cast<unsigned int>(this->
Size()/2); }
00210
00211 std::slice GetSlice(
unsigned int)
const;
00212
00213 protected:
00215
void SetSize()
00216 {
00217
for (
unsigned int i=0; i<VDimension; ++i)
00218 { m_Size[i] = m_Radius[i]*2+1; }
00219 }
00220
00222
virtual void Allocate(
unsigned int i)
00223 { m_DataBuffer.resize(i); }
00224
00226
virtual void PrintSelf(std::ostream&,
Indent) const;
00227
00229 virtual
void ComputeNeighborhoodStrideTable();
00230
00233 virtual
void ComputeNeighborhoodOffsetTable();
00234
00235 private:
00238 SizeType m_Radius;
00239
00242 SizeType m_Size;
00243
00245 AllocatorType m_DataBuffer;
00246
00249
unsigned int m_StrideTable[VDimension];
00250
00252 std::
vector<OffsetType> m_OffsetTable;
00253
00254 };
00255
00256 template <class TPixel,
unsigned int VDimension, class TContainer>
00257 std::ostream & operator<<(std::ostream &os, const Neighborhood<TPixel,VDimension,TContainer> &neighborhood)
00258 {
00259 os <<
"Neighborhood:" << std::endl;
00260 os <<
" Radius:" << neighborhood.GetRadius() << std::endl;
00261 os <<
" Size:" << neighborhood.GetSize() << std::endl;
00262 os <<
" DataBuffer:" << neighborhood.GetBufferReference() << std::endl;
00263
00264
return os;
00265 }
00266
00267 }
00268
00269
#ifndef ITK_MANUAL_INSTANTIATION
00270
#include "itkNeighborhood.txx"
00271
#endif
00272
00273
#endif