pinocchio  2.7.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
utils.hpp
1 //
2 // Copyright (c) 2015 - 2016 CNRS
3 //
4 
5 #ifndef __pinocchio_parsers_utils_hpp__
6 #define __pinocchio_parsers_utils_hpp__
7 
8 #include <iostream>
9 #include <limits>
10 #include <sstream>
11 // #include <stdexcept>
12 
13 #include <boost/filesystem/fstream.hpp>
14 #include <boost/foreach.hpp>
15 #include <boost/format.hpp>
16 
17 #include "pinocchio/utils/file-explorer.hpp"
18 #include <boost/filesystem.hpp>
19 
20 #include <exception>
21 
22 namespace pinocchio
23 {
28  UNKNOWN = 0,
29  URDF
30  };
31 
39  inline ModelFileExtensionType checkModelFileExtension(const std::string & filename)
40  {
41  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
42 
43  if (extension == "urdf")
44  return URDF;
45 
46  return UNKNOWN;
47  }
48 
49 
50 
61  inline std::string retrieveResourcePath(const std::string & string,
62  const std::vector<std::string> & package_dirs)
63  {
64 
65  namespace bf = boost::filesystem;
66  std::string result_path;
67 
68  const std::string separator("://");
69  const std::size_t pos_separator = string.find(separator);
70  bf::path string_path(string);
71 
72  if (pos_separator != std::string::npos)
73  {
74  std::string scheme = string.substr(0, pos_separator);
75  std::string path = string.substr(pos_separator+3, std::string::npos);
76 
77  if(scheme == "package")
78  {
79  // if exists p1/string, path = p1/string,
80  // else if exists p2/string, path = p2/string
81  // else return an empty string that may provoke an error in loadPolyhedronFromResource()
82 
83  // concatenate package_path with filename
84  for (std::size_t i = 0; i < package_dirs.size(); ++i)
85  {
86  if ( bf::exists( bf::path(package_dirs[i] + "/" + path)))
87  {
88  result_path = std::string( package_dirs[i] + "/" + path );
89  break;
90  }
91  }
92  }
93  else if (scheme == "file")
94  {
95  result_path = path;
96  }
97  else
98  {
99  const std::string exception_message ("Schemes of form" + scheme + "are not handled");
100  throw std::invalid_argument(exception_message);
101  }
102  }
103  else if (string_path.is_relative())
104  {
105  // handle the case where a relative mesh path is specified without using //package
106  for (std::size_t i = 0; i < package_dirs.size(); ++i)
107  {
108  if ( bf::exists( bf::path(package_dirs[i] + "/" + string)))
109  {
110  result_path = std::string( package_dirs[i] + "/" + string);
111  break;
112  }
113  }
114  }
115  else // return the entry string
116  {
117  result_path = string;
118  }
119 
120  return result_path;
121  }
122 
123 } // namespace pinocchio
124 
125 #endif // __pinocchio_parsers_utils_hpp__
pinocchio::checkModelFileExtension
ModelFileExtensionType checkModelFileExtension(const std::string &filename)
Extract the type of the given model file according to its extension.
Definition: utils.hpp:39
pinocchio::ModelFileExtensionType
ModelFileExtensionType
Supported model file extensions.
Definition: utils.hpp:27
pinocchio::retrieveResourcePath
std::string retrieveResourcePath(const std::string &string, const std::vector< std::string > &package_dirs)
Retrieve the path of the file whose path is given in URL-format. Currently convert from the following...
Definition: utils.hpp:61
pinocchio
Main pinocchio namespace.
Definition: treeview.dox:11