HierarchicalShapeModel.cpp
Go to the documentation of this file.
1 
19 
21 
23  {
24  ros::NodeHandle nodeHandle("~");
25  // Try to get the type of the conditional probability algorithm.
26  if(!nodeHandle.getParam("conditional_probability_algorithm", mConditionalProbabilityAlgorithm))
27  {
28  ROS_INFO_STREAM("Could not find ROS parameter conditional_probability_algorithm. Using standard method of using the minimum (value=\"minimum\").");
29  mConditionalProbabilityAlgorithm = "minimum"; // for compatability with old launch files.
30  }
31  }
32 
34  {
35  }
36 
37  void HierarchicalShapeModel::load(boost::property_tree::ptree& pPt)
38  {
39  // Load the volume of the space we're working in. It is required for the uniform distribution.
40  mWorkspaceVolume = pPt.get<double>("shape.root.<xmlattr>.volume");
41 
42  unsigned int id = 1;
43  // this, the root, implicitly has id 0.
44 
45  // Load the childs of the root node.
46  for(boost::property_tree::ptree::value_type &v: pPt.get_child("shape.root"))
47  {
48  // Only access the 'child' child nodes.
49  if(!std::strcmp(v.first.c_str(), "child"))
51  }
52 
53  // Assign references based on the IDs and set parent types:
54  std::vector<boost::shared_ptr<HierarchicalShapeModelNode>> nodesToVisit;
56  nodesToVisit.push_back(child);
57 
58  std::vector<boost::shared_ptr<HierarchicalShapeModelNode>> references;
59  std::map<unsigned int, boost::shared_ptr<HierarchicalShapeModelNode>> nonReferencesById;
60  while(!nodesToVisit.empty())
61  {
62  // pop first node:
63  boost::shared_ptr<HierarchicalShapeModelNode> currentNode = nodesToVisit[0];
64  nodesToVisit.erase(nodesToVisit.begin());
65 
66  unsigned int referenceTo;
67  bool isReference = currentNode->isReference(referenceTo);
68  if (!isReference)
69  {
70  nonReferencesById[referenceTo] = currentNode;
71  for (boost::shared_ptr<HierarchicalShapeModelNode> child: currentNode->getChildren())
72  nodesToVisit.push_back(child);
73  }
74  else
75  {
76  references.push_back(currentNode);
77  }
78  }
79 
80  for (boost::shared_ptr<HierarchicalShapeModelNode> currentReference: references)
81  {
82  unsigned int referenceTo;
83  if (!currentReference->isReference(referenceTo)) throw std::runtime_error("Trying to make a non-reference node a reference.");
84 
85  currentReference->setReferencedNode(nonReferencesById[referenceTo]);
86  }
87  }
88 
90  {
91  // Store a copy of the primary scene object visualizer.
92  mVisualizer = mSuperior;
93 
94  // Forward visualizer to the child nodes representing the secondary scene objects.
95  for(unsigned int i = 0; i < mChildren.size(); i++)
96  mChildren[i]->initializeVisualizer(mSuperior);
97  }
98 
99  double HierarchicalShapeModel::calculateProbabilityForHypothesis(std::vector<ISM::Object> pEvidenceList, std::vector<unsigned int> pAssignments)
100  {
101  for (boost::shared_ptr<HierarchicalShapeModelNode> child: mChildren) child->resetVisit();
102 
103  double result = 1.0;
104 
105  // If no part was assigned to the root node, we don't need to continue (because the model doesn't allow cases like this).
106  // Otherwise go down the rabbit hole and see what you'll find!
107  if(pAssignments[0] > 0)
108  {
109 
110  // This helper variable keeps track of the slot we're currently on. It will be given via reference to the nodes.
111  // We have a tree structure and do a iteration similar to deep search here to 'serialize the tree'.
112  unsigned int slotId = 0;
113 
114  // Converts the AsrObject assigned to this slot into the Pose data structure.
115  // Subtract one from the assignment, because the first evidence is stored at position zero.
116  mAbsolutePose.reset(new ISM::Pose(*pEvidenceList[pAssignments[0] - 1].pose));
117 
118  // Evaluate evidence for root node under uniform distribution (FOR EVERY DIMENSION. Could only be done, it a root object was assigned to the root node.
119  // THIS IS NECESSARY! When we have only one object, a scene containing it and a background scene,
120  // then the probabilities MUST BE 50/50 (of course assumed that both scenes only consider shape)!
121  double rootResult = 1.0 / (mWorkspaceVolume * 2.0 * pow(M_PI, 2.0));
122  result *= rootResult;
123 
124  // vector to hold the conditional probability of each slot:
125  std::vector<boost::shared_ptr<ConditionalProbability>> conditionalProbabilities(pAssignments.size());
126  // initialize conditional probabilites:
127  if (mConditionalProbabilityAlgorithm == "minimum")
128  for (unsigned int i = 0; i < conditionalProbabilities.size(); i++)
130  else if (mConditionalProbabilityAlgorithm == "multiplied")
131  for (unsigned int i = 0; i < conditionalProbabilities.size(); i++)
133  else if (mConditionalProbabilityAlgorithm == "root_of_multiplied")
134  for (unsigned int i = 0; i < conditionalProbabilities.size(); i++)
136  else if (mConditionalProbabilityAlgorithm == "average")
137  for (unsigned int i = 0; i < conditionalProbabilities.size(); i++)
139  else throw std::runtime_error("In HierarchicalShapeModel::calculateProbabilityForHypothesis(): conditional probability algorithm type "
140  + mConditionalProbabilityAlgorithm + " is invalid. Valid types are minimum, multiplied, root_of_multiplied, average.");
141 
142  // set probability of reference object (represented by this class):
143  conditionalProbabilities[0]->setProbability("Root", rootResult);
144 
145  // Iterate over all children, assign the given evidence to them and evaluate
147  {
148  child->setParentObjectType(mVisualizer->getSceneObjectName());
149 
150  // Update the transformation from world coordinates to parent coordinates.
151  child->setAbsoluteParentPose(mAbsolutePose);
152 
153  // If zero-object was assigned to child, continue iterating down the tree to set the right slot id,
154  // but don't use the probabilities based on the occluded subtree.
155  result *= child->calculateProbabilityForHypothesis(pEvidenceList, pAssignments, slotId, pAssignments[0] == 0, conditionalProbabilities);
156  }
157 
158  double slotProduct = 1.0;
159  for (boost::shared_ptr<ConditionalProbability> cP: conditionalProbabilities) {
160  double slotProbability = cP->getProbability();
161  //ROS_DEBUG_STREAM("Parent probabilities: " << cP->printParentProbabilities() << " => " << slotProbability);
162  slotProduct *= slotProbability;
163  }
164  // keep the old result around for comparison:
165  //ROS_DEBUG_STREAM("Product of all probabilities: " << result << ". Product of conditional probabilities: " << slotProduct);
166 
167  result = slotProduct; // overwrite it for output.
168 
169  // Forward position of this primary scene object to visualizer.
170  mVisualizer->setBestPoseCandidate(mAbsolutePose);
171  } else {
172  // A hypothesis without an assigned root object is invalid and will therefore be scored with the impossible event.
173  result = 0.0;
174  }
175  return result;
176  }
177 
178  void HierarchicalShapeModel::visualize(std::vector<ISM::Object> pEvidenceList)
179  {
180  // Get the name of the primary scene object.
181  std::string name = mVisualizer->getSceneObjectName();
182 
183  // Try to find evidence for this scene object.
184  for(unsigned int i = 0; i < pEvidenceList.size(); i++)
185  {
186  // Is this evidence for this scene object?
187  if(name.compare(pEvidenceList[i].type) == 0)
188  {
189  // Extract pose of the object.
190  mAbsolutePose.reset(new ISM::Pose(*pEvidenceList[i].pose));
191 
192  // Forward the absolute pose of the primary scene object to the visualizer.
193  mVisualizer->setPose(mAbsolutePose);
194 
195  // Forward the pose of this node as parent node pose to the child nodes.
196  for(unsigned int i = 0; i < mChildren.size(); i++)
197  mChildren[i]->setAbsoluteParentPose(mAbsolutePose);
198  }
199  }
200 
201  // Forward visualization command to the child nodes representing the secondary scene objects.
202  for(unsigned int i = 0; i < mChildren.size(); i++)
203  mChildren[i]->visualize(pEvidenceList);
204  }
205 
207  {
208  unsigned int result = 1;
209 
210  // Let the children of this node count their children.
212  {
213  result += child->getNumberOfNodes();
214  }
215  return result;
216  }
217 
218 }
void initializeVisualizer(boost::shared_ptr< Visualization::ProbabilisticPrimarySceneObjectVisualization > mSuperior)
double calculateProbabilityForHypothesis(std::vector< ISM::Object > pEvidenceList, std::vector< unsigned int > pAssignments)
boost::shared_ptr< Visualization::ProbabilisticPrimarySceneObjectVisualization > mVisualizer
void visualize(std::vector< ISM::Object > pEvidenceList)
std::vector< boost::shared_ptr< HierarchicalShapeModelNode > > mChildren
#define ROS_INFO_STREAM(args)
bool getParam(const std::string &key, std::string &s) const


asr_psm
Author(s): Braun Kai, Gehrung Joachim, Heizmann Heinrich, Meißner Pascal
autogenerated on Fri Nov 15 2019 03:57:54