roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
volume.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#include "roo_quantity/area.h"
9
10#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
11#include <string>
12#endif
13
14#if defined(ARDUINO)
15#include <Arduino.h>
16#endif
17
18namespace roo_quantity {
19
20/// Representation of volume, internally stored as floating-point cubic meters.
21class Volume {
22 public:
23 /// Creates a volume object representing an 'unknown' volume.
24 Volume() : volume_(std::nanf("")) {}
25
26 /// Returns the volume in cubic kilometers.
27 float inCubicKilometers() const { return volume_ * 0.000000001f; }
28
29 /// Returns the volume in cubic meters.
30 float inCubicMeters() const { return volume_; }
31
32 /// Returns the volume in liters.
33 float inLiters() const { return volume_ * 1000.0f; }
34
35 /// Returns the volume in liters.
36 float inCubicDecimeters() const { return inLiters(); }
37
38 /// Returns the volume in milliliters.
39 float inMilliliters() const { return volume_ * 1000000.0f; }
40
41 /// Returns the volume in milliliters.
42 float inCubicCentimeters() const { return inMilliliters(); }
43
44 /// Returns the volume in microliters.
45 float inMicroliters() const { return volume_ * 1000000000.0f; }
46
47 /// Returns the volume in microliters.
48 float inCubicMillimeters() const { return inMicroliters(); }
49
50 /// Returns the volume in cubic micrometers.
51 float inCubicMicrometers() const { return volume_ * 1000000000000000000.0f; }
52
53 /// Returns the volume in cubic inches.
54 float inCubicInches() const { return volume_ * 61023.744095f; }
55
56 /// Returns whether the object represents an unknown volume.
57 bool isUnknown() const { return std::isnan(volume_); }
58
59 bool operator<(const Volume &other) const { return volume_ < other.volume_; }
60
61 bool operator==(const Volume &other) const {
62 return volume_ == other.volume_;
63 }
64
65 bool operator>(const Volume &other) const { return other.volume_ < volume_; }
66
67 bool operator<=(const Volume &other) const {
68 return !(other.volume_ < volume_);
69 }
70
71 bool operator>=(const Volume &other) const {
72 return !(volume_ < other.volume_);
73 }
74
75 bool operator!=(const Volume &other) const {
76 return !(volume_ == other.volume_);
77 }
78
79 inline Volume &operator+=(const Volume &other) {
80 volume_ += other.inCubicMeters();
81 return *this;
82 }
83
84 inline Volume &operator-=(const Volume &other) {
85 volume_ -= other.inCubicMeters();
86 return *this;
87 }
88
89 inline Volume &operator*=(float multi) {
90 volume_ *= multi;
91 return *this;
92 }
93
94 inline Volume &operator/=(float div) {
95 volume_ /= div;
96 return *this;
97 }
98
99#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
100 /// Returns the string representation of the volume.
101 std::string asString() const;
102#endif
103
104#if defined(ARDUINO)
105 String asArduinoString() const;
106#endif
107
108 private:
109 friend Volume UnknownVolume();
110
111 friend Volume VolumeInCubicMeters(float);
112
113 explicit Volume(float volume) : volume_(volume) {}
114
115 /// Stored in cubic meters.
116 float volume_;
117};
118
119inline Volume VolumeInCubicMeters(float volume);
120
121/// Returns a volume object representing an unknown volume.
122inline Volume UnknownVolume() { return Volume(); }
123
124/// Returns a volume object equivalent to the specified volume
125/// expressed in cubic kilometers.
126inline Volume VolumeInCubicKilometers(float volume) {
127 return VolumeInCubicMeters(volume * 1000000000.0f);
128}
129
130/// Returns a volume object equivalent to the specified volume
131/// expressed in cubic meters.
132inline Volume VolumeInCubicMeters(float volume) { return Volume(volume); }
133
134/// Returns a volume object equivalent to the specified volume
135/// expressed in liters.
136inline Volume VolumeInLiters(float volume) {
137 return VolumeInCubicMeters(volume * 0.001f);
138}
139
140/// Returns a volume object equivalent to the specified volume
141/// expressed in liters.
142inline Volume VolumeInCubicDecimeters(float volume) {
143 return VolumeInLiters(volume);
144}
145
146/// Returns a volume object equivalent to the specified volume
147/// expressed in milliliters.
148inline Volume VolumeInMilliliters(float volume) {
149 return VolumeInCubicMeters(volume * 0.000001f);
150}
151
152/// Returns a volume object equivalent to the specified volume
153/// expressed in milliliters.
154inline Volume VolumeInCubicCentimeters(float volume) {
155 return VolumeInMilliliters(volume);
156}
157
158/// Returns a volume object equivalent to the specified volume
159/// expressed in cubic millimeters.
160inline Volume VolumeInCubicMillimeters(float volume) {
161 return VolumeInCubicMeters(volume * 0.000000001f);
162}
163
164/// Returns a volume object equivalent to the specified volume
165/// expressed in cubic micrometers.
166inline Volume VolumeInCubicMicrometers(float volume) {
167 return VolumeInCubicMeters(volume * 0.000000000000000001f);
168}
169
170/// Returns a volume object equivalent to the specified volume
171/// expressed in cubic inches.
172inline Volume VolumeInCubicInches(float volume) {
173 return VolumeInCubicMeters(volume * 1.6387e-5f);
174}
175
179
183
186}
187
188inline Volume operator*(Volume a, float b) {
189 return VolumeInCubicMeters(a.inCubicMeters() * b);
190}
191
192inline Volume operator*(float a, Volume b) {
193 return VolumeInCubicMeters(a * b.inCubicMeters());
194}
195
196inline Volume operator/(Volume a, float b) {
197 return VolumeInCubicMeters(a.inCubicMeters() / b);
198}
199
200inline float operator/(Volume a, Volume b) {
201 return a.inCubicMeters() / b.inCubicMeters();
202}
203
206}
207
210}
211
213 return AreaInSquareMeters(a.inCubicMeters() / b.inMeters());
214}
215
218}
219
220roo_logging::Stream &operator<<(roo_logging::Stream &os, const Volume &val);
221
222} // namespace roo_quantity
Representation of area, internally stored as floating-point square meters.
Definition area.h:20
float inSquareMeters() const
Returns the area in square meters.
Definition area.h:35
Representation of length, internally stored as floating-point meters.
Definition length.h:19
float inMeters() const
Returns the length in meters.
Definition length.h:28
Representation of volume, internally stored as floating-point cubic meters.
Definition volume.h:21
float inCubicMillimeters() const
Returns the volume in microliters.
Definition volume.h:48
float inCubicMeters() const
Returns the volume in cubic meters.
Definition volume.h:30
Volume & operator+=(const Volume &other)
Definition volume.h:79
float inMilliliters() const
Returns the volume in milliliters.
Definition volume.h:39
float inLiters() const
Returns the volume in liters.
Definition volume.h:33
bool operator>(const Volume &other) const
Definition volume.h:65
bool operator>=(const Volume &other) const
Definition volume.h:71
bool isUnknown() const
Returns whether the object represents an unknown volume.
Definition volume.h:57
friend Volume UnknownVolume()
Returns a volume object representing an unknown volume.
Definition volume.h:122
bool operator!=(const Volume &other) const
Definition volume.h:75
bool operator==(const Volume &other) const
Definition volume.h:61
Volume & operator-=(const Volume &other)
Definition volume.h:84
float inCubicInches() const
Returns the volume in cubic inches.
Definition volume.h:54
float inCubicMicrometers() const
Returns the volume in cubic micrometers.
Definition volume.h:51
float inCubicKilometers() const
Returns the volume in cubic kilometers.
Definition volume.h:27
float inCubicDecimeters() const
Returns the volume in liters.
Definition volume.h:36
Volume()
Creates a volume object representing an 'unknown' volume.
Definition volume.h:24
Volume & operator/=(float div)
Definition volume.h:94
Volume & operator*=(float multi)
Definition volume.h:89
float inMicroliters() const
Returns the volume in microliters.
Definition volume.h:45
float inCubicCentimeters() const
Returns the volume in milliliters.
Definition volume.h:42
bool operator<=(const Volume &other) const
Definition volume.h:67
friend Volume VolumeInCubicMeters(float)
Returns a volume object equivalent to the specified volume expressed in cubic meters.
Definition volume.h:132
bool operator<(const Volume &other) const
Definition volume.h:59
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
Volume VolumeInCubicMeters(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic meters.
Definition volume.h:132
Area AreaInSquareMeters(float area)
Returns a area object equivalent to the specified area expressed in square meters.
Definition area.h:133
Volume VolumeInCubicKilometers(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic kilometers.
Definition volume.h:126
Volume VolumeInLiters(float volume)
Returns a volume object equivalent to the specified volume expressed in liters.
Definition volume.h:136
Area operator+(Area a, Area b)
Definition area.h:171
Volume VolumeInCubicDecimeters(float volume)
Returns a volume object equivalent to the specified volume expressed in liters.
Definition volume.h:142
Volume UnknownVolume()
Returns a volume object representing an unknown volume.
Definition volume.h:122
Volume VolumeInCubicMicrometers(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic micrometers.
Definition volume.h:166
Area operator-(Area a, Area b)
Definition area.h:175
Volume VolumeInMilliliters(float volume)
Returns a volume object equivalent to the specified volume expressed in milliliters.
Definition volume.h:148
Volume VolumeInCubicCentimeters(float volume)
Returns a volume object equivalent to the specified volume expressed in milliliters.
Definition volume.h:154
Area operator/(Area a, float b)
Definition area.h:191
Volume VolumeInCubicInches(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic inches.
Definition volume.h:172
Volume VolumeInCubicMillimeters(float volume)
Returns a volume object equivalent to the specified volume expressed in cubic millimeters.
Definition volume.h:160
Area operator*(Area a, float b)
Definition area.h:183