MRPT  1.9.9
CEdgeCounter.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include "graphslam-precomp.h" // Precompiled headers
12 
13 // implementation file of CEdgeCounter class
14 using namespace mrpt::graphslam::detail;
15 
19 {
20  MRPT_START;
21  ASSERTDEB_(win_manager);
22  ASSERTDEB_(win_manager->win);
23 
24  m_win_manager = win_manager;
26 
27  MRPT_END;
28 }
29 
30 void CEdgeCounter::setRemovedEdges(int removed_edges)
31 {
32  m_unique_edges = this->getTotalNumOfEdges() - removed_edges;
33 }
34 
35 void CEdgeCounter::setLoopClosureEdgesManually(int num_loop_closures)
36 {
37  m_num_loop_closures = num_loop_closures;
38 }
39 
42 {
43  int sum;
44  this->getTotalNumOfEdges(&sum);
45 
46  return sum;
47 }
48 
49 void CEdgeCounter::getTotalNumOfEdges(int* total_num_edges) const
50 {
51  ASSERTDEB_(total_num_edges);
52  int sum = 0;
53 
54  for (const auto& it : m_name_to_edges_num)
55  {
56  sum += it.second;
57  }
58  *total_num_edges = sum;
59 }
60 
62 {
63  auto search = m_name_to_edges_num.find(name);
64  if (search != m_name_to_edges_num.end())
65  {
66  return search->second;
67  }
68  else
69  {
70  THROW_EXCEPTION("No edge with such name exists");
71  }
72 }
73 
74 void CEdgeCounter::getNumForEdgeType(const std::string& name, int* total_num)
75 {
76  auto search = m_name_to_edges_num.find(name);
77  if (search != m_name_to_edges_num.end())
78  {
79  *total_num = search->second;
80  }
81  else
82  {
83  THROW_EXCEPTION("No edge with such name exists");
84  }
85 }
86 
87 void CEdgeCounter::setEdgesManually(const std::string& name, int num_of_edges)
88 {
89  auto search = m_name_to_edges_num.find(name);
90  if (search != m_name_to_edges_num.end())
91  {
92  search->second = num_of_edges;
93  }
94  else
95  {
96  std::string str_err = "No edge with such name exists.";
97  THROW_EXCEPTION(str_err);
98  }
99  // Update the visualization if the user has already set the vizualization
100  // parameters
102  {
103  this->updateTextMessages();
104  }
105 }
106 
108  const std::string& name, bool is_loop_closure /* =false */,
109  bool is_new /* =false */)
110 {
111  auto search = m_name_to_edges_num.find(name);
112  if (search != m_name_to_edges_num.end())
113  {
114  (search->second)++; // increment to the found element
115 
116  // specify warning if is_new = true
117  if (is_new)
118  {
119  std::string str_err = mrpt::format(
120  "Specified edge type [%s] already exists but is_new is also "
121  "specified!",
122  name.c_str());
123  THROW_EXCEPTION(str_err);
124  // std::stringstream ss_warn;
125  // ss_warn << "Commencing with the increment normally" << std::endl;
126  }
127  if (is_loop_closure)
128  {
129  // throw if user has also specified the is new boolean flag
130  if (is_new)
131  {
132  std::string str_err =
133  "Both is_new and is_loop_closure flags are true. "
134  "Exiting...";
135  THROW_EXCEPTION(str_err);
136  }
138  }
139  }
140  else
141  {
142  if (is_new)
143  {
145  }
146  else
147  {
148  std::string str_err =
149  "No edge with such name exists. Specify is_new parameter if "
150  "you want to add it";
151  THROW_EXCEPTION(str_err);
152  }
153  }
154 
155  // Update the visualization if the user has already set the vizualization
156  // parameters
158  {
160  }
161 }
162 
164 {
165  auto search = m_name_to_edges_num.find(name);
166 
167  if (search != m_name_to_edges_num.end())
168  {
170  "Specified edge type [%s] already exists", name.c_str()));
171  }
172  else
173  {
175  }
176 }
177 
179 {
181 
182  m_name_to_edges_num.clear();
183  m_name_to_offset_y.clear();
184  m_name_to_text_index.clear();
185 
187  m_display_total_edges = false;
188  m_display_loop_closures = false;
189 }
190 
192 {
193  std::string str(getAsString());
194  std::cout << str << std::endl;
195 }
196 
198 {
199  std::stringstream ss_out;
200  std::string sep(80, '#');
201  ss_out << "Summary of Edges: " << std::endl;
202  ss_out << sep << std::endl;
203 
204  ss_out << "\tTotal registered edges: " << this->getTotalNumOfEdges()
205  << std::endl;
206  ss_out << "\tUnique edges (after removal of multiple edges connecting the "
207  "same nodes): "
208  << m_unique_edges << std::endl;
209 
210  for (const auto& it : m_name_to_edges_num)
211  {
212  ss_out << "\t" << it.first << " edges: " << it.second << std::endl;
213  }
214  ss_out << "\tLoop closure edges: " << this->getLoopClosureEdges()
215  << std::endl;
216 
217  // dump the contents to the provided string
218  *str_out = ss_out.str();
219 }
220 
222 {
223  std::string str;
224  this->getAsString(&str);
225  return str;
226 }
227 
228 // VISUALIZATION RELATED METHODS
229 // ////////////////////////////
230 
232  const std::map<std::string, double>& name_to_offset_y,
233  const std::map<std::string, int>& name_to_text_index)
234 {
237  "Visualization of data was requested but no CWindowManager pointer was "
238  "provided");
239  ASSERTDEB_EQUAL_(name_to_offset_y.size(), name_to_text_index.size());
240 
241  for (const auto& it : name_to_offset_y)
242  {
243  std::string name = it.first;
244 
245  // check if name already exist, otherwise throw exception
246  auto search = m_name_to_edges_num.find(name);
247  if (search == m_name_to_edges_num.end())
248  {
249  std::stringstream ss_err;
250  ss_err << "Name " << name << " is not recognized as an Edge type."
251  << std::endl;
252  THROW_EXCEPTION(ss_err.str());
253  }
254  // name exists ...
255 
256  double offset_y = it.second;
257  int text_index = name_to_text_index.find(name)->second;
258 
259  m_name_to_offset_y[name] = offset_y;
260  m_name_to_text_index[name] = text_index;
261  }
262 
264 }
265 
267  const std::map<std::string, double>& name_to_offset_y,
268  const std::map<std::string, int>& name_to_text_index,
269  const double& offset_y_total_edges, const int& text_index_total_edges,
270  const double& offset_y_loop_closures, const int& text_index_loop_closures)
271 {
272  // set the parameters for total edges / loop closures
273  m_display_total_edges = true;
275 
276  m_offset_y_total_edges = offset_y_total_edges;
277  m_offset_y_loop_closures = offset_y_loop_closures;
278 
279  m_text_index_total_edges = text_index_total_edges;
280  m_text_index_loop_closures = text_index_loop_closures;
281 
282  // pass execution to the other setTextMessageParams
283  this->setTextMessageParams(name_to_offset_y, name_to_text_index);
284 }
285 
287 {
291 
292  // Add text message for the total amount of edges
293  std::stringstream title;
294  title << "Total edges: " << this->getTotalNumOfEdges();
295  // if (m_unique_edges) {
296  // title << " |Unique: " << m_unique_edges << std::endl;
297  //}
299  {
301  5, -m_offset_y_total_edges, title.str(),
302  mrpt::img::TColorf(1.0, 1.0, 1.0),
303  /* unique_index = */ m_text_index_total_edges);
304  }
305 
306  // add a textMessage for every stored edge type
307  for (const auto& it : m_name_to_offset_y)
308  {
309  std::string name = it.first;
310  double offset_y = it.second;
311  int text_index = m_name_to_text_index.find(name)->second;
312  int edges_num = m_name_to_edges_num.find(name)->second;
313 
314  std::stringstream ss;
315  ss << " " << name << ": " << edges_num << std::endl;
317  5, -offset_y, ss.str(), mrpt::img::TColorf(1.0, 1.0, 1.0),
318  /* unique_index = */ text_index);
319  }
320 
321  // add text message for the loop closures
323  {
324  std::stringstream ss;
325  ss << " "
326  << "Loop closures: " << m_num_loop_closures << std::endl;
328  5, -m_offset_y_loop_closures, ss.str(),
329  mrpt::img::TColorf(1.0, 1.0, 1.0),
330  /* unique_index = */ m_text_index_loop_closures);
331  }
332 
333  m_win->forceRepaint();
334 }
#define MRPT_START
Definition: exceptions.h:241
int getNumForEdgeType(const std::string &name) const
Return the number of edges for the specified type.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void addEdgeType(const std::string &name)
Explicitly register a new edge type.
mrpt::gui::CDisplayWindow3D * m_win
Definition: CEdgeCounter.h:149
std::map< std::string, double > m_name_to_offset_y
Definition: CEdgeCounter.h:161
mrpt::graphslam::CWindowManager * m_win_manager
Definition: CEdgeCounter.h:150
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:192
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:4116
void forceRepaint()
Repaints the window.
#define ASSERTDEBMSG_(f, __ERROR_MSG)
Definition: exceptions.h:191
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:156
std::map< std::string, int > m_name_to_text_index
Definition: CEdgeCounter.h:162
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:190
#define MRPT_END
Definition: exceptions.h:245
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:78
void setEdgesManually(const std::string &name, int num_of_edges)
Set number of a specific edge type manually.
GLuint const GLchar * name
Definition: glext.h:4068
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.
Internal auxiliary classes.
Definition: levmarq_impl.h:19
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: 836b840ab Mon Nov 18 00:58:29 2019 +0100 at lun nov 18 01:00:14 CET 2019