Skip to main content

Unit.ixx File

Included Headers

#include <string> #include <helios.math.types>

Namespaces Index

namespacehelios
namespacecore

Core utilities shared across the helios engine. More...

namespaceunits

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file units.ixx
3 * @brief Canonical length units and conversion helpers for helios to keep
4 * spatial and temporal data consistent across modules.
5 */
6module;
7
8#include <string>
9
10export module helios.core.units.Unit;
11import helios.math.types;
12
13export namespace helios::core::units {
14
15 /**
16 * @brief Enumeration of length and time units supported by helios.
17 */
18 enum class Unit {
19 /**
20 * @brief Base length unit (meters) in helios.
21 * 1.0f represents 1 meter.
22 */
24
25 /**
26 * @brief Centimeter helper unit for authoring convenience.
27 * Converted to meters (0.01f) when used.
28 */
30
31 /**
32 * @brief Base time unit (seconds) in helios.
33 * 1.0f represents 1 second.
34 */
36
37 /**
38 * @brief Millisecond helper unit derived from seconds.
39 * Converted to seconds (0.001f) when used.
40 */
42 };
43
44
45 /**
46 * @brief Default length unit used throughout helios (1 meter per helios unit).
47 */
49
50 /**
51 * @brief Default time unit used for all timing APIs (seconds).
52 */
54
55 /**
56 * @brief Ratio of centimeters to meters.
57 */
58 constexpr float CENTIMETERS = 0.01f;
59
60 /**
61 * @brief Ratio of meters to meters (identity).
62 */
63 constexpr float METERS = 1.0f;
64
65 /**
66 * @brief Ratio of milliseconds to seconds.
67 */
68 constexpr float MILLISECONDS = 0.001f;
69
70 /**
71 * @brief Ratio of seconds to seconds (identity).
72 */
73 constexpr float SECONDS = 1.0f;
74
75 /**
76 * @brief Converts centimeters to meters.
77 *
78 * @param cm Value expressed in centimeters.
79 *
80 * @return Converted value expressed in meters.
81 */
82 constexpr float fromCm(const float cm) noexcept {
83 return cm * CENTIMETERS;
84 };
85
86 /**
87 * @brief Pass-through helper for meter values.
88 *
89 * @param m Value expressed in meters.
90 *
91 * @return Same value expressed in meters.
92 */
93 constexpr float fromM(const float m) noexcept {
94 return m * METERS;
95 };
96
97 /**
98 * @brief Pass-through helper for meter vectors.
99 *
100 * @param v Vector with components expressed in meters.
101 *
102 * @return Vector with components expressed in meters.
103 */
105 return helios::math::vec3f{v[0] * METERS, v[1] * METERS, v[2] * METERS};
106 };
107
108 /**
109 * @brief Pass-through helper for meter AABBs.
110 *
111 * @param v AABB with bounds expressed in meters.
112 *
113 * @return AABB with bounds expressed in meters.
114 */
117 v.min()[0] * METERS, v.min()[1] * METERS, v.min()[2] * METERS,
118 v.max()[0] * METERS, v.max()[1] * METERS, v.max()[2] * METERS
119 };
120 };
121
122 /**
123 * @brief Converts seconds to seconds (identity helper).
124 *
125 * @param s Value expressed in seconds.
126 *
127 * @return Same value expressed in seconds.
128 */
129 constexpr float fromS(const float s) noexcept {
130 return s * SECONDS;
131 }
132
133 /**
134 * @brief Converts milliseconds to seconds.
135 *
136 * @param ms Value expressed in milliseconds.
137 *
138 * @return Converted value expressed in seconds.
139 */
140 constexpr float fromMs(const float ms) noexcept {
141 return ms * MILLISECONDS;
142 }
143
144 /**
145 * @brief Converts a value from a given unit to helios units.
146 *
147 * This method takes a value in the specified unit and converts it to helios units,
148 * e.g. spatial units to METER, temporal values to SECONDS.
149 *
150 * @param v Value to convert.
151 * @param unit Unit of the value to convert.
152 *
153 * @return Value expressed in temporal or spatial helios units.
154 */
155 [[nodiscard]] constexpr float from(const float v, const Unit unit) noexcept {
156 switch (unit) {
157 case Unit::Meter:
158 return fromM(v);
160 return fromCm(v);
161 case Unit::Seconds:
162 return fromS(v);
164 return fromMs(v);
165 default:
166 std::unreachable();
167 }
168 }
169
170 /**
171 * @brief Converts an AABB from a given unit to helios units.
172 *
173 * @param aabb AABB to convert.
174 * @param unit Unit of the AABB coordinates.
175 *
176 * @return AABB expressed in helios spatial units (meters).
177 */
178 [[nodiscard]] constexpr helios::math::aabbf from(helios::math::aabbf aabb, const Unit unit) noexcept {
179 switch (unit) {
180 case Unit::Meter:
181 return fromM(aabb);
182 default:
183 std::unreachable();
184 }
185 }
186
187}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.