TinyRobotics
Loading...
Searching...
No Matches
Velocity3D.h
1#pragma once
2#include "Speed.h"
3
4namespace tinyrobotics {
5
6/**
7 * @typedef VelocityUnit
8 * @brief Alias for SpeedUnit to represent velocity units.
9 * @ingroup units
10 */
11using VelocityUnit = SpeedUnit;
12
13/**
14 * @class Velocity3D
15 * @ingroup units
16 * @brief Represents a 3D speed or velocity vector with unit support.
17 *
18 * This class encapsulates a 3D speed (or velocity) measurement, storing x, y, and z components
19 * along with a unit (meters per second, kilometers per hour, feet per second, or miles per hour).
20 * It provides methods to retrieve each component in any supported unit, handling conversion as needed.
21 *
22 * Speed3D is useful for robotics, navigation, and simulation applications where 3D motion must be
23 * represented and manipulated in a type-safe and unit-aware manner. It is compatible with the Speed
24 * class for 1D speed and can be used interchangeably with Velocity3D.
25 *
26 * Example usage:
27 * @code
28 * Velocity3D v(1.0, 0.0, 0.0, SpeedUnit::MPS); // 1 m/s along x
29 * float vx_kph = v.getX(SpeedUnit::KPH); // Convert x component to km/h
30 * @endcode
31 */
32class Velocity3D {
33 Velocity3D operator+(const Velocity3D& other) const {
34 return Velocity3D(x + other.getX(unit), y + other.getY(unit), z + other.getZ(unit), unit);
35 }
36
37 Velocity3D operator-(const Velocity3D& other) const {
38 return Velocity3D(x - other.getX(unit), y - other.getY(unit), z - other.getZ(unit), unit);
39 }
40
41 Velocity3D operator*(float scalar) const {
42 return Velocity3D(x * scalar, y * scalar, z * scalar, unit);
43 }
44
45 Velocity3D operator/(float scalar) const {
46 if (scalar == 0) return Velocity3D(0, 0, 0, unit);
47 return Velocity3D(x / scalar, y / scalar, z / scalar, unit);
48 }
49
50 bool operator==(const Velocity3D& other) const {
51 return x == other.getX(unit) && y == other.getY(unit) && z == other.getZ(unit);
52 }
53
54 bool operator!=(const Velocity3D& other) const { return !(*this == other); }
55
56 bool operator<(const Velocity3D& other) const {
57 return (x < other.getX(unit)) && (y < other.getY(unit)) && (z < other.getZ(unit));
58 }
59
60 bool operator<=(const Velocity3D& other) const {
61 return (x <= other.getX(unit)) && (y <= other.getY(unit)) && (z <= other.getZ(unit));
62 }
63
64 bool operator>(const Velocity3D& other) const {
65 return (x > other.getX(unit)) && (y > other.getY(unit)) && (z > other.getZ(unit));
66 }
67
68 bool operator>=(const Velocity3D& other) const {
69 return (x >= other.getX(unit)) && (y >= other.getY(unit)) && (z >= other.getZ(unit));
70 }
71
72 Velocity3D& operator+=(const Velocity3D& other) {
73 x += other.getX(unit);
74 y += other.getY(unit);
75 z += other.getZ(unit);
76 return *this;
77 }
78
79 Velocity3D& operator-=(const Velocity3D& other) {
80 x -= other.getX(unit);
81 y -= other.getY(unit);
82 z -= other.getZ(unit);
83 return *this;
84 }
85
86 Velocity3D& operator*=(float scalar) {
87 x *= scalar;
88 y *= scalar;
89 z *= scalar;
90 return *this;
91 }
92
93 Velocity3D& operator/=(float scalar) {
94 if (scalar == 0) {
95 x = y = z = 0;
96 } else {
97 x /= scalar;
98 y /= scalar;
99 z /= scalar;
100 }
101 return *this;
102 }
103 public:
104 float x = 0.0f;
105 float y = 0.0f;
106 float z = 0.0f;
107 VelocityUnit unit = VelocityUnit::MPS;
108
109 Velocity3D() = default;
110 Velocity3D(float x, float y, float z, VelocityUnit unit) : x(x), y(y), z(z), unit(unit) {}
111
112 float getX(VelocityUnit desiredUnit) const {
113 if (unit == desiredUnit) return x;
114 Speed tempSpeed(x, unit);
115 return tempSpeed.getValue(desiredUnit);
116 }
117 float getY(VelocityUnit desiredUnit) const {
118 if (unit == desiredUnit) return y;
119 Speed tempSpeed(y, unit);
120 return tempSpeed.getValue(desiredUnit);
121 }
122 float getZ(VelocityUnit desiredUnit) const {
123 if (unit == desiredUnit) return z;
124 Speed tempSpeed(z, unit);
125 return tempSpeed.getValue(desiredUnit);
126 }
127};
128
129using Speed3D = Velocity3D;
130
131} // namespace tinyrobotics
Represents a speed measurement with unit conversion support.
Definition: Speed.h:40
Represents a 3D speed or velocity vector with unit support.
Definition: Velocity3D.h:32
SpeedUnit
Supported speed units for conversion and representation.
Definition: Speed.h:10