1#ifndef MCXRAYTRANSPORT_MATERIAL_HELPERS_H
2#define MCXRAYTRANSPORT_MATERIAL_HELPERS_H
6#include <unordered_map>
9namespace MaterialHelpers {
11 template <
typename T,
typename U>
12 std::vector<T> mapKeysToVector(
const std::unordered_map<T, U>& map) {
14 for (
const auto& pair : map) {
15 keys.push_back(pair.first);
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);
29 template <
typename T,
typename U>
30 std::string mapKeysToString(
const std::unordered_map<T, U>& map) {
32 for (
auto itr = map.begin(); itr != map.end(); ++itr) {
34 if (std::next(itr) != map.end())
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];
45 return weighted_average;
49namespace InteractionDataHelpers {
52 inline std::vector<T> castStringVector(
const std::vector<std::string> &string_vector) {
54 std::vector<T> converted_vector(string_vector.size());
57 std::transform(string_vector.begin(), string_vector.end(), converted_vector.begin(),
58 [](
const std::string &str) {
59 std::istringstream iss(str);
66 return converted_vector;
69 inline Eigen::MatrixXd mergeMatrices(std::vector<Eigen::MatrixXd> &matrices) {
70 std::vector<double> merged;
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());
77 std::sort(merged.begin(), merged.end());
79 auto last = std::unique(merged.begin(), merged.end());
80 merged.erase(last, merged.end());
82 Eigen::MatrixXd result = Eigen::Map<Eigen::MatrixXd>(merged.data(), merged.size(), 1);
88 Eigen::MatrixXd convertNVectorsToEigenMatrix(
const std::vector<std::vector<T>> &source) {
89 Eigen::MatrixXd result(source[0].size(), source.size());
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());
99 std::vector<std::vector<T>> distributeNTimes(
const std::vector<T> &source,
int n) {
100 std::vector<std::vector<T>> result(n);
102 for (std::size_t i = 0; i < source.size(); ++i) {
103 result[i % n].push_back(source[i]);
110 inline std::string convertMaterialIdToName(
int material_id,
DataAccessObject &dao) {
113 std::string query =
"SELECT Name FROM Materials WHERE MaterialID = " + std::to_string(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];
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.