ObjectDatabase.cpp
Go to the documentation of this file.
1 
21 #define BOOST_NO_CXX11_SCOPED_ENUMS
22 
23 #include <ros/ros.h>
24 #include <ros/package.h>
25 #include <boost/foreach.hpp>
26 #include <boost/filesystem.hpp>
27 #include <ros_uri/ros_uri.hpp>
30 #include <boost/regex.hpp>
31 #include <boost/algorithm/string.hpp>
32 
33 namespace object_database
34 {
35 ObjectDatabase::ObjectDatabase(fs::path configurationFilePath) :
36  mGlobalNodeHandle("asr_object_database"),
37  mConfigurationFilePath(configurationFilePath)
38 {
39  // Create the config and read it.
40  mConfig.read(configurationFilePath);
41 
42  // Read all objects in database.
44 
46  mGlobalNodeHandle.advertiseService("recognizer_list",
49  mGlobalNodeHandle.advertiseService("object_type_list",
52  mGlobalNodeHandle.advertiseService("object_meta_data",
54 
55 
57  mGlobalNodeHandle.advertiseService("recognizer_list_meshes",
59 
61  mGlobalNodeHandle.advertiseService("generate_object_type",
63 
64 }
65 
66 bool ObjectDatabase::processRecognizerListRequest(RecognizerList::Request &req,
67  RecognizerList::Response &res)
68 {
69  // get all object types and add it to the response collection
71  BOOST_FOREACH(ObjectDatabaseRecognizerPtrMap::value_type mapTuple, objectTypes)
72  {
73  res.recognizer_list.push_back(mapTuple.second->getKeyword());
74  }
75 
76  // in the end, sort by name
77  std::sort(res.recognizer_list.begin(), res.recognizer_list.end());
78 
79  return true;
80 }
81 
82 bool ObjectDatabase::processObjectTypeListRequest(ObjectTypeList::Request &req,
83  ObjectTypeList::Response &res)
84 {
85  // get the parameter passed by the request
86  std::string recognizerName = req.recognizer;
87 
88  // creating a collection
90 
91  if (recognizerName == "all")
92  {
93  // if the user wants all items, replace the collection
94  // by another collection containing all object types
95  BOOST_FOREACH(ObjectDatabaseRecognizerPtrMap::value_type mapVal, mConfig.getObjectCategories())
96  {
97  collection.push_back(mapVal.second);
98  }
99  } else
100  {
101  // in any other case try to find the object type object
102  ObjectDatabaseRecognizerPtr recognizerPtr = mConfig.getRecognizer(recognizerName);
103 
104  // creating null pointer reference
106  if (recognizerPtr == nullPtr)
107  {
108  ROS_DEBUG("No object type definition for name '%s' found", recognizerName.c_str());
109  return false;
110  }
111 
112  // add object type object to collection, if there is a valid object
113  collection.push_back(recognizerPtr);
114  }
115 
116  ROS_DEBUG("Collection contains %lu ObjectTypes", collection.size());
117 
118  // Loop through the collection and read each object type and add its entries to the response
119  BOOST_FOREACH(ObjectDatabaseRecognizerPtr recognizerPtr, collection)
120  {
121  // reading objects into object type
122  recognizerPtr->readEntries();
123 
124  // adding results to response
125  ObjectDatabaseEntryPtrMap entryPtrMap = recognizerPtr->getEntries();
126  BOOST_FOREACH(ObjectDatabaseEntryPtrMapPair entryPtrMapPair, entryPtrMap)
127  {
128  res.object_type_list.push_back(entryPtrMapPair.first);
129  }
130  }
131 
132  // sorting the object for the response
133  std::sort(res.object_type_list.begin(), res.object_type_list.end());
134 
135  ROS_DEBUG("Object List contains %lu items", res.object_type_list.size());
136  ROS_DEBUG("Reached end of processing object list request");
137 
138  return true;
139 }
140 
141 bool ObjectDatabase::processObjectMetaDataRequest(ObjectMetaData::Request &req, ObjectMetaData::Response &res)
142 {
144  std::string object_type = req.object_type;
145  std::string recognizer = req.recognizer;
146 
147  // object information
148  res.object_folder = std::string();
149  res.is_valid = false;
150 
151  for(ObjectDatabaseRecognizerPtrMap::value_type val : recognizerPtrMap) {
152  ObjectDatabaseEntryPtr objectEntryPtr = val.second->getEntry(object_type);
153  if ((objectEntryPtr) && (objectEntryPtr->getRecognizer()->getKeyword() == recognizer)){
154  res.object_folder = ros_uri::package_uri(objectEntryPtr->getPath().string(), "asr_object_database");
155  res.object_mesh_resource = ros_uri::package_uri(objectEntryPtr->getRvizMeshResourcePath().string(), "asr_object_database");
156  res.normal_vectors = objectEntryPtr->getNormalVectors();
157  res.deviations = objectEntryPtr->getDeviationsFromFile();
158  res.is_valid = true;
159  res.is_rotation_invariant = objectEntryPtr->getRotationInvarianceFromFile();
160  break;
161  }
162  }
163 
164  return true;
165 }
166 
167 bool ObjectDatabase::processRecognizerListMeshesRequest(RecognizerListMeshes::Request &req, RecognizerListMeshes::Response &res) {
168  // get the parameter passed by the request
169  std::string recognizerName = req.recognizer;
170 
171  // creating a collection
173 
174  if (recognizerName == "all")
175  {
176  // if the user wants all items, replace the collection
177  // by another collection containing all object types
178  BOOST_FOREACH(ObjectDatabaseRecognizerPtrMap::value_type mapVal, mConfig.getObjectCategories())
179  {
180  collection.push_back(mapVal.second);
181  }
182  } else
183  {
184  // in any other case try to find the object type object
185  ObjectDatabaseRecognizerPtr recognizerPtr = mConfig.getRecognizer(recognizerName);
186 
187  // creating null pointer reference
189  if (recognizerPtr == nullPtr)
190  {
191  ROS_DEBUG("No object type definition for name '%s' found", recognizerName.c_str());
192  return false;
193  }
194 
195  // add object type object to collection, if there is a valid object
196  collection.push_back(recognizerPtr);
197  }
198 
199  ROS_DEBUG("Collection contains %lu ObjectTypes", collection.size());
200 
201  // Loop through the collection and read each object type and add its entries to the response
202  BOOST_FOREACH(ObjectDatabaseRecognizerPtr recognizerPtr, collection)
203  {
204  // reading objects into object type
205  recognizerPtr->readEntries();
206 
207  // adding results to response
208  ObjectDatabaseEntryPtrMap entryPtrMap = recognizerPtr->getEntries();
209  BOOST_FOREACH(ObjectDatabaseEntryPtrMapPair entryPtrMapPair, entryPtrMap)
210  {
211  std::string mesh_path = ros_uri::package_uri(entryPtrMapPair.second->getRvizMeshResourcePath().string(), "asr_object_database");
212  if (mesh_path != "package://asr_object_database/") {
213  res.meshes.push_back(mesh_path);
214  }
215  }
216  }
217 
218 
219  ROS_DEBUG("Object meshes list contains %lu items", res.meshes.size());
220  ROS_DEBUG("Reached end of processing object meshes request");
221 
222  return true;
223 }
224 
225 bool ObjectDatabase::processObjectTypeGeneratorRequest(ObjectTypeGenerator::Request &req, ObjectTypeGenerator::Response &res)
226 {
227  //check if given file exists
228  const fs::path sourcepath(req.sourcefile.c_str());
229  if (!fs::exists(sourcepath))
230  {
231  ROS_ERROR("File does not exist.");
232  return false;
233  }
234 
235  //check if given file ends with _tex.obj
236  std::string fileending = req.sourcefile.substr(req.sourcefile.find_last_of("_"));
237  if (fileending.compare("_tex.obj") != 0)
238  {
239  ROS_ERROR("File has to end with '_tex.obj.'");
240  return false;
241  }
242 
243  //check for the .mtl file in given directory
244  std::string::size_type const q(req.sourcefile.find_last_of('.'));
245  const fs::path mtlSourcePath ((req.sourcefile.substr(0, q) + ".obj.mtl").c_str());
246  if (!fs::exists(mtlSourcePath))
247  {
248  ROS_ERROR("No .mtl file can be found in given directory.");
249  return false;
250  }
251 
252  //extract filename from given path
253  std::string extfilename = req.sourcefile.substr(req.sourcefile.find_last_of("/\\") + 1);
254  std::string::size_type const p(extfilename.find_first_of('_'));
255  std::string filename = extfilename.substr(0, p);
256 
257  //get path to this package
258  std::string packagepath = ros::package::getPath("asr_object_database");
259 
260  //copy source file to database
261  std::string predestpath = packagepath + "/rsc/databases/textured_objects/" + filename + "/" + filename + ".obj";
262  const fs::path destpath(predestpath.c_str());
263 
264  std::string prenewDirectory = packagepath + "/rsc/databases/textured_objects/" + filename;
265  const fs::path newDirectory(prenewDirectory.c_str());
266 
267  if (!fs::exists(newDirectory))
268  {
269  fs::create_directory(newDirectory);
270  }
271 
272  fs::copy_file(sourcepath, destpath, fs::copy_option::overwrite_if_exists);
273 
274  //generate destination path for the .mtl file
275  std::string::size_type const r(extfilename.find_last_of('.'));
276  std::string premtlDestPath = prenewDirectory + "/" + extfilename.substr(0, r) + ".obj.mtl";
277  const fs::path mtlDestPath = premtlDestPath.c_str();
278 
279  //copy .mtl file
280  fs::copy_file(mtlSourcePath, mtlDestPath, fs::copy_option::overwrite_if_exists);
281 
282  //generate source path of the .png file
283  std::string prepngSourcePath = req.sourcefile.substr(0, q) + ".png";
284  const fs::path pngSourcePath = prepngSourcePath.c_str();
285 
286  if (!fs::exists(pngSourcePath))
287  {
288  ROS_ERROR("No .png file could be found in the given directory.");
289  return false;
290  }
291 
292  //generate destination path for the .png file
293  std::string prepngDestPath = prenewDirectory + "/" + extfilename.substr(0, r) + ".png";
294  const fs::path pngDestPath = prepngDestPath.c_str();
295 
296  //copy .png file
297  if (!fs::exists(pngDestPath))
298  {
299  fs::copy_file(pngSourcePath, pngDestPath);
300  }
301 
302  //create .dae file
303  std::string predaefilepath = packagepath + "/rsc/databases/textured_objects/" + filename + "/" + filename + ".dae";
304  const fs::path daefilepath(predaefilepath.c_str());
305  if (!fs::exists(daefilepath))
306  {
307  std::string databaseFolder = prenewDirectory + "/";
308  const char *navigateToDatabaseFolder = databaseFolder.c_str();
309  chdir(navigateToDatabaseFolder);
310  std::string mlsoutput = filename + ".dae";
311  const char *meshlabserver = ("meshlabserver -i " + filename+".obj" + " -o " + mlsoutput +
312  " -om vc vn wt").c_str();
313  system(meshlabserver);
314  }
315  return true;
316 }
317 
319 {
321  ROS_DEBUG("Start reading all object types");
322  BOOST_FOREACH(ObjectDatabaseRecognizerPtrMap::value_type mapValue, recognizerMap)
323  {
324  ROS_DEBUG("Reading %s", mapValue.first.c_str());
325  mapValue.second->readEntries();
326  }
327 }
328 }
bool processObjectMetaDataRequest(ObjectMetaData::Request &req, ObjectMetaData::Response &res)
std::map< std::string, ObjectDatabaseRecognizerPtr > ObjectDatabaseRecognizerPtrMap
Definition: typedef.h:34
ros::ServiceServer mObjectMetaDataServiceHandle
bool processObjectTypeListRequest(ObjectTypeList::Request &req, ObjectTypeList::Response &res)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
ros::ServiceServer mObjectTypeGeneratorServiceHandle
const ObjectDatabaseRecognizerPtr getRecognizer(const std::string recognizerName)
bool processObjectTypeGeneratorRequest(ObjectTypeGenerator::Request &req, ObjectTypeGenerator::Response &res)
bool processRecognizerListRequest(RecognizerList::Request &req, RecognizerList::Response &res)
bool processRecognizerListMeshesRequest(RecognizerListMeshes::Request &req, RecognizerListMeshes::Response &res)
std::map< std::string, ObjectDatabaseEntryPtr > ObjectDatabaseEntryPtrMap
Definition: typedef.h:40
bool read(const fs::path config_file)
ObjectDatabaseConfig mConfig
ROSLIB_DECL std::string getPath(const std::string &package_name)
::xsd::cxx::tree::string< char, simple_type > string
std::vector< ObjectDatabaseRecognizerPtr > ObjectDatabaseRecognizerPtrCollection
Definition: typedef.h:35
ros::ServiceServer mObjectTypeListServiceHandle
ros::ServiceServer mRecognizerListMeshesServiceHandle
std::pair< std::string, ObjectDatabaseEntryPtr > ObjectDatabaseEntryPtrMapPair
Definition: typedef.h:39
r
#define ROS_ERROR(...)
ObjectDatabase(fs::path configurationFilePath)
ros::ServiceServer mRecognizerListServiceHandle
#define ROS_DEBUG(...)
const ObjectDatabaseRecognizerPtrMap getObjectCategories()


asr_object_database
Author(s): Allgeyer Tobias, Aumann Florian, Borella Jocelyn, Braun Kai, Heizmann Heinrich, Heller Florian, Kasper Alexander, Marek Felix, Mehlhaus Jonas, Meißner Pascal, Schleicher Ralf, Stöckle Patrick, Walter Milena
autogenerated on Wed Jan 8 2020 03:12:13