Main MRPT website > C++ reference for MRPT 1.9.9
CHMHMapNode.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-2017, 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 "hmtslam-precomp.h" // Precomp header
11 
12 #include <mrpt/random.h>
13 
14 using namespace mrpt::slam;
15 using namespace mrpt::hmtslam;
16 using namespace std;
17 
19 
20 /*---------------------------------------------------------------
21  Default constructor
22  ---------------------------------------------------------------*/
24  CHierarchicalMHMap* parent, const THypothesisIDSet& hyps)
25  : m_hypotheses(hyps),
26  m_ID(),
27  m_arcs(),
28  m_parent(parent),
29  m_nodeType(NODE_TYPES, DEFAULT_NODE_TYPE),
30  m_label("none")
31 {
32  // Assure that ID is unique in the graph:
33  // -----------------------------------------
34  if (m_parent.get())
35  {
36  // Parent will be nullptr only in the default constructor for a
37  // temporary
38  // initialization before loading the object from "readFromStream"
39  m_ID = 0;
40  do
41  {
42  m_ID++; /* = (((uint64_t)getRandomGenerator().drawUniform(0.0f,0xFFFF))
43  << 32) |
44  (((uint64_t)getRandomGenerator().drawUniform(0.0f,0xFFFF)) << 16) |
45  (((uint64_t)getRandomGenerator().drawUniform(0.0f,0xFFFF)));*/
46  } while (m_parent->getNodeByID(m_ID));
47  }
48 }
49 
50 /*---------------------------------------------------------------
51  Destructor
52  ---------------------------------------------------------------*/
53 CHMHMapNode::~CHMHMapNode()
54 {
55  // To the graph:
56  if (m_parent.get()) m_parent->onNodeDestruction(this);
57 
58  // To the arcs:
59  for (TArcList::iterator it = m_arcs.begin(); it != m_arcs.end(); ++it)
60  (*it)->onNodeDestruction(this);
61 }
62 
63 /*---------------------------------------------------------------
64  Implements the writing to a CStream capability of
65  CSerializable objects
66  ---------------------------------------------------------------*/
67 void CHMHMapNode::writeToStream(mrpt::utils::CStream& out, int* version) const
68 {
69  if (version)
70  *version = 0;
71  else
72  {
73  // Data:
74  out << m_ID << m_label;
75  out << m_nodeType.getType();
76  out << m_annotations;
77  out << m_hypotheses;
78  }
79 }
80 
81 /*---------------------------------------------------------------
82  Implements the reading from a CStream capability of
83  CSerializable objects
84  ---------------------------------------------------------------*/
85 void CHMHMapNode::readFromStream(mrpt::utils::CStream& in, int version)
86 {
87  switch (version)
88  {
89  case 0:
90  {
92  // Data:
93  in >> m_ID >> m_label >> type >> m_annotations >> m_hypotheses;
94 
95  m_nodeType.setType(type);
96 
97  // It's not necessary since at ::Create this is already done
98  // (but...check!)
99  // if (m_parent.get())
100  // m_parent->onNodeAddition(this);
101  }
102  break;
103  default:
105  };
106 }
107 
108 /*---------------------------------------------------------------
109  onArcDestruction
110  ---------------------------------------------------------------*/
111 void CHMHMapNode::onArcDestruction(CHMHMapArc* arc)
112 {
113  MRPT_START
114 
115  // Important note: We cannot create a temporary smart pointer here, since
116  // it will lead to an infinity recursion! (BUGFIX, JLBC SEP-2009)
117 
118  // Check if arc is from/to this node:
119  if (arc->m_nodeFrom == m_ID || arc->m_nodeTo == m_ID)
120  {
121  // Remove from the list:
122  TArcList::iterator it = m_arcs.find_ptr_to(arc);
123  if (it != m_arcs.end()) m_arcs.erase(it);
124  }
125 
126  MRPT_END
127 }
128 
129 /*---------------------------------------------------------------
130  onArcAddition
131  ---------------------------------------------------------------*/
132 void CHMHMapNode::onArcAddition(const CHMHMapArc::Ptr& arc)
133 {
134  MRPT_START
135 
136  // Check if arc is from/to this node:
137  if (arc->m_nodeFrom == m_ID || arc->m_nodeTo == m_ID)
138  {
139  // Already in the list?
140  TArcList::iterator it = m_arcs.find(arc);
141  if (it == m_arcs.end()) m_arcs.push_back(arc); // Add to the list:
142  }
143 
144  MRPT_END
145 }
146 /*---------------------------------------------------------------
147  onArcAddition
148  ---------------------------------------------------------------*/
149 CHMHMapNode::TNodeID CHMHMapNode::getID() const { return m_ID; }
150 /*---------------------------------------------------------------
151  getLevelInTheHierarchy
152  ---------------------------------------------------------------*/
153 unsigned int CHMHMapNode::getLevelInTheHierarchy()
154 {
155  TArcList::iterator itArc;
156  unsigned int level = 0;
157 
158  for (itArc = m_arcs.begin(); itArc != m_arcs.end(); itArc++)
159  {
160  // I am a "level+1" from the level below if a "belongs" arc points to
161  // me:
162  if ((*itArc)->m_arcType.isType("Membership") &&
163  (*itArc)->m_nodeTo == this->m_ID)
164  {
165  unsigned int L = m_parent->getNodeByID((*itArc)->m_nodeFrom)
166  ->getLevelInTheHierarchy();
167  level = max(L + 1, level);
168  }
169  }
170 
171  return level;
172 }
173 
174 /*---------------------------------------------------------------
175  getRelatedArcsCount
176  ---------------------------------------------------------------*/
177 unsigned int CHMHMapNode::getRelatedArcsCount()
178 {
179  return (unsigned int)m_arcs.size();
180 }
181 
182 /*---------------------------------------------------------------
183  getArcs
184  ---------------------------------------------------------------*/
185 void CHMHMapNode::getArcs(TArcList& out, const THypothesisID& hyp_id) const
186 {
187  out.clear();
188  for (TArcList::const_iterator it = m_arcs.begin(); it != m_arcs.end(); ++it)
189  if ((*it)->m_hypotheses.has(hyp_id)) out.push_back(*it);
190 }
191 
192 /*---------------------------------------------------------------
193  getArcs
194  ---------------------------------------------------------------*/
195 void CHMHMapNode::getArcs(
196  TArcList& out, const char* arcType, const THypothesisID& hyp_id) const
197 {
198  out.clear();
199  for (TArcList::const_iterator it = m_arcs.begin(); it != m_arcs.end(); ++it)
200  if ((*it)->m_hypotheses.has(hyp_id) && (*it)->m_arcType.isType(arcType))
201  out.push_back(*it);
202 }
203 
204 /*---------------------------------------------------------------
205  isNeighbor
206  ---------------------------------------------------------------*/
207 bool CHMHMapNode::isNeighbor(
208  const TNodeID& otherArea, const THypothesisID& hyp_id) const
209 {
210  for (TArcList::const_iterator it = m_arcs.begin(); it != m_arcs.end(); ++it)
211  if ((*it)->m_hypotheses.has(hyp_id) &&
212  ((*it)->m_nodeFrom == otherArea || (*it)->m_nodeTo == otherArea))
213  return true;
214  return false; // Nope
215 }
std::shared_ptr< CHMHMapArc > Ptr
Definition: CHMHMapArc.h:38
Classes related to the implementation of Hybrid Metric Topological (HMT) SLAM.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
Scalar * iterator
Definition: eigen_plugins.h:26
STL namespace.
CHMHMapNode::TNodeID m_nodeTo
Definition: CHMHMapArc.h:48
const Scalar * const_iterator
Definition: eigen_plugins.h:27
int64_t THypothesisID
An integer number uniquely identifying each of the concurrent hypotheses for the robot topological pa...
CHMHMapNode::TNodeID m_nodeFrom
The origin/target nodes for this arc.
Definition: CHMHMapArc.h:48
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A set of hypothesis IDs, used for arcs and nodes in multi-hypothesis hybrid maps. ...
#define MRPT_END
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLsizei const GLchar ** string
Definition: glext.h:4101
#define MRPT_START
GLuint in
Definition: glext.h:7274
GLint level
Definition: glext.h:3600
The most high level class for storing hybrid, multi-hypothesis maps in a graph-based model...
#define DEFAULT_NODE_TYPE
#define NODE_TYPES
Used in constructor of mrpt::hmtslam::CHMHMapNode.
A class for representing an arc between two nodes in a hierarchical, multi-hypothesis map...
Definition: CHMHMapArc.h:31
A class for storing a sequence of arcs (a path).
GLuint GLuint GLsizei GLenum type
Definition: glext.h:3528
A class for representing a node in a hierarchical, multi-hypothesis map.
Definition: CHMHMapNode.h:36
mrpt::utils::TNodeID TNodeID
The type of the IDs of nodes.
Definition: CHMHMapNode.h:47



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019