braid_mvg/extrinsics.rs
1// Copyright (C) The Strand-Braid Authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use nalgebra as na;
5use nalgebra::RealField;
6use nalgebra::core::Vector3;
7use nalgebra::geometry::{Point3, UnitQuaternion};
8
9use cam_geom::ExtrinsicParameters;
10
11/// Creates default extrinsic parameters for testing and prototyping.
12///
13/// This function generates reasonable default extrinsic parameters that place
14/// the camera at a specific location with no rotation. The defaults are:
15///
16/// - **Camera center**: (1, 2, 3) in world coordinates
17/// - **Rotation**: Identity (no rotation from world coordinate system)
18///
19/// These parameters are useful for algorithm testing, unit tests, and as
20/// starting points for calibration procedures.
21///
22/// # ⚠️ Important Note
23///
24/// These parameters are **not suitable for real applications** - always perform
25/// proper camera calibration for production use. The default position is arbitrary
26/// and chosen only for testing convenience.
27///
28/// # Returns
29///
30/// [`ExtrinsicParameters`] with the default camera position and orientation.
31///
32/// # Example
33///
34/// ```rust
35/// use braid_mvg::extrinsics::make_default_extrinsics;
36///
37/// let extrinsics = make_default_extrinsics::<f64>();
38/// println!("Default camera center: {:?}", extrinsics.camcenter());
39/// ```
40pub fn make_default_extrinsics<R: RealField + Copy>() -> ExtrinsicParameters<R> {
41 let axis = na::core::Unit::new_normalize(Vector3::x());
42 let angle = na::convert(0.0);
43 let rquat = UnitQuaternion::from_axis_angle(&axis, angle);
44
45 let camcenter = Point3::new(na::convert(1.0), na::convert(2.0), na::convert(3.0));
46 ExtrinsicParameters::from_rotation_and_camcenter(rquat, camcenter)
47}
48
49/// Creates extrinsic parameters from a rotation quaternion and translation vector.
50///
51/// This function constructs camera extrinsic parameters from a rotation represented
52/// as a unit quaternion and a translation vector. This is a common parameterization
53/// used in robotics and SLAM applications.
54///
55/// # Mathematical Details
56///
57/// The relationship between camera center `C` and translation vector `t` is:
58/// ```text
59/// t = -R * C
60/// C = -R^T * t
61/// ```
62/// where `R` is the rotation matrix corresponding to `rquat`.
63///
64/// # Arguments
65///
66/// * `rquat` - Unit quaternion representing the camera rotation
67/// * `translation` - Translation vector from world origin to camera position
68///
69/// # Returns
70///
71/// [`ExtrinsicParameters`] constructed from the rotation and translation
72///
73/// # Example
74///
75/// ```rust
76/// use braid_mvg::extrinsics::from_rquat_translation;
77/// use nalgebra::{UnitQuaternion, Point3, Vector3};
78///
79/// let rotation = UnitQuaternion::from_axis_angle(&Vector3::z_axis(), 0.5);
80/// let translation = Point3::new(1.0, 2.0, 3.0);
81///
82/// let extrinsics = from_rquat_translation(rotation, translation);
83/// ```
84pub fn from_rquat_translation<R: RealField + Copy>(
85 rquat: UnitQuaternion<R>,
86 translation: Point3<R>,
87) -> ExtrinsicParameters<R> {
88 let camcenter = -(rquat.inverse() * translation);
89 ExtrinsicParameters::from_rotation_and_camcenter(rquat, camcenter)
90}
91
92#[cfg(test)]
93mod tests {
94 use cam_geom::ExtrinsicParameters;
95 use na::Vector3;
96 use na::geometry::Point3;
97 use nalgebra as na;
98
99 #[test]
100 fn test_from_view() {
101 let cc = Vector3::new(0.1, 2.3, 4.5);
102 let lookdir = Vector3::new(1.0, 0.0, 0.0);
103 let lookat = cc + lookdir;
104 let up = Vector3::new(0.0, 0.0, 1.0);
105 let up_unit = na::core::Unit::new_normalize(up);
106
107 let extrinsics1 = ExtrinsicParameters::from_view(&cc, &lookat, &up_unit);
108 let extrinsics2 = ExtrinsicParameters::from_pose(extrinsics1.pose());
109
110 // We have a right-handed system but use `look_at_lh` have +z axis being
111 // forward. We flip the up axis to keep our right-handedness.
112 let pose = nalgebra::Isometry3::look_at_lh(
113 &Point3 { coords: cc },
114 &Point3 { coords: lookat },
115 &-up_unit,
116 );
117 let extrinsics3 = ExtrinsicParameters::from_pose(&pose);
118
119 // println!("extrinsics1 {:?}", extrinsics1);
120 // println!("extrinsics2 {:?}", extrinsics2);
121 // println!("extrinsics3 {:?}", extrinsics3);
122
123 for extrinsics in &[extrinsics1, extrinsics2, extrinsics3] {
124 // println!("{:?} {}:{}", extrinsics, file!(), line!());
125 let iso = extrinsics.pose();
126 approx::assert_relative_eq!(
127 iso * Point3 { coords: cc },
128 Point3::origin(),
129 epsilon = 1e-10
130 );
131
132 let zero = na::convert(0.0);
133 let one = na::convert(1.0);
134 let forward_cam: cam_geom::Points<cam_geom::CameraFrame, _, _, _> =
135 cam_geom::Points::new(na::Matrix1x3::new(zero, zero, one));
136 let lookat_actual1: crate::PointWorldFrame<_> =
137 extrinsics.camera_to_world(&forward_cam).into();
138 let lookat_actual = lookat_actual1.coords.coords;
139 let lookdir_actual = lookat_actual - cc;
140
141 let up_cam: cam_geom::Points<cam_geom::CameraFrame, _, _, _> =
142 cam_geom::Points::new(na::Matrix1x3::new(zero, -one, zero));
143
144 let up_actual1: crate::PointWorldFrame<_> = extrinsics.camera_to_world(&up_cam).into();
145 let up_actual = up_actual1.coords.coords - cc;
146
147 approx::assert_relative_eq!(lookat, lookat_actual, epsilon = 1e-10);
148
149 approx::assert_relative_eq!(lookdir, lookdir_actual, epsilon = 1e-10);
150
151 approx::assert_relative_eq!(up, up_actual, epsilon = 1e-10);
152 }
153 }
154
155 #[test]
156 fn test_serde() {
157 let expected = crate::extrinsics::make_default_extrinsics();
158 let buf = serde_json::to_string(&expected).unwrap();
159 let actual: crate::ExtrinsicParameters<f64> = serde_json::from_str(&buf).unwrap();
160 assert!(expected == actual);
161 }
162}