OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
oscquery_units.hpp
1#pragma once
2#include <ossia/detail/fmt.hpp>
3#include <ossia/detail/hash_map.hpp>
4#include <ossia/detail/small_vector.hpp>
5#include <ossia/network/dataspace/dataspace_variant_visitors.hpp>
7#include <ossia/network/oscquery/detail/json_writer_detail.hpp>
8
9namespace std
10{
11template <>
12struct hash<ossia::small_vector<std::string, 4>>
13{
14 std::size_t operator()(const ossia::small_vector<std::string, 4>& v) const noexcept
15 {
16 std::size_t seed{};
17 for(auto& str : v)
18 ossia::hash_combine(seed, std::hash<std::string>{}(str));
19 return seed;
20 }
21};
22}
23namespace ossia::oscquery::detail
24{
25
26struct unit_writer
27{
28 const json_writer_impl& writer;
29 void operator()() { }
30
31 void operator()(const ossia::degree_u& u) { write_unit(u); }
32 void operator()(const ossia::radian_u& u) { write_unit(u); }
33
34 void operator()(const ossia::argb_u&)
35 {
36 writer.writeKey("EXTENDED_TYPE");
37 writer.writer.StartArray();
38 writer.writer.String("color.rgb.a");
39 writer.writer.String("color.rgb.r");
40 writer.writer.String("color.rgb.g");
41 writer.writer.String("color.rgb.b");
42 writer.writer.EndArray();
43 }
44
45 void operator()(const ossia::rgba_u&)
46 {
47 writer.writeKey("EXTENDED_TYPE");
48 writer.writer.StartArray();
49 writer.writer.String("color.rgb.r");
50 writer.writer.String("color.rgb.g");
51 writer.writer.String("color.rgb.b");
52 writer.writer.String("color.rgb.a");
53 writer.writer.EndArray();
54 }
55
56 void operator()(const ossia::rgba8_u&)
57 {
58 // write nothing since it's already given by the OSC "r" type
59 }
60
61 void operator()(const ossia::rgb_u&)
62 {
63 writer.writeKey("EXTENDED_TYPE");
64 writer.writer.StartArray();
65 writer.writer.String("color.rgb.r");
66 writer.writer.String("color.rgb.g");
67 writer.writer.String("color.rgb.b");
68 writer.writer.EndArray();
69 }
70
71 void operator()(const ossia::bgr_u&)
72 {
73 writer.writeKey("EXTENDED_TYPE");
74 writer.writer.StartArray();
75 writer.writer.String("color.rgb.b");
76 writer.writer.String("color.rgb.g");
77 writer.writer.String("color.rgb.r");
78 writer.writer.EndArray();
79 }
80
81 void operator()(const ossia::argb8_u&)
82 {
83 writer.writeKey("EXTENDED_TYPE");
84 writer.writer.StartArray();
85 writer.writer.String("color.rgb8.a");
86 writer.writer.String("color.rgb8.r");
87 writer.writer.String("color.rgb8.g");
88 writer.writer.String("color.rgb8.b");
89 writer.writer.EndArray();
90 }
91
92 void operator()(const ossia::hsv_u&)
93 {
94 writer.writeKey("EXTENDED_TYPE");
95 writer.writer.StartArray();
96 writer.writer.String("color.hsv.h");
97 writer.writer.String("color.hsv.s");
98 writer.writer.String("color.hsv.v");
99 writer.writer.EndArray();
100 }
101
102 void operator()(const ossia::cmy8_u&)
103 {
104 writer.writeKey("EXTENDED_TYPE");
105 writer.writer.StartArray();
106 writer.writer.String("color.cmyk8.c");
107 writer.writer.String("color.cmyk8.m");
108 writer.writer.String("color.cmyk8.y");
109 writer.writer.EndArray();
110 }
111
112 void operator()(const ossia::xyz_u&)
113 {
114 writer.writeKey("EXTENDED_TYPE");
115 writer.writer.StartArray();
116 writer.writer.String("color.ciexyz.x");
117 writer.writer.String("color.ciexyz.y");
118 writer.writer.String("color.ciexyz.z");
119 writer.writer.EndArray();
120 }
121
122 void operator()(const ossia::hsl_u&)
123 {
124 writer.writeKey("EXTENDED_TYPE");
125 writer.writer.StartArray();
126 writer.writer.String("color.hsl.h");
127 writer.writer.String("color.hsl.s");
128 writer.writer.String("color.hsl.l");
129 writer.writer.EndArray();
130 }
131
132 void operator()(const ossia::cmyk8_u&)
133 {
134 writer.writeKey("EXTENDED_TYPE");
135 writer.writer.StartArray();
136 writer.writer.String("color.cmyk8.c");
137 writer.writer.String("color.cmyk8.m");
138 writer.writer.String("color.cmyk8.y");
139 writer.writer.String("color.cmyk8.k");
140 writer.writer.EndArray();
141 }
142
143 void operator()(const ossia::yxy_u&)
144 {
145 writer.writeKey("EXTENDED_TYPE");
146 writer.writer.StartArray();
147 writer.writer.String("color.cieYxy.Y");
148 writer.writer.String("color.cieYxy.x");
149 writer.writer.String("color.cieYxy.y");
150 writer.writer.EndArray();
151 }
152
153 void operator()(const ossia::hunter_lab_u&)
154 {
155 writer.writeKey("EXTENDED_TYPE");
156 writer.writer.StartArray();
157 writer.writer.String("color.hunterLab.l");
158 writer.writer.String("color.hunterLab.a");
159 writer.writer.String("color.hunterLab.b");
160 writer.writer.EndArray();
161 }
162 void operator()(const ossia::cie_lab_u&)
163 {
164 writer.writeKey("EXTENDED_TYPE");
165 writer.writer.StartArray();
166 writer.writer.String("color.cieLab.l");
167 writer.writer.String("color.cieLab.a");
168 writer.writer.String("color.cieLab.b");
169 writer.writer.EndArray();
170 }
171 void operator()(const ossia::cie_luv_u&)
172 {
173 writer.writeKey("EXTENDED_TYPE");
174 writer.writer.StartArray();
175 writer.writer.String("color.cieLuv.l");
176 writer.writer.String("color.cieLuv.u");
177 writer.writer.String("color.cieLuv.v");
178 writer.writer.EndArray();
179 }
180
181 void operator()(const ossia::meter_u& u) { write_unit(u); }
182 void operator()(const ossia::kilometer_u& u) { write_unit(u); }
183 void operator()(const ossia::decimeter_u& u) { write_unit(u); }
184 void operator()(const centimeter_u& u) { write_unit(u); }
185 void operator()(const millimeter_u& u) { write_unit(u); }
186 void operator()(const micrometer_u& u) { write_unit(u); }
187 void operator()(const nanometer_u& u) { write_unit(u); }
188 void operator()(const picometer_u& u) { write_unit(u); }
189 void operator()(const inch_u& u) { write_unit(u); }
190 void operator()(const foot_u& u) { write_unit(u); }
191 void operator()(const mile_u& u) { write_unit(u); }
192
193 void operator()(const linear_u& u) { write_unit(u); }
194 void operator()(const midigain_u& u) { write_unit(u); }
195 void operator()(const decibel_u& u) { write_unit(u); }
196 void operator()(const decibel_raw_u& u) { write_unit(u); }
197
198 void operator()(const quaternion_u& u) { write_extended(u); }
199 void operator()(const euler_u& u) { write_extended(u); }
200 void operator()(const axis_u& u) { write_extended(u); }
201
202 void operator()(const spherical_u& u) { write_extended(u); }
203 void operator()(const polar_u& u) { write_extended(u); }
204 void operator()(const aed_u& u) { write_extended(u); }
205 void operator()(const ad_u& u) { write_extended(u); }
206 void operator()(const opengl_u& u) { write_extended(u); }
207 void operator()(const cylindrical_u& u) { write_extended(u); }
208 void operator()(const azd_u& u) { write_extended(u); }
209
210 void operator()(const cartesian_3d_u&)
211 {
212 writer.writeKey("EXTENDED_TYPE");
213 writer.writer.StartArray();
214 writer.writer.String("position.cartesian.x");
215 writer.writer.String("position.cartesian.y");
216 writer.writer.String("position.cartesian.z");
217 writer.writer.EndArray();
218 }
219
220 void operator()(const cartesian_2d_u&)
221 {
222 writer.writeKey("EXTENDED_TYPE");
223 writer.writer.StartArray();
224 writer.writer.String("position.cartesian.x");
225 writer.writer.String("position.cartesian.y");
226 writer.writer.EndArray();
227 }
228
229 void operator()(const ossia::meter_per_second_u& u) { write_unit(u); }
230 void operator()(const ossia::miles_per_hour_u& u) { write_unit(u); }
231 void operator()(const ossia::kilometer_per_hour_u& u) { write_unit(u); }
232 void operator()(const ossia::knot_u& u) { write_unit(u); }
233 void operator()(const ossia::foot_per_hour_u& u) { write_unit(u); }
234 void operator()(const ossia::foot_per_second_u& u) { write_unit(u); }
235
236 void operator()(const ossia::second_u& u) { write_unit(u); }
237 void operator()(const ossia::bark_u& u) { write_unit(u); }
238 void operator()(const ossia::bpm_u& u) { write_unit(u); }
239 void operator()(const ossia::cent_u& u) { write_unit(u); }
240 void operator()(const ossia::frequency_u& u) { write_unit(u); }
241 void operator()(const ossia::mel_u& u) { write_unit(u); }
242 void operator()(const ossia::midi_pitch_u& u) { write_unit(u); }
243 void operator()(const ossia::millisecond_u& u) { write_unit(u); }
244 void operator()(const ossia::playback_speed_u& u) { write_unit(u); }
245
246 template <typename T>
247 void write_unit(const T&)
248 {
249 writer.writeKey("UNIT");
250 writer.writer.StartArray();
251 writer.writer.String(fmt::format(
252 "{}.{}", dataspace_traits<typename T::dataspace_type>::text()[0],
253 unit_traits<T>::text()[0]));
254 writer.writer.EndArray();
255 }
256
257 template <typename T>
258 void write_extended(const T&)
259 {
260 writer.writeKey("EXTENDED_TYPE");
261
262 writer.writer.StartArray();
263 if constexpr(is_array_unit<T>::value)
264 {
265 for(char val : T::array_parameters())
266 {
267 writer.writer.String(fmt::format(
268 "{}.{}.{}", dataspace_traits<typename T::dataspace_type>::text()[0],
269 unit_traits<T>::text()[0], val));
270 }
271 }
272 else
273 {
274 writer.writer.String(fmt::format(
275 "{}.{}", dataspace_traits<typename T::dataspace_type>::text()[0],
276 unit_traits<T>::text()[0]));
277 }
278 writer.writer.EndArray();
279 }
280};
281
282struct unit_parser
283{
284 ossia::hash_map<ossia::small_vector<std::string, 4>, ossia::unit_t> map{
285 unit_matcher(degree_u{}),
286 unit_matcher(radian_u{}),
287
288 {{"color.rgb.a", "color.rgb.r", "color.rgb.g", "color.rgb.b"}, ossia::argb_u{}},
289 {{"color.rgb.r", "color.rgb.g", "color.rgb.b", "color.rgb.a"}, ossia::rgba_u{}},
290 // { { "color.rgb.r", "color.rgb.g", "color.rgb.b" },
291 // ossia::rgba8_u{} },
292 // // not needed
293 {{"color.rgb.r", "color.rgb.g", "color.rgb.b"}, ossia::rgb_u{}},
294 {{"color.rgb.b", "color.rgb.g", "color.rgb.r"}, ossia::bgr_u{}},
295 {{"color.rgb8.a", "color.rgb8.r", "color.rgb8.g", "color.rgb8.b"},
296 ossia::argb8_u{}},
297 {{"color.hsv.h", "color.hsv.s", "color.hsv.v"}, ossia::hsv_u{}},
298 {{"color.cmyk8.c", "color.cmyk8.m", "color.cmyk8.y"}, ossia::cmy8_u{}},
299 {{"color.ciexyz.x", "color.ciexyz.y", "color.ciexyz.z"}, ossia::xyz_u{}}, /*
300{ { "color.hsl.h", "color.hsl.s", "color.hsl.l" }, ossia::hsl_u{} },
301{ { "color.cmyk8.c", "color.cmyk8.m", "color.cmyk8.y", "color.cmyk8.k" },
302ossia::cmyk8_u{} }, { { "color.cieYxy.y", "color.cieYxy.x",
303"color.cieYxy.y" }, ossia::yxy_u{} }, { { "color.hunterLab.l",
304"color.hunterLab.a", "color.hunterLab.b" }, ossia::hunter_lab_u{} }, { {
305"color.cieLab.l", "color.cieLab.a", "color.cieLab.b" }, ossia::cie_lab_u{}
306}, { { "color.cieLuv.l", "color.cieLuv.a", "color.cieLuv.b" },
307ossia::cie_luv_u{} },*/
308
309 unit_matcher(meter_u{}),
310 unit_matcher(kilometer_u{}),
311 unit_matcher(decimeter_u{}),
312 unit_matcher(centimeter_u{}),
313 unit_matcher(millimeter_u{}),
314 unit_matcher(micrometer_u{}),
315 unit_matcher(nanometer_u{}),
316 unit_matcher(picometer_u{}),
317 unit_matcher(inch_u{}),
318 unit_matcher(foot_u{}),
319 unit_matcher(mile_u{}),
320
321 unit_matcher(linear_u{}),
322 unit_matcher(midigain_u{}),
323 unit_matcher(decibel_u{}),
324 unit_matcher(decibel_raw_u{}),
325
326 unit_matcher(quaternion_u{}),
327 unit_matcher(euler_u{}),
328 unit_matcher(axis_u{}),
329
330 unit_matcher(spherical_u{}),
331 unit_matcher(polar_u{}),
332 unit_matcher(aed_u{}),
333 unit_matcher(ad_u{}),
334 unit_matcher(opengl_u{}),
335 unit_matcher(cylindrical_u{}),
336 unit_matcher(azd_u{}),
337
338 {{"position.cartesian.x", "position.cartesian.y", "position.cartesian.z"},
339 ossia::cartesian_3d_u{}},
340 {{"position.cartesian.x", "position.cartesian.y"}, ossia::cartesian_2d_u{}},
341
342 unit_matcher(meter_per_second_u{}),
343 unit_matcher(miles_per_hour_u{}),
344 unit_matcher(kilometer_per_hour_u{}),
345 unit_matcher(knot_u{}),
346 unit_matcher(foot_per_hour_u{}),
347 unit_matcher(foot_per_second_u{}),
348
349 unit_matcher(second_u{}),
350 unit_matcher(bark_u{}),
351 unit_matcher(bpm_u{}),
352 unit_matcher(cent_u{}),
353 unit_matcher(frequency_u{}),
354 unit_matcher(mel_u{}),
355 unit_matcher(midi_pitch_u{}),
356 unit_matcher(millisecond_u{}),
357 unit_matcher(playback_speed_u{})};
358
359 template <typename T>
360 static std::pair<ossia::small_vector<std::string, 4>, ossia::unit_t>
361 unit_matcher(const T&)
362 {
363 ossia::small_vector<std::string, 4> vec;
364 if constexpr(is_array_unit<T>::value)
365 {
366 for(char val : T::array_parameters())
367 {
368 vec.push_back(fmt::format(
369 "{}.{}.{}", dataspace_traits<typename T::dataspace_type>::text()[0],
370 unit_traits<T>::text()[0], val));
371 }
372 }
373 else
374 {
375 vec.push_back(fmt::format(
376 "{}.{}", dataspace_traits<typename T::dataspace_type>::text()[0],
377 unit_traits<T>::text()[0]));
378 }
379
380 return {std::move(vec), T{}};
381 }
382};
383}
Definition git_info.h:7
Definition dataspace.hpp:24