OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
distance.hpp
1#pragma once
2#include <ossia/network/dataspace/dataspace_base.hpp>
3#include <ossia/network/domain/domain.hpp>
4
5namespace ossia
6{
7
8template <typename T>
9struct distance_ratio;
10struct distance_u;
11
12using meter_u = distance_ratio<std::ratio<1>>;
13using kilometer_u = distance_ratio<std::kilo>;
14using decimeter_u = distance_ratio<std::deci>;
15using centimeter_u = distance_ratio<std::centi>;
16using millimeter_u = distance_ratio<std::milli>;
17using micrometer_u = distance_ratio<std::micro>;
18using nanometer_u = distance_ratio<std::nano>;
19using picometer_u = distance_ratio<std::pico>;
20using inch_u = distance_ratio<std::ratio<254, 10000>>;
21using foot_u = distance_ratio<std::ratio<254 * 12, 10000>>;
22using mile_u = distance_ratio<std::ratio<254 * 12 * 5280, 10000>>;
23
24template <typename Impl>
25struct distance_unit
26{
27 using is_unit = std::true_type;
28 using neutral_unit = meter_u;
29 using value_type = float;
30 using concrete_type = Impl;
31 using dataspace_type = distance_u;
32 using is_multidimensional = std::false_type;
33};
34
35template <typename T>
36struct distance_ratio : public linear_unit<distance_unit<distance_ratio<T>>, T>
37{
38 using linear_unit<distance_unit<distance_ratio<T>>, T>::linear_unit;
39
40 static ossia::domain domain() { return {}; }
41};
42
43struct OSSIA_EXPORT pixel_u : public distance_unit<pixel_u>
44{
45 static constexpr auto text()
46 {
47 constexpr_return(ossia::make_string_array("px", "pixels"));
48 }
49 using value_type = float;
50 float ppm{}; // pixels per meter
51
52 void set_ppcm(float v) { ppm = v * 100.f; }
53 void set_dpi(float v) { ppm = v / 254.f; }
54 strong_value<neutral_unit> to_neutral(strong_value<concrete_type> self)
55 {
56 return float(self.dataspace_value / ppm);
57 }
58
59 value_type from_neutral(strong_value<neutral_unit> self)
60 {
61 return float(self.dataspace_value * ppm);
62 }
63
64 static ossia::domain domain() { return {}; }
65
66 static constexpr auto bounding() { return ossia::bounding_mode::FREE; }
67};
68
69template <>
70struct unit_traits<meter_u>
71{
72 static constexpr auto text()
73 {
74 constexpr_return(ossia::make_string_array("m", "meters"));
75 }
76};
77
78template <>
79struct unit_traits<kilometer_u>
80{
81 static constexpr auto text()
82 {
83 constexpr_return(ossia::make_string_array("km", "kilometers"));
84 }
85};
86template <>
87struct unit_traits<decimeter_u>
88{
89 static constexpr auto text()
90 {
91 constexpr_return(ossia::make_string_array("dm", "decimeters"));
92 }
93};
94template <>
95struct unit_traits<centimeter_u>
96{
97 static constexpr auto text()
98 {
99 constexpr_return(ossia::make_string_array("cm", "centimeters"));
100 }
101};
102template <>
103struct unit_traits<millimeter_u>
104{
105 static constexpr auto text()
106 {
107 constexpr_return(ossia::make_string_array("mm", "millimeters"));
108 }
109};
110template <>
111struct unit_traits<micrometer_u>
112{
113 static constexpr auto text()
114 {
115 constexpr_return(ossia::make_string_array("um", "micrometers"));
116 }
117};
118template <>
119struct unit_traits<nanometer_u>
120{
121 static constexpr auto text()
122 {
123 constexpr_return(ossia::make_string_array("nm", "nanometers"));
124 }
125};
126template <>
127struct unit_traits<picometer_u>
128{
129 static constexpr auto text()
130 {
131 constexpr_return(ossia::make_string_array("pm", "picometers"));
132 }
133};
134
135template <>
136struct unit_traits<inch_u>
137{
138 static constexpr auto text()
139 {
140 constexpr_return(ossia::make_string_array("inches", "\""));
141 }
142};
143template <>
144struct unit_traits<foot_u>
145{
146 static constexpr auto text()
147 {
148 constexpr_return(ossia::make_string_array("feet", "'"));
149 }
150};
151template <>
152struct unit_traits<mile_u>
153{
154 static constexpr auto text() { constexpr_return(ossia::make_string_array("miles")); }
155};
156}
Definition git_info.h:7
domain A domain of values
Definition domain_base.hpp:23