json2msg_helpers.cpp
Go to the documentation of this file.
1 //
2 // Created by yijiangh on 4/7/18.
3 //
4 
6 
8 
9 namespace {
10 // the validity of input json data should be checked in the top level function
11 void jsonVectorToEigenVectorImpl(const rapidjson::Value& json, Eigen::Vector3d& v)
12 {
13  v = Eigen::Vector3d(json["X"].GetDouble(), json["Y"].GetDouble(), json["Z"].GetDouble());
14 }
15 
16 void jsonPlaneToPoseMsgImpl(const rapidjson::Value& json, geometry_msgs::Pose& p)
17 {
18  Eigen::Vector3d o, x_axis, y_axis, z_axis;
19 
20  if(json.HasMember("OriginX") && json.HasMember("OriginY") && json.HasMember("OriginZ"))
21  {
22  o = Eigen::Vector3d(json["OriginX"].GetDouble(), json["OriginY"].GetDouble(), json["OriginZ"].GetDouble());
23  }
24  else
25  {
26  // use "Origin" attribute
27  jsonVectorToEigenVectorImpl(json["Origin"], o);
28  }
29 
30  jsonVectorToEigenVectorImpl(json["XAxis"], x_axis);
31  jsonVectorToEigenVectorImpl(json["YAxis"], y_axis);
32  jsonVectorToEigenVectorImpl(json["ZAxis"], z_axis);
33 
34  choreo_geometry_conversion_helpers::planeToPoseMsg(o, x_axis, y_axis, z_axis, p);
35 }
36 
37 } // anon util namespace
38 
40 {
41 
42 bool isValidJsonGHVector(const rapidjson::Value& j_vector)
43 {
44  if(j_vector.HasMember("X") && j_vector.HasMember("Y") && j_vector.HasMember("Z"))
45  {
46  if(j_vector["X"].IsDouble() && j_vector["Y"].IsDouble() && j_vector["Z"].IsDouble())
47  {
48  return true;
49  }
50  }
51 
52  return false;
53 }
54 
55 bool isValidJsonGHPlane(const rapidjson::Value& j_plane)
56 {
57  bool has_valid_origin = false;
58  bool has_valid_axes = false;
59 
60  if(j_plane.HasMember("OriginX") && j_plane.HasMember("OriginY") && j_plane.HasMember("OriginZ"))
61  {
62  if(j_plane["OriginX"].IsDouble() && j_plane["OriginY"].IsDouble() && j_plane["OriginZ"].IsDouble())
63  {
64  has_valid_origin = true;
65  }
66  }
67  else
68  {
69  if(j_plane.HasMember("Origin"))
70  {
71  if(isValidJsonGHVector(j_plane["Origin"]))
72  {
73  has_valid_origin = true;
74  }
75  }
76  }
77 
78  if(j_plane.HasMember("XAxis") && j_plane.HasMember("YAxis") && j_plane.HasMember("ZAxis"))
79  {
80  has_valid_axes = isValidJsonGHVector(j_plane["XAxis"])
81  && isValidJsonGHVector(j_plane["YAxis"])
82  && isValidJsonGHVector(j_plane["ZAxis"]);
83  }
84 
85  return has_valid_origin && has_valid_axes;
86 }
87 
88 void jsonVectorToEigenVector(const rapidjson::Value& json, Eigen::Vector3d& v)
89 {
90  assert(isValidJsonGHVector(json));
91  jsonVectorToEigenVectorImpl(json, v);
92 }
93 
94 void jsonPlaneToPoseMsg(const rapidjson::Value& json, geometry_msgs::Pose& p)
95 {
96  assert(isValidJsonGHPlane(json));
97  jsonPlaneToPoseMsgImpl(json, p);
98 }
99 
100 void jsonToGraspFrameFabMsg(const rapidjson::Value& json, choreo_msgs::Grasp& g)
101 {
102  assert(json.HasMember("pick_grasp_plane"));
103  assert(json.HasMember("pick_grasp_approach_plane"));
104  assert(json.HasMember("pick_grasp_retreat_plane"));
105 
106  assert(json.HasMember("place_grasp_plane"));
107  assert(json.HasMember("place_grasp_approach_plane"));
108  assert(json.HasMember("place_grasp_retreat_plane"));
109 
110  assert(isValidJsonGHPlane(json["pick_grasp_plane"]));
111  assert(isValidJsonGHPlane(json["pick_grasp_approach_plane"]));
112  assert(isValidJsonGHPlane(json["pick_grasp_retreat_plane"]));
113 
114  assert(isValidJsonGHPlane(json["place_grasp_plane"]));
115  assert(isValidJsonGHPlane(json["place_grasp_approach_plane"]));
116  assert(isValidJsonGHPlane(json["place_grasp_retreat_plane"]));
117 
118  jsonPlaneToPoseMsgImpl(json["pick_grasp_plane"], g.pick_grasp_pose);
119  jsonPlaneToPoseMsgImpl(json["pick_grasp_approach_plane"], g.pick_grasp_approach_pose);
120  jsonPlaneToPoseMsgImpl(json["pick_grasp_retreat_plane"], g.pick_grasp_retreat_pose);
121 
122  jsonPlaneToPoseMsgImpl(json["place_grasp_plane"], g.place_grasp_pose);
123  jsonPlaneToPoseMsgImpl(json["place_grasp_approach_plane"], g.place_grasp_approach_pose);
124  jsonPlaneToPoseMsgImpl(json["place_grasp_retreat_plane"], g.place_grasp_retreat_pose);
125 }
126 
127 }
bool isValidJsonGHPlane(const rapidjson::Value &j_plane)
void jsonToGraspFrameFabMsg(const rapidjson::Value &json, choreo_msgs::Grasp &g)
void jsonPlaneToPoseMsg(const rapidjson::Value &json, geometry_msgs::Pose &p)
bool isValidJsonGHVector(const rapidjson::Value &j_vector)
void planeToPoseMsg(const Eigen::Vector3d &origin, const Eigen::Vector3d &x_axis, const Eigen::Vector3d &y_axis, const Eigen::Vector3d &z_axis, geometry_msgs::Pose &p)
void jsonVectorToEigenVector(const rapidjson::Value &json, Eigen::Vector3d &v)


choreo_task_sequence_processor
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:29