helper_functions.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <string>
00019 #include <map>
00020 
00021 #include <ros/ros.h>
00022 #include <ros/package.h>
00023 #include <boost/filesystem.hpp>
00024 #include "cob_obstacle_distance/helpers/helper_functions.hpp"
00025 
00026 
00028 const std::string resolveURI(const std::string& path)
00029 {
00030     static std::map<std::string, std::string> package_cache;
00031     std::string uri = path;
00032 
00033     if (uri.find("file://") == 0)
00034     {
00035       // Strip off the file://
00036       uri.erase(0, strlen("file://"));
00037 
00038       // Resolve the mesh path as a file URI
00039       boost::filesystem::path file_path(uri);
00040       return file_path.string();
00041     }
00042     else if (uri.find("package://") == 0)
00043     {
00044       // Strip off the package://
00045       uri.erase(0, strlen("package://"));
00046 
00047       // Resolve the mesh path as a ROS package URI
00048       size_t package_end = uri.find("/");
00049       std::string package = uri.substr(0, package_end);
00050       std::string package_path;
00051 
00052       // Use the package cache if we have resolved this package before
00053       std::map<std::string, std::string>::iterator it = package_cache.find(package);
00054       if (it != package_cache.end())
00055       {
00056           package_path = it->second;
00057       }
00058       else
00059       {
00060           package_path = ros::package::getPath(package);
00061           package_cache[package] = package_path;
00062       }
00063 
00064       // Show a warning if the package was not resolved
00065       if (package_path.empty())
00066       {
00067           ROS_ERROR("Unable to find package [%s].", package.c_str());
00068           return "";
00069       }
00070 
00071       // Append the remaining relative path
00072       boost::filesystem::path file_path(package_path);
00073       uri.erase(0, package_end);
00074       file_path /= uri;
00075 
00076       // Return the canonical path
00077       return file_path.string();
00078     }
00079     else
00080     {
00081       ROS_ERROR("Cannot handle mesh URI type [%s].", uri.c_str());
00082       return "";
00083     }
00084 }


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Jun 6 2019 21:19:14