10 std::vector<std::vector<vtkVector3d>> polygon_point_list;
17 std::string node_name(
"layer");
21 YAML::Exception yaml_exception(YAML::Mark(),
"Cannot parse " + node_name +
" node");
27 for (
auto iterator_node : layer_node)
31 std::string node_name(
"polygon");
33 if (!iterator_node[node_name])
35 YAML::Exception iterator_node_exception(YAML::Mark(),
"Cannot parse " + node_name +
" node");
36 throw iterator_node_exception;
38 std::vector<vtkVector3d> polygon;
42 for (
auto point_node : polygon_node)
44 std::vector<double> point_values;
47 if (point_values.size() != 3)
49 vtkVector3d point(point_values[0], point_values[1], point_values[2]);
50 polygon.push_back(point);
54 polygon_point_list.push_back(polygon);
57 catch (
const YAML::Exception &e)
59 std::cerr <<
"Problem occurred during parsing YAML file, problem is:\n" << e.msg << endl <<
"File name = " 60 << yaml_file << std::endl;
64 if (polygon_point_list.empty())
67 vtkSmartPointer<vtkCellArray> polygon_array = vtkSmartPointer<vtkCellArray>::New();
68 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
69 for (
auto polygon_iterator : polygon_point_list)
72 vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
73 for (
auto point_iterator : polygon_iterator)
75 polygon->GetPointIds()->InsertNextId(points->GetNumberOfPoints());
76 points->InsertNextPoint(point_iterator.GetData());
78 polygon_array->InsertNextCell(polygon);
81 poly_data->SetPolys(polygon_array);
82 poly_data->SetPoints(points);
83 cerr <<
"yamlFileToPolydata end" << endl;
89 std::vector<unsigned> &layer_count)
91 std::vector<std::vector<vtkVector3d>> polygon_point_list;
94 YAML::Node traj_node = YAML::LoadFile(yaml_file);
96 for (
const auto &node : traj_node)
98 for (
const auto &layer_array : node)
100 unsigned polygon_number(0);
102 YAML::Node key_layer = layer_array.first;
103 if (!key_layer.IsScalar())
106 YAML::Node value_layer = layer_array.second;
107 if (!value_layer.IsSequence())
110 if (key_layer.as<std::string>() !=
"layer")
113 for (
const auto &layer : value_layer)
115 std::vector<vtkVector3d> vtk_polygon;
116 for (
const auto &polygon : layer)
118 YAML::Node key_polygon = polygon.first;
119 if (!key_polygon.IsScalar())
122 YAML::Node value_polygon = polygon.second;
123 if (!value_polygon.IsSequence())
126 if (key_polygon.as<std::string>() !=
"polygon")
130 for (
const auto &pose : value_polygon)
132 if (!pose.IsSequence())
135 std::vector<double> p = pose.as<std::vector<double>>();
136 if (p.size() != 3 && p.size() != 7)
139 vtkVector3d point(p[0], p[1], p[2]);
140 vtk_polygon.push_back(point);
145 if (!vtk_polygon.empty())
146 polygon_point_list.push_back(vtk_polygon);
148 layer_count.push_back(polygon_number);
152 catch (YAML::Exception &e)
154 std::cerr << e.what() << std::endl;
158 if (polygon_point_list.empty())
161 vtkSmartPointer<vtkCellArray> polygon_array = vtkSmartPointer<vtkCellArray>::New();
162 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
163 for (
auto polygon_iterator : polygon_point_list)
166 vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
167 for (
auto point_iterator : polygon_iterator)
169 polygon->GetPointIds()->InsertNextId(points->GetNumberOfPoints());
170 points->InsertNextPoint(point_iterator.GetData());
172 polygon_array->InsertNextCell(polygon);
175 poly_data->SetPolys(polygon_array);
176 poly_data->SetPoints(points);
183 vtkIdType n_cells = poly_data->GetNumberOfCells();
186 std::cerr <<
"polydataToYamlFile: the polydata is empty" << std::endl;
191 std::ofstream yaml_file_ofstream;
192 yaml_file_ofstream.open(yaml_file);
193 yaml_file_ofstream <<
"---\n" 196 for (vtkIdType cell_id(0); cell_id < n_cells; ++cell_id)
198 vtkIdType n_points = poly_data->GetCell(cell_id)->GetNumberOfPoints();
201 std::cerr <<
"polydataToYamlFile: the polygon " << cell_id <<
" in polydata is empty" << std::endl;
204 yaml_file_ofstream <<
" -\n" 206 for (vtkIdType point_id(0); point_id < n_points; ++point_id)
208 poly_data->GetCell(cell_id)->GetPoints()->GetPoint(point_id, p);
209 yaml_file_ofstream <<
" - point: [" << p[0] <<
", " << p[1] <<
", " << p[2] <<
"]\n";
213 yaml_file_ofstream <<
"\n";
214 yaml_file_ofstream.close();
215 std::cout <<
"File " << yaml_file <<
" was written on the disk" << std::endl;
222 vtkIdType n_cells = poly_data->GetNumberOfCells();
225 std::cerr <<
"polydataToYamlFile: the polydata is empty" << std::endl;
230 std::ofstream yaml_file_ofstream;
231 yaml_file_ofstream.open(yaml_file);
232 yaml_file_ofstream <<
"---\n" 235 for (vtkIdType cell_id(0); cell_id < n_cells; ++cell_id)
237 vtkIdType n_points = poly_data->GetCell(cell_id)->GetNumberOfPoints();
240 std::cerr <<
"polydataToYamlFile2: the polygon " << cell_id <<
" in polydata is empty" << std::endl;
243 yaml_file_ofstream <<
" - polygon:\n";
244 for (vtkIdType point_id(0); point_id < n_points; ++point_id)
246 poly_data->GetCell(cell_id)->GetPoints()->GetPoint(point_id, p);
247 yaml_file_ofstream <<
" - [" << p[0] <<
", " << p[1] <<
", " << p[2] <<
"]\n";
251 yaml_file_ofstream <<
"\n";
252 yaml_file_ofstream.close();
253 std::cout <<
"File " << yaml_file <<
" was written on the disk" << std::endl;
258 const std::string file_name,
259 const bool no_linear,
260 const std::string header)
262 std::ofstream yaml_file;
263 yaml_file.open(file_name);
266 yaml_file <<
"---\n";
268 for (
auto layer : trajectory)
270 yaml_file <<
"- layer:\n";
271 for (
auto polygon : layer)
273 yaml_file <<
" - polygon:\n";
274 for (
auto pose : polygon)
278 yaml_file <<
" - [" << pose.translation()[0] <<
", " << pose.translation()[1] <<
", " 279 << pose.translation()[2] <<
"]\n";
283 Eigen::Quaterniond quat(pose.linear());
285 yaml_file <<
" - [" << pose.translation()[0] <<
", " << pose.translation()[1] <<
", " 286 << pose.translation()[2] <<
", " << quat.x() <<
", " << quat.y() <<
", " << quat.z() <<
", " << quat.w()
295 std::cout <<
"File " << file_name <<
" has been written" << std::endl;
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)
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)
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)