roo_quantity
API Documentation for roo_quantity
Loading...
Searching...
No Matches
force.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"
9#include "roo_quantity/work.h"
10
11#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
12#include <string>
13#endif
14
15#if defined(ARDUINO)
16#include <Arduino.h>
17#endif
18
19namespace roo_quantity {
20
21/// Representation of force, internally stored as floating-point Newtons.
22class Force {
23 public:
24 /// Creates a force object representing an 'unknown' force.
25 Force() : force_(std::nanf("")) {}
26
27 /// Returns the force in kiloNewtons.
28 float inKiloNewtons() const { return force_ / 1000.0f; }
29
30 /// Returns the force in Newtons.
31 float inNewtons() const { return force_; }
32
33 /// Returns the force in milliNewtons.
34 float inMilliNewtons() const { return force_ * 1000.0f; }
35
36 /// Returns the force in microNewtons.
37 float inMicroNewtons() const { return force_ * 1000000.0f; }
38
39 /// Returns whether the object represents an unknown force.
40 bool isUnknown() const { return std::isnan(force_); }
41
42 bool operator<(const Force& other) const { return force_ < other.force_; }
43
44 bool operator==(const Force& other) const { return force_ == other.force_; }
45
46 bool operator>(const Force& other) const { return other.force_ < force_; }
47
48 bool operator<=(const Force& other) const { return !(other.force_ < force_); }
49
50 bool operator>=(const Force& other) const { return !(force_ < other.force_); }
51
52 bool operator!=(const Force& other) const {
53 return !(force_ == other.force_);
54 }
55
56 inline Force& operator+=(const Force& other) {
57 force_ += other.inNewtons();
58 return *this;
59 }
60
61 inline Force& operator-=(const Force& other) {
62 force_ -= other.inNewtons();
63 return *this;
64 }
65
66 inline Force& operator*=(float multi) {
67 force_ *= multi;
68 return *this;
69 }
70
71 inline Force& operator/=(float div) {
72 force_ /= div;
73 return *this;
74 }
75
76#if defined(ESP32) || defined(ESP8266) || defined(__linux__)
77 /// Returns the string representation of the force.
78 std::string asString() const;
79#endif
80
81#if defined(ARDUINO)
82 String asArduinoString() const;
83#endif
84
85 private:
86 friend Force UnknownForce();
87
88 friend Force ForceInNewtons(float);
89
90 explicit Force(float force) : force_(force) {}
91
92 /// Stored in Newtons.
93 float force_;
94};
95
96inline Force ForceInNewtons(float force);
97
98/// Returns a force object representing an unknown force.
99inline Force UnknownForce() { return Force(); }
100
101/// Returns a force object equivalent to the specified force
102/// expressed in gigaNewtons.
103inline Force ForceInGigaNewtons(float force) {
104 return ForceInNewtons(force * 1000000000.0f);
105}
106
107/// Returns a force object equivalent to the specified force
108/// expressed in megaNewtons.
109inline Force ForceInMegaNewtons(float force) {
110 return ForceInNewtons(force * 1000000.0f);
111}
112
113/// Returns a force object equivalent to the specified force
114/// expressed in kiloNewtons.
115inline Force ForceInKiloNewtons(float force) {
116 return ForceInNewtons(force * 1000.0f);
117}
118
119/// Returns a force object equivalent to the specified force
120/// expressed in Newtons.
121inline Force ForceInNewtons(float force) { return Force(force); }
122
123/// Returns a force object equivalent to the specified force
124/// expressed in milliNewtons.
125inline Force ForceInMilliNewtons(float force) {
126 return ForceInNewtons(force / 1000.0f);
127}
128
129/// Returns a force object equivalent to the specified force
130/// expressed in microNewtons.
131inline Force ForceInMicroNewtons(float force) {
132 return ForceInNewtons(force / 1000000.0f);
133}
134
136 return ForceInNewtons(a.inNewtons() + b.inNewtons());
137}
138
140 return ForceInNewtons(a.inNewtons() - b.inNewtons());
141}
142
143inline Force operator-(Force a) { return ForceInNewtons(-a.inNewtons()); }
144
145inline Force operator*(Force a, float b) {
146 return ForceInNewtons(a.inNewtons() * b);
147}
148
149inline Force operator*(float a, Force b) {
150 return ForceInNewtons(a * b.inNewtons());
151}
152
154 return WorkInJoules(a.inNewtons() * b.inMeters());
155}
156
158 return WorkInJoules(a.inMeters() * b.inNewtons());
159}
160
161inline Force operator/(Force a, float b) {
162 return ForceInNewtons(a.inNewtons() / b);
163}
164
166 return ForceInNewtons(a.inJoules() / b.inMeters());
167}
168
170 return LengthInMeters(a.inJoules() / b.inNewtons());
171}
172
174 return ForceInNewtons(a.inJoules() * b.inUnitsPerMeter());
175}
176
177inline float operator/(Force a, Force b) {
178 return a.inNewtons() / b.inNewtons();
179}
180
181roo_logging::Stream& operator<<(roo_logging::Stream& os, const Force& val);
182
183} // namespace roo_quantity
Representation of force, internally stored as floating-point Newtons.
Definition force.h:22
float inMicroNewtons() const
Returns the force in microNewtons.
Definition force.h:37
Force & operator*=(float multi)
Definition force.h:66
float inNewtons() const
Returns the force in Newtons.
Definition force.h:31
Force & operator-=(const Force &other)
Definition force.h:61
Force & operator+=(const Force &other)
Definition force.h:56
friend Force ForceInNewtons(float)
Returns a force object equivalent to the specified force expressed in Newtons.
Definition force.h:121
bool operator<=(const Force &other) const
Definition force.h:48
bool operator!=(const Force &other) const
Definition force.h:52
bool operator>=(const Force &other) const
Definition force.h:50
float inMilliNewtons() const
Returns the force in milliNewtons.
Definition force.h:34
bool operator<(const Force &other) const
Definition force.h:42
float inKiloNewtons() const
Returns the force in kiloNewtons.
Definition force.h:28
bool operator==(const Force &other) const
Definition force.h:44
friend Force UnknownForce()
Returns a force object representing an unknown force.
Definition force.h:99
bool isUnknown() const
Returns whether the object represents an unknown force.
Definition force.h:40
Force & operator/=(float div)
Definition force.h:71
Force()
Creates a force object representing an 'unknown' force.
Definition force.h:25
bool operator>(const Force &other) const
Definition force.h:46
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 lineic number, a reciprocal of length, internally stored as floating-point units pe...
float inUnitsPerMeter() const
Returns the lineic number in units per meter.
Representation of work, internally stored as floating-point Joules.
Definition work.h:19
float inJoules() const
Returns the work in joules.
Definition work.h:28
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
Force UnknownForce()
Returns a force object representing an unknown force.
Definition force.h:99
Area operator+(Area a, Area b)
Definition area.h:171
Force ForceInMegaNewtons(float force)
Returns a force object equivalent to the specified force expressed in megaNewtons.
Definition force.h:109
Area operator-(Area a, Area b)
Definition area.h:175
Force ForceInKiloNewtons(float force)
Returns a force object equivalent to the specified force expressed in kiloNewtons.
Definition force.h:115
Work WorkInJoules(float work)
Returns a work object equivalent to the specified work expressed in Joules.
Definition work.h:116
Force ForceInNewtons(float force)
Returns a force object equivalent to the specified force expressed in Newtons.
Definition force.h:121
Force ForceInMilliNewtons(float force)
Returns a force object equivalent to the specified force expressed in milliNewtons.
Definition force.h:125
Force ForceInMicroNewtons(float force)
Returns a force object equivalent to the specified force expressed in microNewtons.
Definition force.h:131
Force ForceInGigaNewtons(float force)
Returns a force object equivalent to the specified force expressed in gigaNewtons.
Definition force.h:103
Area operator/(Area a, float b)
Definition area.h:191
Area operator*(Area a, float b)
Definition area.h:183