g2o
Loading...
Searching...
No Matches
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
29#include <iostream>
30#include <list>
31
32#include "cache.h"
33#include "g2o/stuff/logger.h"
34#include "g2o/stuff/macros.h"
35#include "optimizable_graph.h"
36
37namespace g2o {
38using namespace std;
39
40std::unique_ptr<HyperGraphActionLibrary>
42
44
47
49
53
55
57 : _typeName(typeName_) {}
58
59void HyperGraphElementAction::setTypeName(const std::string& typeName_) {
60 _typeName = typeName_;
61}
62
67
73
75
80
84 ActionMap::iterator it = _actionMap.find(typeid(*element).name());
85 if (it == _actionMap.end()) return nullptr;
86 HyperGraphElementAction* action = it->second.get();
87 return (*action)(element, params);
88}
89
91 const HyperGraph::HyperGraphElement* element,
93 ActionMap::iterator it = _actionMap.find(typeid(*element).name());
94 if (it == _actionMap.end()) return nullptr;
95 HyperGraphElementAction* action = it->second.get();
96 return (*action)(element, params);
97}
98
101#ifdef G2O_DEBUG_ACTIONLIB
102 G2O_DEBUG("{} {} {}", __PRETTY_FUNCTION__, action->name(),
103 action->typeName());
104#endif
105 if (action->name() != name()) {
106 G2O_ERROR(
107 "{}: invalid attempt to register an action in a collection with a "
108 "different name {} {}",
109 __PRETTY_FUNCTION__, name(), action->name());
110 }
111 _actionMap.insert(make_pair(action->typeName(), action));
112 return true;
113}
114
117 for (HyperGraphElementAction::ActionMap::iterator it = _actionMap.begin();
118 it != _actionMap.end(); ++it) {
119 if (it->second == action) {
120 _actionMap.erase(it);
121 return true;
122 }
123 }
124 return false;
125}
126
128 if (actionLibInstance == 0) {
130 std::unique_ptr<HyperGraphActionLibrary>(new HyperGraphActionLibrary);
131 }
132 return actionLibInstance.get();
133}
134
136 std::unique_ptr<HyperGraphActionLibrary> aux;
137 actionLibInstance.swap(aux);
138}
139
141 const std::string& name) {
142 HyperGraphElementAction::ActionMap::iterator it = _actionMap.find(name);
143 if (it != _actionMap.end()) return it->second.get();
144 return nullptr;
145}
146
149 HyperGraphElementAction* oldAction = actionByName(action->name());
150 HyperGraphElementActionCollection* collection = nullptr;
151 if (oldAction) {
152 collection = dynamic_cast<HyperGraphElementActionCollection*>(oldAction);
153 if (!collection) {
154 G2O_ERROR(
155 "{}: : fatal error, a collection is not at the first level in the "
156 "library",
158 return false;
159 }
160 }
161 if (collection) {
162 return collection->registerAction(action);
163 }
164#ifdef G2O_DEBUG_ACTIONLIB
165 G2O_DEBUG("{}: creating collection for {}", __PRETTY_FUNCTION__,
166 action->name());
167#endif
168 collection = new HyperGraphElementActionCollection(action->name());
169 _actionMap.insert(make_pair(
170 action->name(),
172 return collection->registerAction(action);
173}
174
177 list<HyperGraphElementActionCollection*> collectionDeleteList;
178
179 // Search all the collections and delete the registered actions; if a
180 // collection becomes empty, schedule it for deletion; note that we can't
181 // delete the collections as we go because this will screw up the state of the
182 // iterators
183 for (HyperGraphElementAction::ActionMap::iterator it = _actionMap.begin();
184 it != _actionMap.end(); ++it) {
186 dynamic_cast<HyperGraphElementActionCollection*>(it->second.get());
187 if (collection != nullptr) {
188 collection->unregisterAction(action);
189 if (collection->actionMap().size() == 0) {
190 collectionDeleteList.push_back(collection);
191 }
192 }
193 }
194
195 // Delete any empty action collections
196 for (list<HyperGraphElementActionCollection*>::iterator itc =
197 collectionDeleteList.begin();
198 itc != collectionDeleteList.end(); ++itc) {
199 // cout << "Deleting collection " << (*itc)->name() << endl;
200 _actionMap.erase((*itc)->name());
201 }
202
203 return true;
204}
205
206WriteGnuplotAction::WriteGnuplotAction(const std::string& typeName_)
207 : HyperGraphElementAction(typeName_) {
208 _name = "writeGnuplot";
209}
210
212
213DrawAction::DrawAction(const std::string& typeName_)
214 : HyperGraphElementAction(typeName_) {
215 _name = "draw";
219}
220
223 if (_previousParams == params_) return false;
224 DrawAction::Parameters* p = dynamic_cast<DrawAction::Parameters*>(params_);
225 if (!p) {
226 _previousParams = 0;
227 _show = 0;
228 } else {
229 _previousParams = p;
230 _show = p->makeProperty<BoolProperty>(_typeName + "::SHOW", true);
231 }
232 return true;
233}
234
241
244 if (caches) {
245 for (CacheContainer::iterator it = caches->begin(); it != caches->end();
246 ++it) {
247 Cache* c = it->second;
248 (*_cacheDrawActions)(c, params_);
249 }
250 }
251}
252
255 while (data && _cacheDrawActions) {
256 (*_cacheDrawActions)(data, params_);
257 data = data->next();
258 }
259}
260
263 const std::string& typeName) {
264 for (HyperGraph::VertexIDMap::iterator it = graph->vertices().begin();
265 it != graph->vertices().end(); ++it) {
266 auto& aux = *it->second;
267 if (typeName.empty() || typeid(aux).name() == typeName) {
268 (*action)(it->second, params);
269 }
270 }
271 for (HyperGraph::EdgeSet::iterator it = graph->edges().begin();
272 it != graph->edges().end(); ++it) {
273 auto& aux = **it;
274 if (typeName.empty() || typeid(aux).name() == typeName)
275 (*action)(*it, params);
276 }
277}
278
279} // namespace g2o
BoolProperty * _show
HyperGraphElementAction * _cacheDrawActions
void drawCache(CacheContainer *caches, HyperGraphElementAction::Parameters *params_)
DrawAction(const std::string &typeName_)
void drawUserData(HyperGraph::Data *data, HyperGraphElementAction::Parameters *params_)
virtual bool refreshPropertyPtrs(HyperGraphElementAction::Parameters *params_)
Parameters * _previousParams
library of actions, indexed by the action name;
bool registerAction(const HyperGraphElementAction::HyperGraphElementActionPtr &action)
static HyperGraphActionLibrary * instance()
return the single instance of the HyperGraphActionLibrary
static void destroy()
free the instance
HyperGraphElementAction * actionByName(const std::string &name)
static std::unique_ptr< HyperGraphActionLibrary > actionLibInstance
bool unregisterAction(const HyperGraphElementAction::HyperGraphElementActionPtr &action)
HyperGraphElementAction::ActionMap _actionMap
Abstract action that operates on an entire graph.
virtual HyperGraphAction * operator()(const HyperGraph *graph, Parameters *parameters=0)
virtual HyperGraphElementAction * operator()(HyperGraph::HyperGraphElement *element, Parameters *parameters)
bool registerAction(const HyperGraphElementAction::HyperGraphElementActionPtr &action)
HyperGraphElementActionCollection(const std::string &name_)
constructor. name_ is the name of the action e.g.draw).
bool unregisterAction(const HyperGraphElementAction::HyperGraphElementActionPtr &action)
Abstract action that operates on a graph entity.
HyperGraphElementAction(const std::string &typeName_="")
virtual HyperGraphElementAction * operator()(HyperGraph::HyperGraphElement *element, Parameters *parameters)
const std::string & name() const
returns the name of an action, e.g "draw"
std::shared_ptr< HyperGraphElementAction > HyperGraphElementActionPtr
void setTypeName(const std::string &typeName_)
sets the type on which an action has to operate
data packet for a vertex. Extend this class to store in the vertices the potential additional informa...
Definition hyper_graph.h:93
const EdgeSet & edges() const
const VertexIDMap & vertices() const
P * makeProperty(const std::string &name_, const typename P::ValueType &v)
Definition property.h:116
WriteGnuplotAction(const std::string &typeName_)
#define G2O_ERROR(...)
Definition logger.h:89
#define G2O_DEBUG(...)
Definition logger.h:90
#define __PRETTY_FUNCTION__
Definition macros.h:90
void applyAction(HyperGraph *graph, HyperGraphElementAction *action, HyperGraphElementAction::Parameters *params, const std::string &typeName)
Definition jet.h:876