Mujoco KDL Wrapper  0.2.2
MuJoCo + KDL bridge for robot kinematics and dynamics
Loading...
Searching...
No Matches
example_paths.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
4#include <filesystem>
5#include <stdexcept>
6#include <string>
7
8namespace mj_kdl_examples {
9namespace fs = std::filesystem;
10
11// Mirrored in python/mj_kdl_wrapper/menagerie.py:_cache_root and CMakeLists.txt's
12// _MJ_KDL_CACHE_ROOT; keep all three in sync.
13inline fs::path cache_root()
14{
15 if (const char *xdg = std::getenv("XDG_CACHE_HOME"); xdg && *xdg) {
16 return fs::path(xdg) / "mj_kdl_wrapper";
17 }
18 if (const char *home = std::getenv("HOME"); home && *home) {
19 return fs::path(home) / ".cache" / "mj_kdl_wrapper";
20 }
21 return {};
22}
23
24inline std::string find_menagerie_model(const fs::path &relative)
25{
26 if (const char *root = std::getenv("MJ_KDL_MENAGERIE"); root && *root) {
27 const auto path = fs::path(root) / relative;
28 if (fs::exists(path)) return path.string();
29 }
30 const auto path = cache_root() / "menagerie" / relative;
31 return fs::exists(path) ? path.string() : "";
32}
33
34inline std::string menagerie_model(const fs::path &relative)
35{
36 if (std::string path = find_menagerie_model(relative); !path.empty()) return path;
37 throw std::runtime_error(
38 relative.string() + " not found. Searched $MJ_KDL_MENAGERIE and "
39 + (cache_root() / "menagerie").string()
40 + ". Run 'mj-kdl-fetch-menagerie' or set MJ_KDL_MENAGERIE.");
41}
42
43inline std::string find_asset(const fs::path &relative)
44{
45 const auto path = cache_root() / "assets" / relative;
46 return fs::exists(path) ? path.string() : "";
47}
48
49inline std::string asset(const fs::path &relative)
50{
51 if (std::string path = find_asset(relative); !path.empty()) return path;
52 throw std::runtime_error(
53 relative.string() + " not found in " + (cache_root() / "assets").string()
54 + ". Run 'mj-kdl-fetch-menagerie' to populate bundled assets.");
55}
56} // namespace mj_kdl_examples
std::string asset(const fs::path &relative)
std::string menagerie_model(const fs::path &relative)
std::string find_asset(const fs::path &relative)
std::string find_menagerie_model(const fs::path &relative)
fs::path cache_root()