apex-solver 1.2.1

High-performance nonlinear least squares optimization with Lie group support for SLAM and bundle adjustment
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Conversion utilities for Rerun visualization types.
//!
//! This module provides clean conversions from apex-solver's manifold types
//! (SE2, SE3, SO2, SO3, Rn) to Rerun's visualization types. These conversions
//! enable seamless integration with Rerun's real-time visualization system.
//!
//! # Design Philosophy
//!
//! We use extension traits to provide ergonomic conversion methods while
//! respecting Rust's orphan rule. This approach provides:
//! - Clear, self-documenting method names
//! - Better IDE autocomplete and discoverability
//! - Type-safe conversions with zero runtime overhead
//! - Consistent color schemes (red/green/blue for x/y/z axes)
//!
//! # Examples
//!
//! ## Single Pose Conversion
//!
//! ```no_run
//! # #[cfg(feature = "visualization")]
//! # {
//! use apex_solver::manifold::se3::SE3;
//! use apex_solver::observers::ToRerunTransform3D;
//!
//! let pose = SE3::identity();
//! let transform = pose.to_rerun_transform();
//! # }
//! ```
//!
//! ## Batch Conversion
//!
//! ```no_run
//! # #[cfg(feature = "visualization")]
//! # {
//! use apex_solver::manifold::se3::SE3;
//! use apex_solver::observers::CollectRerunPoints3D;
//!
//! let poses = vec![SE3::identity(), SE3::identity()];
//! let points = poses.iter().collect_points3d();
//! # }
//! ```

#[cfg(feature = "visualization")]
use apex_manifolds::{se2::SE2, se3::SE3};
#[cfg(feature = "visualization")]
use rerun::{Arrows2D, Arrows3D, Points2D, Points3D, Transform3D, Vec2D, Vec3D};

// ============================================================================
// Extension Traits for SE3 → Rerun Conversions
// ============================================================================

/// Convert SE3 to Rerun Transform3D (translation + rotation).
#[cfg(feature = "visualization")]
pub trait ToRerunTransform3D {
    /// Convert this SE3 pose to a Rerun Transform3D.
    ///
    /// # Returns
    /// Transform3D with translation and rotation from the SE3 pose.
    fn to_rerun_transform(&self) -> Transform3D;
}

/// Convert SE3 to Rerun Vec3D (translation only).
#[cfg(feature = "visualization")]
pub trait ToRerunVec3D {
    /// Extract the translation component as a Rerun Vec3D.
    ///
    /// # Returns
    /// Vec3D containing only the translation (x, y, z) of the SE3 pose.
    fn to_rerun_vec3d(&self) -> Vec3D;
}

/// Convert SE3 to Rerun Points3D (single point at translation).
#[cfg(feature = "visualization")]
pub trait ToRerunPoints3D {
    /// Convert this SE3 pose to a single point in Rerun Points3D format.
    ///
    /// # Returns
    /// Points3D containing one point at the pose's translation.
    fn to_rerun_points3d(&self) -> Points3D;
}

/// Convert SE3 to Rerun Arrows3D (pose with orientation axes).
#[cfg(feature = "visualization")]
pub trait ToRerunArrows3D {
    /// Convert this SE3 pose to coordinate frame arrows in Rerun.
    ///
    /// Creates three arrows (X=red, Y=green, Z=blue) showing the pose's
    /// orientation, rooted at the pose's translation.
    ///
    /// # Returns
    /// Arrows3D showing the three basis vectors of the pose's rotation.
    fn to_rerun_arrows3d(&self) -> Arrows3D;
}

// ============================================================================
// Extension Traits for SE2 → Rerun Conversions
// ============================================================================

/// Convert SE2 to Rerun Vec2D (translation only).
#[cfg(feature = "visualization")]
pub trait ToRerunVec2D {
    /// Extract the 2D translation component as a Rerun Vec2D.
    ///
    /// # Returns
    /// Vec2D containing the (x, y) translation of the SE2 pose.
    fn to_rerun_vec2d(&self) -> Vec2D;
}

/// Convert SE2 to Rerun Points2D (single point at translation).
#[cfg(feature = "visualization")]
pub trait ToRerunPoints2D {
    /// Convert this SE2 pose to a single point in Rerun Points2D format.
    ///
    /// # Returns
    /// Points2D containing one point at the pose's translation.
    fn to_rerun_points2d(&self) -> Points2D;
}

/// Convert SE2 to Rerun Arrows2D (pose with orientation axes).
#[cfg(feature = "visualization")]
pub trait ToRerunArrows2D {
    /// Convert this SE2 pose to coordinate frame arrows in Rerun.
    ///
    /// Creates two arrows (X=red, Y=green) showing the pose's orientation,
    /// rooted at the pose's translation.
    ///
    /// # Returns
    /// Arrows2D showing the two basis vectors of the pose's rotation.
    fn to_rerun_arrows2d(&self) -> Arrows2D;
}

/// Convert SE2 to Rerun Transform3D (places SE2 at z=0 plane).
#[cfg(feature = "visualization")]
pub trait ToRerunTransform3DFrom2D {
    /// Convert this SE2 pose to a 3D transform at the z=0 plane.
    ///
    /// Useful for visualizing 2D poses in a 3D viewer. The rotation is
    /// converted to a rotation around the Z-axis.
    ///
    /// # Returns
    /// Transform3D with (x, y, 0) translation and rotation around Z-axis.
    fn to_rerun_transform_3d(&self) -> Transform3D;
}

// ============================================================================
// Extension Traits for Batch Conversions (Iterator Extensions)
// ============================================================================

/// Collect an iterator of SE3 poses into Rerun Points3D.
#[cfg(feature = "visualization")]
pub trait CollectRerunPoints3D<'a> {
    /// Collect SE3 poses into a Points3D cloud (translation components only).
    ///
    /// # Returns
    /// Points3D containing one point for each SE3 pose in the iterator.
    fn collect_points3d(self) -> Points3D;
}

/// Collect an iterator of SE3 poses into Rerun Arrows3D.
#[cfg(feature = "visualization")]
pub trait CollectRerunArrows3D<'a> {
    /// Collect SE3 poses into coordinate frame arrows.
    ///
    /// Creates three arrows (X=red, Y=green, Z=blue) for each pose.
    ///
    /// # Returns
    /// Arrows3D showing orientation axes for all poses in the iterator.
    fn collect_arrows3d(self) -> Arrows3D;
}

/// Collect an iterator of SE2 poses into Rerun Points2D.
#[cfg(feature = "visualization")]
pub trait CollectRerunPoints2D<'a> {
    /// Collect SE2 poses into a Points2D cloud (translation components only).
    ///
    /// # Returns
    /// Points2D containing one point for each SE2 pose in the iterator.
    fn collect_points2d(self) -> Points2D;
}

/// Collect an iterator of SE2 poses into Rerun Arrows2D.
#[cfg(feature = "visualization")]
pub trait CollectRerunArrows2D<'a> {
    /// Collect SE2 poses into coordinate frame arrows.
    ///
    /// Creates two arrows (X=red, Y=green) for each pose.
    ///
    /// # Returns
    /// Arrows2D showing orientation axes for all poses in the iterator.
    fn collect_arrows2d(self) -> Arrows2D;
}

// ============================================================================
// SE3 Conversions Implementation
// ============================================================================

#[cfg(feature = "visualization")]
impl ToRerunTransform3D for SE3 {
    fn to_rerun_transform(&self) -> Transform3D {
        let trans = self.translation();
        let rot = self.rotation_quaternion();

        let position =
            rerun::external::glam::Vec3::new(trans.x as f32, trans.y as f32, trans.z as f32);

        let rotation = rerun::external::glam::Quat::from_xyzw(
            rot.as_ref().i as f32,
            rot.as_ref().j as f32,
            rot.as_ref().k as f32,
            rot.as_ref().w as f32,
        );

        Transform3D::from_translation_rotation(position, rotation)
    }
}

#[cfg(feature = "visualization")]
impl ToRerunVec3D for SE3 {
    fn to_rerun_vec3d(&self) -> Vec3D {
        let trans = self.translation();
        Vec3D::new(trans.x as f32, trans.y as f32, trans.z as f32)
    }
}

#[cfg(feature = "visualization")]
impl ToRerunPoints3D for SE3 {
    fn to_rerun_points3d(&self) -> Points3D {
        let vec = self.to_rerun_vec3d();
        Points3D::new([vec])
    }
}

#[cfg(feature = "visualization")]
impl ToRerunArrows3D for SE3 {
    fn to_rerun_arrows3d(&self) -> Arrows3D {
        let rot_quat = self.rotation_quaternion();
        let rot_mat = rot_quat.to_rotation_matrix();
        let trans = self.translation();

        // Extract basis vectors (columns of rotation matrix)
        let x_axis = [
            rot_mat[(0, 0)] as f32,
            rot_mat[(1, 0)] as f32,
            rot_mat[(2, 0)] as f32,
        ];
        let y_axis = [
            rot_mat[(0, 1)] as f32,
            rot_mat[(1, 1)] as f32,
            rot_mat[(2, 1)] as f32,
        ];
        let z_axis = [
            rot_mat[(0, 2)] as f32,
            rot_mat[(1, 2)] as f32,
            rot_mat[(2, 2)] as f32,
        ];

        let origin = [trans.x as f32, trans.y as f32, trans.z as f32];

        Arrows3D::from_vectors([x_axis, y_axis, z_axis])
            .with_origins([origin, origin, origin])
            .with_colors([[255, 0, 0], [0, 255, 0], [0, 0, 255]]) // RGB for XYZ
    }
}

/// Collect multiple SE3 poses into a Points3D (just translation components)
#[cfg(feature = "visualization")]
impl<'a, I> CollectRerunPoints3D<'a> for I
where
    I: Iterator<Item = &'a SE3>,
{
    fn collect_points3d(self) -> Points3D {
        let points: Vec<Vec3D> = self.map(|se3| se3.to_rerun_vec3d()).collect();
        Points3D::new(points)
    }
}

/// Collect multiple SE3 poses into Arrows3D (translation + orientation axes)
#[cfg(feature = "visualization")]
impl<'a, I> CollectRerunArrows3D<'a> for I
where
    I: Iterator<Item = &'a SE3>,
{
    fn collect_arrows3d(self) -> Arrows3D {
        let mut vectors = Vec::new();
        let mut origins = Vec::new();
        let mut colors = Vec::new();

        for se3 in self {
            let rot_quat = se3.rotation_quaternion();
            let rot_mat = rot_quat.to_rotation_matrix();
            let trans = se3.translation();

            let x_axis = [
                rot_mat[(0, 0)] as f32,
                rot_mat[(1, 0)] as f32,
                rot_mat[(2, 0)] as f32,
            ];
            let y_axis = [
                rot_mat[(0, 1)] as f32,
                rot_mat[(1, 1)] as f32,
                rot_mat[(2, 1)] as f32,
            ];
            let z_axis = [
                rot_mat[(0, 2)] as f32,
                rot_mat[(1, 2)] as f32,
                rot_mat[(2, 2)] as f32,
            ];

            let origin = [trans.x as f32, trans.y as f32, trans.z as f32];

            vectors.push(x_axis);
            vectors.push(y_axis);
            vectors.push(z_axis);
            origins.push(origin);
            origins.push(origin);
            origins.push(origin);
            colors.push([255, 0, 0]); // X = red
            colors.push([0, 255, 0]); // Y = green
            colors.push([0, 0, 255]); // Z = blue
        }

        Arrows3D::from_vectors(vectors)
            .with_origins(origins)
            .with_colors(colors)
    }
}

// ============================================================================
// SE2 Conversions Implementation
// ============================================================================

#[cfg(feature = "visualization")]
impl ToRerunVec2D for SE2 {
    fn to_rerun_vec2d(&self) -> Vec2D {
        Vec2D::new(self.x() as f32, self.y() as f32)
    }
}

#[cfg(feature = "visualization")]
impl ToRerunPoints2D for SE2 {
    fn to_rerun_points2d(&self) -> Points2D {
        let vec = self.to_rerun_vec2d();
        Points2D::new([vec])
    }
}

#[cfg(feature = "visualization")]
impl ToRerunArrows2D for SE2 {
    fn to_rerun_arrows2d(&self) -> Arrows2D {
        let rot_mat = self.rotation_matrix();

        let x_axis = [rot_mat[(0, 0)] as f32, rot_mat[(1, 0)] as f32];
        let y_axis = [rot_mat[(0, 1)] as f32, rot_mat[(1, 1)] as f32];

        let origin = [self.x() as f32, self.y() as f32];

        Arrows2D::from_vectors([x_axis, y_axis])
            .with_origins([origin, origin])
            .with_colors([[255, 0, 0], [0, 255, 0]]) // Red/Green for X/Y
    }
}

/// Convert SE2 to 3D transform (places at z=0 plane)
#[cfg(feature = "visualization")]
impl ToRerunTransform3DFrom2D for SE2 {
    fn to_rerun_transform_3d(&self) -> Transform3D {
        let position = rerun::external::glam::Vec3::new(self.x() as f32, self.y() as f32, 0.0);

        // Create quaternion from 2D rotation (rotation around Z-axis)
        let angle = self.angle();
        let half_angle = (angle / 2.0) as f32;
        let rotation =
            rerun::external::glam::Quat::from_xyzw(0.0, 0.0, half_angle.sin(), half_angle.cos());

        Transform3D::from_translation_rotation(position, rotation)
    }
}

/// Collect multiple SE2 poses into Points2D
#[cfg(feature = "visualization")]
impl<'a, I> CollectRerunPoints2D<'a> for I
where
    I: Iterator<Item = &'a SE2>,
{
    fn collect_points2d(self) -> Points2D {
        let points: Vec<Vec2D> = self.map(|se2| se2.to_rerun_vec2d()).collect();
        Points2D::new(points)
    }
}

/// Collect multiple SE2 poses into Arrows2D (position + orientation)
#[cfg(feature = "visualization")]
impl<'a, I> CollectRerunArrows2D<'a> for I
where
    I: Iterator<Item = &'a SE2>,
{
    fn collect_arrows2d(self) -> Arrows2D {
        let mut vectors = Vec::new();
        let mut origins = Vec::new();
        let mut colors = Vec::new();

        for se2 in self {
            let rot_mat = se2.rotation_matrix();

            let x_axis = [rot_mat[(0, 0)] as f32, rot_mat[(1, 0)] as f32];
            let y_axis = [rot_mat[(0, 1)] as f32, rot_mat[(1, 1)] as f32];

            let origin = [se2.x() as f32, se2.y() as f32];

            vectors.push(x_axis);
            vectors.push(y_axis);
            origins.push(origin);
            origins.push(origin);
            colors.push([255, 0, 0]); // X = red
            colors.push([0, 255, 0]); // Y = green
        }

        Arrows2D::from_vectors(vectors)
            .with_origins(origins)
            .with_colors(colors)
    }
}

#[cfg(test)]
#[cfg(feature = "visualization")]
mod tests {
    use super::*;

    #[test]
    fn test_se3_to_vec3d() {
        use apex_manifolds::se3::SE3;

        let pose = SE3::identity();
        let vec = pose.to_rerun_vec3d();

        assert_eq!(vec.x(), 0.0);
        assert_eq!(vec.y(), 0.0);
        assert_eq!(vec.z(), 0.0);
    }

    #[test]
    fn test_se2_to_vec2d() {
        use apex_manifolds::se2::SE2;

        let pose = SE2::identity();
        let vec = pose.to_rerun_vec2d();

        assert_eq!(vec.x(), 0.0);
        assert_eq!(vec.y(), 0.0);
    }

    #[test]
    fn test_se3_collection_to_points() {
        use apex_manifolds::se3::SE3;

        let poses = [SE3::identity(), SE3::identity(), SE3::identity()];
        let points = poses.iter().collect_points3d();

        // Should create Points3D with 3 points
        // (Rerun API doesn't expose count directly, so just verify it compiles)
        let _ = points;
    }

    #[test]
    fn test_se2_collection_to_arrows() {
        use apex_manifolds::se2::SE2;

        let poses = [SE2::identity(), SE2::identity()];
        let arrows = poses.iter().collect_arrows2d();

        // Should create Arrows2D with orientation vectors
        let _ = arrows;
    }
}