1
2
3
4
5
6
7
8
9
10
11
12
13
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::CartesianTreeError;
use crate::frame::{Frame, FrameData};
use crate::lazy_access::{LazyRotation, LazyTranslation};
use crate::rotation::Rotation;
use crate::tree::Walking;
use nalgebra::{Isometry3, Translation3, Vector3};
use std::cell::RefCell;
use std::ops::{Add, Mul, Sub};
use std::rc::Weak;
/// Use [`Frame::add_pose`] to create a new pose.
#[derive(Clone, Debug)]
pub struct Pose {
/// Reference to the parent frame.
parent: Weak<RefCell<FrameData>>,
/// Transformation from this frame to its parent frame.
transform_to_parent: Isometry3<f64>,
}
impl Pose {
/// Creates a new pose relative to a frame.
///
/// This function is intended for internal use. To create a pose associated with a frame,
/// use [`Frame::add_pose`], which handles the association safely.
pub(crate) fn new(
frame: Weak<RefCell<FrameData>>,
position: Vector3<f64>,
orientation: impl Into<Rotation>,
) -> Self {
Self {
parent: frame,
transform_to_parent: Isometry3::from_parts(
Translation3::from(position),
orientation.into().as_quaternion(),
),
}
}
/// Returns the parent frame of this pose.
///
/// # Returns
/// `Some(Frame)` if the parent frame is still valid, or `None` if the frame
/// has been dropped or no longer exists.
///
/// # Example
/// ```
/// use cartesian_tree::Frame;
/// use nalgebra::{Vector3, UnitQuaternion};
///
/// let frame = Frame::new_origin("base");
/// let pose = frame.add_pose(Vector3::new(0.0, 0.0, 0.0), UnitQuaternion::identity());
/// assert_eq!(pose.frame().unwrap().name(), "base");
/// ```
#[must_use]
pub fn frame(&self) -> Option<Frame> {
self.parent.upgrade().map(|data| Frame { data })
}
/// Returns the transformation from this pose to its parent frame.
///
/// # Returns
/// The transformation of the pose in its parent frame.
#[must_use]
pub const fn transformation(&self) -> Isometry3<f64> {
self.transform_to_parent
}
/// Returns the position of this pose relative to its parent frame.
/// # Returns
/// The position of the pose in its parent frame.
#[must_use]
pub const fn position(&self) -> Vector3<f64> {
self.transform_to_parent.translation.vector
}
/// Returns the orientation of this pose relative to its parent frame.
/// # Returns
/// The orientation of the pose in its parent frame.
#[must_use]
pub fn orientation(&self) -> Rotation {
self.transform_to_parent.rotation.into()
}
/// Sets the pose's transformation relative to its parent.
///
/// # Arguments
/// - `position`: A 3D vector representing the new translational offset from the parent.
/// - `orientation`: An orientation convertible into a unit quaternion for new orientational offset from the parent.
///
/// # Example
/// ```
/// use cartesian_tree::Frame;
/// use nalgebra::{Vector3, UnitQuaternion};
///
/// let root = Frame::new_origin("root");
/// let mut pose = root.add_pose(Vector3::new(0.0, 0.0, 1.0), UnitQuaternion::identity());
/// pose.set(Vector3::new(1.0, 0.0, 0.0), UnitQuaternion::identity());
/// ```
pub fn set(&mut self, position: Vector3<f64>, orientation: impl Into<Rotation>) {
self.transform_to_parent = Isometry3::from_parts(
Translation3::from(position),
orientation.into().as_quaternion(),
);
}
/// Applies the provided isometry interpreted in the parent frame to the pose.
///
/// # Arguments
/// - `isometry`: The isometry (describing a motion in the parent frame coordinates) to apply to the current transformation.
///
/// # Example
/// ```
/// use cartesian_tree::Frame;
/// use nalgebra::{Isometry3, Translation3, Vector3, UnitQuaternion};
///
/// let root = Frame::new_origin("root");
/// let mut pose = root.add_pose(Vector3::new(0.0, 0.0, 1.0), UnitQuaternion::identity());
/// pose.apply_in_parent_frame(&Isometry3::from_parts(Translation3::new(1.0, 0.0, 0.0), UnitQuaternion::identity()));
/// ```
pub fn apply_in_parent_frame(&mut self, isometry: &Isometry3<f64>) {
self.transform_to_parent = isometry * self.transform_to_parent;
}
/// Applies the provided isometry interpreted in the body frame to this pose.
///
/// # Arguments
/// - `isometry`: The isometry (describing a motion in the body frame coordinates) to apply to the current transformation.
///
/// # Example
/// ```
/// use cartesian_tree::Frame;
/// use nalgebra::{Isometry3, Translation3, Vector3, UnitQuaternion};
///
/// let root = Frame::new_origin("root");
/// let mut pose = root.add_pose(Vector3::new(0.0, 0.0, 1.0), UnitQuaternion::identity());
/// pose.apply_in_local_frame(&Isometry3::from_parts(Translation3::new(1.0, 0.0, 0.0), UnitQuaternion::identity()));
/// ```
pub fn apply_in_local_frame(&mut self, isometry: &Isometry3<f64>) {
self.transform_to_parent *= isometry;
}
/// Transforms this pose into the coordinate system of the given target frame.
///
/// # Arguments
/// * `target` - The frame to express this pose in.
///
/// # Returns
/// A new `Pose`, expressed in the `target` frame.
///
/// # Errors
/// Returns a [`CartesianTreeError`] if:
/// - The frame hierarchy cannot be resolved (e.g., due to dropped frames).
/// - There is no common ancestor between `self` and `target`.
///
/// # Example
/// ```
/// use cartesian_tree::Frame;
/// use nalgebra::{Vector3, UnitQuaternion};
///
/// let root = Frame::new_origin("root");
/// let pose = root.add_pose(Vector3::new(0.0, 0.0, 1.0), UnitQuaternion::identity());
/// let new_frame = root.add_child("child", Vector3::new(1.0, 0.0, 0.0), UnitQuaternion::identity()).unwrap();
/// let pose_in_new_frame = pose.in_frame(&new_frame);
/// ```
pub fn in_frame(&self, target: &Frame) -> Result<Self, CartesianTreeError> {
let source_data = self
.parent
.upgrade()
.ok_or(CartesianTreeError::WeakUpgradeFailed())?;
let source = Frame { data: source_data };
let ancestor = source
.lca_with(target)
.ok_or_else(|| CartesianTreeError::NoCommonAncestor(source.name(), target.name()))?;
// Transformation from source frame up to ancestor
let tf_up = source.walk_up_and_transform(&ancestor)? * self.transform_to_parent;
// Transformation from target frame up to ancestor (to be inverted)
let tf_down = target.walk_up_and_transform(&ancestor)?;
Ok(Self {
parent: target.downgrade(),
transform_to_parent: tf_down.inverse() * tf_up,
})
}
}
impl Add<LazyTranslation> for &Pose {
type Output = Pose;
fn add(self, rhs: LazyTranslation) -> Self::Output {
let parent = self.frame().unwrap();
let mut new_pose = parent.add_pose(
self.transform_to_parent.translation.vector,
self.transform_to_parent.rotation,
);
new_pose.apply_in_parent_frame(&rhs.inner);
new_pose
}
}
impl Sub<LazyTranslation> for &Pose {
type Output = Pose;
fn sub(self, rhs: LazyTranslation) -> Self::Output {
let parent = self.frame().unwrap();
let mut new_pose = parent.add_pose(
self.transform_to_parent.translation.vector,
self.transform_to_parent.rotation,
);
new_pose.apply_in_parent_frame(&rhs.inner.inverse());
new_pose
}
}
impl Mul<LazyRotation> for &Pose {
type Output = Pose;
fn mul(self, rhs: LazyRotation) -> Self::Output {
let parent = self.frame().unwrap();
let mut new_pose = parent.add_pose(
self.transform_to_parent.translation.vector,
self.transform_to_parent.rotation,
);
new_pose.apply_in_local_frame(&rhs.inner);
new_pose
}
}