Skip to content
Snippets Groups Projects
Commit 43d7cdd8 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add SimoxPath

parent 132e9d9d
No related branches found
No related tags found
No related merge requests found
......@@ -82,6 +82,7 @@ SET(SOURCES
math/statistics/Histogram1D.cpp
simox/SimoxPath.cpp
shapes/AxisAlignedBoundingBox.cpp
shapes/json_conversions.cpp
......@@ -237,6 +238,8 @@ SET(INCLUDES
meta/enum/adapt_enum.h
meta/enum/EnumNames.hpp
simox/SimoxPath.h
xml/rapidxml/rapidxml.hpp
xml/rapidxml/rapidxml_print.hpp
xml/rapidxml/RapidXMLWrapper.h
......
#pragma once
// This file is generated!
#include "simox/SimoxPath.h"
#include "SimoxPath.h"
#include <functional>
#include <stdlib.h>
namespace simox
{
bool SimoxPath::simoxPathInitialized = false;
std::filesystem::path SimoxPath::simoxRootPath = "";
void SimoxPath::init()
{
namespace fs = std::filesystem;
// Adapted from VirtualRobot::RuntimeEnvironment
if (simoxPathInitialized)
{
return;
}
SimoxPath::simoxRootPath = "";
std::vector<std::function<void(fs::path& vrDataPath)>> sources;
// Test special environment variables.
sources.push_back([](fs::path& vrDataPath)
{
char* vrDataPathEnv = getenv("SIMOX_DATA_PATH");
if (!vrDataPathEnv)
{
vrDataPathEnv = getenv("VIRTUAL_ROBOT_DATA_PATH");
}
if (vrDataPathEnv && checkPath(vrDataPathEnv))
{
vrDataPath = fs::path(vrDataPathEnv);
}
});
// Test for Simox_DIR
sources.push_back([](fs::path& vrDataPath)
{
char *simox_dir = getenv("Simox_DIR");
if (simox_dir)
{
const std::vector<std::string> candidates =
{
"data",
"VirtualRobot/data",
"../VirtualRobot/data"
};
vrDataPath = checkCandidatePaths(candidates, std::filesystem::path(simox_dir));
}
});
auto ConstantSource = [](const std::string& value)
{
return [value](fs::path& vrDataPath)
{
if (checkPath(value))
{
vrDataPath = value;
};
};
};
#ifdef Simox_DATA_PATH
sources.push_back(ConstantSource(Simox_DATA_PATH));
#endif
#ifdef VirtualRobot_DATA_PATH
sources.push_back(ConstantSource(VirtualRobot_DATA_PATH));
#endif
#ifdef VirtualRobot_SRC_DATA_PATH
sources.push_back(ConstantSource(VirtualRobot_SRC_DATA_PATH));
#endif
// Check standard linux install path
sources.push_back(ConstantSource("/usr/local/data"));
sources.push_back(ConstantSource("/usr/data"));
// Last chance, check for inbuild paths
sources.push_back([](fs::path& vrDataPath)
{
const std::vector<std::string> candidates =
{
"../VirtualRobot/data",
"../../VirtualRobot/data",
"../../../VirtualRobot/data",
"../../../../VirtualRobot/data"
};
vrDataPath = checkCandidatePaths(candidates, std::filesystem::current_path());
});
fs::path virtualRobotDataPath;
for (auto source : sources)
{
source(virtualRobotDataPath);
if (!virtualRobotDataPath.empty())
{
break;
}
}
SimoxPath::simoxRootPath = virtualRobotDataPath.parent_path().parent_path().lexically_normal();
simoxPathInitialized = true;
}
bool SimoxPath::checkPath(const char* path)
{
if (!path)
{
return false;
}
std::filesystem::path p(path);
return checkPath(p);
}
bool SimoxPath::checkPath(const std::filesystem::path& path)
{
return std::filesystem::is_directory(path) || std::filesystem::is_symlink(path);
}
std::filesystem::path SimoxPath::checkCandidatePaths(
const std::vector<std::string>& candidates, std::filesystem::path prefix)
{
for (const std::string& cand : candidates)
{
if (checkPath(prefix / cand))
{
return prefix / cand;
}
}
return "";
}
}
#include <filesystem>
namespace simox
{
class SimoxPath
{
public:
static std::filesystem::path getSimoxDir()
{
init();
return simoxRootPath;
}
static std::filesystem::path getSimoxUtilityDir()
{
return getSimoxDir() / "SimoxUtility";
}
static std::filesystem::path getVirtualRobotDir()
{
return getSimoxDir() / "VirtualRobot";
}
static std::filesystem::path getVirtualRobotDataDir()
{
return getVirtualRobotDir() / "data";
}
static std::filesystem::path getSimDynamicsDir()
{
return getSimoxDir() / "SimDynamics";
}
static std::filesystem::path getGraspPlanningDir()
{
return getSimoxDir() / "GraspPlanning";
}
static std::filesystem::path getMotionPlanningDir()
{
return getSimoxDir() / "MotionPlanning";
}
private:
SimoxPath();
static void init();
static bool checkPath(const char* path);
static bool checkPath(const std::filesystem::path& path);
static std::filesystem::path checkCandidatePaths(
const std::vector<std::string>& candidates, std::filesystem::path prefix = "");
private:
static bool simoxPathInitialized;
static std::filesystem::path simoxRootPath;
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment