hyper_graph_action.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "hyper_graph_action.h"
00018 
00019 #include <iostream>
00020 
00021 namespace g2o {
00022   using namespace std;
00023 
00024   HyperGraphActionLibrary* HyperGraphActionLibrary::actionLibInstance = 0;
00025 
00026   HyperGraphAction::Parameters::~Parameters()
00027   {
00028   }
00029 
00030   HyperGraphAction::ParametersIteration::ParametersIteration(int iter) :
00031     HyperGraphAction::Parameters(),
00032     iteration(iter)
00033   {
00034   }
00035 
00036   HyperGraphAction::~HyperGraphAction()
00037   {
00038   }
00039 
00040   HyperGraphAction* HyperGraphAction::operator()(const HyperGraph*, Parameters*)
00041   {
00042     return 0;
00043   }
00044 
00045   HyperGraphElementAction::Parameters::~Parameters()
00046   {
00047   }
00048 
00049   HyperGraphElementAction::HyperGraphElementAction(const std::string& typeName_)
00050   {
00051     _typeName = typeName_;
00052   }
00053 
00054   HyperGraphElementAction* HyperGraphElementAction::operator()(HyperGraph::HyperGraphElement* , HyperGraphElementAction::Parameters* )
00055   {
00056     return 0;
00057   }
00058   
00059   HyperGraphElementAction* HyperGraphElementAction::operator()(const HyperGraph::HyperGraphElement* , HyperGraphElementAction::Parameters* )
00060   {
00061     return 0;
00062   }
00063   
00064   HyperGraphElementAction::~HyperGraphElementAction()
00065   {
00066   }
00067 
00068   HyperGraphElementActionCollection::HyperGraphElementActionCollection(const std::string& name_)
00069   {
00070     _name = name_;
00071   }
00072 
00073   HyperGraphElementActionCollection::~HyperGraphElementActionCollection()
00074   {
00075     _actionMap.clear();
00076   }
00077 
00078   HyperGraphElementAction* HyperGraphElementActionCollection::operator()(HyperGraph::HyperGraphElement* element, HyperGraphElementAction::Parameters* params)
00079   {
00080     ActionMap::iterator it=_actionMap.find(typeid(*element).name());
00081     //cerr << typeid(*element).name() << endl;
00082     if (it==_actionMap.end())
00083       return 0;
00084     HyperGraphElementAction* action=it->second;
00085     return (*action)(element, params);
00086   }
00087 
00088   HyperGraphElementAction* HyperGraphElementActionCollection::operator()(const HyperGraph::HyperGraphElement* element, HyperGraphElementAction::Parameters* params)
00089   {
00090     ActionMap::iterator it=_actionMap.find(typeid(*element).name());
00091     if (it==_actionMap.end())
00092       return 0;
00093     HyperGraphElementAction* action=it->second;
00094     return (*action)(element, params);
00095   }
00096 
00097   bool HyperGraphElementActionCollection::registerAction(HyperGraphElementAction* action)
00098   {
00099     if (action->name()!=name()){
00100       cerr << __PRETTY_FUNCTION__  << ": invalid attempt to register an action in a collection with a different name " <<  name() << " " << action->name() << endl;
00101     }
00102     _actionMap.insert(make_pair ( action->typeName(), action) );
00103     return true;
00104   }
00105 
00106   HyperGraphActionLibrary::HyperGraphActionLibrary()
00107   {
00108   }
00109 
00110   HyperGraphActionLibrary* HyperGraphActionLibrary::instance()
00111   {
00112     if (! actionLibInstance)
00113       actionLibInstance = new HyperGraphActionLibrary();
00114     return actionLibInstance;
00115   }
00116 
00117   void HyperGraphActionLibrary::destroy()
00118   {
00119     delete actionLibInstance;
00120     actionLibInstance = 0;
00121   }
00122 
00123   HyperGraphActionLibrary::~HyperGraphActionLibrary()
00124   {
00125     _actionMap.clear();
00126   }
00127   
00128   HyperGraphElementAction* HyperGraphActionLibrary::actionByName(const std::string& name)
00129   {
00130 
00131     HyperGraphElementAction::ActionMap::iterator it=_actionMap.find(name);
00132     if (it!=_actionMap.end())
00133       return it->second;
00134     return 0;
00135   }
00136 
00137   bool HyperGraphActionLibrary::registerAction(HyperGraphElementAction* action)
00138   {
00139     HyperGraphElementAction* oldAction = actionByName(action->name());
00140     HyperGraphElementActionCollection* collection = 0;
00141     if (oldAction) {
00142       collection = dynamic_cast<HyperGraphElementActionCollection*>(oldAction);
00143       if (! collection) {
00144         cerr << __PRETTY_FUNCTION__ << ": fatal error, a collection is not at the first level in the library" << endl;
00145         return 0;
00146       }
00147     }
00148     if (! collection) {
00149       cerr << __PRETTY_FUNCTION__ << ": creating collection for \"" << action->name() << "\"" << endl;
00150       collection = new HyperGraphElementActionCollection(action->name());
00151       _actionMap.insert(make_pair(action->name(), collection));
00152     }
00153     return collection->registerAction(action);
00154   }
00155   
00156 
00157   WriteGnuplotAction::WriteGnuplotAction(const std::string& typeName_)
00158     : HyperGraphElementAction(typeName_)
00159   {
00160     _name="writeGnuplot";
00161   }
00162 
00163   DrawAction::DrawAction(const std::string& typeName_) 
00164     : HyperGraphElementAction(typeName_)
00165   {
00166     _name="draw";
00167   }
00168 
00169   void applyAction(HyperGraph* graph, HyperGraphElementAction* action, HyperGraphElementAction::Parameters* params, const std::string& typeName)
00170   {
00171     for (HyperGraph::VertexIDMap::iterator it=graph->vertices().begin(); 
00172         it!=graph->vertices().end(); it++){
00173       if ( typeName.empty() || typeid(*it->second).name()==typeName)
00174         (*action)(it->second, params);
00175     }
00176     for (HyperGraph::EdgeSet::iterator it=graph->edges().begin(); 
00177         it!=graph->edges().end(); it++){
00178       if ( typeName.empty() || typeid(**it).name()==typeName)
00179         (*action)(*it, params);
00180     }
00181   }
00182 
00183 } // end namespace


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:24