roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
length.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "roo_flags.h"
6#include "roo_logging.h"
7
8#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
9#include <string>
10#endif
11
12#if defined(ARDUINO)
13#include <Arduino.h>
14#endif
15
16namespace roo_quantity {
17
18/// Representation of length, internally stored as floating-point meters.
19class Length {
20 public:
21 /// Creates a length object representing an 'unknown' length.
22 Length() : length_(std::nanf("")) {}
23
24 /// Returns the length in kilometers.
25 float inKilometers() const { return length_ * 0.001f; }
26
27 /// Returns the length in meters.
28 float inMeters() const { return length_; }
29
30 /// Returns the length in decimeters.
31 float inDecimeters() const { return length_ * 10.0f; }
32
33 /// Returns the length in centimeters.
34 float inCentimeters() const { return length_ * 100.0f; }
35
36 /// Returns the length in millimeters.
37 float inMillimeters() const { return length_ * 1000.0f; }
38
39 /// Returns the length in micrometers.
40 float inMicrometers() const { return length_ * 1000000.0f; }
41
42 /// Returns the length in nanometers.
43 float inNanometers() const { return length_ * 1000000000.0f; }
44
45 /// Returns the length in inches.
46 float inInches() const { return length_ * 39.37007874f; }
47
48 /// Returns the length in feet.
49 float inFeet() const { return length_ * 3.280839895f; }
50
51 /// Returns the length in yards.
52 float inYards() const { return length_ * 1.0936132983f; }
53
54 /// Returns the length in light years.
55 float inLightYears() const { return length_ * 1.057000834E-16f; }
56
57 /// Returns whether the object represents an unknown length.
58 bool isUnknown() const { return std::isnan(length_); }
59
60 bool operator<(const Length& other) const { return length_ < other.length_; }
61
62 bool operator==(const Length& other) const {
63 return length_ == other.length_;
64 }
65
66 bool operator>(const Length& other) const { return other.length_ < length_; }
67
68 bool operator<=(const Length& other) const {
69 return !(other.length_ < length_);
70 }
71
72 bool operator>=(const Length& other) const {
73 return !(length_ < other.length_);
74 }
75
76 bool operator!=(const Length& other) const {
77 return !(length_ == other.length_);
78 }
79
80 inline Length& operator+=(const Length& other) {
81 length_ += other.inMeters();
82 return *this;
83 }
84
85 inline Length& operator-=(const Length& other) {
86 length_ -= other.inMeters();
87 return *this;
88 }
89
90 inline Length& operator*=(float multi) {
91 length_ *= multi;
92 return *this;
93 }
94
95 inline Length& operator/=(float div) {
96 length_ /= div;
97 return *this;
98 }
99
100#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
101 /// Returns the string representation of the length.
102 std::string asString() const;
103#endif
104
105#if defined(ARDUINO)
106 String asArduinoString() const;
107#endif
108
109 private:
110 friend Length UnknownLength();
111
112 friend Length LengthInMeters(float);
113
114 explicit Length(float length) : length_(length) {}
115
116 /// Stored in meters.
117 float length_;
118};
119
120inline Length LengthInMeters(float length);
121
122/// Returns a length object representing an unknown length.
123inline Length UnknownLength() { return Length(); }
124
125/// Returns a length object equivalent to the specified length
126/// expressed in kilometers.
127inline Length LengthInKilometers(float length) {
128 return LengthInMeters(length * 1000.0f);
129}
130
131/// Returns a length object equivalent to the specified length
132/// expressed in meters.
133inline Length LengthInMeters(float length) { return Length(length); }
134
135/// Returns a length object equivalent to the specified length
136/// expressed in decimeters.
137inline Length LengthInDecimeters(float length) {
138 return LengthInMeters(length * 0.1f);
139}
140
141/// Returns a length object equivalent to the specified length
142/// expressed in centimeters.
143inline Length LengthInCentimeters(float length) {
144 return LengthInMeters(length * 0.01f);
145}
146
147/// Returns a length object equivalent to the specified length
148/// expressed in millimeters.
149inline Length LengthInMillimeters(float length) {
150 return LengthInMeters(length * 0.001f);
151}
152
153/// Returns a length object equivalent to the specified length
154/// expressed in micrometers.
155inline Length LengthInMicrometers(float length) {
156 return LengthInMeters(length * 0.000001f);
157}
158
159/// Returns a length object equivalent to the specified length
160/// expressed in nanometers.
161inline Length LengthInNanometers(float length) {
162 return LengthInMeters(length * 0.000000001f);
163}
164
165/// Returns a length object equivalent to the specified length
166/// expressed in inches.
167inline Length LengthInInches(float length) {
168 return LengthInMeters(length * 0.0254f);
169}
170
171/// Returns a length object equivalent to the specified length
172/// expressed in feet.
173inline Length LengthInFeet(float length) {
174 return LengthInMeters(length * 0.3048f);
175}
176
177/// Returns a length object equivalent to the specified length
178/// expressed in yards.
179inline Length LengthInYards(float length) {
180 return LengthInMeters(length * 0.9144f);
181}
182
183/// Returns a length object equivalent to the specified length
184/// expressed in light years.
185inline Length LengthInLightYears(float length) {
186 return LengthInMeters(length * 9460730472580044.0f);
187}
188
190 return LengthInMeters(a.inMeters() + b.inMeters());
191}
192
194 return LengthInMeters(a.inMeters() - b.inMeters());
195}
196
197inline Length operator-(Length a) { return LengthInMeters(-a.inMeters()); }
198
199inline Length operator*(Length a, float b) {
200 return LengthInMeters(a.inMeters() * b);
201}
202
203inline Length operator*(float a, Length b) {
204 return LengthInMeters(a * b.inMeters());
205}
206
207inline Length operator/(Length a, float b) {
208 return LengthInMeters(a.inMeters() / b);
209}
210
211inline float operator/(Length a, Length b) {
212 return a.inMeters() / b.inMeters();
213}
214
215roo_logging::Stream& operator<<(roo_logging::Stream& os, const Length& val);
216
217} // namespace roo_quantity
Representation of length, internally stored as floating-point meters.
Definition length.h:19
Length & operator*=(float multi)
Definition length.h:90
float inInches() const
Returns the length in inches.
Definition length.h:46
bool operator==(const Length &other) const
Definition length.h:62
bool operator>=(const Length &other) const
Definition length.h:72
float inDecimeters() const
Returns the length in decimeters.
Definition length.h:31
bool operator<=(const Length &other) const
Definition length.h:68
float inNanometers() const
Returns the length in nanometers.
Definition length.h:43
float inMicrometers() const
Returns the length in micrometers.
Definition length.h:40
Length()
Creates a length object representing an 'unknown' length.
Definition length.h:22
float inLightYears() const
Returns the length in light years.
Definition length.h:55
friend Length UnknownLength()
Returns a length object representing an unknown length.
Definition length.h:123
float inYards() const
Returns the length in yards.
Definition length.h:52
float inMeters() const
Returns the length in meters.
Definition length.h:28
Length & operator/=(float div)
Definition length.h:95
Length & operator+=(const Length &other)
Definition length.h:80
bool operator>(const Length &other) const
Definition length.h:66
float inCentimeters() const
Returns the length in centimeters.
Definition length.h:34
Length & operator-=(const Length &other)
Definition length.h:85
bool operator!=(const Length &other) const
Definition length.h:76
bool isUnknown() const
Returns whether the object represents an unknown length.
Definition length.h:58
float inKilometers() const
Returns the length in kilometers.
Definition length.h:25
float inFeet() const
Returns the length in feet.
Definition length.h:49
friend Length LengthInMeters(float)
Returns a length object equivalent to the specified length expressed in meters.
Definition length.h:133
bool operator<(const Length &other) const
Definition length.h:60
float inMillimeters() const
Returns the length in millimeters.
Definition length.h:37
For convenience conversion from roo_time::Duration.
Definition area.cpp:3
Length LengthInMeters(float length)
Returns a length object equivalent to the specified length expressed in meters.
Definition length.h:133
roo_logging::Stream & operator<<(roo_logging::Stream &os, const Area &val)
Definition area.cpp:57
Length LengthInNanometers(float length)
Returns a length object equivalent to the specified length expressed in nanometers.
Definition length.h:161
Length LengthInYards(float length)
Returns a length object equivalent to the specified length expressed in yards.
Definition length.h:179
Length LengthInMillimeters(float length)
Returns a length object equivalent to the specified length expressed in millimeters.
Definition length.h:149
Length LengthInDecimeters(float length)
Returns a length object equivalent to the specified length expressed in decimeters.
Definition length.h:137
Area operator+(Area a, Area b)
Definition area.h:171
Length LengthInMicrometers(float length)
Returns a length object equivalent to the specified length expressed in micrometers.
Definition length.h:155
Area operator-(Area a, Area b)
Definition area.h:175
Length LengthInLightYears(float length)
Returns a length object equivalent to the specified length expressed in light years.
Definition length.h:185
Length LengthInCentimeters(float length)
Returns a length object equivalent to the specified length expressed in centimeters.
Definition length.h:143
Length LengthInKilometers(float length)
Returns a length object equivalent to the specified length expressed in kilometers.
Definition length.h:127
Length LengthInInches(float length)
Returns a length object equivalent to the specified length expressed in inches.
Definition length.h:167
Area operator/(Area a, float b)
Definition area.h:191
Length LengthInFeet(float length)
Returns a length object equivalent to the specified length expressed in feet.
Definition length.h:173
Area operator*(Area a, float b)
Definition area.h:183
Length UnknownLength()
Returns a length object representing an unknown length.
Definition length.h:123