OcmSceneGraphNode.h
Go to the documentation of this file.
1 
18 #pragma once
19 
20 #include <ros/ros.h>
21 #include <boost/shared_ptr.hpp>
22 
23 
24 #include <Eigen/Core>
25 #include <Eigen/Geometry>
26 #include <random>
27 #include <math.h>
28 #include "AttributedPoint.h"
29 
30 #include "GMM.h"
31 
32 namespace ASR
33 {
34 
35 class OcmSceneGraphNode
36 {
37 private:
38  bool isObserved;
39  std::vector<Eigen::Vector3f> samples; // Samples for position
40  std::vector<Eigen::Vector4f> samplesOrientation; // Samples for orientation
41  std::vector<boost::shared_ptr<OcmSceneGraphNode> > childs;
42  std::vector<GMM_Ptr> childsRelativePoseGmm; // gmm for the position
43  std::vector<GMM_Ptr> childsRelativeRotationGmm; // gmm for the orientation
44 
45  std::string name;
46 
48  unsigned int votes_per_node;
49 
50 
51  bool print_debug_messages;
52 
53 
54 public:
55 
59  OcmSceneGraphNode(bool enable_debug_mode = true)
60  {
61  this->name = "";
62  isObserved = false;
63 
64  // Get a parameter from the parameter server
65  print_debug_messages = enable_debug_mode;
66  }
67 
71  OcmSceneGraphNode(std::string name, Eigen::Vector3f root_position, Eigen::Vector4f root_orientation, int votes = 100, bool enable_debug_mode = true)
72  {
73  this->print_debug_messages = enable_debug_mode;
74 
75  this->name = name;
76  this->votes_per_node = votes;
77  observe(root_position, root_orientation);
78  }
79 
87  bool findNode(const std::string key, boost::shared_ptr<ASR::OcmSceneGraphNode> &out_pointer)
88  {
89  for(unsigned int i=0;i<childs.size(); i++)
90  {
91  //printf("Comparing '%s' to '%s' = %i\n", key.c_str(), childs.at(i)->getName().c_str(), getChild(i)->getName().compare(key) );
92 
93  if(getChild(i)->getName().compare(key) == 0)
94  {
95  //printf("Object %s found\n", key.c_str());
96  out_pointer = getChild(i);
97  return true;
98  }
99  }
100 
101  for(unsigned int i=0;i<childs.size(); i++)
102  {
103  if(childs.at(i)->findNode(key, out_pointer))
104  return true;
105  }
106 
107  if(print_debug_messages) printf("Object %s not found\n", key.c_str());
108  return false;
109  }
110 
114  void initNewChild(std::string name, boost::shared_ptr<OcmSceneGraphNode> parent, GMM_Ptr gmm, GMM_Ptr orientationGMM)
115  {
116  boost::shared_ptr<OcmSceneGraphNode> node = boost::shared_ptr<OcmSceneGraphNode>(new OcmSceneGraphNode(print_debug_messages));
117  node->setParent(parent);
118  childsRelativePoseGmm.push_back(gmm);
119  childsRelativeRotationGmm.push_back(orientationGMM);
120  node->setName(name);
121  node->setVotesPerNode(parent->getVotesPerNode());
122 
123  childs.push_back(node);
124  if(print_debug_messages) printf("Child %s added to node %s\n", name.c_str(), parent->getName().c_str());
125  }
126 
132  Eigen::Vector3f getPose()
133  {
134  if(isObjectObserved())
135  return samples.at(0);
136  else
137  {
138  std::random_device rd;
139  std::mt19937 mt(rd());
140 
141  std::uniform_real_distribution<float> rand_id(1, samples.size());
142  int id = floor(rand_id(mt));
143  return samples.at(id);
144  }
145  }
146 
147  Eigen::Vector4f getOrientation()
148  {
149  if(isObjectObserved())
150  return samplesOrientation.at(0);
151  else
152  {
153  std::random_device rd;
154  std::mt19937 mt(rd());
155 
156  std::uniform_real_distribution<float> rand_id(1, samplesOrientation.size());
157  int id = floor(rand_id(mt));
158  return samplesOrientation.at(id);
159  }
160  }
161 
166  void observe(Eigen::Vector3f position, Eigen::Vector4f orientation)
167  {
168  isObserved = true;
169 
170  samples.clear();
171  samples.push_back(position);
172 
173  samplesOrientation.clear();
174  samplesOrientation.push_back(orientation);
175 
176  //ROS_INFO("Object %s observed at %.2f, %.2f, %.2f.", getName().c_str(), position.x(), position.y(), position.z());
177  }
178 
184  void calcChildPoseHypothesesRecursive()
185  {
186  ROS_ASSERT(childs.size() == childsRelativePoseGmm.size());
187  if(print_debug_messages) ROS_INFO("Current Node %s, Number of childs: %i, Number of GMMs: %i, #Votes %i", name.c_str(), (int)childs.size(), (int)childsRelativePoseGmm.size(), getVotesPerNode());
188 
189  for(unsigned int i=0;i<childs.size();i++)
190  {
191  if (!childs.at(i)->isObjectObserved())
192  {
193  // Clear all old samples
194  childs.at(i)->samples.clear();
195 
196  //printf("Votes per node %i\n", getVotesPerNode());
197  for(unsigned int j=0; j < getVotesPerNode() ;j++)
198  {
199  std::vector<float> v;
200  Eigen::Vector3f hyp;
201  childsRelativePoseGmm.at(i)->sampleRandomValues(v);
202  hyp.x() = v.at(0);
203  hyp.y() = v.at(1);
204  hyp.z() = v.at(2);
205 
206  // The child poses are relative to the parents pose
207  hyp += this->getPose();
208  //ROS_INFO("Hyp (%s): %.2f %.2f %.2f", getChild(i)->getName().c_str(), hyp.x(), hyp.y(), hyp.z());
209 
210  v.clear();
211  Eigen::Vector4f orientationSample;
212  childsRelativeRotationGmm.at(i)->sampleRandomValues(v);
213 
214  orientationSample.w() = v.at(0);
215  orientationSample.x() = v.at(1);
216  orientationSample.y() = v.at(2);
217  orientationSample.z() = v.at(3);
218 
219  Eigen::Quaternion<float> q(orientationSample);
220  Eigen::Quaternion<float> q_parent(getOrientation());
221  // Rotate the childs orientation with the parents orientation
222  q = q_parent * q;
223 
224  orientationSample.w() = q.w();
225  orientationSample.x() = q.x();
226  orientationSample.y() = q.y();
227  orientationSample.z() = q.z();
228 
229  childs.at(i)->addSample(hyp, orientationSample);
230  }
231  }
232  // Traverse tree recursivly
233  childs.at(i)->calcChildPoseHypothesesRecursive();
234  }
235  }
236 
244  void collectPoseHypothesesRecursive(std::vector<ASR::AttributedPoint> &out_hypotheses,
245  std::vector<ASR::AttributedPoint> &out_found_objects)
246  {
247  // Add the nodes position to the samples
248  if(this->isObjectObserved())
249  {
250  Eigen::Vector3f v = this->getPose();
251  Eigen::Vector4f ori = this->getOrientation();
252  out_found_objects.push_back(AttributedPoint(getName().c_str(),
253  v.x(), v.y(), v.z(),
254  ori.w(), ori.x(), ori.y(), ori.z()));
255  }
256 
257  for(unsigned int i=0;i<childs.size();i++)
258  {
259  //for (Vector3f sample : getChild(i)->getPoseSamples())
260  for(unsigned int s=0; s < getChild(i)->getPoseSamples().size(); s++)
261  {
262  Eigen::Vector3f pos_sample = getChild(i)->getPoseSamples().at(s);
263  Eigen::Vector4f ori_sample = getChild(i)->getOrientationSamples().at(s);
264 
265  if(print_debug_messages) ROS_INFO("type %s, pos: (%.2f %.2f %.2f), ori: (%.2f %.2f %.2f %.2f) ",
266  getChild(i)->getName().c_str(),
267  pos_sample.x(), pos_sample.y(), pos_sample.z(),
268  ori_sample.w(), ori_sample.x(), ori_sample.y(), ori_sample.z());
269 
270  ASR::AttributedPoint hyp = AttributedPoint(getChild(i)->getName().c_str(),
271  pos_sample.x(), pos_sample.y(), pos_sample.z(),
272  ori_sample.w(), ori_sample.x(), ori_sample.y(), ori_sample.z());
273 
274 
275  if(getChild(i)->isObjectObserved())
276  {
277  out_found_objects.push_back(hyp);
278  } else
279  {
280  out_hypotheses.push_back(hyp);
281  }
282  }
283 
284  // Traverse tree recursivly
285  childs.at(i)->collectPoseHypothesesRecursive(out_hypotheses, out_found_objects);
286  }
287  }
288 
289 
290 
295  void setName(std::string name) { this->name = name;}
296  std::string getName() {return this->name;}
297 
298  //void setGMMs(std::vector<GaussianMixtureModel> childsRelativePoseGmm) { this->childsRelativePoseGmm = childsRelativePoseGmm;}
299 
300  bool isObjectObserved() {return isObserved;}
301 
302  std::vector<Eigen::Vector3f> getPoseSamples() { return samples; }
303  std::vector<Eigen::Vector4f> getOrientationSamples() { return samplesOrientation; }
304 
305  void setParent(boost::shared_ptr<OcmSceneGraphNode> p ) {this->parent = p;}
306 
307  void addSample(Eigen::Vector3f positionSample, Eigen::Vector4f orientationSample) { samples.push_back(positionSample); samplesOrientation.push_back(orientationSample); }
308 
309  boost::shared_ptr<OcmSceneGraphNode> getChild(unsigned int index) {
310  ROS_ASSERT(index < childs.size());
311  return childs.at(index);
312  }
313 
314  unsigned int getNumberOfChilds() {return childs.size();}
315 
316  unsigned int getVotesPerNode() { return this->votes_per_node;}
317 
318  Eigen::Vector3f getMeanOfChildPosition(unsigned int index)
319  {
320  ROS_ASSERT(index < childsRelativePoseGmm.size());
321  std::vector<float> means = childsRelativePoseGmm.at(index)->getModel(0)->getMean();
322  Eigen::Vector3f mean;
323  mean.x() = means.at(0);
324  mean.y() = means.at(1);
325  mean.z() = means.at(2);
326 
327  return mean;
328  }
329 
334  void setVotesPerNode(unsigned int n)
335  {
336  //printf("Set votes per node from node %s to %i\n", this->getName().c_str(), n);
337  this->votes_per_node = n;
338  for(unsigned int i=0;i<childs.size();i++)
339  {
340  childs.at(i)->setVotesPerNode(n);
341  }
342  }
343 
344 };
345 
346 typedef boost::shared_ptr<OcmSceneGraphNode> asrSceneGraphNodePtr;
347 
348 }
XmlRpcServer s
std::string getName(void *handle)
#define ROS_INFO(...)
boost::shared_ptr< GMM > GMM_Ptr
Definition: GMM.h:132
#define ROS_ASSERT(cond)


asr_recognizer_prediction_psm
Author(s): Braun Kai, Meißner Pascal
autogenerated on Wed Feb 19 2020 03:31:30