trajectory_files_manager_2.cpp
Go to the documentation of this file.
2 
3 namespace ram_utils
4 {
5 
6 bool yamlFileToPoses(std::string yaml_file,
7  PosesTrajectory &poses)
8 {
9  poses.clear();
10 
11  PosesLayer t_layer;
12  PosesPolygon t_polygon;
13  try
14  {
15  YAML::Node traj_node = YAML::LoadFile(yaml_file);
16 
17  for (const auto &node : traj_node)
18  {
19  for (const auto &layer_array : node)
20  {
21  YAML::Node key_layer = layer_array.first;
22  if (!key_layer.IsScalar())
23  continue;
24 
25  YAML::Node value_layer = layer_array.second;
26  if (!value_layer.IsSequence())
27  continue;
28 
29  if (key_layer.as<std::string>() != "layer")
30  continue;
31 
32  for (const auto &layer : value_layer)
33  {
34  for (const auto &polygon : layer)
35  {
36  YAML::Node key_polygon = polygon.first;
37  if (!key_polygon.IsScalar())
38  continue;
39 
40  YAML::Node value_polygon = polygon.second;
41  if (!value_polygon.IsSequence())
42  continue;
43 
44  if (key_polygon.as<std::string>() != "polygon")
45  continue;
46 
47  for (const auto &pose : value_polygon)
48  {
49  if (!pose.IsSequence())
50  continue;
51 
52  Pose t_pose(Pose::Identity());
53  std::vector<double> p = pose.as<std::vector<double>>();
54  if (p.size() == 7)
55  {
56  // Warning: W, X, Y, Z initialization!
57  t_pose.linear() = Eigen::Quaterniond(p[6], p[3], p[4], p[5]).toRotationMatrix();
58  }
59  else if (p.size() != 3)
60  continue;
61 
62  t_pose.translation()[0] = p[0];
63  t_pose.translation()[1] = p[1];
64  t_pose.translation()[2] = p[2];
65 
66  t_polygon.push_back(t_pose);
67  }
68  if (!t_polygon.empty())
69  {
70  t_layer.push_back(t_polygon);
71  t_polygon.clear();
72  }
73  }
74  }
75  if (!t_layer.empty())
76  {
77  poses.push_back(t_layer);
78  t_layer.clear();
79  }
80  }
81  }
82  }
83  catch (YAML::Exception &e)
84  {
85  ROS_ERROR_STREAM(e.what());
86  return false;
87  }
88 
89  if (poses.empty())
90  return false;
91 
92  return true;
93 }
94 
95 bool yamlFileToTrajectory(const std::string yaml_file,
96  ram_msgs::AdditiveManufacturingTrajectory &traj)
97 {
98  traj.poses.clear();
99  unsigned layer_number(0);
100  try
101  {
102  YAML::Node traj_node = YAML::LoadFile(yaml_file);
103 
104  for (const auto &node : traj_node)
105  {
106  for (const auto &layer_array : node)
107  {
108  YAML::Node key_layer = layer_array.first;
109  if (!key_layer.IsScalar())
110  continue;
111 
112  YAML::Node value_layer = layer_array.second;
113  if (!value_layer.IsSequence())
114  continue;
115 
116  if (key_layer.as<std::string>() != "layer")
117  continue;
118 
119  for (const auto &layer : value_layer)
120  {
121  for (const auto &polygon : layer)
122  {
123  YAML::Node key_polygon = polygon.first;
124  if (!key_polygon.IsScalar())
125  continue;
126 
127  YAML::Node value_polygon = polygon.second;
128  if (!value_polygon.IsSequence())
129  continue;
130 
131  if (key_polygon.as<std::string>() != "polygon")
132  continue;
133 
134  unsigned i(0);
135  for (const auto &pose : value_polygon)
136  {
137  if (!pose.IsSequence())
138  continue;
139 
140  ram_msgs::AdditiveManufacturingPose ram_pose;
141  std::vector<double> p = pose.as<std::vector<double>>();
142  if (p.size() == 3)
143  {
144  ram_pose.pose.orientation.x = 0;
145  ram_pose.pose.orientation.y = 0;
146  ram_pose.pose.orientation.z = 0;
147  ram_pose.pose.orientation.w = 1;
148  }
149  else if (p.size() == 7)
150  {
151  ram_pose.pose.orientation.x = p[3];
152  ram_pose.pose.orientation.y = p[4];
153  ram_pose.pose.orientation.z = p[5];
154  ram_pose.pose.orientation.w = p[6];
155  }
156  else
157  continue;
158 
159  ram_pose.layer_index = layer_number;
160  ram_pose.layer_level = layer_number;
161  ram_pose.entry_pose = false;
162  ram_pose.exit_pose = false;
163  ram_pose.unique_id = unique_id::toMsg(unique_id::fromRandom());
164  ram_pose.pose.position.x = p[0];
165  ram_pose.pose.position.y = p[1];
166  ram_pose.pose.position.z = p[2];
167 
168  if (i == 0)
169  ram_pose.polygon_start = true;
170 
171  traj.poses.push_back(ram_pose);
172  ++i;
173  }
174  traj.poses.back().polygon_end = true;
175  }
176  }
177  ++layer_number;
178  }
179  }
180  }
181  catch (YAML::Exception &e)
182  {
183  ROS_ERROR_STREAM(e.what());
184  return false;
185  }
186 
187  if (traj.poses.empty())
188  return false;
189 
190  traj.file = yaml_file;
191  traj.generated = ros::Time::now();
192  traj.modified = traj.generated;
193  traj.similar_layers = false;
194  traj.generation_info = "From YAML (poses) file";
195  return true;
196 }
197 
198 }
static boost::uuids::uuid fromRandom(void)
bool yamlFileToPoses(std::string yaml_file, PosesTrajectory &poses)
Eigen::Isometry3d Pose
std::vector< PosesPolygon > PosesLayer
static Time now()
static uuid_msgs::UniqueID toMsg(boost::uuids::uuid const &uu)
bool yamlFileToTrajectory(const std::string yaml_file, ram_msgs::AdditiveManufacturingTrajectory &traj)
#define ROS_ERROR_STREAM(args)
std::vector< PosesLayer > PosesTrajectory
std::vector< Eigen::Isometry3d > PosesPolygon


ram_utils
Author(s): Victor Lamoine - Institut Maupertuis
autogenerated on Mon Jun 10 2019 14:49:54