TinyRobotics
Loading...
Searching...
No Matches
Distance.h
1#pragma once
2
3namespace tinyrobotics {
4
5/**
6 * @enum DistanceUnit
7 * @ingroup units
8 * @brief Supported distance units for conversion and representation.
9 */
10enum class DistanceUnit { M, CM, MM, FEET };
11
12/**
13 * @class Distance
14 * @ingroup units
15 * @brief Represents a distance measurement with unit conversion support.
16 *
17 * The Distance class encapsulates a distance value and its unit, supporting
18 * meters (M), centimeters (CM), millimeters (MM), and feet (FEET). It provides
19 * methods to set and retrieve the distance in any supported unit, automatically
20 * handling conversions.
21 *
22 * - Internal state is always consistent with the last set value and unit.
23 * - Designed for embedded and robotics applications where unit flexibility and
24 * efficiency are required.
25 * - Use getValue() to retrieve the distance in any unit; use setValue() to
26 * update the value and unit.
27 *
28 * Example:
29 * @code
30 * Distance d(1.0, DistanceUnit::M);
31 * float feet = d.getValue(DistanceUnit::FEET); // Convert to feet
32 * d.setValue(100.0, DistanceUnit::CM); // Update value in centimeters
33 * float meters = d.getValue(DistanceUnit::M); // Convert back to meters
34 * @endcode
35 *
36 * @note Invalid conversions return -1.0f.
37 *
38 * @see DistanceUnit
39 */
40class Distance {
41 public:
42 Distance() = default;
43 Distance(float distance, DistanceUnit unit) { setValue(distance, unit); }
44
45 void setValue(float newDistance, DistanceUnit newUnit) {
46 distance = newDistance;
47 unit = newUnit;
48 }
49
50 float getValue(DistanceUnit desiredUnit) const {
51 if (unit == desiredUnit) return distance;
52 switch (unit) {
53 case DistanceUnit::M:
54 if (desiredUnit == DistanceUnit::CM) return distance * 100;
55 if (desiredUnit == DistanceUnit::MM) return distance * 1000;
56 if (desiredUnit == DistanceUnit::FEET) return distance * 3.28084f;
57 break;
58 case DistanceUnit::CM:
59 if (desiredUnit == DistanceUnit::M) return distance / 100;
60 if (desiredUnit == DistanceUnit::MM) return distance * 10;
61 if (desiredUnit == DistanceUnit::FEET) return distance * 0.0328084f;
62 break;
63 case DistanceUnit::MM:
64 if (desiredUnit == DistanceUnit::M) return distance / 1000;
65 if (desiredUnit == DistanceUnit::CM) return distance / 10;
66 if (desiredUnit == DistanceUnit::FEET) return distance * 0.00328084f;
67 break;
68 case DistanceUnit::FEET:
69 if (desiredUnit == DistanceUnit::M) return distance / 3.28084f;
70 if (desiredUnit == DistanceUnit::CM) return distance / 0.0328084f;
71 if (desiredUnit == DistanceUnit::MM) return distance / 0.00328084f;
72 break;
73 }
74 return -1; // Invalid conversion
75 }
76
77 Distance operator+(const Distance& other) const {
78 float otherDistance = other.getValue(unit);
79 return Distance(distance + otherDistance, unit);
80 }
81
82 Distance operator-(const Distance& other) const {
83 float otherDistance = other.getValue(unit);
84 return Distance(distance - otherDistance, unit);
85 }
86
87 Distance operator*(float scalar) const {
88 return Distance(distance * scalar, unit);
89 }
90
91 Distance operator/(float scalar) const {
92 if (scalar == 0) return Distance(0, unit);
93 return Distance(distance / scalar, unit);
94 }
95
96 bool operator==(const Distance& other) const {
97 return distance == other.getValue(unit);
98 }
99
100 bool operator!=(const Distance& other) const { return !(*this == other); }
101
102 bool operator<(const Distance& other) const {
103 return distance < other.getValue(unit);
104 }
105
106 bool operator<=(const Distance& other) const {
107 return distance <= other.getValue(unit);
108 }
109
110 bool operator>(const Distance& other) const {
111 return distance > other.getValue(unit);
112 }
113
114 bool operator>=(const Distance& other) const {
115 return distance >= other.getValue(unit);
116 }
117
118 Distance& operator+=(const Distance& other) {
119 distance += other.getValue(unit);
120 return *this;
121 }
122
123 Distance& operator-=(const Distance& other) {
124 distance -= other.getValue(unit);
125 return *this;
126 }
127
128 Distance& operator*=(float scalar) {
129 distance *= scalar;
130 return *this;
131 }
132
133 Distance& operator/=(float scalar) {
134 if (scalar == 0) {
135 distance = 0;
136 } else {
137 distance /= scalar;
138 }
139 return *this;
140 }
141
142 protected:
143 float distance = 0.0f;
144 DistanceUnit unit = DistanceUnit::M; ///< Unit of the distance. @see DistanceUnit
145};
146
147/**
148 * @brief Represents a 3D distance or position vector with unit support.
149 *
150 * This class encapsulates a 3D distance measurement, storing x, y, and z
151 * components along with a unit (meters, centimeters, millimeters, or feet). It
152 * provides methods to retrieve each component in any supported unit, handling
153 * conversion as needed.
154 *
155 * Distance3D is useful for robotics, navigation, mapping, and simulation
156 * applications where 3D positions or displacements must be represented and
157 * manipulated in a type-safe and unit-aware manner. It is compatible with the
158 * Distance class for 1D distance.
159 *
160 * Example usage:
161 * Distance3D p(1.0, 2.0, 3.0, DistanceUnit::M); // 1m, 2m, 3m in x, y, z
162 * float x_cm = p.getX(DistanceUnit::CM); // Convert x to centimeters
163 */
164class Distance3D {
165 public:
166 Distance3D() = default;
167 Distance3D(float x, float y, float z, DistanceUnit unit)
168 : x(x), y(y), z(z), unit(unit) {}
169
170 float x = 0.0f;
171 float y = 0.0f;
172 float z = 0.0f;
174 DistanceUnit::M; ///< Unit of the 3D distance. @see DistanceUnit
175
176 float getX(DistanceUnit desiredUnit) const {
177 if (unit == desiredUnit) return x;
178 Distance tempDistance(x, unit);
179 return tempDistance.getValue(desiredUnit);
180 }
181 float getY(DistanceUnit desiredUnit) const {
182 if (unit == desiredUnit) return y;
183 Distance tempDistance(y, unit);
184 return tempDistance.getValue(desiredUnit);
185 }
186 float getZ(DistanceUnit desiredUnit) const {
187 if (unit == desiredUnit) return z;
188 Distance tempDistance(z, unit);
189 return tempDistance.getValue(desiredUnit);
190 }
191};
192
193} // namespace tinyrobotics
Represents a 3D distance or position vector with unit support.
Definition: Distance.h:164
DistanceUnit unit
Unit of the 3D distance.
Definition: Distance.h:173
Represents a distance measurement with unit conversion support.
Definition: Distance.h:40
DistanceUnit unit
Unit of the distance.
Definition: Distance.h:144
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10