CellModules
utils.hpp
Go to the documentation of this file.
1#ifndef UTILS_H
2#define UTILS_H
3#include <deque>
4#include <random>
5#include <sstream>
6#include <unordered_map>
7#include <utility>
8#include <vector>
10#include "config.hpp"
11#include "logger.hpp"
12
17namespace MecaCell {
18
22using Vec = Vector3D;
23
32template <typename T> inline T *ptr(T &obj) { return &obj; }
41template <typename T> inline T *ptr(T *obj) { return obj; }
42
53template <typename T> constexpr T lerp(const T &a, const T &b, const double &c) {
54 return a * (1.0 - c) + c * b;
55}
56
57template <typename T> inline bool fuzzyEqual(const T &a, const T &b, double eps = 1e-6) {
58 return (fabs(a - b) < eps * fabs(a));
59}
60
61// containers helpers
62template <typename T> inline bool isInVector(const T &elem, const std::vector<T> &vec) {
63 return std::find(vec.begin(), vec.end(), elem) != vec.end();
64}
65template <typename T> inline void eraseFromVector(const T &elem, std::deque<T> &vec) {
66 vec.erase(std::remove(vec.begin(), vec.end(), elem), vec.end());
67}
68template <typename T> inline void eraseFromVector(const T &elem, std::vector<T> &vec) {
69 vec.erase(std::remove(vec.begin(), vec.end(), elem), vec.end());
70}
71
80inline std::vector<std::string> splitStr(const std::string &s, char delim) {
82 std::stringstream ss(s);
83 std::string st;
84 while (std::getline(ss, st, delim)) res.push_back(st);
85 return res;
86}
87
97inline std::array<double, 3> hsvToRgb(double h, double s, double v) {
98 if (s <= 0.0) return {{v, v, v}};
99 double hh = fmod(h, 1.0);
100 hh *= 6.0;
101 unsigned int i = static_cast<unsigned int>(hh);
102 double ff = hh - static_cast<double>(i);
103 double p = v * (1.0 - s);
104 double q = v * (1.0 - (s * ff));
105 double t = v * (1.0 - (s * (1.0 - ff)));
106 switch (i) {
107 case 0:
108 return {{v, t, p}};
109 case 1:
110 return {{q, v, p}};
111 case 2:
112 return {{p, v, t}};
113 case 3:
114 return {{p, q, v}};
115 case 4:
116 return {{t, p, v}};
117 case 5:
118 default:
119 return {{v, p, q}};
120 }
121}
122
123// -------- metaprog (compile time) general helpersi ----------
124// power
125template <typename T> inline constexpr T constpow(const T base, unsigned const exponent) {
126 return (exponent == 0) ? 1 : (base * constpow(base, exponent - 1));
127}
128// converts a class enum to its corresponding size_t
129template <typename T> constexpr size_t eToUI(const T &t) {
130 return static_cast<size_t>(t);
131}
132
133inline double dampingFromRatio(const double r, const double m, const double k) {
134 return r * 2.0 * sqrt(m * k); // for angular springs m is the moment of inertia
135}
136
137} // namespace MecaCell
138
139namespace std {
140// hash function} for generic pair
141template <typename T, typename U> struct hash<pair<T, U>> {
142 size_t operator()(const pair<T, U> &x) const {
143 return hash<T>()(x.first) ^ hash<U>()(x.second);
144 }
145};
146} // namespace std
147#endif
general purpose 3D vector/point class.
Definition: vector3D.h:20
A simple vector class template.
Definition: std.hpp:290
void erase(iterator position)
Removes the element at the specified position.
Definition: std.hpp:381
void push_back(const T &value)
Adds an element to the end of the vector.
Definition: std.hpp:304
iterator begin()
Returns an iterator to the first element.
Definition: std.hpp:409
iterator end()
Returns an iterator to the last element.
Definition: std.hpp:418
this file contains various miscellanious utility functions & helpers *
constexpr T lerp(const T &a, const T &b, const double &c)
linear interpolation
Definition: utils.hpp:53
constexpr size_t eToUI(const T &t)
Definition: utils.hpp:129
bool isInVector(const T &elem, const std::vector< T > &vec)
Definition: utils.hpp:62
T * ptr(T &obj)
returns a pointer (transforms reference into pointer)
Definition: utils.hpp:32
bool fuzzyEqual(const T &a, const T &b, double eps=1e-6)
Definition: utils.hpp:57
void eraseFromVector(const T &elem, std::deque< T > &vec)
Definition: utils.hpp:65
double dampingFromRatio(const double r, const double m, const double k)
Definition: utils.hpp:133
std::vector< std::string > splitStr(const std::string &s, char delim)
String spliting.
Definition: utils.hpp:80
constexpr T constpow(const T base, unsigned const exponent)
Definition: utils.hpp:125
std::array< double, 3 > hsvToRgb(double h, double s, double v)
transform hsv color space to rgb
Definition: utils.hpp:97
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
double sqrt(double x)
Computes the square root of a number.
Definition: std.hpp:32
double fabs(double x)
Computes the absolute value of a floating-point number.
Definition: std.hpp:93
double fmod(double x, double y)
Computes the remainder of the division of two numbers.
Definition: std.hpp:205
size_t operator()(const pair< T, U > &x) const
Definition: utils.hpp:142