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 "../stuff/macros.h"
30 
31 
32 #include <iostream>
33 
34 namespace g2o {
35  using namespace std;
36 
37  HyperGraphActionLibrary* HyperGraphActionLibrary::actionLibInstance = 0;
38 
40  {
41  }
42 
45  iteration(iter)
46  {
47  }
48 
50  {
51  }
52 
54  {
55  return 0;
56  }
57 
59  {
60  }
61 
63  {
64  _typeName = typeName_;
65  }
66 
67  void HyperGraphElementAction::setTypeName(const std::string& typeName_)
68  {
69  _typeName = typeName_;
70  }
71 
72 
74  {
75  return 0;
76  }
77 
79  {
80  return 0;
81  }
82 
84  {
85  }
86 
88  {
89  _name = name_;
90  }
91 
93  {
94  for (ActionMap::iterator it = _actionMap.begin(); it != _actionMap.end(); ++it) {
95  delete it->second;
96  }
97  }
98 
100  {
101  ActionMap::iterator it=_actionMap.find(typeid(*element).name());
102  //cerr << typeid(*element).name() << endl;
103  if (it==_actionMap.end())
104  return 0;
105  HyperGraphElementAction* action=it->second;
106  return (*action)(element, params);
107  }
108 
110  {
111  ActionMap::iterator it=_actionMap.find(typeid(*element).name());
112  if (it==_actionMap.end())
113  return 0;
114  HyperGraphElementAction* action=it->second;
115  return (*action)(element, params);
116  }
117 
119  {
120 # ifdef G2O_DEBUG_ACTIONLIB
121  cerr << __PRETTY_FUNCTION__ << " " << action->name() << " " << action->typeName() << endl;
122 # endif
123  if (action->name()!=name()){
124  cerr << __PRETTY_FUNCTION__ << ": invalid attempt to register an action in a collection with a different name " << name() << " " << action->name() << endl;
125  }
126  _actionMap.insert(make_pair ( action->typeName(), action) );
127  return true;
128  }
129 
131  {
132  for (HyperGraphElementAction::ActionMap::iterator it=_actionMap.begin(); it != _actionMap.end(); ++it) {
133  if (it->second == action){
134  _actionMap.erase(it);
135  return true;
136  }
137  }
138  return false;
139  }
140 
142  {
143  }
144 
146  {
147  if (actionLibInstance == 0) {
148  actionLibInstance = new HyperGraphActionLibrary;
149  }
150  return actionLibInstance;
151  }
152 
154  {
155  delete actionLibInstance;
156  actionLibInstance = 0;
157  }
158 
160  {
161  for (HyperGraphElementAction::ActionMap::iterator it = _actionMap.begin(); it != _actionMap.end(); ++it) {
162  delete it->second;
163  }
164  }
165 
167  {
168  HyperGraphElementAction::ActionMap::iterator it=_actionMap.find(name);
169  if (it!=_actionMap.end())
170  return it->second;
171  return 0;
172  }
173 
175  {
176  HyperGraphElementAction* oldAction = actionByName(action->name());
177  HyperGraphElementActionCollection* collection = 0;
178  if (oldAction) {
179  collection = dynamic_cast<HyperGraphElementActionCollection*>(oldAction);
180  if (! collection) {
181  cerr << __PRETTY_FUNCTION__ << ": fatal error, a collection is not at the first level in the library" << endl;
182  return 0;
183  }
184  }
185  if (! collection) {
186 #ifdef G2O_DEBUG_ACTIONLIB
187  cerr << __PRETTY_FUNCTION__ << ": creating collection for \"" << action->name() << "\"" << endl;
188 #endif
189  collection = new HyperGraphElementActionCollection(action->name());
190  _actionMap.insert(make_pair(action->name(), collection));
191  }
192  return collection->registerAction(action);
193  }
194 
196  {
197  list<HyperGraphElementActionCollection*> collectionDeleteList;
198 
199  // 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
200  for (HyperGraphElementAction::ActionMap::iterator it=_actionMap.begin(); it != _actionMap.end(); ++it) {
201  HyperGraphElementActionCollection* collection = dynamic_cast<HyperGraphElementActionCollection*> (it->second);
202  if (collection != 0) {
203  collection->unregisterAction(action);
204  if (collection->actionMap().size() == 0) {
205  collectionDeleteList.push_back(collection);
206  }
207  }
208  }
209 
210  // Delete any empty action collections
211  for (list<HyperGraphElementActionCollection*>::iterator itc = collectionDeleteList.begin(); itc != collectionDeleteList.end(); ++itc) {
212  //cout << "Deleting collection " << (*itc)->name() << endl;
213  _actionMap.erase((*itc)->name());
214  }
215 
216  return true;
217  }
218 
219 
220  WriteGnuplotAction::WriteGnuplotAction(const std::string& typeName_)
221  : HyperGraphElementAction(typeName_)
222  {
223  _name="writeGnuplot";
224  }
225 
227  }
228 
229  DrawAction::DrawAction(const std::string& typeName_)
230  : HyperGraphElementAction(typeName_)
231  {
232  _name="draw";
233  _previousParams = (Parameters*)0x42;
235  }
236 
238  if (_previousParams == params_)
239  return false;
240  DrawAction::Parameters* p=dynamic_cast<DrawAction::Parameters*>(params_);
241  if (! p){
242  _previousParams = 0;
243  _show = 0;
244  _showId = 0;
245  } else {
246  _previousParams = p;
247  _show = p->makeProperty<BoolProperty>(_typeName+"::SHOW", true);
248  _showId = p->makeProperty<BoolProperty>(_typeName+"::SHOW_ID", false);
249  }
250  return true;
251  }
252 
254  {
255  for (HyperGraph::VertexIDMap::iterator it=graph->vertices().begin();
256  it!=graph->vertices().end(); ++it){
257  if ( typeName.empty() || typeid(*it->second).name()==typeName){
258  (*action)(it->second, params);
259  }
260  }
261  for (HyperGraph::EdgeSet::iterator it=graph->edges().begin();
262  it!=graph->edges().end(); ++it){
263  if ( typeName.empty() || typeid(**it).name()==typeName)
264  (*action)(*it, params);
265  }
266  }
267 
268 } // end namespace
DrawAction(const std::string &typeName_)
#define __PRETTY_FUNCTION__
Definition: macros.h:95
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
HyperGraphElementActionCollection(const std::string &name_)
constructor. name_ is the name of the action e.g.draw).
const VertexIDMap & vertices() const
Definition: hyper_graph.h:177
bool unregisterAction(HyperGraphElementAction *action)
const std::string & name() const
returns the name of an action, e.g "draw"
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:118
virtual ~HyperGraphElementActionCollection()
destructor: it deletes all actions in the pool.
BoolProperty * _showId
const EdgeSet & edges() const
Definition: hyper_graph.h:182
static HyperGraphActionLibrary * instance()
return the single instance of the HyperGraphActionLibrary
bool registerAction(HyperGraphElementAction *action)
HyperGraphElementAction * actionByName(const std::string &name)
void applyAction(HyperGraph *graph, HyperGraphElementAction *action, HyperGraphElementAction::Parameters *params, const std::string &typeName)
action
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)
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)


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47