ObjectDatabaseEntry.cpp
Go to the documentation of this file.
00001 
00021 #include "object_database/ObjectDatabaseEntry.h"
00022 #include <ros_uri/ros_uri.hpp>
00023 #include <iostream>
00024 #include <fstream>
00025 #include <string>
00026 #include <vector>
00027 #include <boost/algorithm/string/predicate.hpp>
00028 #include <boost/lexical_cast.hpp>
00029 
00030 #include <math.h>
00031 #include "object_database/NormalGenerator.h"
00032 
00033 namespace object_database
00034 
00035 {
00036 namespace fs = boost::filesystem;
00037 
00038 ObjectDatabaseEntry::ObjectDatabaseEntry(ObjectDatabaseRecognizer* recognizerPtr,
00039                                          const std::string uniqueName, const fs::path path,
00040                                          const fs::path rvizMeshResourcePath,
00041                                          const fs::path normalVectorResourcePath) :
00042     mRecognizerPtr(recognizerPtr),
00043     mUniqueName(uniqueName),
00044     mPath(path),
00045     mRvizMeshResourcePath(rvizMeshResourcePath),
00046     mNormalVectorResourcePath(normalVectorResourcePath),
00047     mNormalVectors()
00048 { }
00049 
00050 ObjectDatabaseEntry::ObjectDatabaseEntry(ObjectDatabaseRecognizer *recognizerPtr,
00051                                          const std::string uniqueName,
00052                                          const fs::path path, const fs::path rvizMeshResourcePath,
00053                                          const fs::path normalVectorResourcePath,
00054                                          const fs::path deviation_resource_path,
00055                                          const fs::path rotation_invariance_resource_path):
00056     mRecognizerPtr(recognizerPtr),
00057     mUniqueName(uniqueName),
00058     mPath(path),
00059     mRvizMeshResourcePath(rvizMeshResourcePath),
00060     mNormalVectorResourcePath(normalVectorResourcePath),
00061     deviation_resource_path_(deviation_resource_path),
00062     rotation_invariance_resource_path(rotation_invariance_resource_path),
00063     mNormalVectors(),
00064     deviations_(),
00065     rotation_invariant(false)
00066 {}
00067 
00068 ObjectDatabaseRecognizer* ObjectDatabaseEntry::getRecognizer()
00069 {
00070     return mRecognizerPtr;
00071 }
00072 
00073 const std::string ObjectDatabaseEntry::getUniqueName()
00074 {
00075     return mUniqueName;
00076 }
00077 
00078 const fs::path ObjectDatabaseEntry::getPath()
00079 {
00080     return mPath;
00081 }
00082 
00083 const fs::path ObjectDatabaseEntry::getRvizMeshResourcePath()
00084 {
00085     return mRvizMeshResourcePath;
00086 }
00087 
00088 const fs::path ObjectDatabaseEntry::getNormalVectorResourcePath()
00089 {
00090     return mNormalVectorResourcePath;
00091 }
00092 
00093 std::vector<geometry_msgs::Point> ObjectDatabaseEntry::getNormalVectors()
00094 {
00095     // if there is no path given or the normal vectors are already given
00096     if (mNormalVectors.size() > 0)
00097     {
00098         ROS_DEBUG("Returning cached normal vectors for object '%s'.", this->getUniqueName().c_str());
00099         return mNormalVectors;
00100     }
00101     else if (this->getNormalVectorResourcePath() == fs::path())
00102     {
00103         ROS_WARN("No normal vector file found for object '%s'.", this->getUniqueName().c_str());
00104         return mNormalVectors;
00105     }
00106 
00107     if (fs::exists(this->getNormalVectorResourcePath()))
00108     {
00109         std::ifstream normalVectorFileHandle(this->getNormalVectorResourcePath().c_str());
00110         if (normalVectorFileHandle.is_open())
00111         {
00112             ROS_INFO("Reading normal vector file for object '%s'", this->getUniqueName().c_str());
00113             std::string normalVectorString;
00114             std::size_t startSearchPos = 0;
00115             while (std::getline(normalVectorFileHandle, normalVectorString))
00116             {
00117                 while (startSearchPos < normalVectorString.size())
00118                 {
00119                     std::size_t searchPos = normalVectorString.find(';', startSearchPos);
00120                     if (searchPos == std::string::npos)
00121                     {
00122                         searchPos = normalVectorString.size();
00123                     }
00124 
00125                     // get the coordinate string.
00126                     std::string coordinateString = normalVectorString.substr(startSearchPos,
00127                                                                              (searchPos - startSearchPos));
00128 
00129                     std::size_t startValueSearchPos = 0;
00130                     std::vector<double> coordinateStack;
00131                     while (startValueSearchPos < coordinateString.size())
00132                     {
00133                         std::size_t valueSearchPos = coordinateString.find(',', startValueSearchPos);
00134                         if (valueSearchPos == std::string::npos)
00135                         {
00136                             valueSearchPos = coordinateString.size();
00137                         }
00138 
00139                         std::string valueString = coordinateString.substr(startValueSearchPos,
00140                                                                           (valueSearchPos - startValueSearchPos));
00141 
00142 
00143                         double coordinateValue = boost::lexical_cast<double>(valueString);
00144                         coordinateStack.push_back(coordinateValue);
00145 
00146                         startValueSearchPos = valueSearchPos + 1;
00147                     }
00148 
00149                     if (coordinateStack.size() != 3)
00150                     {
00151                         ROS_WARN("Ignoring a coordinate because there were not exactly three values given.");
00152                         continue;
00153                     }
00154 
00155                     // create the normal vector and add it
00156                     geometry_msgs::Point normalVector;
00157                     normalVector.x = coordinateStack [0];
00158                     normalVector.y = coordinateStack [1];
00159                     normalVector.z = coordinateStack [2];
00160                     mNormalVectors.push_back(normalVector);
00161 
00162                     startSearchPos = searchPos + 1;
00163                 }
00164             }
00165 
00166 
00167             // close file.
00168             normalVectorFileHandle.close();
00169             return mNormalVectors;
00170         }
00171     }
00172 
00173     //if(fs::exists(this->getRvizMeshResourcePath()) && fs::exists(this->getPath()))
00174     else if(fs::exists(this->getPath()))
00175     {
00176         ROS_INFO("Calculating normal vectors for object '%s'", this->getUniqueName().c_str());
00177         // calculate the normal vector
00178         mNormalVectors = NormalGenerator::getNormalVectors(30 * M_PI / 180, 30 * M_PI / 180, 30 * M_PI / 180,
00179                                                            this->getPath().string() + "/" +
00180                                                            this->getUniqueName() + ".nv.txt");
00181     }
00182 
00183     return mNormalVectors;
00184 }
00185 
00186 fs::path ObjectDatabaseEntry::getDeviationResourcePath() const
00187 {
00188     return deviation_resource_path_;
00189 }
00190 
00191 std::vector<double> ObjectDatabaseEntry::getDeviationsFromFile()
00192 {
00193     if (deviations_.size() == 6)
00194     {
00195         return deviations_;
00196     }
00197     if (fs::exists(getDeviationResourcePath()))
00198     {
00199         std::ifstream deviation_file_handle(getDeviationResourcePath().c_str());
00200         if (deviation_file_handle.is_open())
00201         {
00202             ROS_INFO("Reading deviation file for object '%s'", getUniqueName().c_str());
00203             std::string deviation_string;
00204 
00205             unsigned int line_number = 0;
00206             while (std::getline(deviation_file_handle, deviation_string))
00207             {
00208 
00209                 line_number++;
00210                 if (boost::starts_with(deviation_string, "#"))
00211                     continue;
00212                 try
00213                 {
00214                     deviations_.push_back(std::stod(deviation_string));
00215                 }
00216                 catch (const std::invalid_argument& ia)
00217                 {
00218                     ROS_ERROR_STREAM("Error by parsing" << getDeviationResourcePath().c_str() <<
00219                                      " in line " << line_number << ": " << ia.what());
00220                 }
00221                 if(deviations_.size() >= 6)
00222                     break;
00223             }
00224             if (deviations_.size() < 6)
00225             {
00226                 ROS_ERROR_STREAM("There were only " << deviations_.size()
00227                                  << "values for the deviation, but 6 are needed");
00228                 deviations_.clear();
00229             }
00230             for (double d: deviations_)
00231                 ROS_INFO_STREAM("read deviation:" << d );
00232             deviation_file_handle.close();
00233         }
00234     }
00235     return deviations_;
00236 }
00237 
00238 fs::path ObjectDatabaseEntry::getRotationInvarianceResourcePath() const
00239 {
00240     return rotation_invariance_resource_path;
00241 }
00242 
00243 const bool ObjectDatabaseEntry::getRotationInvarianceFromFile()
00244 {
00245     // if there is no path given or the normal vectors are already given
00246     if (rotation_invariant)
00247     {
00248         return rotation_invariant;
00249     }
00250     else if (this->getRotationInvarianceResourcePath() == fs::path())
00251     {
00252         ROS_WARN("No rotation invariance file found for object '%s'.", this->getUniqueName().c_str());
00253         return rotation_invariant;
00254     }
00255 
00256     if (fs::exists(this->getRotationInvarianceResourcePath()))
00257     {
00258         std::ifstream rotation_invariance_file_handle(this->getRotationInvarianceResourcePath().c_str());
00259         if (rotation_invariance_file_handle.is_open())
00260         {
00261             ROS_INFO("Reading rotation invariance file for object '%s'", this->getUniqueName().c_str());
00262             std::string rotation_invariance_string;
00263             std::getline(rotation_invariance_file_handle, rotation_invariance_string);
00264             rotation_invariant = boost::lexical_cast<bool>(rotation_invariance_string);
00265 
00266             // close file.
00267             rotation_invariance_file_handle.close();
00268         }
00269     }
00270     return rotation_invariant;
00271 }
00272 }
00273 


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