ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSparseFieldLayer.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSparseFieldLayer_h
00019 #define __itkSparseFieldLayer_h
00020 
00021 #include "itkObjectFactory.h"
00022 #include "itkObject.h"
00023 #include <vector>
00024 
00025 namespace itk
00026 {
00035 template< class TNodeType >
00036 class ConstSparseFieldLayerIterator
00037 {
00038 public:
00039   const TNodeType & operator*() const
00040   { return *m_Pointer; }
00041 
00042   const TNodeType * operator->() const
00043   { return m_Pointer; }
00044 
00045   const TNodeType * GetPointer() const
00046   { return m_Pointer; }
00047 
00048   bool operator==(const ConstSparseFieldLayerIterator o) const
00049   {
00050     if ( m_Pointer == o.m_Pointer ) { return true; }
00051     else { return false; }
00052   }
00053 
00054   bool operator!=(const ConstSparseFieldLayerIterator o) const
00055   {
00056     if ( m_Pointer != o.m_Pointer ) { return true; }
00057     else { return false; }
00058   }
00059 
00060   ConstSparseFieldLayerIterator & operator++()
00061   {
00062     m_Pointer = m_Pointer->Next;
00063     return *this;
00064   }
00065 
00066   ConstSparseFieldLayerIterator & operator--()
00067   {
00068     m_Pointer = m_Pointer->Previous;
00069     return *this;
00070   }
00071 
00072   ConstSparseFieldLayerIterator()
00073   { m_Pointer = 0; }
00074 
00075   ConstSparseFieldLayerIterator(TNodeType *p)
00076   { m_Pointer = p; }
00077 
00078   ~ConstSparseFieldLayerIterator() {}
00079 protected:
00080   TNodeType *m_Pointer;
00081 };
00082 
00087 template< class TNodeType >
00088 class SparseFieldLayerIterator:
00089   public ConstSparseFieldLayerIterator< TNodeType >
00090 {
00091 public:
00092   typedef ConstSparseFieldLayerIterator< TNodeType > Superclass;
00093 
00094   SparseFieldLayerIterator():Superclass()
00095   {}
00096 
00097   SparseFieldLayerIterator(TNodeType *p):Superclass(p)
00098   {}
00099 
00100   TNodeType & operator*()
00101   { return *this->m_Pointer; }
00102 
00103   TNodeType * operator->()
00104   { return this->m_Pointer; }
00105 
00106   TNodeType * GetPointer()
00107   { return this->m_Pointer; }
00108 
00109   SparseFieldLayerIterator & operator++()
00110   {
00111     this->m_Pointer = this->m_Pointer->Next;
00112     return *this;
00113   }
00114 
00115   SparseFieldLayerIterator & operator--()
00116   {
00117     this->m_Pointer = this->m_Pointer->Previous;
00118     return *this;
00119   }
00120 
00121   SparseFieldLayerIterator & operator=(Superclass & sc)
00122   {
00123     this->m_Pointer = const_cast< TNodeType * >( sc.GetPointer() );
00124     return *this;
00125   }
00126 };
00127 
00150 template< class TNodeType >
00151 class ITK_EXPORT SparseFieldLayer:
00152   public Object
00153 {
00154 public:
00156   typedef SparseFieldLayer           Self;
00157   typedef Object                     Superclass;
00158   typedef SmartPointer< Self >       Pointer;
00159   typedef SmartPointer< const Self > ConstPointer;
00160 
00162   itkNewMacro(Self);
00163 
00165   itkTypeMacro(SparseFieldLayer, Object);
00166 
00168   typedef TNodeType NodeType;
00169 
00172   typedef NodeType ValueType;
00173 
00175   typedef SparseFieldLayerIterator< NodeType > Iterator;
00176 
00178   typedef ConstSparseFieldLayerIterator< NodeType > ConstIterator;
00179 
00181   struct RegionType {
00182     ConstIterator first;
00183     ConstIterator last;  // this is one past the actual last element
00184   };
00185 
00186   typedef std::vector< RegionType > RegionListType;
00187 
00190   NodeType * Front()
00191   { return m_HeadNode->Next; }
00192 
00194   const NodeType * Front() const
00195   { return m_HeadNode->Next; }
00196 
00198   void PopFront()
00199   {
00200     m_HeadNode->Next = m_HeadNode->Next->Next;
00201     m_HeadNode->Next->Previous = m_HeadNode;
00202     m_Size -= 1;
00203   }
00204 
00206   void PushFront(NodeType *n)
00207   {
00208     n->Next = m_HeadNode->Next;
00209     n->Previous = m_HeadNode;
00210     m_HeadNode->Next->Previous = n;
00211     m_HeadNode->Next = n;
00212     m_Size += 1;
00213   }
00214 
00216   void Unlink(NodeType *n)
00217   {
00218     n->Previous->Next = n->Next;
00219     n->Next->Previous = n->Previous;
00220     m_Size -= 1;
00221   }
00222 
00224   Iterator Begin()
00225   { return Iterator(m_HeadNode->Next); }
00226 
00229   ConstIterator Begin() const
00230   { return ConstIterator(m_HeadNode->Next); }
00231 
00233   Iterator End()
00234   { return Iterator(m_HeadNode); }
00235 
00237   ConstIterator End() const
00238   { return ConstIterator(m_HeadNode); }
00239 
00242   bool Empty() const
00243   {
00244     if ( m_HeadNode->Next == m_HeadNode ) { return true; }
00245     else { return false; }
00246   }
00248 
00251   unsigned int Size() const;
00252 
00255   RegionListType SplitRegions(int num) const;
00256 
00257 protected:
00258   SparseFieldLayer();
00259   ~SparseFieldLayer();
00260   virtual void PrintSelf(std::ostream & os, Indent indent) const;
00261 
00262 private:
00263   SparseFieldLayer(const Self &);    //purposely not implemented
00264   void operator=(const Self &);      //purposely not implemented
00265 
00268   NodeType *   m_HeadNode;
00269   unsigned int m_Size;
00270 };
00271 } // end namespace itk
00272 
00273 #ifndef ITK_MANUAL_INSTANTIATION
00274 #include "itkSparseFieldLayer.hxx"
00275 #endif
00276 
00277 #endif
00278