ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSubsample.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 __itkSubsample_h
00019 #define __itkSubsample_h
00020 
00021 #include "itkSample.h"
00022 #include "itkMacro.h"
00023 #include "itkObjectFactory.h"
00024 
00025 namespace itk
00026 {
00027 namespace Statistics
00028 {
00040 template< class TSample >
00041 class ITK_EXPORT Subsample:
00042   public TSample
00043 {
00044 public:
00046   typedef Subsample                  Self;
00047   typedef TSample                    Superclass;
00048   typedef SmartPointer< Self >       Pointer;
00049   typedef SmartPointer< const Self > ConstPointer;
00050 
00052   itkTypeMacro(Subsample, TSample);
00053 
00055   itkNewMacro(Self);
00056 
00058   typedef typename TSample::Pointer SamplePointer;
00059 
00062   typedef typename TSample::MeasurementVectorType MeasurementVectorType;
00063   typedef typename TSample::MeasurementType       MeasurementType;
00064   typedef typename TSample::InstanceIdentifier    InstanceIdentifier;
00065   typedef MeasurementVectorType                   ValueType;
00066 
00067   typedef typename TSample::AbsoluteFrequencyType      AbsoluteFrequencyType;
00068   typedef typename TSample::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00069 
00074   typedef std::vector< InstanceIdentifier > InstanceIdentifierHolder;
00075 
00077   virtual const InstanceIdentifierHolder & GetIdHolder() const
00078   { \
00079     return this->m_IdHolder;
00080   }
00081 
00083   void SetSample(const TSample *sample);
00084 
00085   const TSample * GetSample() const;
00086 
00088   void InitializeWithAllInstances();
00089 
00091   void AddInstance(InstanceIdentifier id);
00092 
00095   InstanceIdentifier Size() const;
00096 
00098   void Clear();
00099 
00102   const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
00103 
00105   AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
00106 
00108   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00109 
00110   void Swap(unsigned int index1, unsigned int index2);
00111 
00112   InstanceIdentifier GetInstanceIdentifier(unsigned int index);
00113 
00114   const MeasurementVectorType & GetMeasurementVectorByIndex(unsigned int index) const;
00115 
00116   AbsoluteFrequencyType GetFrequencyByIndex(unsigned int index) const;
00117 
00119   virtual void Graft(const DataObject *thatObject);
00120 
00121   class ConstIterator
00122   {
00123     friend class Subsample;
00124 public:
00125 
00126     ConstIterator(const Self *sample)
00127     {
00128       *this = sample->Begin();
00129     }
00130 
00131     ConstIterator(const ConstIterator & iter)
00132     {
00133       m_Iter = iter.m_Iter;
00134       m_Subsample = iter.m_Subsample;
00135       m_Sample = iter.m_Sample;
00136     }
00137 
00138     ConstIterator & operator=(const ConstIterator & iter)
00139     {
00140       m_Iter = iter.m_Iter;
00141       m_Subsample = iter.m_Subsample;
00142       m_Sample = iter.m_Sample;
00143       return *this;
00144     }
00145 
00146     bool operator!=(const ConstIterator & it)
00147     {
00148       return ( m_Iter != it.m_Iter );
00149     }
00150 
00151     bool operator==(const ConstIterator & it)
00152     {
00153       return ( m_Iter == it.m_Iter );
00154     }
00155 
00156     ConstIterator & operator++()
00157     {
00158       ++m_Iter;
00159       return *this;
00160     }
00161 
00162     AbsoluteFrequencyType GetFrequency() const
00163     {
00164       return m_Sample->GetFrequency(*m_Iter);
00165     }
00166 
00167     const MeasurementVectorType & GetMeasurementVector() const
00168     {
00169       return m_Sample->GetMeasurementVector(*m_Iter);
00170     }
00171 
00172     InstanceIdentifier GetInstanceIdentifier() const
00173     {
00174       return ( m_Iter - m_Subsample->GetIdHolder().begin() );
00175     }
00176 
00177 protected:
00178     // Purposely not implemented
00179     ConstIterator();
00180 
00181     // Only to be called from the Subsample
00182     ConstIterator(typename InstanceIdentifierHolder::const_iterator iter,
00183                   const Self *classSample):
00184       m_Iter(iter), m_Subsample(classSample), m_Sample( classSample->GetSample() )
00185     {}
00186 
00187     // ConstIterator pointing to ImageToListSampleAdaptor
00188     typename InstanceIdentifierHolder::const_iterator m_Iter;
00189 
00190     // Pointer to Subsample object
00191     const Self *   m_Subsample;
00192     const TSample *m_Sample;
00193 private:
00194   };
00195 
00196   class Iterator:public ConstIterator
00197   {
00198     friend class Subsample;
00199 public:
00200 
00201     Iterator(Self *sample):ConstIterator(sample)
00202     {}
00203 
00204     Iterator(const Iterator & iter):ConstIterator(iter)
00205     {}
00206 
00207     Iterator & operator=(const Iterator & iter)
00208     {
00209       this->ConstIterator::operator=(iter);
00210       return *this;
00211     }
00212 
00213 protected:
00214     // To ensure const-correctness these method must not be in the public API.
00215     // The are purposly not implemented, since they should never be called.
00216     Iterator();
00217     Iterator(const Self *sample);
00218     Iterator(typename InstanceIdentifierHolder::const_iterator iter,
00219              const Self *classSample);
00220     Iterator(const ConstIterator & it);
00221     ConstIterator & operator=(const ConstIterator & it);
00222 
00223     // Only to be called from the Subsample
00224     Iterator(typename InstanceIdentifierHolder::iterator iter,
00225              Self *classSample):
00226       ConstIterator(iter, classSample)
00227     {}
00228 private:
00229   };
00230 
00233   Iterator Begin()
00234   {
00235     Iterator iter(m_IdHolder.begin(), this);
00236 
00237     return iter;
00238   }
00239 
00242   Iterator  End()
00243   {
00244     Iterator iter(m_IdHolder.end(), this);
00245 
00246     return iter;
00247   }
00248 
00249   ConstIterator Begin() const
00250   {
00251     ConstIterator iter(m_IdHolder.begin(), this);
00252 
00253     return iter;
00254   }
00255 
00256   ConstIterator  End()  const
00257   {
00258     ConstIterator iter(m_IdHolder.end(), this);
00259 
00260     return iter;
00261   }
00262 
00263 protected:
00264   Subsample();
00265   virtual ~Subsample() {}
00266   void PrintSelf(std::ostream & os, Indent indent) const;
00267 
00268 private:
00269   Subsample(const Self &);      //purposely not implemented
00270   void operator=(const Self &); //purposely not implemented
00271 
00272   const TSample *            m_Sample;
00273   InstanceIdentifierHolder   m_IdHolder;
00274   unsigned int               m_ActiveDimension;
00275   TotalAbsoluteFrequencyType m_TotalFrequency;
00276 };  // end of class
00277 } // end of namespace Statistics
00278 } // end of namespace itk
00279 
00280 #ifndef ITK_MANUAL_INSTANTIATION
00281 #include "itkSubsample.hxx"
00282 #endif
00283 
00284 #endif
00285