Pogosim
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#ifndef UTILS_H
2#define UTILS_H
3
4#include <spdlog/spdlog.h>
5#include <spdlog/sinks/stdout_color_sinks.h> // For colored console output
6#include <spdlog/fmt/ostr.h> // Enables << operator for logging
7
8#include <box2d/box2d.h>
9
10#include <random>
11#include <cstdint>
12#include <algorithm>
13
14#include "configuration.h"
15
16#define _USE_MATH_DEFINES
17
18
19#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
20 #include <span>
21 template<class T> using span_t = std::span<const T>;
22#else
23 #include <vector>
24 template<class T> using span_t = const std::vector<T>&; // read-only view
25#endif
26
27
29extern std::shared_ptr<spdlog::logger> glogger;
31extern std::shared_ptr<spdlog::logger> robotlogger;
32
34extern std::random_device rd;
36extern std::mt19937 rnd_gen;
37
43void seed_random_generators(uint32_t seed);
44
50uint32_t make_random_seed();
51
67template<typename T>
68[[nodiscard]] constexpr bool contains(const std::vector<T>& vec,
69 const T& value) {
70 return std::find(vec.begin(), vec.end(), value) != vec.end();
71}
72
73
88[[nodiscard]] constexpr bool all_true(const std::vector<bool>& flags) {
89 return std::ranges::all_of(flags, [](bool v) { return v; });
90}
91
92
100void init_logger(Configuration& config);
101
111void loggers_add_file_sink(std::string const& filename);
112
125bool string_to_bool( std::string const& str);
126
135std::string to_lowercase(std::string const& str);
136
146void ensure_directories_exist(const std::string& filename);
147
159void delete_files_with_extension(const std::string& path, const std::string& extension, bool recursive = false);
160
174std::string resolve_path(const std::string& inputPath);
175
176
177#endif // UTILS_H
178
179// MODELINE "{{{1
180// vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
181// vim:foldmethod=marker
Class for managing hierarchical configuration parameters.
Definition configuration.h:64
std::shared_ptr< spdlog::logger > robotlogger
Global logger for robot-specific logging.
Definition utils.cpp:13
std::random_device rd
Random device used for seeding the random generator.
Definition utils.cpp:16
std::shared_ptr< spdlog::logger > glogger
Global logger for general logging.
Definition utils.cpp:12
std::string to_lowercase(std::string const &str)
Converts a string to lowercase.
Definition utils.cpp:82
void seed_random_generators(uint32_t seed)
Seed all simulator RNGs from a single seed value.
Definition utils.cpp:19
constexpr bool all_true(const std::vector< bool > &flags)
Check whether every element in a std::vector<bool> is true.
Definition utils.h:88
bool string_to_bool(std::string const &str)
Converts a string to a boolean value.
Definition utils.cpp:67
std::string resolve_path(const std::string &inputPath)
Resolves an input file path to an absolute path.
Definition utils.cpp:137
uint32_t make_random_seed()
Generate a non-deterministic seed value.
Definition utils.cpp:24
void ensure_directories_exist(const std::string &filename)
Ensures that the directory for a given file exists.
Definition utils.cpp:89
const std::vector< T > & span_t
Definition utils.h:24
std::mt19937 rnd_gen
Random number generator seeded with rd.
void delete_files_with_extension(const std::string &path, const std::string &extension, bool recursive=false)
Deletes files with a specified extension in a given directory.
Definition utils.cpp:103
constexpr bool contains(const std::vector< T > &vec, const T &value)
Check whether a vector contains a given value.
Definition utils.h:68
void init_logger(Configuration &config)
Initializes the global loggers.
Definition utils.cpp:29
void loggers_add_file_sink(std::string const &filename)
Adds a file sink to the global loggers.
Definition utils.cpp:53