Brick
Loading...
Searching...
No Matches
twist2d.h
1#pragma once
2
3#include "cevalm.hpp"
4#include "core/math/eigen_interface.h"
5#include "core/math/units.h"
6
7/**
8 * Class representing a difference between two poses,
9 * more specifically a distance along an arc from a pose.
10 * The angular displacement is continuous and is never wrapped.
11 * More specifically, dx and dy represent integrated motion in
12 * the moving reference frame of the robot.
13 *
14 * Assumes conventional cartesian coordinate system:
15 * Looking down at the coordinate plane,
16 * +X is right
17 * +Y is up
18 * +Theta is counterclockwise
19 */
20class Twist2d {
21 private:
22 units::Length dx_;
23 units::Length dy_;
24 units::Angle dtheta_;
25
26 public:
27 /// Default Constructor for Twist2d
28 constexpr Twist2d() : dx_(0), dy_(0), dtheta_(0) {}
29
30 /**
31 * Constructs a twist with given translation and angle deltas.
32 *
33 * @param dx the linear dx component.
34 * @param dy the linear dy component.
35 * @param dtheta the angular dtheta component.
36 */
37 constexpr Twist2d(units::Length dx, units::Length dy, units::Angle dtheta)
38 : dx_{dx}, dy_{dy}, dtheta_{dtheta} {}
39
40 /**
41 * Constructs a twist with given translation and angle deltas.
42 *
43 * @param twist_vector vector of the form [dx, dy, dtheta]
44 * @param length_unit unit for dx and dy.
45 * @param angle_unit unit for dtheta, defaults to radians.
46 */
47 constexpr Twist2d(
48 const Eigen::Vector3d& twist_vector, units::Length length_unit,
49 units::Angle angle_unit = units::radians
50 )
51 : dx_{twist_vector[0], length_unit},
52 dy_{twist_vector[1], length_unit},
53 dtheta_{twist_vector[2], angle_unit} {}
54
55 /// Gets the x displacement.
56 constexpr units::Length dx() const { return dx_; }
57
58 /// Gets the x displacement in the supplied unit.
59 constexpr double dx(units::Length unit) const { return dx_.to(unit); }
60
61 /// Sets the x displacement.
62 constexpr void set_dx(units::Length val) { dx_ = val; }
63
64 /// Gets the y displacement.
65 constexpr units::Length dy() const { return dy_; }
66
67 /// Gets the y displacement in the supplied unit.
68 constexpr double dy(units::Length unit) const { return dy_.to(unit); }
69
70 /// Sets the y displacement.
71 constexpr void set_dy(units::Length val) { dy_ = val; }
72
73 /// Gets the Angle displacement
74 constexpr units::Angle dtheta() const { return dtheta_; }
75
76 /// Gets the angle displacement in the supplied unit.
77 constexpr double dtheta(units::Angle unit) const { return dtheta_.to(unit); }
78
79 /// Sets the angle displacement.
80 constexpr void set_dtheta(units::Angle val) { dtheta_ = val; }
81
82 /**
83 * Returns [dx, dy, dtheta] in the supplied units. Angles default to radians.
84 *
85 * @param length_unit the unit of length to get the values as
86 * @param angle_unit the unit of angle to get the rotation as, default radians
87 * @return EVec<3> containing the values.
88 */
89 EVec<3> as_vector(units::Length length_unit, units::Angle angle_unit = units::radians) const {
90 return EVec<3>{dx_.to(length_unit), dy_.to(length_unit), dtheta_.to(angle_unit)};
91 }
92
93 /// Multiplies this twist by a scalar.
94 constexpr Twist2d operator*(double scalar) const {
95 return Twist2d{dx_ * scalar, dy_ * scalar, dtheta_ * scalar};
96 }
97
98 /// Multiplies a scalar by this twist.
99 friend constexpr Twist2d operator*(double scalar, const Twist2d& twist) { return twist * scalar; }
100
101 /// Multiplies this twist by a scalar.
102 constexpr Twist2d& operator*=(double scalar) { return *this = *this * scalar; }
103
104 /// Divides this twist by a scalar.
105 constexpr Twist2d operator/(double scalar) const { return *this * (1.0 / scalar); }
106
107 /// Divides this twist by a scalar.
108 constexpr Twist2d& operator/=(double scalar) { return *this = *this / scalar; }
109
110 /// Checks exact equality between this and another twist.
111 constexpr bool operator==(const Twist2d& other) const {
112 return dx_ == other.dx_ && dy_ == other.dy_ && dtheta_ == other.dtheta_;
113 }
114
115 /**
116 * Checks linear distance and unwrapped angle difference against tolerances.
117 * Defaults to 1um and 1e-6 radians.
118 */
119 constexpr bool is_near(
120 const Twist2d& other, units::Length distance_tolerance = units::Length(1e-6),
121 units::Angle angle_tolerance = units::Angle(1e-6)
122 ) const {
123 return units::hypot(dx_ - other.dx_, dy_ - other.dy_) <= distance_tolerance &&
124 units::abs(dtheta_ - other.dtheta_) <= angle_tolerance;
125 }
126};
constexpr Twist2d & operator*=(double scalar)
Multiplies this twist by a scalar.
Definition twist2d.h:102
constexpr Twist2d & operator/=(double scalar)
Divides this twist by a scalar.
Definition twist2d.h:108
constexpr void set_dtheta(units::Angle val)
Sets the angle displacement.
Definition twist2d.h:80
constexpr units::Length dx() const
Gets the x displacement.
Definition twist2d.h:56
constexpr double dtheta(units::Angle unit) const
Gets the angle displacement in the supplied unit.
Definition twist2d.h:77
constexpr double dx(units::Length unit) const
Gets the x displacement in the supplied unit.
Definition twist2d.h:59
constexpr Twist2d(const Eigen::Vector3d &twist_vector, units::Length length_unit, units::Angle angle_unit=units::radians)
Definition twist2d.h:47
constexpr units::Length dy() const
Gets the y displacement.
Definition twist2d.h:65
constexpr Twist2d()
Default Constructor for Twist2d.
Definition twist2d.h:28
constexpr Twist2d(units::Length dx, units::Length dy, units::Angle dtheta)
Definition twist2d.h:37
friend constexpr Twist2d operator*(double scalar, const Twist2d &twist)
Multiplies a scalar by this twist.
Definition twist2d.h:99
constexpr bool operator==(const Twist2d &other) const
Checks exact equality between this and another twist.
Definition twist2d.h:111
EVec< 3 > as_vector(units::Length length_unit, units::Angle angle_unit=units::radians) const
Definition twist2d.h:89
constexpr void set_dy(units::Length val)
Sets the y displacement.
Definition twist2d.h:71
constexpr Twist2d operator*(double scalar) const
Multiplies this twist by a scalar.
Definition twist2d.h:94
constexpr double dy(units::Length unit) const
Gets the y displacement in the supplied unit.
Definition twist2d.h:68
constexpr void set_dx(units::Length val)
Sets the x displacement.
Definition twist2d.h:62
constexpr units::Angle dtheta() const
Gets the Angle displacement.
Definition twist2d.h:74
constexpr Twist2d operator/(double scalar) const
Divides this twist by a scalar.
Definition twist2d.h:105
constexpr bool is_near(const Twist2d &other, units::Length distance_tolerance=units::Length(1e-6), units::Angle angle_tolerance=units::Angle(1e-6)) const
Definition twist2d.h:119