Dynamic Neural Field Composer 0.0.0
A C++20 library and interactive application for building and simulating Dynamic Neural Field (DNF) architectures.
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include <random>
6#include <sstream>
7#include <fstream>
8#include <chrono>
9#include <filesystem>
10
12{
13 // Returns the runtime install prefix (parent of bin/).
14 // Falls back to the compile-time PROJECT_DIR in dev builds.
15 std::string getResourceRoot();
16 int countNumOfLinesInFile(const std::string& filename);
17
18 bool saveVectorToFile(const std::vector<double>& vector, const std::string& filename);
19
20 std::string replaceForwardSlashesWithBackslashes(const std::string& str);
21
22 template <typename T>
23 void resizeMatrix(std::vector<std::vector<T>>& matrix, int newRowSize, int newColSize)
24 {
25 matrix.resize(newRowSize);
26 for (int i = 0; i < newRowSize; i++)
27 matrix[i].resize(newColSize);
28 }
29
30 template <typename T>
31 T generateRandomNumber(const T& min, const T& max)
32 {
33 // Seed the random number generator with a random device
34 std::random_device rd;
35 std::mt19937 gen(rd());
36 // Create a uniform distribution from 1 to 2 (inclusive)
37 std::uniform_real_distribution<> dis(min, max);
38 // Generate a random integer between 1 and 2
39 T randomNum = dis(gen);
40 return randomNum;
41 }
42
43 template <typename T>
44 void fillMatrixWithRandomValues(std::vector<std::vector<T>>& matrix, double minRange = -1.0, double maxRange = 1.0) {
45 std::random_device rd;
46 std::mt19937 gen(rd());
47 std::uniform_real_distribution<> dis(minRange, maxRange);
48 for (auto& row : matrix)
49 for (auto& element : row)
50 element = dis(gen);
51 }
52
53 inline bool safe_localtime(const std::time_t* time, std::tm* result)
54 {
55#ifdef _WIN32
56 return localtime_s(result, time) == 0;
57#else
58 return localtime_r(time, result) != nullptr;
59#endif
60 }
61
62 float getProcessMemoryMb();
63}
Definition utils.h:12
std::string getResourceRoot()
Definition utils.cpp:17
void resizeMatrix(std::vector< std::vector< T > > &matrix, int newRowSize, int newColSize)
Definition utils.h:23
int countNumOfLinesInFile(const std::string &filename)
Definition utils.cpp:54
std::string replaceForwardSlashesWithBackslashes(const std::string &str)
Definition utils.cpp:83
bool safe_localtime(const std::time_t *time, std::tm *result)
Definition utils.h:53
T generateRandomNumber(const T &min, const T &max)
Definition utils.h:31
float getProcessMemoryMb()
Definition utils.cpp:92
void fillMatrixWithRandomValues(std::vector< std::vector< T > > &matrix, double minRange=-1.0, double maxRange=1.0)
Definition utils.h:44
bool saveVectorToFile(const std::vector< double > &vector, const std::string &filename)
Definition utils.cpp:70