MRPT  1.9.9
CEdgeCounter.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "graphslam-precomp.h" // Precompiled headers
12 
13 // implementation file of CEdgeCounter class
14 using namespace mrpt::graphslam::detail;
15 
19 {
20  this->clearAllEdges();
22 
23  // visualization parameters for total edges / loop closures
24  m_display_total_edges = false;
26 
29 
32 
33  m_unique_edges = 0;
34 
35  m_win = nullptr;
36  m_win_manager = nullptr;
37 }
38 
41 {
42  MRPT_START;
43  ASSERTDEB_(win_manager);
44  ASSERTDEB_(win_manager->win);
45 
46  m_win_manager = win_manager;
48 
49  MRPT_END;
50 }
51 
52 void CEdgeCounter::setRemovedEdges(int removed_edges)
53 {
54  m_unique_edges = this->getTotalNumOfEdges() - removed_edges;
55 }
56 
57 void CEdgeCounter::setLoopClosureEdgesManually(int num_loop_closures)
58 {
59  m_num_loop_closures = num_loop_closures;
60 }
61 
64 {
65  int sum;
66  this->getTotalNumOfEdges(&sum);
67 
68  return sum;
69 }
70 
71 void CEdgeCounter::getTotalNumOfEdges(int* total_num_edges) const
72 {
73  ASSERTDEB_(total_num_edges);
74  int sum = 0;
75 
77  m_name_to_edges_num.begin();
78  it != m_name_to_edges_num.end(); ++it)
79  {
80  sum += it->second;
81  }
82  *total_num_edges = sum;
83 }
84 
86 {
89  if (search != m_name_to_edges_num.end())
90  {
91  return search->second;
92  }
93  else
94  {
95  THROW_EXCEPTION("No edge with such name exists");
96  }
97 }
98 
99 void CEdgeCounter::getNumForEdgeType(const std::string& name, int* total_num)
100 {
103  if (search != m_name_to_edges_num.end())
104  {
105  *total_num = search->second;
106  }
107  else
108  {
109  THROW_EXCEPTION("No edge with such name exists");
110  }
111 }
112 
113 void CEdgeCounter::setEdgesManually(const std::string& name, int num_of_edges)
114 {
117  if (search != m_name_to_edges_num.end())
118  {
119  search->second = num_of_edges;
120  }
121  else
122  {
123  std::string str_err = "No edge with such name exists.";
124  THROW_EXCEPTION(str_err);
125  }
126  // Update the visualization if the user has already set the vizualization
127  // parameters
129  {
130  this->updateTextMessages();
131  }
132 }
133 
135  const std::string& name, bool is_loop_closure /* =false */,
136  bool is_new /* =false */)
137 {
140  if (search != m_name_to_edges_num.end())
141  {
142  (search->second)++; // increment to the found element
143 
144  // specify warning if is_new = true
145  if (is_new)
146  {
147  std::string str_err = mrpt::format(
148  "Specified edge type [%s] already exists but is_new is also "
149  "specified!",
150  name.c_str());
151  THROW_EXCEPTION(str_err);
152  // std::stringstream ss_warn;
153  // ss_warn << "Commencing with the increment normally" << std::endl;
154  }
155  if (is_loop_closure)
156  {
157  // throw if user has also specified the is new boolean flag
158  if (is_new)
159  {
160  std::string str_err =
161  "Both is_new and is_loop_closure flags are true. "
162  "Exiting...";
163  THROW_EXCEPTION(str_err);
164  }
166  }
167  }
168  else
169  {
170  if (is_new)
171  {
173  }
174  else
175  {
176  std::string str_err =
177  "No edge with such name exists. Specify is_new parameter if "
178  "you want to add it";
179  THROW_EXCEPTION(str_err);
180  }
181  }
182 
183  // Update the visualization if the user has already set the vizualization
184  // parameters
186  {
188  }
189 }
190 
192 {
195 
196  if (search != m_name_to_edges_num.end())
197  {
199  mrpt::format(
200  "Specified edge type [%s] already exists", name.c_str()))
201  }
202  else
203  {
205  }
206 }
207 
209 {
211 
212  m_name_to_edges_num.clear();
213  m_name_to_offset_y.clear();
214  m_name_to_text_index.clear();
215 
217  m_display_total_edges = false;
218  m_display_loop_closures = false;
219 }
220 
222 {
223  std::string str(getAsString());
224  std::cout << str << std::endl;
225 }
226 
228 {
229  std::stringstream ss_out;
230  std::string sep(80, '#');
231  ss_out << "Summary of Edges: " << std::endl;
232  ss_out << sep << std::endl;
233 
234  ss_out << "\tTotal registered edges: " << this->getTotalNumOfEdges()
235  << std::endl;
236  ss_out << "\tUnique edges (after removal of multiple edges connecting the "
237  "same nodes): "
238  << m_unique_edges << std::endl;
239 
241  m_name_to_edges_num.begin();
242  it != m_name_to_edges_num.end(); ++it)
243  {
244  ss_out << "\t" << it->first << " edges: " << it->second << std::endl;
245  }
246  ss_out << "\tLoop closure edges: " << this->getLoopClosureEdges()
247  << std::endl;
248 
249  // dump the contents to the provided string
250  *str_out = ss_out.str();
251 }
252 
254 {
255  std::string str;
256  this->getAsString(&str);
257  return str;
258 }
259 
260 // VISUALIZATION RELATED METHODS
261 // ////////////////////////////
262 
264  const std::map<std::string, double>& name_to_offset_y,
265  const std::map<std::string, int>& name_to_text_index)
266 {
269  "Visualization of data was requested but no CWindowManager pointer was "
270  "provided");
271  ASSERTDEB_EQUAL_(name_to_offset_y.size(), name_to_text_index.size());
272 
274  name_to_offset_y.begin();
275  it != name_to_offset_y.end(); ++it)
276  {
277  std::string name = it->first;
278 
279  // check if name already exist, otherwise throw exception
282  if (search == m_name_to_edges_num.end())
283  {
284  std::stringstream ss_err;
285  ss_err << "Name " << name << " is not recognized as an Edge type."
286  << std::endl;
287  THROW_EXCEPTION(ss_err.str());
288  }
289  // name exists ...
290 
291  double offset_y = it->second;
292  int text_index = name_to_text_index.find(name)->second;
293 
294  m_name_to_offset_y[name] = offset_y;
295  m_name_to_text_index[name] = text_index;
296  }
297 
299 }
300 
302  const std::map<std::string, double>& name_to_offset_y,
303  const std::map<std::string, int>& name_to_text_index,
304  const double& offset_y_total_edges, const int& text_index_total_edges,
305  const double& offset_y_loop_closures, const int& text_index_loop_closures)
306 {
307  // set the parameters for total edges / loop closures
308  m_display_total_edges = true;
310 
311  m_offset_y_total_edges = offset_y_total_edges;
312  m_offset_y_loop_closures = offset_y_loop_closures;
313 
314  m_text_index_total_edges = text_index_total_edges;
315  m_text_index_loop_closures = text_index_loop_closures;
316 
317  // pass execution to the other setTextMessageParams
318  this->setTextMessageParams(name_to_offset_y, name_to_text_index);
319 }
320 
322 {
326 
327  // Add text message for the total amount of edges
328  std::stringstream title;
329  title << "Total edges: " << this->getTotalNumOfEdges();
330  // if (m_unique_edges) {
331  // title << " |Unique: " << m_unique_edges << std::endl;
332  //}
334  {
336  5, -m_offset_y_total_edges, title.str(),
337  mrpt::img::TColorf(1.0, 1.0, 1.0),
338  /* unique_index = */ m_text_index_total_edges);
339  }
340 
341  // add a textMessage for every stored edge type
343  m_name_to_offset_y.begin();
344  it != m_name_to_offset_y.end(); ++it)
345  {
346  std::string name = it->first;
347  double offset_y = it->second;
348  int text_index = m_name_to_text_index.find(name)->second;
349  int edges_num = m_name_to_edges_num.find(name)->second;
350 
351  std::stringstream title;
352  title << " " << name << ": " << edges_num << std::endl;
354  5, -offset_y, title.str(), mrpt::img::TColorf(1.0, 1.0, 1.0),
355  /* unique_index = */ text_index);
356  }
357 
358  // add text message for the loop closures
360  {
361  std::stringstream title;
362  title << " "
363  << "Loop closures: " << m_num_loop_closures << std::endl;
365  5, -m_offset_y_loop_closures, title.str(),
366  mrpt::img::TColorf(1.0, 1.0, 1.0),
367  /* unique_index = */ m_text_index_loop_closures);
368  }
369 
370  m_win->forceRepaint();
371 }
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_START
Definition: exceptions.h:262
int getNumForEdgeType(const std::string &name) const
Return the number of edges for the specified type.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
void addEdgeType(const std::string &name)
Explicitly register a new edge type.
mrpt::gui::CDisplayWindow3D * m_win
Definition: CEdgeCounter.h:153
std::map< std::string, double > m_name_to_offset_y
Definition: CEdgeCounter.h:165
mrpt::graphslam::CWindowManager * m_win_manager
Definition: CEdgeCounter.h:154
void setLoopClosureEdgesManually(int num_loop_closures)
Method for manually setting the number of loop closures registered so far.
int getLoopClosureEdges() const
Returns the edges that form loop closures in the current graph.
void setRemovedEdges(int removed_edges)
State how many of the existing edges have been removed.
void setWindowManagerPtr(mrpt::graphslam::CWindowManager *win_manager)
Provide the instance with a CWindowManager.
mrpt::gui::CDisplayWindow3D * win
CDisplayWindow instance.
#define ASSERTDEB_EQUAL_(__A, __B)
Definition: exceptions.h:211
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
int getTotalNumOfEdges() const
Return the total amount of registered edges.
GLsizei const GLchar ** string
Definition: glext.h:4101
void forceRepaint()
Repaints the window.
#define ASSERTDEBMSG_(f, __ERROR_MSG)
Definition: exceptions.h:208
void clearAllEdges()
Reset the state of the CEdgeCounter instance.
std::map< std::string, int > m_name_to_edges_num
Map edge name <=> num of edges.
Definition: CEdgeCounter.h:160
std::map< std::string, int > m_name_to_text_index
Definition: CEdgeCounter.h:166
void dumpToConsole() const
Dump a report of the registered, so far, edges to the console.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: exceptions.h:205
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define MRPT_END
Definition: exceptions.h:266
void setTextMessageParams(const std::map< std::string, double > &name_to_offset_y, const std::map< std::string, int > &name_to_text_index)
Add the textMessage parameters to the object All the names in the given std::maps have to be already ...
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
void setEdgesManually(const std::string &name, int num_of_edges)
Set number of a specific edge type manually.
void initCEdgeCounter()
Initialization method to be called from the various Constructors.
GLuint const GLchar * name
Definition: glext.h:4054
void addTextMessage(const double x, const double y, const std::string &text, const mrpt::img::TColorf &color=mrpt::img::TColorf(1.0, 1.0, 1.0), const size_t unique_index=0)
Wrapper around the CDisplayWindow3D::addTextMessage method, so that the user does not have to specify...
void updateTextMessages() const
Update the given CDisplayWindow3D with the edges registered so far.
std::string getAsString() const
Return a detailed report of the registered, so far, edges in a string representation.
void addEdge(const std::string &name, bool is_loop_closure=false, bool is_new=false)
Increment the number of edges for the specified type.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
Internal auxiliary classes.
Definition: levmarq_impl.h:17
Class acts as a container for storing pointers to mrpt::gui::CDisplayWindow3D, mrpt::graphslam::CWind...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020