HierarchicalShapeModelNode.cpp
Go to the documentation of this file.
1 
19 
21 
22  HierarchicalShapeModelNode::HierarchicalShapeModelNode(boost::property_tree::ptree& pPt, unsigned int& pID): mWasVisited(false)
23  {
24  // Initialize shared pointer to gaussian mixture distribution;
27 
28  // Execute the loading process.
29  load(pPt, pID);
30  }
31 
33  {
34  }
35 
36  void HierarchicalShapeModelNode::load(boost::property_tree::ptree& pPt, unsigned int& pID)
37  {
38  // Load the name of the object that is represented by this node.
39  mSceneObject = pPt.get<std::string>("<xmlattr>.name");
40 
41  // Try to load the index of the object referenced by this node. If attribute is not found, node is not a reference.
42  boost::optional<std::string> referenceTo = pPt.get_optional<std::string>("<xmlattr>.references");
43  if (referenceTo)
44  {
45  mIsReference = true;
46  mReferenceTo = boost::lexical_cast<unsigned int>(*referenceTo);
47  }
48  else
49  {
50  mIsReference = false;
51  mReferenceTo = pID;
52  pID++;
53  }
54 
55  // Load the gaussian mixture distribution associated with this node.
56  mGaussianMixtureDistributionPosition->load(pPt, "position");
57  mGaussianMixtureDistributionOrientation->load(pPt, "orientation");
58 
59  // Load the childs of the node, if there are any.
60  for(boost::property_tree::ptree::value_type &v: pPt)
61  {
62  // Only access the 'child' nodes.
63  if(!std::strcmp(v.first.c_str(), "child"))
64  {
66  child->setParentObjectType(mSceneObject);
67  mChildren.push_back(child);
68  }
69  }
70  }
71 
73  {
74  // Debug message.
75  ROS_INFO_STREAM("Initializing visualizer for secondary scene object '" << mSceneObject << "'.");
76 
77  // Create a new coordinator for seondary scene object visualization.
78  mVisualizer.reset(new Visualization::ProbabilisticSecondarySceneObjectVisualization());
79 
80  // Append it to supperior visualizer.
81  mSuperior->appendVisualizer(mVisualizer);
82 
83  // Initialize the visualizer for the gaussian mixture distribution.
85 
86  // Iterate over all child nodes and append them, too!
87  for(unsigned int i = 0; i < mChildren.size(); i++)
88  mChildren[i]->initializeVisualizer(mSuperior);
89  }
90 
92  {
93  mAbsoluteParentPose = pPose;
94  }
95 
96  double HierarchicalShapeModelNode::calculateProbabilityForHypothesis(std::vector<ISM::Object> pEvidenceList, std::vector<unsigned int> pAssignments, unsigned int& pSlotId, bool pCut,
97  std::vector<boost::shared_ptr<ConditionalProbability>>& pConditionalProbabilities)
98  {
99  double result = 1.0;
100 
101  unsigned int oldSlotId = pSlotId;
102  if (!mIsReference)
103  // Go to the next slot.
104  pSlotId++;
105  else pSlotId = mReferenceTo;
106 
107  std::vector<boost::shared_ptr<HierarchicalShapeModelNode>> children;
108  if (!mIsReference) children = mChildren;
109  else children = mReferencedNode->getChildren();
110 
111  // Subtree already cut?
112  if(pCut || pAssignments[pSlotId] == 0)
113  {
114  if (!mWasVisited)
115  pConditionalProbabilities[pSlotId]->setProbability(mParentObject, 1.0); // only if this node never gets visited through a not cut path, the associated probability remains set to 1.
116  // Continue moving down the tree to increment the slot id.
118  child->calculateProbabilityForHypothesis(pEvidenceList, pAssignments, pSlotId, true, pConditionalProbabilities);
119  }
120  else
121  {
122  mWasVisited = true;
123 
124  // Extract the pose of the object associates with this node/slot and convert it into the parent frame.
125  mAbsolutePose.reset(new ISM::Pose(*pEvidenceList[pAssignments[pSlotId] - 1].pose));
126  mAbsolutePose->convertPoseIntoFrame(mAbsoluteParentPose, mRelativePose);
127 
128  // Evaluate the relative pose under the the probability distribution describing the scene shape.
129  // We remember: the scene shape is defined in the coordinate frame of the parent node.
130  double scorePos = mGaussianMixtureDistributionPosition->evaluate(mRelativePose);
131  double scoreOri = mGaussianMixtureDistributionOrientation->evaluate(mRelativePose);
132  double score = scorePos * scoreOri;
133 
134  pConditionalProbabilities[pSlotId]->setProbability(mParentObject, score);
135 
136  //ROS_DEBUG_STREAM("Pose fitting report for scene object '" << mSceneObject <<"'. Position is " << scorePos << ", orientation is " << scoreOri << ". Total is " << score << ".");
137 
138  // Add score to global result.
139  result *= score;
140 
141  // Forward the current score to the visualizer. If this is part of the best hypothesis,
142  // it will be used for coloring the link to the parent object.
143  mVisualizer->setBestPoseCandidate(score);
144 
145  // Ok, now we need to give the pose of this object to the children of this node.
146  // Using this information and the evidence they're also able to calculate their probabilities.
148  {
149  // Update the tranformation of the child node (from world frame to this nodes frame).
150  child->setAbsoluteParentPose(mAbsolutePose);
151 
152  // If zero-object was assigned to child, continue iterating down the tree to INCREMENT THE SLOT ID.
153  // The returned probability will be one, so it has no influence at all.
154  result *= child->calculateProbabilityForHypothesis(pEvidenceList, pAssignments, pSlotId, false, pConditionalProbabilities);
155  }
156 
157  // Forward position of this primary scene object to visualizer.
158  mVisualizer->setBestCandidatePose(mAbsolutePose);
159  }
160 
161  if (mIsReference) pSlotId = oldSlotId;
162 
163  return result;
164  }
165 
166  void HierarchicalShapeModelNode::visualize(std::vector<ISM::Object> pEvidenceList)
167  {
168  // Try to find evidence for this scene object.
169  for(unsigned int i = 0; i < pEvidenceList.size(); i++)
170  {
171  // Get the object.
172  ISM::Object object = pEvidenceList[i];
173 
174 
175  // Is this evidence for this scene object (assumed that there is no detection uncertainty)?
176  // If yes and the parent node was also detected, then update the position.
177  if(mSceneObject.compare(object.type) == 0 && mAbsoluteParentPose)
178  {
179  // Extract the pose of the object associates with this node/slot and convert it into the parent frame.
180  mAbsolutePose.reset(new ISM::Pose(*object.pose));
181  mAbsolutePose->convertPoseIntoFrame(mAbsoluteParentPose, mRelativePose);
182 
183  /********************************************************
184  * Now we draw.
185  ********************************************************/
186 
187  // Apply last known absolut object position to the visualizer.
188  mVisualizer->setLastPose(mAbsolutePose);
189 
190  // Apply the absolute position of the parent object to the visualizer.
191  mVisualizer->setParentPose(mAbsoluteParentPose);
192 
193  // Based on the assumption that there is no detection uncertainty for object types,
194  // evaluate the gaussian mixture and visualize the uncertainty for the detection.
196 
197  // For the given child node, set the pose of this node as parent pose in world.
198  for(unsigned int i = 0; i < mChildren.size(); i++)
200  }
201  }
202 
203  // Forward evidence to all child nodes.
204  for(unsigned int i = 0; i < mChildren.size(); i++)
205  mChildren[i]->visualize(pEvidenceList);
206  }
207 
209  {
210  if (mIsReference) return 0; // References not counted as unique nodes
211 
212  unsigned int result = 1;
213 
214  // Let the children of this node count their children.
216  {
217  result += child->getNumberOfNodes();
218  }
219  return result;
220  }
221 
222  bool HierarchicalShapeModelNode::isReference(unsigned int& pReferenceTo)
223  {
224  pReferenceTo = mReferenceTo;
225  return mIsReference;
226  }
227 
228  std::vector<boost::shared_ptr<HierarchicalShapeModelNode>> HierarchicalShapeModelNode::getChildren()
229  {
230  return mChildren;
231  }
232 
234  {
235  if (mSceneObject != pReferencedNode->getSceneObjectType())
236  throw std::runtime_error("Node with type " + mSceneObject + " is trying to reference a node of different type " + pReferencedNode->getSceneObjectType());
237  mReferencedNode = pReferencedNode;
238  }
239 
241  {
242  mWasVisited = false;
243  for (boost::shared_ptr<HierarchicalShapeModelNode> child: mChildren) child->resetVisit();
244  }
245 }
boost::shared_ptr< HierarchicalShapeModelNode > mReferencedNode
void visualize(std::vector< ISM::Object > pEvidenceList)
std::vector< boost::shared_ptr< HierarchicalShapeModelNode > > getChildren()
boost::shared_ptr< GaussianMixtureDistribution > mGaussianMixtureDistributionPosition
boost::shared_ptr< Visualization::ProbabilisticSecondarySceneObjectVisualization > mVisualizer
HierarchicalShapeModelNode(boost::property_tree::ptree &pPt, unsigned int &pID)
double calculateProbabilityForHypothesis(std::vector< ISM::Object > pEvidenceList, std::vector< unsigned int > pAssignments, unsigned int &pSlotId, bool pCut, std::vector< boost::shared_ptr< ConditionalProbability >> &pConditionalProbabilities)
void initializeVisualizer(boost::shared_ptr< Visualization::ProbabilisticPrimarySceneObjectVisualization > mSuperior)
boost::shared_ptr< GaussianMixtureDistribution > mGaussianMixtureDistributionOrientation
void setAbsoluteParentPose(boost::shared_ptr< ISM::Pose > pPose)
std::vector< boost::shared_ptr< HierarchicalShapeModelNode > > mChildren
#define ROS_INFO_STREAM(args)
void load(boost::property_tree::ptree &pPt, unsigned int &pID)
void setReferencedNode(boost::shared_ptr< HierarchicalShapeModelNode > pReferencedNode)


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