/**
* @file GenericLocalFeatureSelection.h
* @brief This class provides a generic chooser function for different descriptors, which are derived from LocalFeature.
* @author Eric Bach, Alexander Freytag
* @date 26.10.2011
*/

#ifndef _NICE_OBJREC_LOCALFEATURESELECTION_INCLUDE
#define _NICE_OBJREC_LOCALFEATURESELECTION_INCLUDE

#include "vislearning/features/localfeatures/LocalFeatureRepresentation.h"

// color-descriptor
#include "vislearning/features/localfeatures/LocalFeatureRGBSift.h"
#include "vislearning/features/localfeatures/LocalFeatureOpponnentSift.h"
#include "vislearning/features/localfeatures/LocalFeatureColorWeijer.h"
// non-color-descriptor
#include "vislearning/features/localfeatures/LocalFeatureCentrist.h"
#include "vislearning/features/localfeatures/LocalFeatureSift.h"




/** NOTE: This class only returns Descriptors, which NEED given positions. NO Descriptors calculate there own positions. **/

namespace OBJREC {

  /** @class GenericLocalFeatureSelection
 * @brief Select a specific LocalFeature-Type. LocalFeatures compute Descriptors, which DO need given positions (no internal position calculation)!
 *
 */  
class GenericLocalFeatureSelection
{
  public:
    /** LocalFeature Selector
    * @brief This methode switches between the different LocalFeature-Types. One has to set the "localfeature_type" on a valid value and the methode returns a LocalFeatureRepresentation derived Object.
    * @param[in] Config* - A pointer to the given configfile, which must contain "section" - "localfeature_type"
    * @param[in] string  - This string defines the value for "section" in the configfile.
    * @return LocalFeature* - The LocalFeature which contains the selected LocalFeature-Type.
    */
    static OBJREC::LocalFeature *selectLocalFeature ( const NICE::Config *conf, std::string section = "Features" )
    {
      // return Value
      LocalFeature *lf = NULL;

      // string which defines the localfeature_type (for Ex. RGB, OppSift, Sift...)
      std::string localfeature_type = conf->gS ( section, "localfeature_type", "not defined" );
      
      // The prefix NICE_* indicates that this implementation comes from a NICE-Developer.
      // Extern implementations can be added without NICE_* in this selection-class.
      if ( ( localfeature_type == "NICE_SIFT" ) || ( localfeature_type == "LocalFeatureSift" ) )
      {
        lf = new OBJREC::LocalFeatureSift ( conf );
      }
      else if ( ( localfeature_type == "NICE_RGBSIFT" ) || ( localfeature_type == "LocalFeatureRGBSift" ) )
      {
        lf = new OBJREC::LocalFeatureRGBSift ( conf );
      }
      else if ( ( localfeature_type == "NICE_OPPSIFT" ) || ( localfeature_type == "LocalFeatureOpponnentSift" ) )
      {
        lf = new OBJREC::LocalFeatureOpponnentSift ( conf );
      }
      else if ( ( localfeature_type == "colornames" ) || ( localfeature_type == "LocalFeatureColorWeijer" ) )
      {
        lf = new OBJREC::LocalFeatureColorWeijer ( conf );
      }     
      else if ( ( localfeature_type == "centrist" ) || ( localfeature_type == "LocalFeatureCentrist" ) )
      {
        lf = new OBJREC::LocalFeatureCentrist ( conf );
      }      
      

      // no correct localfeature_type was given
      if ( lf == NULL )
        fthrow ( NICE::Exception, "Local feature type not found: " << localfeature_type << " for section " << section);

      return lf;
    };
    
      static
      void restoreLocalFeature ( OBJREC::LocalFeature * _lf, std::istream & is, int format = 0 )
      {
                
        if ( is.good() )
        {
          if ( _lf != NULL )
            delete _lf;
          
          
          std::string className;
          is >> className; //class name
                     
                    
          if ( className == "<LocalFeatureSift>" )
          {
            _lf = new OBJREC::LocalFeatureSift();
          }
          else if ( className == "<LocalFeatureRGBSift>" )
          {
            _lf = new OBJREC::LocalFeatureRGBSift();            
          }
          else if ( className == "<LocalFeatureOpponnentSift>" )
          {
            _lf = new OBJREC::LocalFeatureOpponnentSift();
          }
          else if ( className == "<LocalFeatureColorWeijer>" )
          {
            _lf = new OBJREC::LocalFeatureColorWeijer();
          }
          else if ( className == "<LocalFeatureCentrist>" )
          {
            _lf = new OBJREC::LocalFeatureCentrist();
          } 
          else
          {
            fthrow ( NICE::Exception, "GenericLocalFeatureSelection::restoreLocalFeatureRep -- class name " << className << "unknown. Aborting." );
          }
          
          //undo reading of class name
          
          for ( uint i = 0; i < className.size(); i++)
          {
            is.unget();
          }
          
          //now, call the restore method of the underlying object
          //NOTE this could be also done externally, leaving only the actual instantiation of the derived objects here
          _lf->restore ( is );
            
        }
        else
        {
          fthrow ( NICE::Exception, "GenericLocalFeatureSelection::restoreLocalFeatureRep -- InStream not initialized - restoring not possible!" );
        }      
    };    
};
}

#endif
