6#include "TinyRobotics/serialize/Serializable.h"
7#include "TinyRobotics/units/Units.h"
8#include "TinyRobotics/utils/Common.h"
9#include "TinyRobotics/utils/Config.h"
11namespace tinyrobotics {
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
56template <
typename T = DistanceM>
60 Coordinate() =
default;
61 Coordinate(T x, T y, T z = 0) :
x(x),
y(y),
z(z) {}
83 return dist.getValue(unit);
92 return angle.getValue(unit);
100 return angle.getValue(unit);
107 AngleDeg bearingDeg = bearing.getValue(
AngleUnit::DEG);
108 DistanceM distanceM = distance.getValue(
DistanceUnit::M);
109 DistanceM altDiffM = altDiff.getValue(DistanceUnit::M);
110 return navigate(distanceM, bearingDeg, altDiffM);
116 float altDiffM = 0)
const {
117 float headingRad = headingDegrees *
static_cast<
float>(M_PI) / 180.0f;
118 DistanceM newX =
x + distanceM * std::cos(headingRad);
119 DistanceM newY =
y + distanceM * std::sin(headingRad);
138 DistanceM altLimit)
const {
177 return x == other.x &&
y == other.y &&
z == other.z;
180 bool operator!=(
const Coordinate<T>& other)
const {
181 return !(*
this == other);
186 if (
x != other.x)
return x < other.x;
187 if (
y != other.y)
return y < other.y;
195 return std::string(buf);
200 if (str.find(getTypeName()) == std::string::npos)
202 size_t colon = str.find(
':');
203 if (colon == std::string::npos)
return false;
204 size_t comma1 = str.find(
',');
205 if (comma1 == std::string::npos)
return false;
206 size_t comma2 = str.find(
',', comma1 + 1);
207 x = std::stof(str.substr(colon, comma1));
208 y = std::stof(str.substr(comma1 + 1, comma2 - comma1 - 1));
209 if (comma2 != std::string::npos) {
210 z = std::stof(str.substr(comma2 + 1));
218 void setValues(T newX, T newY, T newZ = 0) {
228 z = newZ.getValue(DistanceUnit::M);
232
233
234
235
236
237
238
239
240
243 std::vector<Coordinate<T>> points;
244 T dx = target.x -
this->x;
245 T dy = target.y -
this->y;
246 T dz = target.z -
this->z;
247 T distance = std::sqrt(dx * dx + dy * dy + dz * dz);
248 int steps = std::max(1,
static_cast<
int>(std::ceil(distance / resolution)));
249 for (
int i = 0; i <= steps; ++i) {
250 T t =
static_cast<T>(i) / steps;
251 points.emplace_back(
this->x + t * dx,
this->y + t * dy,
this->z + t * dz);
259 return interpolateTo(target, resolution.getValue(DistanceUnit::M));
263 const char*
getTypeName()
const {
return "Coordinate"; }
268 float dx = other.x -
x;
269 float dy = other.y -
y;
270 float angle = std::atan2(dy, dx) * 180.0f /
static_cast<
float>(M_PI);
271 return normalizeAngleDeg(angle);
278 float dz = other.z -
z;
279 float dxy = std::sqrt((other.x -
x) * (other.x -
x) +
280 (other.y -
y) * (other.y -
y));
281 float angle = std::atan2(dz, dxy) * 180.0f /
static_cast<
float>(M_PI);
282 return normalizeAngleDeg(angle);
287 auto dx =
x - other.x;
288 auto dy =
y - other.y;
289 auto dz =
z - other.z;
290 return std::sqrt(dx * dx + dy * dy + dz * dz);
299 std::size_t operator()(
const tinyrobotics::
Coordinate<T>& c)
const noexcept {
300 std::size_t h1 = std::hash<T>{}(c.x);
301 std::size_t h2 = std::hash<T>{}(c.y);
302 return h1 ^ (h2 << 1);
Represents an angle with unit conversion and wrap-around support.
Definition: Angle.h:42
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Coordinate navigate(DistanceM distanceM, float headingDegrees, float altDiffM=0) const
Definition: Coordinate.h:115
void operator+=(const Coordinate &other)
Add offset defined in other to current coordinate.
Definition: Coordinate.h:155
DistanceM distanceM(const Coordinate &other) const
Calculate Euclidean distance in meters to another coordinate.
Definition: Coordinate.h:286
Coordinate(const Coordinate &other)
Copy constructor.
Definition: Coordinate.h:63
T y
Y coordinate (meters)
Definition: Coordinate.h:73
std::string toString() const
Convert coordinate to string representation.
Definition: Coordinate.h:192
void operator=(const Coordinate &other)
Assign values from another coordinate.
Definition: Coordinate.h:169
Coordinate operator+(const Coordinate &other) const
Definition: Coordinate.h:145
void operator-=(const Coordinate &other)
Subtract offset in other from this one.
Definition: Coordinate.h:162
Coordinate operator-(const Coordinate &other) const
Calculate new coordinate by subtracting offset in other from this one.
Definition: Coordinate.h:150
bool equalsWithAltitude(const Coordinate &other, DistanceM limit, DistanceM altLimit) const
Definition: Coordinate.h:137
DistanceM altitudeDifference(const Coordinate &other) const
Definition: Coordinate.h:125
std::vector< Coordinate< T > > interpolateTo(const Coordinate< T > &target, Distance resolution)
Interpolate points between source and target with Distance resolution.
Definition: Coordinate.h:257
void setValues(T newX, T newY, T newZ=0)
Set coordinate values from numeric types.
Definition: Coordinate.h:218
Coordinate(Distance x, Distance y, Distance z=0)
Construct from Distance objects.
Definition: Coordinate.h:65
bool equals(const Coordinate &other, DistanceM limit) const
Definition: Coordinate.h:131
bool fromString(const std::string &str)
Parse coordinate from string representation.
Definition: Coordinate.h:199
T x
X coordinate (meters)
Definition: Coordinate.h:71
float bearing(const Coordinate &other, AngleUnit unit=AngleUnit::DEG) const
Definition: Coordinate.h:88
T z
Z coordinate (meters)
Definition: Coordinate.h:75
float elevation(const Coordinate &other, AngleUnit unit=AngleUnit::DEG) const
Calculate the elevation angle from this coordinate to another.
Definition: Coordinate.h:96
bool operator==(const Coordinate< T > &other) const
Equality operators for use in std::unordered_map and comparisons.
Definition: Coordinate.h:176
AngleDeg bearingDeg(const Coordinate &other) const
Calculate bearing in degrees to another coordinate.
Definition: Coordinate.h:267
bool operator<(const Coordinate &other) const
Lexicographical comparison for STL containers (priority_queue, set, etc.)
Definition: Coordinate.h:185
std::vector< Coordinate< T > > interpolateTo(const Coordinate< T > &target, T resolution)
Interpolate points between source and target with a defined resolution.
Definition: Coordinate.h:241
Coordinate navigate(Distance distance, Angle bearing, Distance altDiff=0) const
Definition: Coordinate.h:105
void setValues(Distance newX, Distance newY, Distance newZ=0)
Set coordinate values from Distance objects.
Definition: Coordinate.h:225
const char * getTypeName() const
Get the type name string.
Definition: Coordinate.h:263
DistanceM distance(const Coordinate &other, DistanceUnit unit=DistanceUnit::M) const
Definition: Coordinate.h:79
AngleDeg elevationDeg(const Coordinate &other) const
Calculate elevation angle in degrees to another coordinate.
Definition: Coordinate.h:277
Represents a distance measurement with unit conversion support.
Definition: Distance.h:40
This class defines an interface for serializable objects that can be converted to and from a string r...
Definition: Serializable.h:32
DistanceUnit
Supported distance units for conversion and representation.
Definition: Distance.h:10
AngleUnit
Supported angle units for conversion and representation.
Definition: Angle.h:11