trajectory_files_manager.cpp
Go to the documentation of this file.
2 
3 namespace ram_trajectory_utils
4 {
6 {
7 }
8 
9 bool TrajectoryFilesManager::yamlFileToPolydata(const std::string yaml_file,
10  Polygon poly_data)
11 {
12  // Open YAML file
13  std::vector<std::vector<vtkVector3d> > polygon_point_list;
14  try
15  {
16  YAML::Node yaml;
17  yaml_parser::yamlNodeFromFileName(yaml_file, yaml);
18 
19  // Parse "layer" node in YAML file
20  std::string node_name("layer");
21  const YAML::Node &layer_node = yaml_parser::parseNode(yaml, node_name.c_str());
22  if (!yaml[node_name])
23  {
24  YAML::Exception yaml_exception(YAML::Mark(), "Cannot parse " + node_name + " node");
25  throw yaml_exception;
26  }
27 
28  // First Loop
29  // Parse all nodes in "layer" node
30  for (auto iterator_node : layer_node)
31  {
32  //Parse "polygon" node
33 
34  std::string node_name("polygon");
35  const YAML::Node &polygon_node = yaml_parser::parseNode(iterator_node, node_name.c_str());
36  if (!iterator_node[node_name])
37  {
38  YAML::Exception iterator_node_exception(YAML::Mark(), "Cannot parse " + node_name + " node");
39  throw iterator_node_exception;
40  }
41  std::vector<vtkVector3d> polygon;
42 
43  // Second Loop
44  // Parse all "point" tags in "polygon" node
45  for (auto point_node : polygon_node)
46  {
47  std::vector<double> point_values;
48  yaml_parser::parseVectorD(point_node, "point", point_values);
49  vtkVector3d point(point_values[0], point_values[1], point_values[2]);
50  polygon.push_back(point);
51  }
52  polygon_point_list.push_back(polygon);
53  }
54 
55  }
56  catch (const YAML::Exception &e)
57  {
58  std::cerr << "Problem occurred during parsing YAML file, problem is:\n" << e.msg << endl <<
59  "File name = " << yaml_file << std::endl;
60  return false;
61  }
62 
63  vtkSmartPointer<vtkCellArray> polygon_array = vtkSmartPointer<vtkCellArray>::New();
64  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
65  for (auto polygon_iterator : polygon_point_list)
66  {
67  // Create the polygon
68  vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
69  for (auto point_iterator : polygon_iterator)
70  {
71  polygon->GetPointIds()->InsertNextId(points->GetNumberOfPoints());
72  points->InsertNextPoint(point_iterator.GetData());
73  }
74  polygon_array->InsertNextCell(polygon);
75  }
76 
77  poly_data->SetPolys(polygon_array);
78  poly_data->SetPoints(points);
79  return true;
80 }
81 
82 bool TrajectoryFilesManager::polydataToYamlFile(const std::string yaml_file,
83  const Polygon poly_data)
84 {
85  vtkIdType n_cells = poly_data->GetNumberOfCells();
86  if (n_cells == 0)
87  {
88  std::cerr << "polydataToYamlFile: the polydata is empty" << std::endl;
89  return false;
90  }
91 
92  double p[3];
93  std::ofstream yaml_file_ofstream;
94  yaml_file_ofstream.open(yaml_file);
95  yaml_file_ofstream << "---\n"
96  "layer:\n";
97 
98  for (vtkIdType cell_id(0); cell_id < n_cells; ++cell_id)
99  {
100  vtkIdType n_points = poly_data->GetCell(cell_id)->GetNumberOfPoints();
101  if (n_points == 0)
102  {
103  std::cerr << "polydataToYamlFile: the polygon " << cell_id << " in polydata is empty" << std::endl;
104  return false;
105  }
106  yaml_file_ofstream << " -\n"
107  " polygon:\n";
108  for (vtkIdType point_id(0); point_id < n_points; ++point_id)
109  {
110  poly_data->GetCell(cell_id)->GetPoints()->GetPoint(point_id, p);
111  yaml_file_ofstream << " - point: [" << p[0] << ", " << p[1] << ", " << p[2] << "]\n";
112  }
113  }
114 
115  yaml_file_ofstream << "---\n";
116  yaml_file_ofstream << "\n";
117  yaml_file_ofstream.close();
118  std::cout << "File " << yaml_file << " was written on the disk" << std::endl;
119 
120  return true;
121 }
122 
123 }
static 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:90
const YAML::Node parseNode(const YAML::Node &node, char const *var_name)
Definition: yaml_utils.hpp:109
bool yamlNodeFromFileName(std::string filename, YAML::Node &ynode)
Definition: yaml_utils.hpp:127
static bool polydataToYamlFile(const std::string yaml_file, const Polygon poly_data)


ram_trajectory_utils
Author(s): Andres Campos - Institut Maupertuis
autogenerated on Fri Oct 27 2017 02:49:34