ObjectDatabaseRecognizer.cpp
Go to the documentation of this file.
00001 
00021 #include <ros/ros.h>
00022 #include <boost/regex.hpp>
00023 #include "object_database/ObjectDatabaseRecognizer.h"
00024 #include <ros_uri/ros_uri.hpp>
00025 #include <boost/algorithm/string.hpp>
00026 
00027 namespace object_database
00028 {
00029 ObjectDatabaseRecognizer::ObjectDatabaseRecognizer(database db) :
00030     mDatabase(db),
00031     mRead(false)
00032 { }
00033 
00034 const std::string ObjectDatabaseRecognizer::getKeyword()
00035 {
00036     return mDatabase.uniqueName();
00037 }
00038 
00039 void ObjectDatabaseRecognizer::setKeyword(const std::string kw)
00040 {
00041     mDatabase.uniqueName(kw);
00042 }
00043 
00044 const fs::path ObjectDatabaseRecognizer::getPath()
00045 {
00046     std::string realPath = ros_uri::absolute_path(mDatabase.objectDbRootFolder());
00047     return fs::path(realPath);
00048 }
00049 
00050 void ObjectDatabaseRecognizer::setPath(const fs::path pt)
00051 {
00052     std::string packageString = ros_uri::package_uri(pt.string());
00053     mDatabase.objectDbRootFolder(packageString);
00054 }
00055 
00056 const std::string ObjectDatabaseRecognizer::getRegexp()
00057 {
00058     return mDatabase.basenameMatchPattern();
00059 }
00060 
00061 void ObjectDatabaseRecognizer::setRegexp(const std::string regexp)
00062 {
00063     mDatabase.basenameMatchPattern(regexp);
00064 }
00065 
00066 bool ObjectDatabaseRecognizer::isRead()
00067 {
00068     return mRead;
00069 }
00070 
00071 void ObjectDatabaseRecognizer::setRead(bool rd)
00072 {
00073     mRead = rd;
00074 }
00075 
00076 void ObjectDatabaseRecognizer::readEntries()
00077 {
00078     // get the path where the database objects can be found
00079     fs::path folder = this->getPath();
00080     std::string regexp = this->getRegexp();
00081 
00082     // if the folder path is no directory or if the folder is already read
00083     if (!fs::is_directory(folder))
00084     {
00085         ROS_DEBUG("'%s' is not a valid folder", folder.c_str());
00086         return;
00087     } else if (this->isRead())
00088     {
00089         ROS_DEBUG("'%s' already read", folder.c_str());
00090         return;
00091     }
00092 
00093     // Matching the expression regexp
00094     boost::regex expression(regexp);
00095 
00096 
00097     for (fs::directory_iterator diriter(folder); diriter != fs::directory_iterator(); diriter++)
00098     {
00099         fs::path path = *diriter;
00100 
00101         fs::path filename = path.filename();
00102         boost::cmatch what;
00103         if (boost::regex_match(filename.c_str(), what, expression))
00104         {
00105             if (fs::is_directory(path))
00106             {
00107                 std::string strUniqueName;
00108 
00109                 // generate the filename
00110                 if (what.size() > 1)
00111                 {
00112                     strUniqueName = what[1].str();
00113                 } else
00114                 {
00115                     strUniqueName = what[0].str();
00116                 }
00117 
00118                 fs::path rvizMeshResourcePath = fs::path();
00119                 fs::path normalVectorResourcePath = fs::path();
00120                 fs::path deviation_resource_path = fs::path();
00121                 fs::path rotation_invariance_resource_path = fs::path();
00122                 for (fs::directory_iterator subdiriter(path); subdiriter != fs::directory_iterator();
00123                      subdiriter++)
00124                 {
00125                     fs::path subdir_path = *subdiriter;
00126                     fs::path subdir_filename = subdir_path.filename();
00127 
00128                     boost::cmatch match;
00129                     if (boost::regex_match(subdir_filename.c_str(), match, boost::regex(".*\\.dae")))
00130                     {
00131                         rvizMeshResourcePath = subdir_path;
00132                     } else
00133                     {
00134                         if (boost::regex_match(subdir_filename.c_str(), match, boost::regex(".*\\.nv.txt")))
00135                         {
00136                             normalVectorResourcePath = subdir_path;
00137                         }
00138                         if (boost::regex_match(subdir_filename.c_str(), match, boost::regex(".*\\.dev.txt")))
00139                         {
00140                             deviation_resource_path = subdir_path;
00141                         }
00142                         if (boost::regex_match(subdir_filename.c_str(), match, boost::regex(".*\\.ri.txt")))
00143                         {
00144                             rotation_invariance_resource_path = subdir_path;
00145                         }
00146                     }
00147                 }
00148 
00149                 ObjectDatabaseEntryPtr entryPtr(new ObjectDatabaseEntry(this, strUniqueName,
00150                                                                         path, rvizMeshResourcePath,
00151                                                                         normalVectorResourcePath,
00152                                                                         deviation_resource_path,
00153                                                                         rotation_invariance_resource_path));
00154                 mEntries[entryPtr->getUniqueName()] = entryPtr;
00155 
00156                 ROS_DEBUG("File '%s' in db called '%s'", filename.c_str(), strUniqueName.c_str());
00157             }
00158         }
00159     }
00160     this->setRead(true);
00161 }
00162 
00163 ObjectDatabaseEntryPtrMap ObjectDatabaseRecognizer::getEntries()
00164 {
00165     return mEntries;
00166 }
00167 
00168 const ObjectDatabaseEntryPtr& ObjectDatabaseRecognizer::getEntry(const std::string objectName)
00169 {
00170     ObjectDatabaseEntryPtr ptr = this->mEntries[objectName];
00171     return *(new ObjectDatabaseEntryPtr(ptr));
00172 }
00173 }
00174 


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 Thu Jun 6 2019 21:11:02