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


ram_trajectory_files_manager
Author(s): Andres Campos - Institut Maupertuis
autogenerated on Thu Oct 12 2017 02:50:41