24 std::string sceneName,
27 std::string base_frame =
"/map")
29 reference_object(objects.at(0)->type.c_str()),
30 number_obsereved_objects(1),
31 number_of_votes(votes),
32 publish_hypothesis(true),
33 publish_hypothesis_topic(
"pose_prediction_psm_hypothesis")
35 if (objects.size() < 1)
36 ROS_ERROR(
"There has to be at least one reference object");
44 std::string reference_object_name;
45 unsigned int counter = 0;
51 if(!tempObject->sampledPoses.size()){
52 std::cerr <<
"Got a AsrObject without poses." << std::endl;
56 geometry_msgs::PoseWithCovariance currentPose = tempObject->sampledPoses.front();
58 ROS_INFO(
"\nStarted pose prediciton. Reference object name %s", tempObject->type.c_str());
64 Eigen::Vector3f position = Eigen::Vector3f(currentPose.pose.position.x,currentPose.pose.position.y, currentPose.pose.position.z);
66 Eigen::Vector4f ori = Eigen::Vector4f(currentPose.pose.orientation.w,
67 currentPose.pose.orientation.x,
68 currentPose.pose.orientation.y,
69 currentPose.pose.orientation.z);
73 init(sceneName, tempObject->type.c_str(), position, ori, base_frame);
75 reference_object_name = tempObject->type;
86 for(
unsigned int i=0;i<objects.size();i++)
88 if(reference_object_name.compare(objects.at(i)->type) != 0)
89 onObjectMsg(objects.at(i));
95 std::string sceneName,
96 std::string reference_object_name,
97 Eigen::Vector3f reference_object_position,
98 Eigen::Vector4f reference_object_orientation,
100 std::string base_frame =
"/map")
109 init( sceneName, reference_object_name, reference_object_position, reference_object_orientation, base_frame);
115 void asrPosePredictionEngine::init(std::string sceneName,
116 std::string reference_object_name,
117 Eigen::Vector3f reference_object_position,
118 Eigen::Vector4f reference_object_orientation,
119 std::string base_frame =
"/map")
155 xml_node<> * root_node;
158 std::ifstream file(
path);
159 std::stringstream buffer;
160 buffer << file.rdbuf();
162 std::string content(buffer.str());
164 if(content.size() < 2)
171 doc.parse<0>(&content[0]);
174 root_node = doc.first_node(
"psm");
177 for (xml_node<> * brewery_node = root_node->first_node(
"scenes"); brewery_node; brewery_node = brewery_node->next_sibling())
181 for(xml_node<> * scene_node = brewery_node->first_node(
"scene"); scene_node; scene_node = scene_node->next_sibling())
183 std::string name = scene_node->first_attribute(
"name")->value();
187 printf(
"Scene: %s, name: %s, priori %s \n",
188 scene_node->first_attribute(
"type")->value(),
190 scene_node->first_attribute(
"priori")->value());
195 if(name.compare(sceneName) == 0)
198 for(xml_node<> * object_node = scene_node->first_node(
"object"); object_node; object_node = object_node->next_sibling())
202 std::string object_name = object_node->first_attribute(
"name")->value();
204 if(object_name.compare(reference_object_name) == 0)
208 printf(
"\nLoaded reference object. name: %s, type: %s, priori %s \n",
209 object_node->first_attribute(
"name")->value(),
210 object_node->first_attribute(
"type")->value(),
211 object_node->first_attribute(
"priori")->value());
216 if(
print_debug_messages) printf(
"Number of objects in the scene %i\n", number_objects_in_scene);
227 for(xml_node<> * object_attributes = object_node->first_node(
"shape"); object_attributes; object_attributes = object_attributes->next_sibling())
230 for(xml_node<> * shape_attributes = object_attributes->first_node(
"root"); shape_attributes; shape_attributes = shape_attributes->next_sibling())
235 for(xml_node<> * root_children = shape_attributes->first_node(
"child"); root_children; root_children = root_children->next_sibling())
245 for(xml_node<> * position_node = root_children->first_node(
"position"); position_node; position_node = position_node->next_sibling())
247 for(xml_node<> * kernel_node = position_node->first_node(
"kernel"); kernel_node; kernel_node = kernel_node->next_sibling())
251 float kernel_weight = atof(kernel_node->first_attribute(
"weight")->value());
252 std::vector<float> pos;
253 std::vector<float> cov;
254 initVectorFromCSVString(pos, kernel_node->first_attribute(
"mean")->value());
255 initVectorFromCSVString(cov, kernel_node->first_attribute(
"covariance")->value());
257 if (pos.size() == 3 && cov.size() == 9)
259 MatrixXf mat = initMatrixXf(cov, 3,3);
260 gmm->addGaussianModel(kernel_weight, pos, mat);
269 for(xml_node<> * orientation_node = root_children->first_node(
"orientation"); orientation_node; orientation_node = orientation_node->next_sibling())
271 for(xml_node<> * kernel_node2 = orientation_node->first_node(
"kernel"); kernel_node2; kernel_node2 = kernel_node2->next_sibling())
276 float kernel_weight = atof(kernel_node2->first_attribute(
"weight")->value());
277 std::vector<float> pos;
278 std::vector<float> cov;
279 initVectorFromCSVString(pos, kernel_node2->first_attribute(
"mean")->value());
280 initVectorFromCSVString(cov, kernel_node2->first_attribute(
"covariance")->value());
282 if (pos.size() == 4 && cov.size() == 16)
284 MatrixXf mat = initMatrixXf(cov, 4, 4);
285 orientationGMM->addGaussianModel(kernel_weight, pos, mat);
292 root->initNewChild(root_children->first_attribute(
"name")->value(),
root, gmm, orientationGMM);
295 std::string name = root_children->first_attribute(
"name")->value();
296 traverseSceneShapeRecursive(name, root_children,
root);
313 unsigned int asrPosePredictionEngine::distributeVotes()
323 bool asrPosePredictionEngine::observeObject(std::string object_name, Eigen::Vector3f position, Eigen::Vector4f orientation)
327 ROS_INFO(
"Warning there ist no root object. Bobserving obj failed!");
331 bool integrated_evidence =
false;
333 asrSceneGraphNodePtr observed;
338 if(
getSceneGraphRoot()->findNode(object_name, observed) && !observed->isObjectObserved())
343 observed->observe(position, orientation);
344 int n = distributeVotes();
347 integrated_evidence =
true;
348 if(
print_debug_messages) printf(
"Observed object %s, Set votes per node to %i\n\n", object_name.c_str(), n);
352 integrated_evidence =
false;
356 return integrated_evidence;
365 for(
auto p : hypothesis_list)
367 asr_msgs::AsrAttributedPoint point;
368 point.type = p.objectType;
375 void asrPosePredictionEngine::calcHypotheses()
385 ROS_ERROR(
"Warning: calcHypothesis: There is no Scene Graph build. Return.");
389 bool new_evidence_integrated =
false;
394 if(!pObject->sampledPoses.size()){
395 std::cerr <<
"Got a AsrObject without poses." << std::endl;
399 auto position = pObject->sampledPoses.front().pose.position;
401 printf(
"A new was object observed! object %s at (%.2f, %.2f, %.2f) %s\n",
402 pObject->type.c_str(),
403 position.x, position.y, position.z,
404 pObject->header.frame_id.c_str());
406 auto evidence = pObject;
409 transformObject(pObject, this->
baseFrameId.c_str());
412 auto orientation = pObject->sampledPoses.front().pose.orientation;
417 bool b = observeObject(evidence->type,
418 Eigen::Vector3f(position.x, position.y, position.z),
419 Eigen::Vector4f(orientation.w, orientation.x, orientation.y, orientation.z));
420 new_evidence_integrated |= b;
422 if(new_evidence_integrated)
424 ROS_INFO (
"Object %s transformed to frame %s (%.2f, %.2f, %.2f) (w: %.2f, x: %.2f, y: %.2f, z: %.2f,)",
426 position.x, position.y, position.z,
427 orientation.w, orientation.x, orientation.y, orientation.z);
428 ROS_INFO(
"Object %s will be integrated into the scene graph", evidence->type.c_str());
442 if(new_evidence_integrated)
447 ROS_INFO(
"Evidence(s) successfully integrated into scene graph");
452 evidence_buffer.clear();
455 void asrPosePredictionEngine::collectPoseHypothesesRecursive(std::vector<ASR::AttributedPoint> &out_hypotheses,
456 std::vector<ASR::AttributedPoint> &out_found_objects)
466 ROS_ERROR(
"Warning: collectHypothesis: There is no Scene Graph build. Return.");
469 getSceneGraphRoot()->collectPoseHypothesesRecursive(out_hypotheses, out_found_objects);
478 if(o->type.compare(pObject->type) == 0)
480 if(
print_debug_messages) printf(
"Observation callback. Object %s already in buffer.\n", pObject->type.c_str());
486 printf(
"Observation callback. Put object %s to evidence buffer.\n", pObject->type.c_str());
488 evidence_buffer.push_back(pObject);
495 if(in_out_object->header.frame_id.compare(targetFrame) == 0)
505 }
catch(std::exception e)
507 ROS_ERROR(
"Failed to resolve transformation in target coordinate frame. Object: %s frame: %s",
508 in_out_object->type.c_str(), targetFrame.c_str());
512 if(!in_out_object->sampledPoses.size()){
513 std::cerr <<
"Got a AsrObject without poses." << std::endl;
518 auto position = in_out_object->sampledPoses.front().pose.position;
519 auto orientation = in_out_object->sampledPoses.front().pose.orientation;
521 ROS_INFO (
"Object %s transformed to frame %s (%.2f, %.2f, %.2f) (w: %.2f, x: %.2f, y: %.2f, z: %.2f,)",
522 in_out_object->type.c_str(), targetFrame.c_str(),
523 position.x, position.y, position.z,
524 orientation.w, orientation.x, orientation.y, orientation.z);
530 void asrPosePredictionEngine::traverseSceneShapeRecursive(std::string parent_name, xml_node<> * parent_node, ASR::asrSceneGraphNodePtr
root)
533 for(xml_node<> * child = parent_node->first_node(
"child"); child; child = child->next_sibling())
538 for(xml_node<> * position_node = child->first_node(
"position"); position_node; position_node = position_node->next_sibling())
540 for(xml_node<> * kernel_node = position_node->first_node(
"kernel"); kernel_node; kernel_node = kernel_node->next_sibling())
544 float kernel_weight = atof(kernel_node->first_attribute(
"weight")->value());
545 std::vector<float> pos;
546 std::vector<float> cov;
547 initVectorFromCSVString(pos, kernel_node->first_attribute(
"mean")->value());
548 initVectorFromCSVString(cov, kernel_node->first_attribute(
"covariance")->value());
550 if (pos.size() == 3 && cov.size() == 9)
552 MatrixXf mat = initMatrixXf(cov, 3,3);
553 gmm->addGaussianModel(kernel_weight, pos, mat);
559 for(xml_node<> * orientation_node = child->first_node(
"orientation"); orientation_node; orientation_node = orientation_node->next_sibling())
561 for(xml_node<> * kernel_node2 = orientation_node->first_node(
"kernel"); kernel_node2; kernel_node2 = kernel_node2->next_sibling())
565 float kernel_weight = atof(kernel_node2->first_attribute(
"weight")->value());
566 std::vector<float> pos;
567 std::vector<float> cov;
568 initVectorFromCSVString(pos, kernel_node2->first_attribute(
"mean")->value());
569 initVectorFromCSVString(cov, kernel_node2->first_attribute(
"covariance")->value());
571 if (pos.size() == 4 && cov.size() == 16)
573 MatrixXf mat = initMatrixXf(cov, 4, 4);
574 orientationGMM->addGaussianModel(kernel_weight, pos, mat);
583 ASR::asrSceneGraphNodePtr node;
584 if(root->findNode(parent_name.c_str(), node))
587 node->initNewChild(child->first_attribute(
"name")->value(), node, gmm, orientationGMM);
593 std::string child_name = child->first_attribute(
"name")->value();
594 traverseSceneShapeRecursive(child_name, child, root);
599 void asrPosePredictionEngine::initVectorFromCSVString(std::vector<float> &
x, std::string csv)
601 std::stringstream ss(csv);
607 if (ss.peek() ==
',' || ss.peek() ==
' ')
613 MatrixXf asrPosePredictionEngine::initMatrixXf(std::vector<float>
values,
unsigned int rows,
unsigned int cols)
618 MatrixXf x(rows, cols);
619 for(
unsigned int i=0; i < rows;i++)
621 for(
unsigned int j=0; j < cols; j++)
623 x(i,j) = values.at(i*cols + j);
void publish(const boost::shared_ptr< M > &message) const
void publishHypothesisMessages(std::vector< ASR::AttributedPoint > hypothesis_list)
std::vector< double > values
ASR::asrSceneGraphNodePtr root
ASR::asrSceneGraphNodePtr getSceneGraphRoot()
unsigned int getNumberOfMissingObjects()
ProbabilisticSceneRecognition::ObjectTransformation mObjectTransform
int number_objects_in_scene
std::vector< boost::shared_ptr< asr_msgs::AsrObject > > evidence_buffer
asrPosePredictionEngine(std::string pathToXML, std::string sceneName, std::vector< boost::shared_ptr< asr_msgs::AsrObject >> objects, int votes, std::string base_frame)
TFSIMD_FORCE_INLINE const tfScalar & x() const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
std::string publish_hypothesis_topic
bool getParam(const std::string &key, std::string &s) const
bool print_debug_messages
ros::Publisher hypothesis_publisher
int number_obsereved_objects
std::string reference_object