18 #ifndef __ROSEE_UTILS__ 19 #define __ROSEE_UTILS__ 27 #include <boost/filesystem.hpp> 40 boost::filesystem::path path(pathDirectory);
41 return boost::filesystem::create_directories(path);
44 static void out2file ( std::string pathFile, std::string output) {
45 std::ofstream fout ( pathFile );
55 static std::vector <std::string>
getFilesInDir ( std::string pathFolder ) {
57 boost::filesystem::path p (pathFolder);
58 std::vector <std::string> retVect;
60 if (! boost::filesystem::exists(p) ) {
61 std::cerr <<
"[ERROR " << __func__ <<
"] path '" << pathFolder <<
"' does not exists" << std::endl;
65 if (! boost::filesystem::is_directory(p)){
66 std::cerr <<
"[ERROR " << __func__ <<
"] path '" << pathFolder <<
"' is not a directory" << std::endl;
70 for (boost::filesystem::directory_entry& x : boost::filesystem::directory_iterator(p)) {
71 retVect.push_back (x.path().filename().string() );
79 if (k == 0 || k == n){
89 boost::filesystem::path path(__FILE__);
90 path.remove_filename();
91 return path.string() +
"/../../";
94 template <
class KeyType,
class ValueType>
95 static std::vector<KeyType>
extract_keys(std::map<KeyType, ValueType>
const& input_map) {
96 std::vector<KeyType> retval;
97 for (
auto const& element : input_map) {
98 retval.push_back(element.first);
114 std::map<std::set<std::string>, T>
const& input_map,
unsigned int max_string_number = 0) {
116 std::set<std::string> allStrings;
119 if (max_string_number == 0) {
120 for (
auto const& element : input_map) {
121 allStrings.insert( element.first.begin(), element.first.end() );
125 for (
auto const& element : input_map) {
126 allStrings.insert(element.first.begin(), element.first.end());
127 if (max_string_number == allStrings.size()){
130 if (max_string_number < allStrings.size() ) {
131 std::cerr <<
"[ERROR]" << __func__ <<
" You passed " << max_string_number
132 <<
" but I found more unique strings in the set keys ( " << allStrings.size()
133 <<
" found)" << std::endl;
134 return std::vector<std::string>();
138 std::vector<std::string> retval (allStrings.begin(), allStrings.end());
147 std::map<std::pair<std::string,std::string>, T>
const& input_map,
unsigned int max_string_number = 0) {
149 std::set<std::string> allStrings;
152 if (max_string_number == 0) {
153 for (
auto const& element : input_map) {
154 allStrings.insert( element.first.first);
155 allStrings.insert( element.first.second);
159 for (
auto const& element : input_map) {
160 allStrings.insert( element.first.first);
161 allStrings.insert( element.first.second);
162 if (max_string_number == allStrings.size()){
165 if (max_string_number < allStrings.size() ) {
166 std::cerr <<
"[ERROR]" << __func__ <<
" You passed " << max_string_number
167 <<
" but I found more unique strings in the pair keys ( " << allStrings.size()
168 <<
" found)" << std::endl;
169 return std::vector<std::string>();
173 std::vector<std::string> retval (allStrings.begin(), allStrings.end());
181 template <
typename keyType,
typename valueType1,
typename valueType2>
182 bool keys_equal (std::map <keyType, valueType1>
const &lhs, std::map<keyType, valueType2>
const &rhs) {
184 auto pred = [] (decltype(*lhs.begin()) a, decltype(*rhs.begin()) b)
185 {
return (a.first == b.first); };
188 return lhs.size() == rhs.size()
189 && std::equal(lhs.begin(), lhs.end(), rhs.begin(), pred);
192 template <
typename Map1,
typename Map2>
198 map1(map1), map2(map2) {}
200 const char *
what ()
const throw () {
201 std::stringstream output;
202 output <<
"First map keys:\n";
203 for (
auto it : *map1) {
204 output <<
"\t" << it.first <<
"\n";
206 output << (
"Second map keys:\n");
207 for (
auto it : *map2) {
208 output <<
"\t" << it.first <<
"\n";
210 std::cerr << output.str().c_str() << std::endl;
212 return "Maps have different keys";
227 template <
typename RetType,
typename... Args>
229 std::string function_name,
232 if (lib_name.empty()) {
234 std::cerr <<
"[Utils::loadObject] ERROR: Please specify lib_name" << std::endl;
238 std::string lib_name_path =
"lib" + lib_name +
".so";
243 void* lib_handle = dlopen(lib_name_path.c_str(), RTLD_LAZY);
244 auto error = dlerror();
246 if (!lib_handle || error != NULL) {
247 std::cerr <<
"[Utils::loadObject] ERROR in opening the library: " << error << std::endl;
254 RetType* (*function)(Args... args);
255 function =
reinterpret_cast<RetType* (*)(Args... args)
>(dlsym(lib_handle, function_name.c_str()));
257 if ( error != NULL) {
258 std::cerr <<
"[Utils::loadObject] ERROR in returning the function: " << error << std::endl;
262 RetType* objectRaw =
function(args...);
264 std::unique_ptr<RetType> objectPtr(objectRaw);
273 template <
typename Clock = std::chrono::high_resolution_clock>
279 Timer() : start_point(Clock::now()) {}
281 void reset() { start_point = Clock::now(); }
283 template <
typename Rep =
typename Clock::duration::rep,
typename Units =
typename Clock::duration>
286 std::atomic_thread_fence(std::memory_order_relaxed);
287 auto counted_time = std::chrono::duration_cast<Units>(Clock::now() - start_point).count();
288 std::atomic_thread_fence(std::memory_order_relaxed);
289 return static_cast<Rep
>(counted_time);
293 template <
typename SignalType>
299 typedef std::shared_ptr<SecondOrderFilter<SignalType>>
Ptr;
305 _reset_has_been_called ( false )
314 _reset_has_been_called ( false )
317 reset ( initial_state );
320 void reset (
const SignalType& initial_state )
322 _reset_has_been_called =
true;
326 _ydd = initial_state;
327 _udd = initial_state;
331 const SignalType&
process (
const SignalType& input )
334 if ( !_reset_has_been_called ) {
346 _y = 1.0/_a0 * ( _u + _b1*_ud + _b2*_udd - _a1*_yd - _a2*_ydd );
396 _a0 = 1.0 + 4.0*_eps/ ( _omega*_ts ) + 4.0/std::pow ( _omega*_ts, 2.0 );
397 _a1 = 2 - 8.0/std::pow ( _omega*_ts, 2.0 );
398 _a2 = 1.0 + 4.0/std::pow ( _omega*_ts, 2.0 ) - 4.0*_eps/ ( _omega*_ts );
411 SignalType _y, _yd,
_ydd, _u, _ud, _udd;
420 #endif // __ROSEE_UTILS__ void setOmega(double omega)
std::unique_ptr< RetType > loadObject(std::string lib_name, std::string function_name, Args...args)
Utils to dynamically load an object.
static std::vector< std::string > getFilesInDir(std::string pathFolder)
const SignalType & getOutput() const
const SignalType & process(const SignalType &input)
bool _reset_has_been_called
static std::vector< KeyType > extract_keys(std::map< KeyType, ValueType > const &input_map)
static bool create_directory(std::string pathDirectory)
bool keys_equal(std::map< keyType, valueType1 > const &lhs, std::map< keyType, valueType2 > const &rhs)
Return false if two maps have different keys.
void setDamping(double eps)
static std::vector< std::string > extract_keys_merged(std::map< std::set< std::string >, T > const &input_map, unsigned int max_string_number=0)
Extract all the string in the set keys of a map.
static void out2file(std::string pathFile, std::string output)
void setTimeStep(double ts)
Clock::time_point start_point
SecondOrderFilter(double omega, double eps, double ts, const SignalType &initial_state)
static int binomial_coefficent(int n, int k)
const char * what() const
static std::string getPackagePath()
DifferentKeysException(const Map1 *map1, const Map2 *map2)
void reset(const SignalType &initial_state)
std::shared_ptr< SecondOrderFilter< SignalType > > Ptr