InteractiveWorldModelClient.cpp
Go to the documentation of this file.
00001 
00011 // worldlib
00012 #include "worldlib/geometry/Pose.h"
00013 #include "worldlib/model/PlacementModel.h"
00014 #include "worldlib/remote/InteractiveWorldModelClient.h"
00015 
00016 // ROS
00017 #include <ros/ros.h>
00018 
00019 // JSON
00020 #include <jsoncpp/json/json.h>
00021 
00022 using namespace std;
00023 using namespace rail::spatial_temporal_learning::worldlib::geometry;
00024 using namespace rail::spatial_temporal_learning::worldlib::model;
00025 using namespace rail::spatial_temporal_learning::worldlib::remote;
00026 using namespace rail::spatial_temporal_learning::worldlib::world;
00027 
00028 InteractiveWorldModelClient::InteractiveWorldModelClient(const InteractiveWorldModelClient &client) : HttpClient(client)
00029 {
00030 }
00031 
00032 InteractiveWorldModelClient::InteractiveWorldModelClient(const string &host, const uint16_t port)
00033     : HttpClient(host, port)
00034 {
00035 }
00036 
00037 bool InteractiveWorldModelClient::getTaskModel(const uint32_t task_id, TaskModel &task_model) const
00038 {
00039   // build the URL request
00040   stringstream ss;
00041   ss << "iwmodels/view/" << task_id;
00042 
00043   // make the GET request
00044   const string &response = this->get(ss.str());
00045   // check the size
00046   if (response.empty())
00047   {
00048     ROS_WARN("No interactive world models found for task ID %lu.", (ulong) task_id);
00049     return false;
00050   } else
00051   {
00052     // set the ID
00053     task_model.setTaskID(task_id);
00054 
00055     // parse the root of the JSON response
00056     Json::Reader reader;
00057     Json::Value json_response;
00058     reader.parse(response, json_response, false);
00059     const Json::Value &iwmodel = json_response["Iwmodel"];
00060     const string &value = iwmodel["value"].asString();
00061 
00062     // parse the models array
00063     Json::Value value_json;
00064     reader.parse(value, value_json, false);
00065     const Json::Value &models = value_json["models"];
00066     // go through each
00067     for (int i = 0; i < models.size(); i++)
00068     {
00069       const Json::Value &json_model = models[i];
00070 
00071       // parse the Gaussian values
00072       const double decision_value = json_model["decision_value"].asDouble();
00073       const double sigma_x = json_model["sigma_x"].asDouble();
00074       const double sigma_y = json_model["sigma_y"].asDouble();
00075       const double sigma_theta = json_model["sigma_theta"].asDouble();
00076 
00077       // parse the placement
00078       const Json::Value &json_placement = json_model["placement"];
00079       const Json::Value &json_position = json_placement["position"];
00080       const Position placement_position(json_position["x"].asDouble(), json_position["y"].asDouble(),
00081                                         json_position["z"].asDouble());
00082       const Orientation placement_orientation(json_placement["rotation"].asDouble());
00083       const Pose placement_pose(placement_position, placement_orientation);
00084 
00085       // parse the item
00086       const Json::Value &json_item = json_placement["item"];
00087       const string &item_name = json_item["name"].asString();
00088       // sizes are only in two dimensions in the interactive world
00089       const double item_width = json_item["width"].asDouble();
00090       const double item_depth = json_item["height"].asDouble();
00091       // there is no reference frame for the item in the interactive world
00092       const Item item(item_name, "", placement_pose, item_width, item_depth);
00093 
00094       // there is no reference frame for the reference object in the interactive world (they are indexed by names)
00095       const Object object(json_placement["reference_frame_id"].asString());
00096 
00097       // parse the surface it was placed on
00098       const Json::Value &json_surface = json_placement["surface"];
00099       const string &surface_name = json_surface["name"].asString();
00100       // sizes are only in two dimensions in the interactive world
00101       const double surface_width = json_surface["width"].asDouble();
00102       const double surface_depth = json_surface["height"].asDouble();
00103       // there is no reference frame for the item in the interactive world (they are indexed by names)
00104       const Surface surface(surface_name, "", Pose(), surface_width, surface_depth);
00105 
00106       // build the final model
00107       Placement placement(item, object, surface);
00108       PlacementModel placement_model(placement, decision_value, sigma_x, sigma_y, sigma_theta);
00109 
00110       task_model.addPlacementModel(placement_model);
00111     }
00112 
00113     return true;
00114   }
00115 }
00116 
00117 bool InteractiveWorldModelClient::getTaskItems(const uint32_t task_id, vector<Item> &items) const
00118 {
00119   // load the task model
00120   TaskModel task_model;
00121   if (this->getTaskModel(task_id, task_model))
00122   {
00123     task_model.getUniqueItems(items);
00124     return items.size() > 0;
00125   } else
00126   {
00127     return false;
00128   }
00129 }
00130 
00131 bool InteractiveWorldModelClient::getTaskSurfaces(const uint32_t task_id, vector<Surface> &surfaces) const
00132 {
00133   // load the task model
00134   TaskModel task_model;
00135   if (this->getTaskModel(task_id, task_model))
00136   {
00137     task_model.getUniqueSurfaces(surfaces);
00138     return surfaces.size() > 0;
00139   } else
00140   {
00141     return false;
00142   }
00143 }


worldlib
Author(s): Russell Toris
autogenerated on Fri Feb 12 2016 00:24:18