42 return std::make_pair(matrix.at(0).at(0) * v.first + matrix.at(0).at(1) * v.second,
43 matrix.at(1).at(0) * v.first + matrix.at(1).at(1) * v.second);
55 inline double dot(std::pair<double, double> &u, std::pair<double, double> &v) {
57 return u.first * v.first + u.second * v.second;
69 inline double cross(std::pair<double, double> &u, std::pair<double, double> &v) {
72 return u.first * v.second - u.second * v.first;
84 const std::pair<double, double>& position2) {
86 double xDiff = position1.first - position2.first;
87 double yDiff = position1.second - position2.second;
90 return std::sqrt(xDiff * xDiff + yDiff * yDiff);
104 inline double power(
double base,
int exponent) {
108 else if (exponent < 0) {
109 return 1.0 /
power(base, -exponent);
111 else if (exponent % 2 == 0) {
112 double temp =
power(base, exponent / 2);
116 return base *
power(base, exponent - 1);
122 requires std::is_arithmetic<T>::value
137 int sign = (n < 0) ? -1 : 1;
138 double approx =
power((
double)10.0, decimals);
141 long long intPart = (
long long)n;
143 bool hasFracPart = (n - intPart > 0);
145 double fracPart = round((n - intPart) * (approx)) / (approx);
149 char separator = use_facet< numpunct<char> >(loc).thousands_sep();
151 string intStr = to_string(intPart);
153 for (
int i = intStr.length() - 3; i > 0; i -= 3) {
154 intStr.insert(i, 1, separator);
159 stream << fixed << setprecision(decimals) << fracPart;
161 string fracStr = (hasFracPart) ? stream.str().substr(1) :
"";
163 string result = intStr + fracStr;
166 result =
"-" + result;
187 int minutes =
static_cast<int>(seconds) / 60;
188 int hours = minutes / 60;
189 int days = hours / 24;
190 int remainingSeconds =
static_cast<int>(seconds) % 60;
191 int remainingMinutes = minutes % 60;
192 int remainingHours = hours % 24;
194 std::stringstream ss;
195 ss << days <<
":" << setw(2) << setfill(
'0') << remainingHours <<
":" << setw(2) << setfill(
'0') << remainingMinutes <<
":" << setw(2) << setfill(
'0') << remainingSeconds;
196 std::string result = ss.str();
201 size_t pos = path.find_last_of(
"/\\");
202 string filename = (pos == string::npos) ? path : path.substr(pos + 1);
203 size_t dotPos = filename.find_last_of(
".");
204 if (dotPos != string::npos) {
205 filename = filename.substr(0, dotPos);
212 bool skipFirstChar = isupper(str[0]);
216 for (
size_t i = skipFirstChar ? 1 : 0; i < str.length(); i++) {
218 if (c ==
'_' || isalpha(c) || c ==
' ') {
219 if (isupper(c) || c ==
' ' || c ==
'_') {
242 std::stringstream ss(input);
244 while (std::getline(ss, item, delimiter)) {
245 result.push_back(std::stod(item));
263 std::stringstream ss(str);
267 intVector.push_back(temp);
268 if (ss.peek() ==
',') {
271 else if (ss.peek() !=
' ') {
295 std::istringstream tokenStream(s);
296 while (std::getline(tokenStream, token, delimiter))
298 tokens.push_back(token);
306 for(
const auto& str : stringVector) {
308 intVector.push_back(std::stoi(str));
309 }
catch(
const std::exception& e) {
310 throw std::runtime_error(
"Error: unable to convert string " + str +
" to integer.");
328 inline std::string
trim(
const std::string& str)
331 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
int ch) {
332 return !std::isspace(ch);
334 s.erase(std::find_if(s.rbegin(), s.rend(), [](
int ch) {
335 return !std::isspace(ch);
340 inline bool writeToFile(std::stringstream &s, std::string filename) {
341 std::ofstream outputFile(filename);
342 if (outputFile.is_open()) {
343 outputFile << s.rdbuf();
351 template <
typename... Args>
354 template <
typename Tuple,
size_t... Indices>
356 std::index_sequence<Indices...>,
int limit, std::string delim);
358 template <
typename T>
359 inline void writeToStream(
const T& value, std::stringstream& ss, std::string delim);
363 template <
typename... Args>
365 std::stringstream ss;
367 limit > 0 ? limit: std::tuple_size<std::tuple<Args...>>::value - std::abs(limit) -1, delim);
371 template <
typename Tuple,
size_t... Indices>
373 std::stringstream& ss,
374 std::index_sequence<Indices...>,
375 int limit, std::string delim) {
384 template <
typename T>
386 std::stringstream& ss,
388 if constexpr (std::is_same_v<T, bool>) {
389 ss << (value ?
"1" :
"0") << delim;
391 ss << value << delim;
397 size_t lastSpaceIndex = originalString.find_last_of(
' ');
399 if (lastSpaceIndex != std::string::npos) {
400 std::string updatedString = originalString.substr(0, lastSpaceIndex);
401 return updatedString;
404 return originalString;
409 QVector<double> doubleVector;
410 doubleVector.reserve(stringVector.size());
412 for (
const QString& str : stringVector) {
414 double value = str.toDouble(&ok);
416 doubleVector.append(value);
425 inline QVector<double>
subtractQVector(
const QVector<double> l1,
const QVector<double> l2) {
426 QVector<double> result;
427 if (l1.size() != l2.size()) {
return result; }
429 for (
int i = 0; i < l1.size(); i++ ) {
430 result.push_back(l1[i] - l2[i]);
435 inline QVector<double>
factorQVector(
const QVector<double> l1,
const double factor) {
436 QVector<double> result;
437 if (l1.size() < 1) {
return result; }
439 for (
int i = 0; i < l1.size(); i++ ) {
440 result.push_back(l1[i] * factor);
449 while (std::getline(ss, line)) {
450 std::size_t delimiterPos = line.find(delimiter);
451 if (delimiterPos != std::string::npos) {
452 std::string first = line.substr(0, delimiterPos);
453 std::string second = line.substr(delimiterPos + delimiter.size());
454 result.emplace_back(first, second);
456 result.emplace_back(line,
"");
463 inline std::string
replaceAll(std::string str,
const std::string& from,
const std::string& to) {
464 size_t start_pos = 0;
465 while((start_pos = str.find(from, start_pos)) != std::string::npos) {
466 str.replace(start_pos, from.length(), to);
467 start_pos += to.length();
A vector.
Definition vector.h:24
QVector< double > subtractQVector(const QVector< double > l1, const QVector< double > l2)
Definition utils.h:425
std::string formatDuration(T seconds)
Format duration.
Definition utils.h:186
QVector< double > factorQVector(const QVector< double > l1, const double factor)
Definition utils.h:435
std::pair< double, double > dot(Vector< Vector< double > > matrix, std::pair< double, double > &v)
Compute the dot product of a 2x2 matrix and a 2D vector.
Definition utils.h:38
Vector< int > splitStringToIntVector(const std::string &str)
The function stringToIntVector takes a string as input and converts it into a Vector<int> by parsing ...
Definition utils.h:261
std::string getPrefix(std::string str)
Definition utils.h:210
void writeToStream(const T &value, std::stringstream &ss, std::string delim)
Definition utils.h:385
double power(double base, int exponent)
Powers.
Definition utils.h:104
::value std::string thousandSeparator(T n, int decimals=3)
Convert a plain numeric value to thousand separated value.
Definition utils.h:135
std::string replaceAll(std::string str, const std::string &from, const std::string &to)
Definition utils.h:463
Vector< int > convertStringVectorToIntVector(const std::vector< std::string > &stringVector)
Definition utils.h:303
std::string trim(const std::string &str)
This function trim takes in a string str and returns a copy of it with leading and trailing white spa...
Definition utils.h:328
Vector< double > splitStringToDoubleVector(const std::string &input, char delimiter=',')
Splits string to double vector.
Definition utils.h:240
void addTupleValuesToStreamImpl(const Tuple &t, std::stringstream &ss, std::index_sequence< Indices... >, int limit, std::string delim)
Definition utils.h:372
bool writeToFile(std::stringstream &s, std::string filename)
Definition utils.h:340
Vector< std::pair< std::string, std::string > > splitStringStream(std::stringstream &ss, const std::string &delimiter=":")
Definition utils.h:445
std::stringstream convertTupleToStringStream(const std::tuple< Args... > &t, int limit, std::string delim="\t")
Definition utils.h:364
double cross(std::pair< double, double > &u, std::pair< double, double > &v)
Compute the cross product of two 2D vectors.
Definition utils.h:69
double getDistanceByTwoCoordinates(const std::pair< double, double > &position1, const std::pair< double, double > &position2)
Computes the Euclidean distance between two coordinates.
Definition utils.h:83
std::string getFilenameWithoutExtension(std::string path)
Definition utils.h:200
QVector< double > convertQStringVectorToDouble(const QVector< QString > &stringVector)
Definition utils.h:408
Vector< std::string > split(const std::string &s, char delimiter)
This function is splitting a given string "s" into substrings using a delimiter "delimiter" and retur...
Definition utils.h:291
std::string removeLastWord(const std::string &originalString)
Definition utils.h:395