ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSample.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 __itkSample_h
00019 #define __itkSample_h
00020 
00021 #include "itkPoint.h"
00022 #include "itkDataObject.h"
00023 #include "itkMeasurementVectorTraits.h"
00024 #include <vector> // for the size_type declaration
00025 
00026 namespace itk
00027 {
00028 namespace Statistics
00029 {
00060 template< class TMeasurementVector >
00061 class ITK_EXPORT Sample:public DataObject
00062 {
00063 public:
00065   typedef Sample                     Self;
00066   typedef DataObject                 Superclass;
00067   typedef SmartPointer< Self >       Pointer;
00068   typedef SmartPointer< const Self > ConstPointer;
00069 
00071   itkTypeMacro(Sample, DataObject);
00072 
00074   typedef TMeasurementVector MeasurementVectorType;
00075 
00078   typedef typename MeasurementVectorTraitsTypes<
00079     MeasurementVectorType >::ValueType MeasurementType;
00080 
00082   typedef MeasurementVectorTraits::AbsoluteFrequencyType AbsoluteFrequencyType;
00083 
00085   typedef NumericTraits< AbsoluteFrequencyType >::AccumulateType TotalAbsoluteFrequencyType;
00086 
00089   typedef typename MeasurementVectorTraits::InstanceIdentifier InstanceIdentifier;
00090 
00092   typedef unsigned int MeasurementVectorSizeType;
00093 
00095   virtual InstanceIdentifier Size() const = 0;
00096 
00099   virtual const MeasurementVectorType &
00100   GetMeasurementVector(InstanceIdentifier id) const = 0;
00101 
00104   virtual AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const = 0;
00105 
00107   virtual TotalAbsoluteFrequencyType GetTotalFrequency() const = 0;
00108 
00110   virtual void SetMeasurementVectorSize(MeasurementVectorSizeType s)
00111   {
00112     // Test whether the vector type is resizable or not
00113     MeasurementVectorType m;
00114 
00115     if ( MeasurementVectorTraits::IsResizable(m) )
00116       {
00117       // then this is a resizable vector type
00118       //
00119       // if the new size is the same as the previou size, just return
00120       if ( s == this->m_MeasurementVectorSize )
00121         {
00122         return;
00123         }
00124       else
00125         {
00126         // If the new size is different from the current size, then
00127         // only change the measurement vector size if the container is empty.
00128         if ( this->Size() )
00129           {
00130           itkExceptionMacro("Attempting to change the measurement \
00131           vector size of a non-empty Sample");
00132           }
00133         else
00134           {
00135           this->m_MeasurementVectorSize = s;
00136           this->Modified();
00137           }
00138         }
00139       }
00140     else
00141       {
00142       // If this is a non-resizable vector type
00143       MeasurementVectorType     m3;
00144       MeasurementVectorSizeType defaultLength =
00145         NumericTraits<MeasurementVectorType>::GetLength(m3);
00146       // and the new length is different from the default one, then throw an
00147       // exception
00148       if ( defaultLength != s )
00149         {
00150         itkExceptionMacro(
00151           "Attempting to change the measurement \
00152                            vector size of a non-resizable vector type"                                                  );
00153         }
00154       }
00155   }
00156 
00158   itkGetConstMacro(MeasurementVectorSize, MeasurementVectorSizeType);
00159 
00161   virtual void Graft(const DataObject *thatObject)
00162   {
00163     this->Superclass::Graft(thatObject);
00164 
00165     const Self *thatConst = dynamic_cast< const Self * >( thatObject );
00166     if ( thatConst )
00167       {
00168       this->SetMeasurementVectorSize( thatConst->GetMeasurementVectorSize() );
00169       }
00170   }
00171 
00172 protected:
00173   Sample()
00174   {
00175     m_MeasurementVectorSize = NumericTraits<MeasurementVectorType>::GetLength(
00176       MeasurementVectorType() );
00177   }
00178 
00179   virtual ~Sample() {}
00180 
00181   void PrintSelf(std::ostream & os, Indent indent) const
00182   {
00183     Superclass::PrintSelf(os, indent);
00184     os << indent << "Length of measurement vectors in the sample: "
00185        << m_MeasurementVectorSize << std::endl;
00186   }
00187 
00188 private:
00189   Sample(const Self &);         //purposely not implemented
00190   void operator=(const Self &); //purposely not implemented
00191 
00192   MeasurementVectorSizeType m_MeasurementVectorSize;
00193 };  // end of class
00194 } // end of namespace Statistics
00195 } // end of namespace itk
00196 
00197 #endif
00198