MIDSX 0.1
A x-ray transport code system for dosimetry
Loading...
Searching...
No Matches
material_helpers.h
1#ifndef MCXRAYTRANSPORT_MATERIAL_HELPERS_H
2#define MCXRAYTRANSPORT_MATERIAL_HELPERS_H
3
4#include <string>
5#include <vector>
6#include <unordered_map>
7#include <Eigen/Dense>
8
9namespace MaterialHelpers {
10
11 template <typename T, typename U>
12 std::vector<T> mapKeysToVector(const std::unordered_map<T, U>& map) {
13 std::vector<T> keys;
14 for (const auto& pair : map) {
15 keys.push_back(pair.first);
16 }
17 return keys;
18 }
19
20 template <typename T, typename U>
21 std::vector<U> mapElementsToVector(const std::unordered_map<T, U>& map) {
22 std::vector<U> elements;
23 for (const auto& pair : map) {
24 elements.push_back(pair.second);
25 }
26 return elements;
27 }
28
29 template <typename T, typename U>
30 std::string mapKeysToString(const std::unordered_map<T, U>& map) {
31 std::stringstream ss;
32 for (auto itr = map.begin(); itr != map.end(); ++itr) {
33 ss << itr->first;
34 if (std::next(itr) != map.end())
35 ss << ", ";
36 }
37 return ss.str();
38 }
39
40 inline double calculateWeightedAverage(const std::vector<double>& weights, const std::vector<double>& values) {
41 double weighted_average = 0.0;
42 for (std::size_t i = 0; i < weights.size(); ++i) {
43 weighted_average += weights[i] * values[i];
44 }
45 return weighted_average;
46 }
47}
48
49namespace InteractionDataHelpers {
50
51 template<typename T>
52 inline std::vector<T> castStringVector(const std::vector<std::string> &string_vector) {
53 // Create a vector of the new type
54 std::vector<T> converted_vector(string_vector.size());
55
56 // Convert each string to the new type
57 std::transform(string_vector.begin(), string_vector.end(), converted_vector.begin(),
58 [](const std::string &str) {
59 std::istringstream iss(str);
60 T value;
61 iss >> value;
62 return value;
63 }
64 );
65
66 return converted_vector;
67 }
68
69 inline Eigen::MatrixXd mergeMatrices(std::vector<Eigen::MatrixXd> &matrices) {
70 std::vector<double> merged;
71
72 for (const auto &matrix: matrices) {
73 std::vector<double> v(matrix.data(), matrix.data() + matrix.size());
74 merged.insert(merged.end(), v.begin(), v.end());
75 }
76
77 std::sort(merged.begin(), merged.end());
78
79 auto last = std::unique(merged.begin(), merged.end());
80 merged.erase(last, merged.end());
81
82 Eigen::MatrixXd result = Eigen::Map<Eigen::MatrixXd>(merged.data(), merged.size(), 1);
83
84 return result;
85 }
86
87 template<typename T>
88 Eigen::MatrixXd convertNVectorsToEigenMatrix(const std::vector<std::vector<T>> &source) {
89 Eigen::MatrixXd result(source[0].size(), source.size());
90
91 for (std::size_t i = 0; i < source.size(); ++i) {
92 result.col(i) = Eigen::Map<const Eigen::VectorXd>(source[i].data(), source[i].size());
93 }
94
95 return result;
96 }
97
98 template<typename T>
99 std::vector<std::vector<T>> distributeNTimes(const std::vector<T> &source, int n) {
100 std::vector<std::vector<T>> result(n); // Initialize vector of vectors
101
102 for (std::size_t i = 0; i < source.size(); ++i) {
103 result[i % n].push_back(source[i]);
104 }
105
106 return result;
107 }
108
109 // convert material id to material name
110 inline std::string convertMaterialIdToName(int material_id, DataAccessObject &dao) {
111 // Add each element of the vector to a string if there are multiple elements
112 // (might be split into multiple lines)
113 std::string query = "SELECT Name FROM Materials WHERE MaterialID = " + std::to_string(material_id) + ";";
114 return dao.executeQuery(query)[0];
115 }
116
117 // convert material name to material id
118 inline uint8_t convertMaterialNameToId(const std::string& material_name, DataAccessObject& dao) {
119 std::string query = "SELECT MaterialID FROM Materials WHERE Name = '" + material_name + "';";
120 return castStringVector<int>(dao.executeQuery(query))[0];
121 }
122}
123
124#endif //MCXRAYTRANSPORT_MATERIAL_HELPERS_H
Class which provides an interface to a SQLite database.
std::vector< std::string > executeQuery(const std::string &query)
Executes a query and returns the results as a vector of strings.