OGLplus  (0.59.0) a C++ wrapper for rendering APIs

scales.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_UNITS_SCALES_HPP
10 #define EAGINE_UNITS_SCALES_HPP
11 
12 #include "../math/constants.hpp"
13 #include "../nothing.hpp"
14 
15 #ifdef __clang__
16 EAGINE_DIAG_PUSH()
17 EAGINE_DIAG_OFF(double-promotion)
18 #endif
19 
20 namespace eagine::units {
21 namespace scales {
22 //------------------------------------------------------------------------------
23 template <typename X>
24 struct scale_of {
25  using type = typename X::scale;
26 };
27 //------------------------------------------------------------------------------
28 template <typename X>
29 using scale_of_t = typename scale_of<X>::type;
30 
31 struct one {
32  using type = one;
33 
34  template <typename T>
35  static constexpr auto to_base(T v) {
36  return v;
37  }
38 
39  template <typename T>
40  static constexpr auto from_base(T v) {
41  return v;
42  }
43 };
44 //------------------------------------------------------------------------------
45 template <>
46 struct scale_of<nothing_t> : one {};
47 
48 template <int I>
49 struct constant {
50  using type = constant;
51 
52  template <typename T>
53  static constexpr auto to_base(T v) {
54  return v * I;
55  }
56 
57  template <typename T>
58  static constexpr auto from_base(T v) {
59  return v / float(I);
60  }
61 };
62 //------------------------------------------------------------------------------
63 template <int Num, int Den>
64 struct rational {
65  using type = rational;
66 
67  template <typename T>
68  static constexpr auto to_base(T v) {
69  return (v * Num) / float(Den);
70  }
71 
72  template <typename T>
73  static constexpr auto from_base(T v) {
74  return (v * Den) / float(Num);
75  }
76 };
77 //------------------------------------------------------------------------------
78 template <int X, int Y>
79 struct power {
80  using type = power;
81 
82  template <typename T>
83  static constexpr auto to_base(T v) {
84  using std::pow;
85  return v * pow(X, Y);
86  }
87 
88  template <typename T>
89  static constexpr auto from_base(T v) {
90  using std::pow;
91  return v / pow(X, Y);
92  }
93 };
94 //------------------------------------------------------------------------------
95 template <typename S>
96 struct inverted {
97  using type = inverted;
98 
99  template <typename T>
100  static constexpr auto to_base(T v) {
101  return S::from_base(v);
102  }
103 
104  template <typename T>
105  static constexpr auto from_base(T v) {
106  return S::to_base(v);
107  }
108 };
109 //------------------------------------------------------------------------------
110 template <typename S1, typename S2>
111 struct multiplied {
112  using type = multiplied;
113 
114  template <typename T>
115  static constexpr auto to_base(T v) {
116  return S2::to_base(S1::to_base(v));
117  }
118 
119  template <typename T>
120  static constexpr auto from_base(T v) {
121  return S2::from_base(S1::from_base(v));
122  }
123 };
124 //------------------------------------------------------------------------------
125 template <typename S1, typename S2>
126 struct divided {
127  using type = divided;
128 
129  template <typename T>
130  static constexpr auto to_base(T v) {
131  return S2::from_base(S1::to_base(v));
132  }
133 
134  template <typename T>
135  static constexpr auto from_base(T v) {
136  return S2::to_base(S1::from_base(v));
137  }
138 };
139 //------------------------------------------------------------------------------
140 template <typename S1, typename S2>
141 struct recombined : multiplied<S1, S2> {
142  using type = recombined;
143 };
144 //------------------------------------------------------------------------------
145 // nano
146 using nano = power<1000, -3>;
147 // micro
148 using micro = power<1000, -2>;
149 // milli
150 using milli = power<1000, -1>;
151 // centi
152 using centi = power<10, -2>;
153 // deci
154 using deci = power<10, -1>;
155 // deca
156 using deca = power<10, 1>;
157 // hecto
158 using hecto = power<10, 2>;
159 // kilo
160 using kilo = power<1000, 1>;
161 // mega
162 using mega = power<1000, 2>;
163 // giga
164 using giga = power<1000, 3>;
165 // tera
166 using tera = power<1000, 4>;
167 // kibi
168 using kibi = power<1024, 1>;
169 // mebi
170 using mebi = power<1024, 2>;
171 // gibi
172 using gibi = power<1024, 3>;
173 // tebi
174 using tebi = power<1024, 4>;
175 // pi
176 struct pi {
177  using type = pi;
178 
179  template <typename T>
180  static constexpr auto to_base(T v) {
181  return v * math::pi;
182  }
183 
184  template <typename T>
185  static constexpr auto from_base(T v) {
186  return v / math::pi;
187  }
188 };
189 //------------------------------------------------------------------------------
190 } // namespace scales
191 //------------------------------------------------------------------------------
192 template <>
193 struct name_of<scales::one> {
194  static constexpr const char mp_str[] = "";
195 };
196 template <>
197 struct symbol_of<scales::one> {
198  static constexpr const char mp_str[] = "";
199 };
200 //------------------------------------------------------------------------------
201 template <>
202 struct name_of<scales::nano> {
203  static constexpr const char mp_str[] = "nano";
204 };
205 template <>
206 struct symbol_of<scales::nano> {
207  static constexpr const char mp_str[] = "n";
208 };
209 //------------------------------------------------------------------------------
210 template <>
211 struct name_of<scales::micro> {
212  static constexpr const char mp_str[] = "micro";
213 };
214 template <>
215 struct symbol_of<scales::micro> {
216  static constexpr const char mp_str[3] = {char(0xCE), char(0xBC), '\0'};
217 };
218 //------------------------------------------------------------------------------
219 template <>
220 struct name_of<scales::milli> {
221  static constexpr const char mp_str[] = "milli";
222 };
223 template <>
224 struct symbol_of<scales::milli> {
225  static constexpr const char mp_str[] = "m";
226 };
227 //------------------------------------------------------------------------------
228 template <>
229 struct name_of<scales::centi> {
230  static constexpr const char mp_str[] = "centi";
231 };
232 template <>
233 struct symbol_of<scales::centi> {
234  static constexpr const char mp_str[] = "c";
235 };
236 //------------------------------------------------------------------------------
237 template <>
238 struct name_of<scales::deci> {
239  static constexpr const char mp_str[] = "deci";
240 };
241 template <>
242 struct symbol_of<scales::deci> {
243  static constexpr const char mp_str[] = "d";
244 };
245 //------------------------------------------------------------------------------
246 template <>
247 struct name_of<scales::deca> {
248  static constexpr const char mp_str[] = "deca";
249 };
250 template <>
251 struct symbol_of<scales::deca> {
252  static constexpr const char mp_str[] = "dc";
253 };
254 //------------------------------------------------------------------------------
255 template <>
256 struct name_of<scales::hecto> {
257  static constexpr const char mp_str[] = "hecto";
258 };
259 template <>
260 struct symbol_of<scales::hecto> {
261  static constexpr const char mp_str[] = "h";
262 };
263 //------------------------------------------------------------------------------
264 template <>
265 struct name_of<scales::kilo> {
266  static constexpr const char mp_str[] = "kilo";
267 };
268 template <>
269 struct symbol_of<scales::kilo> {
270  static constexpr const char mp_str[] = "k";
271 };
272 //------------------------------------------------------------------------------
273 template <>
274 struct name_of<scales::mega> {
275  static constexpr const char mp_str[] = "mega";
276 };
277 template <>
278 struct symbol_of<scales::mega> {
279  static constexpr const char mp_str[] = "M";
280 };
281 //------------------------------------------------------------------------------
282 template <>
283 struct name_of<scales::giga> {
284  static constexpr const char mp_str[] = "giga";
285 };
286 template <>
287 struct symbol_of<scales::giga> {
288  static constexpr const char mp_str[] = "G";
289 };
290 //------------------------------------------------------------------------------
291 template <>
292 struct name_of<scales::tera> {
293  static constexpr const char mp_str[] = "tera";
294 };
295 template <>
296 struct symbol_of<scales::tera> {
297  static constexpr const char mp_str[] = "T";
298 };
299 //------------------------------------------------------------------------------
300 template <>
301 struct name_of<scales::kibi> {
302  static constexpr const char mp_str[] = "kibi";
303 };
304 template <>
305 struct symbol_of<scales::kibi> {
306  static constexpr const char mp_str[] = "Ki";
307 };
308 //------------------------------------------------------------------------------
309 template <>
310 struct name_of<scales::mebi> {
311  static constexpr const char mp_str[] = "mebi";
312 };
313 template <>
314 struct symbol_of<scales::mebi> {
315  static constexpr const char mp_str[] = "Mi";
316 };
317 //------------------------------------------------------------------------------
318 template <>
319 struct name_of<scales::gibi> {
320  static constexpr const char mp_str[] = "gibi";
321 };
322 template <>
323 struct symbol_of<scales::gibi> {
324  static constexpr const char mp_str[] = "Gi";
325 };
326 //------------------------------------------------------------------------------
327 template <>
328 struct name_of<scales::tebi> {
329  static constexpr const char mp_str[] = "tebi";
330 };
331 template <>
332 struct symbol_of<scales::tebi> {
333  static constexpr const char mp_str[] = "Ti";
334 };
335 //------------------------------------------------------------------------------
336 template <>
337 struct name_of<scales::pi> {
338  static constexpr const char mp_str[3] = {char(0xCF), char(0x80), '\0'};
339 };
340 template <>
341 struct symbol_of<scales::pi> {
342  static constexpr const char mp_str[] = {char(0xCF), char(0x80), '\0'};
343 };
344 //------------------------------------------------------------------------------
345 } // namespace eagine::units
346 
347 #ifdef __clang__
348 EAGINE_DIAG_POP()
349 #endif
350 
351 #endif // EAGINE_UNITS_SCALES_HPP
static auto scale(std::shared_ptr< generator > gen, std::array< float, 3 > s) noexcept
Constructs instances of scaled_gen modifier.
Definition: scaled.hpp:41
static constexpr const auto pi
The pi constant.
Definition: constants.hpp:23

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).