7 size_t last_index = full_path.find_last_of(
"/");
8 std::string file_name = full_path.substr(last_index + 1, full_path.size());
10 last_index = file_name.find_last_of(
"\\");
11 file_name = file_name.substr(last_index + 1, file_name.size());
13 last_index = file_name.find_last_of(
".");
14 if (last_index == std::string::npos)
17 return file_name.substr(last_index + 1, file_name.size());
21 const vtkSmartPointer<vtkPolyData> poly_data)
23 vtkSmartPointer<ErrorObserver> vtk_observer = vtkSmartPointer<ErrorObserver>::New();
26 vtkSmartPointer<vtkAbstractPolyDataReader> reader;
28 if (!strcasecmp(file_extension.c_str(),
"obj"))
29 reader = vtkSmartPointer<vtkOBJReader>::New();
30 else if (!strcasecmp(file_extension.c_str(),
"ply"))
31 reader = vtkSmartPointer<vtkPLYReader>::New();
32 else if (!strcasecmp(file_extension.c_str(),
"stl"))
33 reader = vtkSmartPointer<vtkSTLReader>::New();
37 reader->SetFileName(file_name.c_str());
38 reader->AddObserver(vtkCommand::WarningEvent, vtk_observer);
39 reader->AddObserver(vtkCommand::ErrorEvent, vtk_observer);
40 vtk_observer->Clear();
43 if (vtk_observer->GetWarning())
45 ROS_WARN_STREAM(
"readPolygonFile: " << file_name << std::endl << vtk_observer->GetWarningMessage());
46 vtk_observer->Clear();
49 if (vtk_observer->GetError())
51 ROS_ERROR_STREAM(
"readPolygonFile: " << file_name << std::endl << vtk_observer->GetErrorMessage());
52 vtk_observer->Clear();
56 if (!reader->GetOutput()->GetNumberOfCells())
62 poly_data->DeepCopy(reader->GetOutput());
67 const std::string file_name,
68 const vtkSmartPointer<vtkPolyData> poly_data,
69 vtkSmartPointer<vtkStripper> &stripper,
70 const double height_between_layers,
71 const std::array<double, 3> slicing_direction,
74 if (height_between_layers == 0)
83 double cutter_normal[3];
84 cutter_normal[0] = slicing_direction[0];
85 cutter_normal[1] = slicing_direction[1];
86 cutter_normal[2] = slicing_direction[2];
88 if (cutter_normal[0] == 0 &&
89 cutter_normal[1] == 0 &&
90 cutter_normal[2] == 0)
97 vtkMath::Normalize(cutter_normal);
99 poly_data->ComputeBounds();
101 min_bound[0] = poly_data->GetBounds()[0];
102 min_bound[1] = poly_data->GetBounds()[2];
103 min_bound[2] = poly_data->GetBounds()[4];
107 max_bound[0] = poly_data->GetBounds()[1];
108 max_bound[1] = poly_data->GetBounds()[3];
109 max_bound[2] = poly_data->GetBounds()[5];
113 double min_bound_proj[3];
114 double max_bound_proj[3];
115 vtkMath::ProjectVector(min_bound, cutter_normal, min_bound_proj);
116 vtkMath::ProjectVector(max_bound, cutter_normal, max_bound_proj);
118 double min_offset =
cos(vtkMath::AngleBetweenVectors(min_bound_proj, cutter_normal)) * vtkMath::Norm(min_bound_proj);
119 double max_offset =
cos(vtkMath::AngleBetweenVectors(max_bound_proj, cutter_normal)) * vtkMath::Norm(max_bound_proj);
120 if (min_offset > max_offset)
122 double aux_offset = min_offset;
123 min_offset = max_offset;
124 max_offset = aux_offset;
127 double origin_offset = min_offset;
128 bool first_layer(
true);
129 while (origin_offset < max_offset)
132 vtkSmartPointer<vtkPlane> plane = vtkSmartPointer<vtkPlane>::New();
133 plane->SetOrigin(poly_data->GetCenter());
134 plane->SetOrigin(0, 0, 0);
135 plane->SetNormal(cutter_normal);
138 vtkSmartPointer<vtkCutter> cutter = vtkSmartPointer<vtkCutter>::New();
139 cutter->SetCutFunction(plane);
140 cutter->SetInputData(poly_data);
144 cutter->SetValue(0, origin_offset + 1e-5);
148 cutter->SetValue(0, origin_offset);
150 origin_offset += height_between_layers;
153 vtkSmartPointer<vtkTriangleFilter> triangle_filter = vtkSmartPointer<vtkTriangleFilter>::New();
154 triangle_filter->SetInputConnection(cutter->GetOutputPort());
155 triangle_filter->Update();
157 stripper = vtkSmartPointer<vtkStripper>::New();
158 stripper->SetInputConnection(triangle_filter->GetOutputPort());
162 poly->DeepCopy(stripper->GetOutput());
163 if (poly->GetNumberOfPoints() < 3)
168 poly_vector.push_back(poly);
170 layer.push_back(poly_vector);
172 trajectory.push_back(layer);
174 ROS_INFO_STREAM(
"sliceMesh: Generated " <<poly->GetNumberOfCells()<<
" contours in this layer");
179 std::string we_dont_care;
180 std::getline(std::cin, we_dont_care);
187 return trajectory.size();
bool readPolygonFile(const std::string file_name, const vtkSmartPointer< vtkPolyData > poly_data)
unsigned sliceMesh(std::vector< Layer > &trajectory, const std::string file_name, const vtkSmartPointer< vtkPolyData > poly_data, vtkSmartPointer< vtkStripper > &stripper, const double height_between_layers, const std::array< double, 3 > slicing_direction, const bool use_gui=false)
std::string fileExtension(const std::string full_path)
std::vector< PolygonVector > Layer
vtkSmartPointer< vtkPolyData > Polygon
std::vector< Polygon > PolygonVector
#define ROS_WARN_STREAM(args)
#define ROS_INFO_STREAM(args)
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
#define ROS_ERROR_STREAM(args)