File indexing completed on 2025-01-19 09:23:24
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <algorithm>
0012 #include <cassert>
0013 #include <cstdint>
0014 #include <limits>
0015 #include <vector>
0016
0017 namespace Acts {
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 class ElementFraction {
0035 public:
0036
0037
0038
0039
0040 constexpr ElementFraction(unsigned int e, float f)
0041 : m_element(static_cast<uint8_t>(e)),
0042 m_fraction(static_cast<uint8_t>(f * UINT8_MAX)) {
0043 assert((0u < e) && ("The atomic number must be positive"));
0044 assert((0.0f <= f) && (f <= 1.0f) && "Relative fraction must be in [0,1]");
0045 }
0046
0047
0048
0049
0050 constexpr explicit ElementFraction(unsigned int e, unsigned int w)
0051 : m_element(static_cast<uint8_t>(e)),
0052 m_fraction(static_cast<uint8_t>(w)) {
0053 assert((0u < e) && ("The atomic number must be positive"));
0054 assert((w < 256u) && "Integer weight must be in [0,256)");
0055 }
0056
0057
0058 ElementFraction() = delete;
0059 ElementFraction(ElementFraction&&) = default;
0060 ElementFraction(const ElementFraction&) = default;
0061 ~ElementFraction() = default;
0062 ElementFraction& operator=(ElementFraction&&) = default;
0063 ElementFraction& operator=(const ElementFraction&) = default;
0064
0065
0066 constexpr uint8_t element() const { return m_element; }
0067
0068 constexpr float fraction() const {
0069 return static_cast<float>(m_fraction) / UINT8_MAX;
0070 }
0071
0072 private:
0073
0074 uint8_t m_element;
0075
0076 uint8_t m_fraction;
0077
0078 friend constexpr bool operator==(ElementFraction lhs, ElementFraction rhs) {
0079 return (lhs.m_fraction == rhs.m_fraction) &&
0080 (lhs.m_element == rhs.m_element);
0081 }
0082
0083 friend constexpr bool operator<(ElementFraction lhs, ElementFraction rhs) {
0084 return lhs.m_fraction < rhs.m_fraction;
0085 }
0086 friend class MaterialComposition;
0087 };
0088
0089
0090
0091
0092 class MaterialComposition {
0093 public:
0094
0095 MaterialComposition() = default;
0096
0097
0098
0099 MaterialComposition(std::vector<ElementFraction> elements)
0100 : m_elements(std::move(elements)) {
0101 std::sort(m_elements.begin(), m_elements.end());
0102
0103 unsigned total = 0u;
0104 for (auto element : m_elements) {
0105 total += element.m_fraction;
0106 }
0107
0108 float scale = float{std::numeric_limits<std::uint8_t>::max()} / total;
0109 for (auto& element : m_elements) {
0110 element.m_fraction =
0111 static_cast<std::uint8_t>(element.m_fraction * scale);
0112 }
0113 }
0114
0115 MaterialComposition(MaterialComposition&&) = default;
0116 MaterialComposition(const MaterialComposition&) = default;
0117 ~MaterialComposition() = default;
0118 MaterialComposition& operator=(MaterialComposition&&) = default;
0119 MaterialComposition& operator=(const MaterialComposition&) = default;
0120
0121
0122 auto begin() const { return m_elements.begin(); }
0123 auto end() const { return m_elements.end(); }
0124
0125
0126 operator bool() const { return !m_elements.empty(); }
0127
0128 std::size_t size() const { return m_elements.size(); }
0129
0130 private:
0131 std::vector<ElementFraction> m_elements;
0132
0133 friend inline bool operator==(const MaterialComposition& lhs,
0134 const MaterialComposition& rhs) {
0135 return (lhs.m_elements == rhs.m_elements);
0136 }
0137 };
0138
0139 }