frePointSetMediaController.h

Go to the documentation of this file.
00001 /*=========================================================================
00002  
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: frePointSetMediaController.h,v $
00007   Language:  C++
00008  
00009  
00010  
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016  
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020  
00021 =========================================================================*/
00022 #ifndef __frePointSetMediaController_h
00023 #define __frePointSetMediaController_h
00024 
00025 #include "freStoreableMediaControllerBase.h"
00026 #include "frePointSetToXMLStreamObject.h"
00027 #include "freTransformFunctionMedia.h"
00028 
00029 #include "itkPointSet.h"
00030 
00031 #include "itkIterativeInverseDeformationFieldImageFilter.h"
00032 
00033 namespace FREE
00034 {
00035 
00041 freControllerIDMacro(PointSetMediaControllerBase, "PointSetMediaBase");
00042 template <class TPixel, unsigned int VDimension>
00043 class PointSetMediaControllerBase : public StoreableMediaControllerBase< itk::PointSet< TPixel, VDimension, itk::DefaultStaticMeshTraits<TPixel, VDimension, VDimension, ScalarType> > >
00044 {
00045 public:
00046     typedef itk::PointSet< TPixel, VDimension, itk::DefaultStaticMeshTraits<TPixel, VDimension, VDimension, ScalarType> > ComponentType;
00047     typedef ComponentType ImageType;
00048     typedef StoreableMediaControllerBase<ComponentType> Superclass;
00049     typedef typename Superclass::ComponentPointer ComponentPointer;
00050     typedef typename Superclass::GenericComponentType GenericComponentType;
00051     typedef typename Superclass::GenericComponentPointer GenericComponentPointer;
00052     typedef typename Superclass::GenericMediaPointer GenericMediaPointer;
00053     
00054     itkTypeMacro(PointSetMediaControllerBase, StoreableMediaControllerBase);
00055 
00056     DeclareParameterMacro(Points);
00057     DeclareParameterMacro(Values);
00058     DeclareParameterMacro(PointsSource);
00059 
00060     PointSetMediaControllerBase()
00061     {
00062         //Profile settings
00063         this->UpdateControllerID(ControllerID::PointSetMediaControllerBase);
00064         this->m_Description = "Base class for media containing point sets.";
00065     };
00066 
00067     virtual ~PointSetMediaControllerBase()
00068     {}
00069     ;
00070 
00071     virtual GenericMediaPointer ComputeActualizedMedia(GenericMediaType* pGenericMedia,
00072                                                         GenericMediaType* pGenericTransformationField) const
00073     {
00074       if (!pGenericMedia) throwCtrlExceptionMacro("","Passed media is NULL.");
00075       if (!pGenericTransformationField) throwCtrlExceptionMacro("","Passed transform field is NULL.");
00076 
00077 
00078       ComponentPointer smpCurrentPointSet;
00079       try
00080       {
00081         smpCurrentPointSet = dynamic_cast<ComponentType*>(pGenericMedia);
00082       }
00083       catchAllNPassMacro("Error. Media that should be actualized seems not to be of correct type.");
00084       if (smpCurrentPointSet.IsNull()) throwCtrlExceptionMacro("","Media has wrong type. Cannot be casted.");
00085 
00086       typedef typename ImageTypes<VDimension>::TransformationFieldType TransformationFieldType;
00087       typename TransformationFieldType::Pointer smpTransformField = static_cast< TransformationFieldType*>(pGenericTransformationField);
00088 
00089       //map Pointset
00090       ComponentPointer smpActualizedPointSet = ComponentType::New();
00091       typedef typename ComponentType::PointsContainer::Iterator PointDataIterator;
00092 
00093       PointDataIterator pointIter = smpCurrentPointSet->GetPoints()->Begin();
00094       PointDataIterator pointEnd = smpCurrentPointSet->GetPoints()->End();
00095 
00096       long lPointID = 0;
00097       while(pointIter != pointEnd)
00098       {
00099           typename ComponentType::PointType currentPoint = pointIter.Value();
00100           typename TransformationFieldType::PixelType translation;
00101           translation.Fill(0.0);
00102 
00103           typename TransformationFieldType::IndexType index;
00104           if (smpTransformField->TransformPhysicalPointToIndex(currentPoint, index))
00105           {
00106               translation = smpTransformField->GetPixel(index);
00107           }
00108 
00109           currentPoint = currentPoint+translation;
00110           smpActualizedPointSet->SetPoint(lPointID++, currentPoint);
00111           ++pointIter;
00112       }
00113 
00114       GenericMediaPointer smpResult = smpActualizedPointSet.GetPointer();
00115       return smpResult;
00116     };
00117 
00118   virtual GenericMediaPointer ComputeActualizedMediaByFunction(GenericMediaType* pGenericMedia,
00119                                                                GenericMediaType* pGenericTransformationFunction) const
00120     {
00121       if (!pGenericMedia) throwCtrlExceptionMacro("","Passed media is NULL.");
00122       if (!pGenericTransformationFunction) throwCtrlExceptionMacro("","Passed transform function is NULL.");
00123 
00124       ComponentPointer smpCurrentPointSet;
00125       try
00126       {
00127         smpCurrentPointSet = dynamic_cast<ComponentType*>(pGenericMedia);
00128       }
00129       catchAllNPassMacro("Error. Media that should be actualized seems not to be of correct type.");
00130       if (smpCurrentPointSet.IsNull()) throwCtrlExceptionMacro("","Media has wrong type. Cannot be casted.");
00131 
00132       typedef TransformFunctionMedia<ScalarType, VDimension, VDimension> TransformationFunctionType;
00133       typename TransformationFunctionType::Pointer smpTransformFunction = dynamic_cast< TransformationFunctionType*>(pGenericTransformationFunction);
00134       if (smpTransformFunction.IsNull()) throwCtrlExceptionMacro("","Transformation function was passed, but seems to be wrong type.");
00135 
00136       //transform Pointset
00137       ComponentPointer smpActualizedPointSet = ComponentType::New();
00138       typedef typename ComponentType::PointsContainer::Iterator PointDataIterator;
00139 
00140       PointDataIterator pointIter = smpCurrentPointSet->GetPoints()->Begin();
00141       PointDataIterator pointEnd = smpCurrentPointSet->GetPoints()->End();
00142 
00143       long lPointID = 0;
00144       while(pointIter != pointEnd)
00145       {
00146           typename ComponentType::PointType currentPoint = pointIter.Value();
00147 
00148           typename ComponentType::PointType transformedPoint = smpTransformFunction->GetTransform()->TransformPoint(currentPoint);
00149 
00150           smpActualizedPointSet->SetPoint(lPointID++, transformedPoint);
00151           ++pointIter;
00152       }
00153 
00154       GenericMediaPointer smpResult = smpActualizedPointSet.GetPointer();
00155       return smpResult;
00156     };
00157 
00158     virtual bool SaveMedia(std::string sMediaPath, GenericMediaType* pMedia) const
00159     {
00160       ComponentPointer smpPointSet;
00161       try
00162       {
00163         smpPointSet = static_cast<ComponentType*>(pMedia);
00164       }
00165       catchAllNPassMacro("Error. Media that should be actualized seems not to be of correct type.");
00166       if (smpPointSet.IsNull()) throwCtrlExceptionMacro("","Media has wrong type. Cannot be casted.");
00167 
00168       PointSetToXMLStreamObject<ComponentType> xmlPointSet;
00169       xmlPointSet.SetPointSet(smpPointSet);
00170       xmlPointSet.SaveToFile(sMediaPath);
00171 
00172       return true;
00173     };
00174 
00175     virtual bool SaveMedia(SessionComponentCache* pCache) const
00176     {
00177         if (!pCache->SetupIsAssigned())
00178             throwCtrlExceptionMacro("","Error. Setup is missing in cache. Point set stores media in component setup");
00179         if (!pCache->ComponentIsAssigned())
00180             throwCtrlExceptionMacro("","Error. Point set is missing in cache.");
00181 
00182         ComponentPointer smpPointSet = 0;
00183 
00184         try
00185         {
00186           smpPointSet = dynamic_cast<ComponentType*>(DirectSessionComponentAccessor::GetComponent(pCache));
00187         }
00188         catchAllNPassMacro("Error; component in cache wasn't a legal point set. IDPath: "<<pCache->GetIDPath().ToStr());
00189         if (smpPointSet.IsNull()) throwCtrlExceptionMacro("","Error; component cache wasn't a legal point set. IDPath: "<<pCache->GetIDPath().ToStr());
00190 
00191         int iPointSource = 0;
00192 
00193         Parameter::Pointer smpSources = pCache->Setup()->Parameters().GetParameter(cParam_PointsSource);
00194         if (smpSources.IsNotNull()) 
00195         {
00196           smpSources->GetParameterValue(iPointSource);
00197         }
00198 
00199         if (iPointSource)
00200         {
00201           std::string sMediaFile;
00202           typename Parameter::Pointer smpFileParameter = pCache->Setup()->Parameters().GetParameter(Superclass::cParam_MediaFile);
00203           if (smpFileParameter.IsNull())
00204             throwCtrlExceptionMacro("", "Missing parameter" << Superclass::cParam_MediaFile);
00205 
00206           smpFileParameter->GetParameterValue(sMediaFile);
00207 
00208           PointSetToXMLStreamObject<ComponentType> xmlPointSet;
00209           xmlPointSet.SetPointSet(smpPointSet);
00210           xmlPointSet.SaveToFile(sMediaFile);
00211         }
00212         else
00213         {
00214           SaveSetToSetup(*(pCache->Setup()),*(smpPointSet.GetPointer()));
00215         }
00216 
00217         return true; //point set is saved to setup
00218     };
00219 
00220     virtual bool LoadMedia(SessionComponentCache* pCache) const
00221     {
00222         if (!pCache) throwCtrlExceptionMacro("", "Cannot load media. Passed session cache is NULL.");
00223 
00224         if (!pCache->SetupIsAssigned()) throwCtrlExceptionMacro("", "Cannot load media. Passed session cache has no setup.");
00225 
00226         this->NotifyProgress(1,"Loading point set media",pCache);
00227 
00228         ComponentSetup* pComponentSetup = pCache->Setup();
00229 
00230         ComponentPointer smpPointSet = ComponentType::New();
00231         smpPointSet->SetPointData(ComponentType::PointDataContainer::New());
00232         //needed because in itk 2.4 point set does not generate the value container automaticaly
00233 
00234         int iPointSource = 0;
00235 
00236         Parameter::Pointer smpSources = pComponentSetup->Parameters().GetParameter(cParam_PointsSource);
00237         if (smpSources.IsNotNull()) 
00238         {
00239           smpSources->GetParameterValue(iPointSource);
00240         }
00241 
00242         if (iPointSource)
00243         {
00244           std::string sMediaFile;
00245           typename Parameter::Pointer smpFileParameter = pComponentSetup->Parameters().GetParameter(Superclass::cParam_MediaFile);
00246           if (smpFileParameter.IsNull())
00247             throwCtrlExceptionMacro("", "Missing parameter" << Superclass::cParam_MediaFile);
00248 
00249           smpFileParameter->GetParameterValue(sMediaFile);
00250 
00251           PointSetToXMLStreamObject<ComponentType> xmlPointSet;
00252           xmlPointSet.SetPointSet(smpPointSet);
00253           xmlPointSet.LoadFromFile(sMediaFile);
00254         }
00255         else
00256         {
00257           LoadSetFromSetup(*pComponentSetup,*(smpPointSet.GetPointer()));
00258         }
00259 
00260         DirectSessionComponentAccessor::SetComponent(smpPointSet,pCache);
00261         this->ActualizeMediaValidityTag("media",pCache);          
00262 
00263         //add the loaded flag, because the media is kept in memory
00264         DirectSessionComponentAccessor::SetRepositoryElement("LoadedFlag",itk::LightObject::New(),pCache);
00265 
00266         this->NotifyProgress(1,"Loading finished",pCache);
00267 
00268         return true;
00269     };
00270 
00271     virtual unsigned long GetMediaDimension() const
00272     {
00273         return VDimension;
00274     };
00275 
00276     virtual ComponentPointer GetComponent(const SessionComponentCache* pComponentCache) const
00277     {
00278       ComponentPointer smpMedia;
00279       
00280       //get loading type
00281       int iPointSource;
00282       
00283       try
00284       {
00285         SessionAccessor::GetParameterValue(pComponentCache,cParam_PointsSource,iPointSource);
00286       }
00287       catchAllNPassMacro("Error while retrieving loading type.");
00288       
00289       //The value of the flag isn't important, only its presence or absence
00290       GenericComponentType* pLoadedFlag = DirectSessionComponentAccessor::GetRepositoryElement("LoadedFlag",pComponentCache);
00291 
00292       if ((!pLoadedFlag) && (iPointSource == 2))
00293       { //there is no flag indicating that the point set was loaded
00294         //and it should be loaded on first demand.
00295         //So load now and add to the cache, therefore
00296         //exceptionally cast away const.
00297         SessionComponentCache* pNonConstComponentCache = const_cast<SessionComponentCache*>(pComponentCache);
00298 
00299         if (!LoadMedia(pNonConstComponentCache)) throwCtrlExceptionMacro("", "Unable to load image media.");
00300 
00301         try
00302         {
00303           smpMedia = dynamic_cast<ComponentType*>(DirectSessionComponentAccessor::GetComponent(pNonConstComponentCache));
00304         }
00305         catchAllNPassMacro("Error; component loaded wasn't the right media type.");
00306         if (smpMedia.IsNull()) throwCtrlExceptionMacro("","Error; component loaded wasn't the right media type.");
00307 
00308         //add the loaded flag, because the media is kept in memory
00309         DirectSessionComponentAccessor::SetRepositoryElement("LoadedFlag",itk::LightObject::New(),pNonConstComponentCache);
00310 
00311         return smpMedia;
00312       }
00313       
00314       return Superclass::GetComponent(pComponentCache);
00315     }; 
00316 
00317 protected:
00318     virtual void GenerateProfile(CtrlProfile::ControllerProfile& profile,
00319                                  const SessionComponentCache* pComponentCache,
00320                                  bool bRegardOldSetup) const
00321     {
00322       Superclass::GenerateProfile(profile, pComponentCache, bRegardOldSetup);
00323 
00324       //Parameters
00325       profile.Parameters().AddParameter(cParam_Points,Parameter::PVTDouble,cParamDsc_Points,VDimension,"0.0",-1);
00326       profile.Parameters().AddParameter(cParam_Values,Parameter::PVTDouble,cParamDsc_Values,VDimension,"0.0",-1);
00327       profile.Parameters().AddParameter(cParam_PointsSource,Parameter::PVTInteger,cParamDsc_PointsSource,1,"0");
00328     };
00329 
00330    virtual ComponentPointer BuildMainComponent(ComponentSetup* pComponentSetup, SessionComponentCache* pComponentCache) const
00331     {
00332       int iPointSource;
00333       
00334       try
00335       {
00336         SessionAccessor::GetParameterValue(pComponentCache,cParam_PointsSource,iPointSource);
00337       }
00338       catchAllNPassMacro("Error while retrieving loading type.");
00339 
00340       //generate empty dummy media because component pointer in cache must not be NULL
00341       ComponentPointer smpPointSet = Superclass::BuildMainComponent(pComponentSetup, pComponentCache);
00342 
00343       if (iPointSource!=2)
00344       { //preload -> load while building
00345         if (!LoadMedia(pComponentCache)) throwCtrlExceptionMacro("", "Unable to load point set.");
00346 
00347         try
00348         {
00349           smpPointSet = dynamic_cast<ComponentType*>(DirectSessionComponentAccessor::GetComponent(pComponentCache));
00350         }
00351         catchAllNPassMacro("Error; component loaded wasn't a legal point set.");
00352         if (smpPointSet.IsNull()) throwCtrlExceptionMacro("","Error; component loaded wasn't a legal point set.");
00353       }
00354       else
00355       {
00356         //Actualize validitiy tag even if no media is loaded yet, because the validity requirement for
00357         //media images not preloaded is at least equal to now.
00358         this->ActualizeMediaValidityTag("media",pComponentCache);
00359       }
00360 
00361       return smpPointSet;
00362    };
00363 
00364    virtual void LoadSetFromSetup(ComponentSetup& rSetup, ComponentType& rSet) const
00365    {
00366       Parameter* pPoints = rSetup.Parameters().GetParameter(cParam_Points);
00367       if (!pPoints)
00368           throwCtrlExceptionMacro("", "Missing parameter" << cParam_Points);
00369 
00370       Parameter* pValues = rSetup.Parameters().GetParameter(cParam_Values);
00371       if (!pValues)
00372           throwCtrlExceptionMacro("", "Missing parameter" << cParam_Values);
00373 
00374       for (Parameter::LayerCountType iLayer = 0; iLayer<pPoints->LayerCount(); iLayer++)
00375       {
00376           typename ComponentType::PointType point = Convert::ParameterLayerToFixedArray<typename ComponentType::PointType>(*(pPoints->GetParameterLayer(iLayer)));
00377           rSet.GetPoints()->InsertElement(iLayer,point);
00378 
00379           //ComponentType::PixelType value = 0;
00380           //TODO cast in double as long there is no Getter Function for InternalPixelValueType (float)
00381           double value = 0;
00382           if (iLayer<pValues->LayerCount())
00383               pValues->GetParameterValue(value,0,iLayer);
00384           rSet.GetPointData()->InsertElement(iLayer,value);
00385       }
00386    };
00387 
00388    virtual void SaveSetToSetup(ComponentSetup& pSetup, ComponentType& rSet) const
00389    {
00390       Parameter* pPoints = pSetup.Parameters().GetParameter(cParam_Points);
00391       if (!pPoints)
00392           throwCtrlExceptionMacro("","Missing parameter" << cParam_Points);
00393 
00394       Parameter* pValues = pSetup.Parameters().GetParameter(cParam_Values);
00395       if (!pValues)
00396           throwCtrlExceptionMacro("", "Missing parameter" << cParam_Values);
00397 
00398       pPoints->Reset();
00399       pPoints->Resize(VDimension);
00400       pValues->Reset();
00401       pValues->Resize(1);
00402 
00403       for (typename ComponentType::PointsContainer::ConstIterator iter = rSet.GetPoints()->Begin(); iter != rSet.GetPoints()->End(); ++iter)
00404       {
00405           typename ComponentType::PixelType value;
00406           if (rSet.GetPointData(iter.Index(), &value))
00407               value = 0;
00408 
00409           //add a new layer to the points parameter
00410           ParameterLayer* pPointLayer = pPoints->GetParameterLayer(pPoints->AddParameterLayer());
00411           //copy the point elements into the parameter layer
00412           Convert::FixedArrayToParameterLayer<typename ComponentType::PointType>(iter.Value(),*pPointLayer);
00413 
00414           //add a new layer to the value parameter
00415           ParameterLayer* pValueLayer = pValues->GetParameterLayer(pValues->AddParameterLayer());
00416           pValueLayer->SetValue(value);
00417       }
00418    };
00419 };
00420 
00421 template <class TPixel, unsigned int VDimension>
00422 const char* const PointSetMediaControllerBase<TPixel, VDimension> :: cParam_Points = "Points";
00423 template <class TPixel, unsigned int VDimension>
00424 const char* const PointSetMediaControllerBase<TPixel, VDimension> :: cParamDsc_Points = "Points of the point set.";
00425 template <class TPixel, unsigned int VDimension>
00426 const char* const PointSetMediaControllerBase<TPixel, VDimension> :: cParam_Values= "Values";
00427 template <class TPixel, unsigned int VDimension>
00428 const char* const PointSetMediaControllerBase<TPixel, VDimension> :: cParamDsc_Values = "Values assigned to each point with same ID/layer. If no value is set for a point the default value will be 0.";
00429 template <class TPixel, unsigned int VDimension>
00430 const char* const PointSetMediaControllerBase<TPixel, VDimension> :: cParam_PointsSource = "PointsSource";
00431 template <class TPixel, unsigned int VDimension>
00432 const char* const PointSetMediaControllerBase<TPixel, VDimension> :: cParamDsc_PointsSource = "Defines where the location of the point set data (therefore target of loading and saving processes) is.\n0: Setup\n1: File\n2: File (load only if needed)";
00433 
00439 freControllerIDMacro(PointSet2DMediaController, "Point Set 2D Media");
00440 class PointSet2DMediaController : public PointSetMediaControllerBase< InternalImagePixelType, 2>
00441 {
00442 public:
00443     typedef PointSetMediaControllerBase<InternalImagePixelType, 2> Superclass;
00444     typedef Superclass::ComponentType ComponentType;
00445 
00446     itkTypeMacro(PointSet2DMediaController, PointSetMediaControllerBase);
00447 
00448     PointSet2DMediaController();
00449 
00450     virtual ~PointSet2DMediaController();
00451 };
00452 
00458 freControllerIDMacro(PointSet3DMediaController, "Point Set 3D Media");
00459 class PointSet3DMediaController : public PointSetMediaControllerBase< InternalImagePixelType, 3>
00460 {
00461 public:
00462     typedef PointSetMediaControllerBase<InternalImagePixelType, 3> Superclass;
00463     typedef Superclass::ComponentType ComponentType;
00464 
00465     itkTypeMacro(PointSet3DMediaController, PointSetMediaControllerBase);
00466 
00467     PointSet3DMediaController();
00468 
00469     virtual ~PointSet3DMediaController();
00470 };
00471 
00472 } //end of namespace free
00473 
00474 #endif

Generated at Sat Oct 13 17:01:40 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000