ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMembershipSample.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 __itkMembershipSample_h
00019 #define __itkMembershipSample_h
00020 
00021 #include "itksys/hash_map.hxx"
00022 #include "itkSubsample.h"
00023 
00024 namespace itk
00025 {
00026 namespace Statistics
00027 {
00055 template< class TSample >
00056 class ITK_EXPORT MembershipSample:public DataObject
00057 {
00058 public:
00060   typedef MembershipSample           Self;
00061   typedef DataObject                 Superclass;
00062   typedef SmartPointer< Self >       Pointer;
00063   typedef SmartPointer< const Self > ConstPointer;
00064 
00066   itkTypeMacro(MembershipSample, DataObject);
00067   itkNewMacro(Self);
00069 
00072   typedef TSample                                    SampleType;
00073   typedef typename SampleType::MeasurementVectorType MeasurementVectorType;
00074   typedef typename SampleType::MeasurementType       MeasurementType;
00075   typedef typename SampleType::InstanceIdentifier    InstanceIdentifier;
00076   typedef typename SampleType::ConstPointer          SampleConstPointer;
00077 
00078   typedef typename SampleType::AbsoluteFrequencyType      AbsoluteFrequencyType;
00079   typedef typename SampleType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00080 
00081   typedef IdentifierType ClassLabelType;
00084   typedef std::vector< ClassLabelType > UniqueClassLabelsType;
00085 
00088   typedef itksys::hash_map< InstanceIdentifier, ClassLabelType > ClassLabelHolderType;
00089 
00092   typedef Subsample< SampleType >                ClassSampleType;
00093   typedef typename ClassSampleType::Pointer      ClassSamplePointer;
00094   typedef typename ClassSampleType::ConstPointer ClassSampleConstPointer;
00095 
00097   itkSetConstObjectMacro(Sample, SampleType);
00098   itkGetConstObjectMacro(Sample, SampleType);
00100 
00102   void SetNumberOfClasses(unsigned int numberOfClasses);
00103 
00105   itkGetConstMacro(NumberOfClasses, unsigned int);
00106 
00111   void AddInstance(const ClassLabelType & classLabel, const InstanceIdentifier & id);
00112 
00115   unsigned int GetClassLabel(const InstanceIdentifier & id) const;
00116 
00119   const ClassSampleType * GetClassSample(const ClassLabelType & classLabel) const;
00120 
00123   const ClassLabelHolderType GetClassLabelHolder() const;
00124 
00127   const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier & id) const;
00128 
00131   MeasurementType GetMeasurement(const InstanceIdentifier & id,
00132                                  const unsigned int & dimension);
00133 
00135   AbsoluteFrequencyType GetFrequency(const InstanceIdentifier & id) const;
00136 
00138   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00139 
00141   virtual void Graft(const DataObject *thatObject);
00142 
00143 //  void PrintSelf(std::ostream& os, Indent indent) const;
00144 
00145   class ConstIterator
00146   {
00147     friend class MembershipSample;
00148 public:
00149 
00150     ConstIterator(const Self *sample)
00151     {
00152       *this = sample->Begin();
00153     }
00154 
00155     ConstIterator(const ConstIterator & iter)
00156     {
00157       m_Sample = iter.m_Sample;
00158       m_MembershipSample = iter.m_MembershipSample;
00159       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00160     }
00161 
00162     ConstIterator & operator=(const ConstIterator & iter)
00163     {
00164       m_Sample = iter.m_Sample;
00165       m_MembershipSample = iter.m_MembershipSample;
00166       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00167       return *this;
00168     }
00169 
00170     bool operator!=(const ConstIterator & it)
00171     {
00172       return ( m_InstanceIdentifier != it.m_InstanceIdentifier );
00173     }
00174 
00175     bool operator==(const ConstIterator & it)
00176     {
00177       return ( m_InstanceIdentifier == it.m_InstanceIdentifier );
00178     }
00179 
00180     ConstIterator & operator++()
00181     {
00182       ++m_InstanceIdentifier;
00183       return *this;
00184     }
00185 
00186     AbsoluteFrequencyType GetFrequency() const
00187     {
00188       return m_Sample->GetFrequency(m_InstanceIdentifier);
00189     }
00190 
00191     const MeasurementVectorType & GetMeasurementVector() const
00192     {
00193       return m_Sample->GetMeasurementVector(m_InstanceIdentifier);
00194     }
00195 
00196     InstanceIdentifier GetInstanceIdentifier() const
00197     {
00198       return m_InstanceIdentifier;
00199     }
00200 
00201     unsigned int   GetClassLabel() const
00202     {
00203       return m_MembershipSample->GetClassLabel(m_InstanceIdentifier);
00204     }
00205 
00206 protected:
00207     // Purposely not implemented
00208     ConstIterator();
00209 
00210     // Only to be called from the MembershipSample
00211     ConstIterator(
00212       const Self *memberSample, InstanceIdentifier iid):
00213       m_Sample( memberSample->GetSample() ), m_MembershipSample(memberSample), m_InstanceIdentifier(iid)
00214     {}
00215 
00216     //typename SampleType::ConstIterator m_Iter;
00217     const TSample *         m_Sample;
00218     const MembershipSample *m_MembershipSample;
00219     InstanceIdentifier      m_InstanceIdentifier;
00220   };
00221 
00222   class Iterator:public ConstIterator
00223   {
00224     friend class MembershipSample;
00225 public:
00226 
00227     Iterator(Self *sample):ConstIterator(sample)
00228     {}
00229 
00230     Iterator(const Iterator & iter):ConstIterator(iter)
00231     {}
00232 
00233     Iterator & operator=(const Iterator & iter)
00234     {
00235       this->ConstIterator::operator=(iter);
00236       return *this;
00237     }
00238 
00239 protected:
00240     // To ensure const-correctness these method must not be in the public API.
00241     // The are purposly not implemented, since they should never be called.
00242     Iterator();
00243     Iterator(const Self *sample);
00244     Iterator(const ConstIterator & it);
00245     ConstIterator & operator=(const ConstIterator & it);
00246 
00247     // Only to be called from the MembershipSample
00248     Iterator(Self *memberSample,
00249              InstanceIdentifier iid):
00250       ConstIterator(memberSample, iid)
00251     {}
00252 private:
00253   };
00254 
00257   Iterator Begin()
00258   {
00259     Iterator iter(this, 0);
00260 
00261     return iter;
00262   }
00263 
00266   Iterator  End()
00267   {
00268     Iterator iter( this, m_Sample->Size() );
00269 
00270     return iter;
00271   }
00272 
00273   ConstIterator Begin() const
00274   {
00275     ConstIterator iter(this,  0);
00276 
00277     return iter;
00278   }
00279 
00280   ConstIterator  End()  const
00281   {
00282     ConstIterator iter( this, m_Sample->Size() );
00283 
00284     return iter;
00285   }
00286 
00287 protected:
00288   MembershipSample();
00289   virtual ~MembershipSample() {}
00290   void PrintSelf(std::ostream & os, Indent indent) const;
00291 
00292 private:
00293   MembershipSample(const Self &); //purposely not implemented
00294   void operator=(const Self &);   //purposely not implemented
00295 
00298   int GetInternalClassLabel(const ClassLabelType classLabel) const;
00299 
00300   UniqueClassLabelsType             m_UniqueClassLabels;
00301   ClassLabelHolderType              m_ClassLabelHolder;
00302   std::vector< ClassSamplePointer > m_ClassSamples;
00303   SampleConstPointer                m_Sample;
00304   unsigned int                      m_NumberOfClasses;
00305 };  // end of class
00306 } // end of namespace Statistics
00307 } // end of namespace itk
00308 
00309 #ifndef ITK_MANUAL_INSTANTIATION
00310 #include "itkMembershipSample.hxx"
00311 #endif
00312 
00313 #endif
00314