1use nalgebra::{Matrix3, Matrix4, Point3, SVector, UnitQuaternion, Vector3};
2
3#[derive(Debug, Clone, PartialEq)]
10pub struct Sim3 {
11 pub rotation: UnitQuaternion<f64>,
12 pub translation: Vector3<f64>,
13 pub scale: f64,
15}
16
17pub type Sim3Tangent = SVector<f64, 7>;
20
21impl Sim3 {
22 pub fn identity() -> Self {
23 Self {
24 rotation: UnitQuaternion::identity(),
25 translation: Vector3::zeros(),
26 scale: 1.0,
27 }
28 }
29
30 pub const fn new(rotation: UnitQuaternion<f64>, translation: Vector3<f64>, scale: f64) -> Self {
31 Self {
32 rotation,
33 translation,
34 scale,
35 }
36 }
37
38 pub fn transform_point(&self, point: &Point3<f64>) -> Point3<f64> {
40 Point3::from(self.scale * self.rotation.transform_point(point).coords + self.translation)
41 }
42
43 pub fn compose(&self, other: &Sim3) -> Self {
46 Self {
47 rotation: self.rotation * other.rotation,
48 translation: self.scale * self.rotation.transform_vector(&other.translation)
49 + self.translation,
50 scale: self.scale * other.scale,
51 }
52 }
53
54 pub fn inverse(&self) -> Self {
56 let inverse_scale = 1.0 / self.scale;
57 let inverse_rotation = self.rotation.inverse();
58 Self {
59 rotation: inverse_rotation,
60 translation: -inverse_scale * inverse_rotation.transform_vector(&self.translation),
61 scale: inverse_scale,
62 }
63 }
64
65 pub fn matrix(&self) -> Matrix4<f64> {
67 let mut matrix = Matrix4::identity();
68 matrix
69 .fixed_view_mut::<3, 3>(0, 0)
70 .copy_from(&(self.scale * self.rotation.to_rotation_matrix().into_inner()));
71 matrix
72 .fixed_view_mut::<3, 1>(0, 3)
73 .copy_from(&self.translation);
74 matrix
75 }
76
77 pub fn exp(tangent: &Sim3Tangent) -> Self {
79 let rho = Vector3::new(tangent[0], tangent[1], tangent[2]);
80 let omega = Vector3::new(tangent[3], tangent[4], tangent[5]);
81 let sigma = tangent[6];
82 let rotation = UnitQuaternion::from_scaled_axis(omega);
83 let scale = sigma.exp();
84 let w = sim3_w_matrix(&omega, sigma);
85 Self {
86 rotation,
87 translation: w * rho,
88 scale,
89 }
90 }
91
92 pub fn log(&self) -> Sim3Tangent {
95 let omega = self.rotation.scaled_axis();
96 let sigma = self.scale.ln();
97 let w = sim3_w_matrix(&omega, sigma);
98 let rho = w
99 .try_inverse()
100 .map_or(self.translation, |w_inv| w_inv * self.translation);
101 let mut tangent = Sim3Tangent::zeros();
102 tangent[0] = rho.x;
103 tangent[1] = rho.y;
104 tangent[2] = rho.z;
105 tangent[3] = omega.x;
106 tangent[4] = omega.y;
107 tangent[5] = omega.z;
108 tangent[6] = sigma;
109 tangent
110 }
111}
112
113impl Default for Sim3 {
114 fn default() -> Self {
115 Self::identity()
116 }
117}
118
119fn skew(v: &Vector3<f64>) -> Matrix3<f64> {
121 Matrix3::new(0.0, -v.z, v.y, v.z, 0.0, -v.x, -v.y, v.x, 0.0)
122}
123
124fn expm1_over(sigma: f64) -> f64 {
127 if sigma.abs() < 1.0e-4 {
128 1.0 + sigma / 2.0 + sigma * sigma / 6.0 + sigma * sigma * sigma / 24.0
129 } else {
130 (sigma.exp() - 1.0) / sigma
131 }
132}
133
134fn sim3_w_matrix(omega: &Vector3<f64>, sigma: f64) -> Matrix3<f64> {
140 let theta_sq = omega.norm_squared();
141 let omega_skew = skew(omega);
142 let a = expm1_over(sigma);
143
144 let (b, c) = if theta_sq < 1.0e-10 {
145 if sigma.abs() < 1.0e-4 {
148 (
149 0.5 + sigma / 3.0 + sigma * sigma / 8.0,
150 1.0 / 6.0 + sigma / 8.0 + sigma * sigma / 20.0,
151 )
152 } else {
153 let s = sigma.exp();
154 let sigma_sq = sigma * sigma;
155 (
156 (s * (sigma - 1.0) + 1.0) / sigma_sq,
157 (s * (sigma_sq - 2.0 * sigma + 2.0) - 2.0) / (2.0 * sigma_sq * sigma),
158 )
159 }
160 } else {
161 let theta = theta_sq.sqrt();
164 let s = sigma.exp();
165 let denom = sigma * sigma + theta_sq;
166 let (sin_theta, cos_theta) = theta.sin_cos();
167 let b = (s * (sigma * sin_theta - theta * cos_theta) + theta) / (theta * denom);
168 let c = (a - (s * (sigma * cos_theta + theta * sin_theta) - sigma) / denom) / theta_sq;
169 (b, c)
170 };
171
172 Matrix3::identity() * a + b * omega_skew + c * omega_skew * omega_skew
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178
179 fn make_sim3(omega: Vector3<f64>, translation: Vector3<f64>, scale: f64) -> Sim3 {
180 Sim3::new(UnitQuaternion::from_scaled_axis(omega), translation, scale)
181 }
182
183 fn assert_sim3_close(a: &Sim3, b: &Sim3, eps: f64) {
184 let dt = (a.translation - b.translation).norm();
185 let dr = (a.rotation.to_rotation_matrix().into_inner()
186 - b.rotation.to_rotation_matrix().into_inner())
187 .norm();
188 let ds = (a.scale - b.scale).abs();
189 assert!(dt < eps, "translation diff {dt} >= {eps}");
190 assert!(dr < eps, "rotation diff {dr} >= {eps}");
191 assert!(ds < eps, "scale diff {ds} >= {eps}");
192 }
193
194 #[test]
195 fn transform_point_applies_scale_rotation_translation() {
196 let s = make_sim3(
197 Vector3::new(0.0, 0.0, std::f64::consts::FRAC_PI_2),
198 Vector3::new(1.0, 2.0, 3.0),
199 2.0,
200 );
201 let p = s.transform_point(&Point3::new(1.0, 0.0, 0.0));
203 assert!((p - Point3::new(1.0, 4.0, 3.0)).norm() < 1e-12, "{p:?}");
204 }
205
206 #[test]
207 fn compose_matches_matrix_product() {
208 let a = make_sim3(
209 Vector3::new(0.2, -0.1, 0.3),
210 Vector3::new(0.5, -0.2, 0.7),
211 1.5,
212 );
213 let b = make_sim3(
214 Vector3::new(-0.3, 0.4, 0.1),
215 Vector3::new(-1.0, 0.3, 0.2),
216 0.7,
217 );
218 let composed = a.compose(&b);
219 let matrix_product = a.matrix() * b.matrix();
220 assert!((composed.matrix() - matrix_product).norm() < 1e-12);
221 }
222
223 #[test]
224 fn inverse_composes_to_identity() {
225 let s = make_sim3(
226 Vector3::new(0.4, -0.3, 0.2),
227 Vector3::new(1.0, 0.5, -0.7),
228 2.5,
229 );
230 assert_sim3_close(&s.compose(&s.inverse()), &Sim3::identity(), 1e-12);
231 assert_sim3_close(&s.inverse().compose(&s), &Sim3::identity(), 1e-12);
232 }
233
234 #[test]
235 fn log_of_identity_is_zero() {
236 assert!(Sim3::identity().log().norm() < 1e-12);
237 }
238
239 #[test]
240 fn exp_log_round_trip_general() {
241 let s = make_sim3(
242 Vector3::new(0.3, -0.2, 0.5),
243 Vector3::new(1.0, -0.4, 2.1),
244 1.8,
245 );
246 assert_sim3_close(&Sim3::exp(&s.log()), &s, 1e-10);
247 }
248
249 #[test]
250 fn exp_log_round_trip_pure_scale() {
251 let s = make_sim3(Vector3::zeros(), Vector3::new(1.0, -0.4, 2.1), 3.0);
252 assert_sim3_close(&Sim3::exp(&s.log()), &s, 1e-10);
253 }
254
255 #[test]
256 fn exp_log_round_trip_no_scale_matches_se3_regime() {
257 let s = make_sim3(
259 Vector3::new(0.6, -0.1, 0.2),
260 Vector3::new(-0.5, 0.9, 0.3),
261 1.0,
262 );
263 assert_sim3_close(&Sim3::exp(&s.log()), &s, 1e-10);
264 }
265
266 #[test]
267 fn exp_log_round_trip_small_rotation() {
268 let s = make_sim3(
269 Vector3::new(1e-7, -2e-7, 3e-7),
270 Vector3::new(0.01, -0.02, 0.03),
271 1.4,
272 );
273 assert_sim3_close(&Sim3::exp(&s.log()), &s, 1e-10);
274 }
275
276 #[test]
277 fn exp_log_round_trip_small_rotation_and_small_scale() {
278 let s = make_sim3(
279 Vector3::new(1e-7, -2e-7, 3e-7),
280 Vector3::new(0.01, -0.02, 0.03),
281 (1e-6_f64).exp(),
282 );
283 assert_sim3_close(&Sim3::exp(&s.log()), &s, 1e-10);
284 }
285
286 #[test]
287 fn log_exp_round_trip_tangent() {
288 let mut xi = Sim3Tangent::zeros();
289 xi[0] = 0.5;
290 xi[1] = -0.3;
291 xi[2] = 0.8;
292 xi[3] = 0.1;
293 xi[4] = -0.2;
294 xi[5] = 0.4;
295 xi[6] = 0.25;
296 let xi2 = Sim3::exp(&xi).log();
297 assert!((xi - xi2).norm() < 1e-10, "{:?}", xi - xi2);
298 }
299
300 #[test]
301 fn exp_of_zero_is_identity() {
302 assert_sim3_close(&Sim3::exp(&Sim3Tangent::zeros()), &Sim3::identity(), 1e-12);
303 }
304}