manager.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <fstream>
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string/replace.hpp>
7 #include "mesh_msgs/MeshFaceCluster.h"
8 
9 using namespace boost::filesystem;
10 
11 namespace label_manager
12 {
13  LabelManager::LabelManager(ros::NodeHandle& nodeHandle) :
14  nh(nodeHandle) {
15 
16  if (!nh.getParam("folder_path", folderPath))
17  {
18  folderPath = "/tmp/label_manager/";
19  }
20 
21  path p(folderPath);
22  if (!is_directory(p) && !exists(p))
23  {
24  create_directory(p);
25  }
26 
27  clusterLabelSub = nh.subscribe("cluster_label", 10, &LabelManager::clusterLabelCallback, this);
28  newClusterLabelPub = nh.advertise<mesh_msgs::MeshFaceCluster>("new_cluster_label", 1);
30  "get_labeled_clusters",
32  this
33  );
35  "get_label_groups",
37  this
38  );
40  "get_labeled_cluster_group",
42  this
43  );
45  "delete_label",
47  this
48  );
49 
50  ROS_INFO("Started LabelManager");
51 
52  ros::spin();
53  }
54 
55  void LabelManager::clusterLabelCallback(const mesh_msgs::MeshFaceClusterStamped::ConstPtr& msg)
56  {
57  ROS_INFO_STREAM("Got msg for mesh: " << msg->uuid << " with label: " << msg->cluster.label);
58 
59  std::vector<uint> indices;
60  std::string fileName = getFileName(msg->uuid, msg->cluster.label);
61 
62  // if appending (not override), first figure what new indices we have to add
63  if (!msg->override)
64  {
65  std::vector<uint> readIndices = readIndicesFromFile(fileName);
66 
67  // if read indices is empty no file was found or could not be read
68  if (readIndices.empty())
69  {
70  indices = msg->cluster.face_indices;
71  }
72  else
73  {
74  for (size_t i = 0; i < msg->cluster.face_indices.size(); i++)
75  {
76  uint idx = msg->cluster.face_indices[i];
77 
78  // if msg index is not already in file, add it to indices vector
79  if (std::find(readIndices.begin(), readIndices.end(), idx) == readIndices.end())
80  {
81  indices.push_back(idx);
82  }
83  }
84  }
85  }
86  else
87  {
88  indices = msg->cluster.face_indices;
89  }
90 
91  // publish every new labeled cluster
92  newClusterLabelPub.publish(msg->cluster);
93 
94  // make sure mesh folder exists before writing
95  path p(folderPath + "/" + msg->uuid);
96  if (!is_directory(p) || !exists(p))
97  {
98  create_directory(p);
99  }
100 
101  writeIndicesToFile(fileName, indices, !msg->override);
102  }
103 
105  mesh_msgs::GetLabeledClusters::Request& req,
106  mesh_msgs::GetLabeledClusters::Response& res
107  )
108  {
109  ROS_DEBUG_STREAM("Service call with uuid: " << req.uuid);
110 
111  path p (folderPath + "/" + req.uuid);
112  directory_iterator end_itr;
113 
114  if (!is_directory(p) || !exists(p))
115  {
116  ROS_DEBUG_STREAM("No labeled clusters for uuid '" << req.uuid << "' found");
117 
118  return false;
119  }
120 
121  for (directory_iterator itr(p); itr != end_itr; ++itr)
122  {
123  // if file is no dir
124  if (is_regular_file(itr->path()))
125  {
126  std::string label = itr->path().filename().string();
127  // remove extension from label
128  boost::replace_all(label, itr->path().filename().extension().string(), "");
129 
130  mesh_msgs::MeshFaceCluster c;
131  c.face_indices = readIndicesFromFile(itr->path().string());
132  c.label = label;
133 
134  res.clusters.push_back(c);
135  }
136  }
137 
138  return true;
139  }
140 
142  label_manager::GetLabelGroups::Request& req,
143  label_manager::GetLabelGroups::Response& res)
144  {
145  path p (folderPath + "/" + req.uuid);
146  directory_iterator end_itr;
147 
148  if (!is_directory(p) || !exists(p))
149  {
150  ROS_WARN_STREAM("No labeled clusters for uuid '" << req.uuid << "' found");
151 
152  return false;
153  }
154 
155  for (directory_iterator itr(p); itr != end_itr; ++itr)
156  {
157  // if file is no dir
158  if (is_regular_file(itr->path()))
159  {
160  std::string label = itr->path().filename().string();
161  // remove extension from label
162  boost::replace_all(label, itr->path().filename().extension().string(), "");
163 
164  // assuming the labels will look like this: 'GROUP_SOMETHINGELSE',
165  // remove everthing not representing the group
166  // TODO make seperator configurable
167  label = label.substr(0, label.find_first_of("_", 0));
168 
169  // only add label group to response if not already added
170  if (std::find(res.labels.begin(), res.labels.end(), label) == res.labels.end())
171  {
172  res.labels.push_back(label);
173  }
174  }
175  }
176 
177 
178  return true;
179  }
180 
182  label_manager::DeleteLabel::Request& req,
183  label_manager::DeleteLabel::Response& res)
184  {
185  path p(getFileName(req.uuid, req.label));
186 
187  if (!is_regular_file(p) || !exists(p))
188  {
189  ROS_WARN_STREAM("Could not delete label '" << req.label << "' of mesh '" << req.uuid << "'.");
190 
191  return false;
192  }
193 
194  res.cluster.face_indices = readIndicesFromFile(p.filename().string());
195  res.cluster.label = req.label;
196 
197  return remove(p);
198  }
199 
201  label_manager::GetLabeledClusterGroup::Request& req,
202  label_manager::GetLabeledClusterGroup::Response& res)
203  {
204  path p (folderPath + "/" + req.uuid);
205  directory_iterator end_itr;
206 
207  if (!is_directory(p) || !exists(p))
208  {
209  ROS_WARN_STREAM("No labeled clusters for uuid '" << req.uuid << "' found");
210 
211  return false;
212  }
213 
214  for (directory_iterator itr(p); itr != end_itr; ++itr)
215  {
216  // if file is no dir
217  if (is_regular_file(itr->path()) && itr->path().filename().string().find(req.labelGroup) == 0)
218  {
219  std::string label = itr->path().filename().string();
220  // remove extension from label
221  boost::replace_all(label, itr->path().filename().extension().string(), "");
222 
223  mesh_msgs::MeshFaceCluster c;
224  c.face_indices = readIndicesFromFile(itr->path().string());
225  c.label = label;
226 
227  res.clusters.push_back(c);
228  }
229  }
230 
231 
232  return true;
233  }
234 
236  const std::string& fileName,
237  const std::vector<uint>& indices,
238  const bool append
239  )
240  {
241  if (indices.empty())
242  {
243  ROS_WARN_STREAM("Empty indices.");
244 
245  return true;
246  }
247 
248  std::ios_base::openmode mode = append ? (std::ios::out|std::ios::app) : std::ios::out;
249  std::ofstream ofs(fileName.c_str(), mode);
250 
251  ROS_DEBUG_STREAM("Writing indices to file: " << fileName);
252 
253  if (ofs.is_open())
254  {
255  // if in append mode add , after the old data
256  if (append)
257  {
258  ofs << ",";
259  }
260 
261  size_t size = indices.size();
262  for (size_t i = 0; i < size; i++)
263  {
264  ofs << indices[i];
265 
266  if (i < size - 1)
267  {
268  ofs << ",";
269  }
270  }
271 
272  ofs.close();
273  ROS_DEBUG_STREAM("Successfully written indices to file.");
274 
275  return true;
276  }
277  else
278  {
279  ROS_ERROR_STREAM("Could not open file: " << fileName);
280  }
281 
282  return false;
283  }
284 
285  std::vector<uint> LabelManager::readIndicesFromFile(const std::string& fileName)
286  {
287  std::ifstream ifs(fileName.c_str(), std::ios::in);
288  std::vector<uint> faceIndices;
289 
290  // if file dos not exists, return empty vector
291  if (!ifs.good())
292  {
293  ROS_DEBUG_STREAM("File " << fileName << " does not exists. Nothing to read...");
294 
295  return faceIndices;
296  }
297 
298  std::string stringNumber;
299  while (std::getline(ifs, stringNumber, ','))
300  {
301  faceIndices.push_back(atoi(stringNumber.c_str()));
302  }
303 
304  return faceIndices;
305  }
306 
307  std::string LabelManager::getFileName(const std::string& uuid, const std::string& label)
308  {
309  return folderPath + "/" +uuid + "/" + label + ".dat";
310  }
311 }
exists
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
label_manager::LabelManager::service_getLabelGroups
bool service_getLabelGroups(label_manager::GetLabelGroups::Request &req, label_manager::GetLabelGroups::Response &res)
Definition: manager.cpp:141
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
label_manager::LabelManager::nh
ros::NodeHandle nh
Definition: manager.h:22
label_manager::LabelManager::clusterLabelCallback
void clusterLabelCallback(const mesh_msgs::MeshFaceClusterStamped::ConstPtr &msg)
Definition: manager.cpp:55
ros::NodeHandle::advertiseService
ServiceServer advertiseService(AdvertiseServiceOptions &ops)
label_manager::LabelManager::newClusterLabelPub
ros::Publisher newClusterLabelPub
Definition: manager.h:24
label_manager::LabelManager::srv_delete_label
ros::ServiceServer srv_delete_label
Definition: manager.h:28
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
label_manager::LabelManager::folderPath
std::string folderPath
Definition: manager.h:30
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
label_manager::LabelManager::writeIndicesToFile
bool writeIndicesToFile(const std::string &fileName, const std::vector< uint > &indices, const bool append)
Definition: manager.cpp:235
label_manager::LabelManager::srv_get_label_groups
ros::ServiceServer srv_get_label_groups
Definition: manager.h:26
label_manager::LabelManager::service_getLabeledClusterGroup
bool service_getLabeledClusterGroup(label_manager::GetLabeledClusterGroup::Request &req, label_manager::GetLabeledClusterGroup::Response &res)
Definition: manager.cpp:200
label_manager::LabelManager::clusterLabelSub
ros::Subscriber clusterLabelSub
Definition: manager.h:23
label_manager
Definition: manager.h:13
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
label_manager::LabelManager::service_getLabeledClusters
bool service_getLabeledClusters(mesh_msgs::GetLabeledClusters::Request &req, mesh_msgs::GetLabeledClusters::Response &res)
Definition: manager.cpp:104
label_manager::LabelManager::service_deleteLabel
bool service_deleteLabel(label_manager::DeleteLabel::Request &req, label_manager::DeleteLabel::Response &res)
Definition: manager.cpp:181
append
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
manager.h
label_manager::LabelManager::getFileName
std::string getFileName(const std::string &uuid, const std::string &label)
Definition: manager.cpp:307
label_manager::LabelManager::readIndicesFromFile
std::vector< uint > readIndicesFromFile(const std::string &fileName)
Definition: manager.cpp:285
label_manager::LabelManager::srv_get_labeled_clusters
ros::ServiceServer srv_get_labeled_clusters
Definition: manager.h:25
ros::spin
ROSCPP_DECL void spin()
ROS_INFO
#define ROS_INFO(...)
ros::NodeHandle
label_manager::LabelManager::srv_get_labeled_cluster_group
ros::ServiceServer srv_get_labeled_cluster_group
Definition: manager.h:27


label_manager
Author(s): Sebastian Pütz , Jan Philipp Vogtherr
autogenerated on Sun Jan 21 2024 04:06:15