trajectory_files_manager_1.cpp
Go to the documentation of this file.
2 
3 namespace ram_utils
4 {
5 
6 bool yamlFileToPolydata(const std::string yaml_file,
7  Polygon poly_data)
8 {
9  // Open YAML file
10  std::vector<std::vector<vtkVector3d>> polygon_point_list;
11  try
12  {
13  YAML::Node yaml;
14  yaml_parser::yamlNodeFromFileName(yaml_file, yaml);
15 
16  // Parse "layer" node in YAML file
17  std::string node_name("layer");
18  const YAML::Node &layer_node = yaml_parser::parseNode(yaml, node_name.c_str());
19  if (!yaml[node_name])
20  {
21  YAML::Exception yaml_exception(YAML::Mark(), "Cannot parse " + node_name + " node");
22  throw yaml_exception;
23  }
24 
25  // First Loop
26  // Parse all nodes in "layer" node
27  for (auto iterator_node : layer_node)
28  {
29  // Parse "polygon" node
30 
31  std::string node_name("polygon");
32  const YAML::Node &polygon_node = yaml_parser::parseNode(iterator_node, node_name.c_str());
33  if (!iterator_node[node_name])
34  {
35  YAML::Exception iterator_node_exception(YAML::Mark(), "Cannot parse " + node_name + " node");
36  throw iterator_node_exception;
37  }
38  std::vector<vtkVector3d> polygon;
39 
40  // Second Loop
41  // Parse all "point" tags in "polygon" node
42  for (auto point_node : polygon_node)
43  {
44  std::vector<double> point_values;
45  if (!yaml_parser::parseVectorD(point_node, "point", point_values))
46  continue;
47  if (point_values.size() != 3)
48  return false;
49  vtkVector3d point(point_values[0], point_values[1], point_values[2]);
50  polygon.push_back(point);
51  }
52 
53  if (!polygon.empty())
54  polygon_point_list.push_back(polygon);
55  }
56  }
57  catch (const YAML::Exception &e)
58  {
59  std::cerr << "Problem occurred during parsing YAML file, problem is:\n" << e.msg << endl << "File name = "
60  << yaml_file << std::endl;
61  return false;
62  }
63 
64  if (polygon_point_list.empty())
65  return false;
66 
67  vtkSmartPointer<vtkCellArray> polygon_array = vtkSmartPointer<vtkCellArray>::New();
68  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
69  for (auto polygon_iterator : polygon_point_list)
70  {
71  // Create the polygon
72  vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
73  for (auto point_iterator : polygon_iterator)
74  {
75  polygon->GetPointIds()->InsertNextId(points->GetNumberOfPoints());
76  points->InsertNextPoint(point_iterator.GetData());
77  }
78  polygon_array->InsertNextCell(polygon);
79  }
80 
81  poly_data->SetPolys(polygon_array);
82  poly_data->SetPoints(points);
83  cerr << "yamlFileToPolydata end" << endl;
84  return true;
85 }
86 
87 bool yamlFileToPolydata2(const std::string yaml_file,
88  Polygon poly_data,
89  std::vector<unsigned> &layer_count)
90 {
91  std::vector<std::vector<vtkVector3d>> polygon_point_list;
92  try
93  {
94  YAML::Node traj_node = YAML::LoadFile(yaml_file);
95 
96  for (const auto &node : traj_node)
97  {
98  for (const auto &layer_array : node)
99  {
100  unsigned polygon_number(0);
101 
102  YAML::Node key_layer = layer_array.first;
103  if (!key_layer.IsScalar())
104  continue;
105 
106  YAML::Node value_layer = layer_array.second;
107  if (!value_layer.IsSequence())
108  continue;
109 
110  if (key_layer.as<std::string>() != "layer")
111  continue;
112 
113  for (const auto &layer : value_layer)
114  {
115  std::vector<vtkVector3d> vtk_polygon;
116  for (const auto &polygon : layer)
117  {
118  YAML::Node key_polygon = polygon.first;
119  if (!key_polygon.IsScalar())
120  continue;
121 
122  YAML::Node value_polygon = polygon.second;
123  if (!value_polygon.IsSequence())
124  continue;
125 
126  if (key_polygon.as<std::string>() != "polygon")
127  continue;
128 
129  unsigned i(0);
130  for (const auto &pose : value_polygon)
131  {
132  if (!pose.IsSequence())
133  continue;
134 
135  std::vector<double> p = pose.as<std::vector<double>>();
136  if (p.size() != 3 && p.size() != 7)
137  return false;
138 
139  vtkVector3d point(p[0], p[1], p[2]);
140  vtk_polygon.push_back(point);
141  ++i;
142  }
143  ++polygon_number;
144  }
145  if (!vtk_polygon.empty())
146  polygon_point_list.push_back(vtk_polygon);
147  }
148  layer_count.push_back(polygon_number);
149  }
150  }
151  }
152  catch (YAML::Exception &e)
153  {
154  std::cerr << e.what() << std::endl;
155  return false;
156  }
157 
158  if (polygon_point_list.empty())
159  return false;
160 
161  vtkSmartPointer<vtkCellArray> polygon_array = vtkSmartPointer<vtkCellArray>::New();
162  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
163  for (auto polygon_iterator : polygon_point_list)
164  {
165  // Create the polygon
166  vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
167  for (auto point_iterator : polygon_iterator)
168  {
169  polygon->GetPointIds()->InsertNextId(points->GetNumberOfPoints());
170  points->InsertNextPoint(point_iterator.GetData());
171  }
172  polygon_array->InsertNextCell(polygon);
173  }
174 
175  poly_data->SetPolys(polygon_array);
176  poly_data->SetPoints(points);
177  return true;
178 }
179 
180 bool polydataToYamlFile(const std::string yaml_file,
181  const Polygon poly_data)
182 {
183  vtkIdType n_cells = poly_data->GetNumberOfCells();
184  if (n_cells == 0)
185  {
186  std::cerr << "polydataToYamlFile: the polydata is empty" << std::endl;
187  return false;
188  }
189 
190  double p[3];
191  std::ofstream yaml_file_ofstream;
192  yaml_file_ofstream.open(yaml_file);
193  yaml_file_ofstream << "---\n"
194  "layer:\n";
195 
196  for (vtkIdType cell_id(0); cell_id < n_cells; ++cell_id)
197  {
198  vtkIdType n_points = poly_data->GetCell(cell_id)->GetNumberOfPoints();
199  if (n_points == 0)
200  {
201  std::cerr << "polydataToYamlFile: the polygon " << cell_id << " in polydata is empty" << std::endl;
202  return false;
203  }
204  yaml_file_ofstream << " -\n"
205  " polygon:\n";
206  for (vtkIdType point_id(0); point_id < n_points; ++point_id)
207  {
208  poly_data->GetCell(cell_id)->GetPoints()->GetPoint(point_id, p);
209  yaml_file_ofstream << " - point: [" << p[0] << ", " << p[1] << ", " << p[2] << "]\n";
210  }
211  }
212 
213  yaml_file_ofstream << "\n";
214  yaml_file_ofstream.close();
215  std::cout << "File " << yaml_file << " was written on the disk" << std::endl;
216  return true;
217 }
218 
219 bool polydataToYamlFile2(const std::string yaml_file,
220  const Polygon poly_data)
221 {
222  vtkIdType n_cells = poly_data->GetNumberOfCells();
223  if (n_cells == 0)
224  {
225  std::cerr << "polydataToYamlFile: the polydata is empty" << std::endl;
226  return false;
227  }
228 
229  double p[3];
230  std::ofstream yaml_file_ofstream;
231  yaml_file_ofstream.open(yaml_file);
232  yaml_file_ofstream << "---\n"
233  "- layer:\n";
234 
235  for (vtkIdType cell_id(0); cell_id < n_cells; ++cell_id)
236  {
237  vtkIdType n_points = poly_data->GetCell(cell_id)->GetNumberOfPoints();
238  if (n_points == 0)
239  {
240  std::cerr << "polydataToYamlFile2: the polygon " << cell_id << " in polydata is empty" << std::endl;
241  return false;
242  }
243  yaml_file_ofstream << " - polygon:\n";
244  for (vtkIdType point_id(0); point_id < n_points; ++point_id)
245  {
246  poly_data->GetCell(cell_id)->GetPoints()->GetPoint(point_id, p);
247  yaml_file_ofstream << " - [" << p[0] << ", " << p[1] << ", " << p[2] << "]\n";
248  }
249  }
250 
251  yaml_file_ofstream << "\n";
252  yaml_file_ofstream.close();
253  std::cout << "File " << yaml_file << " was written on the disk" << std::endl;
254  return true;
255 }
256 
258  const std::string file_name,
259  const bool no_linear,
260  const std::string header)
261 {
262  std::ofstream yaml_file;
263  yaml_file.open(file_name);
264  if (!header.empty())
265  yaml_file << header;
266  yaml_file << "---\n";
267 
268  for (auto layer : trajectory)
269  {
270  yaml_file << "- layer:\n";
271  for (auto polygon : layer)
272  {
273  yaml_file << " - polygon:\n";
274  for (auto pose : polygon)
275  {
276  if (no_linear)
277  {
278  yaml_file << " - [" << pose.translation()[0] << ", " << pose.translation()[1] << ", "
279  << pose.translation()[2] << "]\n";
280  }
281  else
282  {
283  Eigen::Quaterniond quat(pose.linear());
284 
285  yaml_file << " - [" << pose.translation()[0] << ", " << pose.translation()[1] << ", "
286  << pose.translation()[2] << ", " << quat.x() << ", " << quat.y() << ", " << quat.z() << ", " << quat.w()
287  << "]\n";
288  }
289  }
290  }
291  }
292 
293  yaml_file << "\n";
294  yaml_file.close();
295  std::cout << "File " << file_name << " has been written" << std::endl;
296 }
297 
298 }
bool yamlFileToPolydata(const std::string yaml_file, Polygon poly_data)
bool parseVectorD(const YAML::Node &node, char const *var_name, std::vector< double > &var_value)
Definition: yaml_utils.hpp:82
ram_msgs::AdditiveManufacturingTrajectory trajectory
bool polydataToYamlFile(const std::string yaml_file, const Polygon poly_data)
void writeYAMLFile(const PosesTrajectory &trajectory, const std::string file_name, const bool no_linear=false, const std::string header="")
bool yamlFileToPolydata2(const std::string yaml_file, Polygon poly_data, std::vector< unsigned > &layer_count)
const YAML::Node parseNode(const YAML::Node &node, char const *var_name)
Definition: yaml_utils.hpp:100
vtkSmartPointer< vtkPolyData > Polygon
bool polydataToYamlFile2(const std::string yaml_file, const Polygon poly_data)
std::vector< PosesLayer > PosesTrajectory
bool yamlNodeFromFileName(const std::string filename, YAML::Node &ynode)
Definition: yaml_utils.hpp:116


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