g2o
hyper_graph_action.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "hyper_graph_action.h"
28 #include "optimizable_graph.h"
29 #include "cache.h"
30 #include "g2o/stuff/macros.h"
31 
32 
33 #include <iostream>
34 
35 namespace g2o {
36  using namespace std;
37 
38  HyperGraphActionLibrary* HyperGraphActionLibrary::actionLibInstance = 0;
39 
41  {
42  }
43 
46  iteration(iter)
47  {
48  }
49 
51  {
52  }
53 
55  {
56  return 0;
57  }
58 
60  {
61  }
62 
64  {
65  _typeName = typeName_;
66  }
67 
68  void HyperGraphElementAction::setTypeName(const std::string& typeName_)
69  {
70  _typeName = typeName_;
71  }
72 
73 
75  {
76  return 0;
77  }
78 
80  {
81  return 0;
82  }
83 
85  {
86  }
87 
89  {
90  _name = name_;
91  }
92 
94  {
95  for (ActionMap::iterator it = _actionMap.begin(); it != _actionMap.end(); ++it) {
96  delete it->second;
97  }
98  }
99 
101  {
102  ActionMap::iterator it=_actionMap.find(typeid(*element).name());
103  //cerr << typeid(*element).name() << endl;
104  if (it==_actionMap.end())
105  return 0;
106  HyperGraphElementAction* action=it->second;
107  return (*action)(element, params);
108  }
109 
111  {
112  ActionMap::iterator it=_actionMap.find(typeid(*element).name());
113  if (it==_actionMap.end())
114  return 0;
115  HyperGraphElementAction* action=it->second;
116  return (*action)(element, params);
117  }
118 
120  {
121 # ifdef G2O_DEBUG_ACTIONLIB
122  cerr << __PRETTY_FUNCTION__ << " " << action->name() << " " << action->typeName() << endl;
123 # endif
124  if (action->name()!=name()){
125  cerr << __PRETTY_FUNCTION__ << ": invalid attempt to register an action in a collection with a different name " << name() << " " << action->name() << endl;
126  }
127  _actionMap.insert(make_pair ( action->typeName(), action) );
128  return true;
129  }
130 
132  {
133  for (HyperGraphElementAction::ActionMap::iterator it=_actionMap.begin(); it != _actionMap.end(); ++it) {
134  if (it->second == action){
135  _actionMap.erase(it);
136  return true;
137  }
138  }
139  return false;
140  }
141 
143  {
144  }
145 
147  {
148  if (actionLibInstance == 0) {
149  actionLibInstance = new HyperGraphActionLibrary;
150  }
151  return actionLibInstance;
152  }
153 
155  {
156  delete actionLibInstance;
157  actionLibInstance = 0;
158  }
159 
161  {
162  for (HyperGraphElementAction::ActionMap::iterator it = _actionMap.begin(); it != _actionMap.end(); ++it) {
163  delete it->second;
164  }
165  }
166 
168  {
169  HyperGraphElementAction::ActionMap::iterator it=_actionMap.find(name);
170  if (it!=_actionMap.end())
171  return it->second;
172  return 0;
173  }
174 
176  {
177  HyperGraphElementAction* oldAction = actionByName(action->name());
178  HyperGraphElementActionCollection* collection = 0;
179  if (oldAction) {
180  collection = dynamic_cast<HyperGraphElementActionCollection*>(oldAction);
181  if (! collection) {
182  cerr << __PRETTY_FUNCTION__ << ": fatal error, a collection is not at the first level in the library" << endl;
183  return 0;
184  }
185  }
186  if (! collection) {
187 #ifdef G2O_DEBUG_ACTIONLIB
188  cerr << __PRETTY_FUNCTION__ << ": creating collection for \"" << action->name() << "\"" << endl;
189 #endif
190  collection = new HyperGraphElementActionCollection(action->name());
191  _actionMap.insert(make_pair(action->name(), collection));
192  }
193  return collection->registerAction(action);
194  }
195 
197  {
198  list<HyperGraphElementActionCollection*> collectionDeleteList;
199 
200  // Search all the collections and delete the registered actions; if a collection becomes empty, schedule it for deletion; note that we can't delete the collections as we go because this will screw up the state of the iterators
201  for (HyperGraphElementAction::ActionMap::iterator it=_actionMap.begin(); it != _actionMap.end(); ++it) {
202  HyperGraphElementActionCollection* collection = dynamic_cast<HyperGraphElementActionCollection*> (it->second);
203  if (collection != 0) {
204  collection->unregisterAction(action);
205  if (collection->actionMap().size() == 0) {
206  collectionDeleteList.push_back(collection);
207  }
208  }
209  }
210 
211  // Delete any empty action collections
212  for (list<HyperGraphElementActionCollection*>::iterator itc = collectionDeleteList.begin(); itc != collectionDeleteList.end(); ++itc) {
213  //cout << "Deleting collection " << (*itc)->name() << endl;
214  _actionMap.erase((*itc)->name());
215  }
216 
217  return true;
218  }
219 
220 
221  WriteGnuplotAction::WriteGnuplotAction(const std::string& typeName_)
222  : HyperGraphElementAction(typeName_)
223  {
224  _name="writeGnuplot";
225  }
226 
228  }
229 
230  DrawAction::DrawAction(const std::string& typeName_)
231  : HyperGraphElementAction(typeName_)
232  {
233  _name="draw";
234  _previousParams = (Parameters*)0x42;
236  _cacheDrawActions = 0;
237  }
238 
240  if (_previousParams == params_)
241  return false;
242  DrawAction::Parameters* p=dynamic_cast<DrawAction::Parameters*>(params_);
243  if (! p){
244  _previousParams = 0;
245  _show = 0;
246  _showId = 0;
247  } else {
248  _previousParams = p;
249  _show = p->makeProperty<BoolProperty>(_typeName+"::SHOW", true);
250  _showId = p->makeProperty<BoolProperty>(_typeName+"::SHOW_ID", false);
251  }
252  return true;
253  }
254 
256  if (! _cacheDrawActions){
258  }
259  }
260 
262  if (caches){
263  for (CacheContainer::iterator it=caches->begin(); it!=caches->end(); it++){
264  Cache* c = it->second;
265  (*_cacheDrawActions)(c, params_);
266  }
267  }
268  }
269 
271  while (data && _cacheDrawActions ){
272  (*_cacheDrawActions)(data, params_);
273  data=data->next();
274  }
275  }
276 
278  {
279  for (HyperGraph::VertexIDMap::iterator it=graph->vertices().begin();
280  it!=graph->vertices().end(); ++it){
281  if ( typeName.empty() || typeid(*it->second).name()==typeName){
282  (*action)(it->second, params);
283  }
284  }
285  for (HyperGraph::EdgeSet::iterator it=graph->edges().begin();
286  it!=graph->edges().end(); ++it){
287  if ( typeName.empty() || typeid(**it).name()==typeName)
288  (*action)(*it, params);
289  }
290  }
291 
292 } // end namespace
DrawAction(const std::string &typeName_)
#define __PRETTY_FUNCTION__
Definition: macros.h:89
data packet for a vertex. Extend this class to store in the vertices the potential additional informa...
Definition: hyper_graph.h:97
virtual HyperGraphElementAction * operator()(HyperGraph::HyperGraphElement *element, Parameters *parameters)
redefine this to do the action stuff. If successful, the action returns a pointer to itself ...
bool registerAction(HyperGraphElementAction *action)
Abstract action that operates on a graph entity.
Parameters * _previousParams
static void destroy()
free the instance
void applyAction(HyperGraph *graph, HyperGraphElementAction *action, HyperGraphElementAction::Parameters *params, const std::string &typeName)
HyperGraphElementActionCollection(const std::string &name_)
constructor. name_ is the name of the action e.g.draw).
const VertexIDMap & vertices() const
Definition: hyper_graph.h:225
bool unregisterAction(HyperGraphElementAction *action)
const std::string & name() const
returns the name of an action, e.g "draw"
HyperGraphElementAction * _cacheDrawActions
void setTypeName(const std::string &typeName_)
sets the type on which an action has to operate
P * makeProperty(const std::string &name_, const typename P::ValueType &v)
Definition: property.h:119
virtual ~HyperGraphElementActionCollection()
destructor: it deletes all actions in the pool.
BoolProperty * _showId
void drawCache(CacheContainer *caches, HyperGraphElementAction::Parameters *params_)
const EdgeSet & edges() const
Definition: hyper_graph.h:230
static HyperGraphActionLibrary * instance()
return the single instance of the HyperGraphActionLibrary
bool registerAction(HyperGraphElementAction *action)
HyperGraphElementAction * actionByName(const std::string &name)
void drawUserData(HyperGraph::Data *data, HyperGraphElementAction::Parameters *params_)
WriteGnuplotAction(const std::string &typeName_)
virtual HyperGraphElementAction * operator()(HyperGraph::HyperGraphElement *element, Parameters *parameters)
HyperGraphElementAction(const std::string &typeName_="")
const std::string & typeName() const
returns the typeid name of the action
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
static HyperGraphActionLibrary * actionLibInstance
bool unregisterAction(HyperGraphElementAction *action)
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the graph
Definition: protocol.txt:7
virtual ~HyperGraphElementAction()
destroyed actions release the memory
Abstract action that operates on an entire graph.
library of actions, indexed by the action name;
BoolProperty * _show
virtual HyperGraphAction * operator()(const HyperGraph *graph, Parameters *parameters=0)