Skip to content

Commit

Permalink
some minor clean-ups / corrections in nominclatures (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
strasdat authored Feb 2, 2025
1 parent 7f185c7 commit eb4088e
Show file tree
Hide file tree
Showing 46 changed files with 529 additions and 504 deletions.
2 changes: 1 addition & 1 deletion crates/sophus/examples/viewer_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn create_tiny_image_view_packet() -> Packet {
}

fn create_scene(pinhole: bool) -> Vec<Packet> {
let unified_cam = DynCameraF64::new_unified(
let unified_cam = DynCameraF64::new_enhanced_unified(
VecF64::from_array([500.0, 500.0, 320.0, 240.0, 0.629, 1.02]),
ImageSize::new(639, 479),
);
Expand Down
2 changes: 1 addition & 1 deletion crates/sophus/src/examples/viewer_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn make_distorted_frame() -> ImageFrame {
let cx = 320.0;
let cy = 240.0;

let unified_cam = DynCameraF64::new_unified(
let unified_cam = DynCameraF64::new_enhanced_unified(
VecF64::from_array([focal_length, focal_length, cx, cy, 0.629, 1.22]),
image_size,
);
Expand Down
18 changes: 9 additions & 9 deletions crates/sophus_autodiff/src/dual/dual_batch_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ where
}

impl<const ROWS: usize, const COLS: usize, const BATCH: usize>
IsScalarFieldDualMatrix<DualBatchScalar<BATCH, 1, 1>, ROWS, COLS, BATCH>
IsDualMatrixFromCurve<DualBatchScalar<BATCH, 1, 1>, ROWS, COLS, BATCH>
for DualBatchMatrix<ROWS, COLS, BATCH, 1, 1>
where
BatchScalarF64<BATCH>: IsCoreScalar,
LaneCount<BATCH>: SupportedLaneCount,
{
fn scalarfield_derivative(&self) -> BatchMatF64<ROWS, COLS, BATCH> {
fn curve_derivative(&self) -> BatchMatF64<ROWS, COLS, BATCH> {
let mut out = BatchMatF64::<ROWS, COLS, BATCH>::zeros();

for i in 0..ROWS {
for j in 0..COLS {
out.set_elem([i, j], self.inner[(i, j)].derivative()[(0, 0)]);
*out.elem_mut([i, j]) = self.inner[(i, j)].derivative()[(0, 0)];
}
}
out
Expand Down Expand Up @@ -240,10 +240,14 @@ where
DualBatchMatrix::from_real_matrix(BatchMatF64::<ROWS, COLS, BATCH>::identity())
}

fn get_elem(&self, idx: [usize; 2]) -> DualBatchScalar<BATCH, DM, DN> {
fn elem(&self, idx: [usize; 2]) -> DualBatchScalar<BATCH, DM, DN> {
self.inner[(idx[0], idx[1])]
}

fn elem_mut(&mut self, idx: [usize; 2]) -> &mut DualBatchScalar<BATCH, DM, DN> {
&mut self.inner[(idx[0], idx[1])]
}

fn from_array2<A>(duals: A) -> Self
where
A: Borrow<[[DualBatchScalar<BATCH, DM, DN>; COLS]; ROWS]>,
Expand Down Expand Up @@ -405,17 +409,13 @@ where
let other = other.borrow();
for i in 0..ROWS {
for j in 0..COLS {
v[(i, j)] = self.get_elem([i, j]).select(mask, other.get_elem([i, j]));
v[(i, j)] = self.elem([i, j]).select(mask, other.elem([i, j]));
}
}

Self { inner: v }
}

fn set_elem(&mut self, idx: [usize; 2], val: DualBatchScalar<BATCH, DM, DN>) {
self.inner[(idx[0], idx[1])] = val;
}

fn transposed(
&self,
) -> <DualBatchScalar<BATCH, DM, DN> as IsScalar<BATCH, DM, DN>>::Matrix<COLS, ROWS> {
Expand Down
11 changes: 11 additions & 0 deletions crates/sophus_autodiff/src/dual/dual_batch_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ where
}
}

impl<const BATCH: usize> IsDualScalarFromCurve<DualBatchScalar<BATCH, 1, 1>, BATCH>
for DualBatchScalar<BATCH, 1, 1>
where
BatchScalarF64<BATCH>: IsCoreScalar,
LaneCount<BATCH>: SupportedLaneCount,
{
fn curve_derivative(&self) -> BatchScalarF64<BATCH> {
self.derivative()[0]
}
}

impl<const BATCH: usize, const DM: usize, const DN: usize> DualBatchScalar<BATCH, DM, DN>
where
BatchScalarF64<BATCH>: IsCoreScalar,
Expand Down
37 changes: 24 additions & 13 deletions crates/sophus_autodiff/src/dual/dual_batch_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ where
}
}

impl<const ROWS: usize, const BATCH: usize>
IsDualVectorFromCurve<DualBatchScalar<BATCH, 1, 1>, ROWS, BATCH>
for DualBatchVector<ROWS, BATCH, 1, 1>
where
BatchScalarF64<BATCH>: IsCoreScalar,
LaneCount<BATCH>: SupportedLaneCount,
{
fn curve_derivative(&self) -> BatchVecF64<ROWS, BATCH> {
self.jacobian()
}
}

impl<const ROWS: usize, const BATCH: usize, const DM: usize>
HasJacobian<DualBatchScalar<BATCH, DM, 1>, ROWS, BATCH, DM>
for DualBatchVector<ROWS, BATCH, DM, 1>
Expand Down Expand Up @@ -218,17 +230,21 @@ where
}

fn norm(&self) -> DualBatchScalar<BATCH, DM, DN> {
self.clone().dot(*self).sqrt()
self.dot(*self).sqrt()
}

fn squared_norm(&self) -> DualBatchScalar<BATCH, DM, DN> {
self.clone().dot(*self)
self.dot(*self)
}

fn get_elem(&self, idx: usize) -> DualBatchScalar<BATCH, DM, DN> {
fn elem(&self, idx: usize) -> DualBatchScalar<BATCH, DM, DN> {
self.inner[idx]
}

fn elem_mut(&mut self, idx: usize) -> &mut DualBatchScalar<BATCH, DM, DN> {
&mut self.inner[idx]
}

fn from_array<A>(duals: A) -> Self
where
A: Borrow<[DualBatchScalar<BATCH, DM, DN>; ROWS]>,
Expand Down Expand Up @@ -269,7 +285,7 @@ where
fn real_vector(&self) -> BatchVecF64<ROWS, BATCH> {
let mut r = BatchVecF64::<ROWS, BATCH>::zeros();
for i in 0..ROWS {
r[i] = self.get_elem(i).real_part;
r[i] = self.elem(i).real_part;
}
r
}
Expand Down Expand Up @@ -312,15 +328,14 @@ where
let mut sum = <DualBatchScalar<BATCH, DM, DN>>::from_f64(0.0);

for i in 0..ROWS {
sum += self.get_elem(i) * rhs.borrow().get_elem(i);
sum += self.elem(i) * rhs.borrow().elem(i);
}

sum
}

fn normalized(&self) -> Self {
self.clone()
.scaled(<DualBatchScalar<BATCH, DM, DN>>::from_f64(1.0) / self.norm())
self.scaled(<DualBatchScalar<BATCH, DM, DN>>::from_f64(1.0) / self.norm())
}

fn from_f64_array<A>(vals: A) -> Self
Expand All @@ -345,10 +360,6 @@ where
}
}

fn set_elem(&mut self, idx: usize, v: DualBatchScalar<BATCH, DM, DN>) {
self.inner[idx] = v;
}

fn to_dual_const<const M: usize, const N: usize>(
&self,
) -> <DualBatchScalar<BATCH, DM, DN> as IsScalar<BATCH, DM, DN>>::DualVector<ROWS, M, N> {
Expand All @@ -365,7 +376,7 @@ where
let mut out = DualBatchMatrix::<ROWS, R2, BATCH, DM, DN>::zeros();
for i in 0..ROWS {
for j in 0..R2 {
out.set_elem([i, j], self.get_elem(i) * rhs.borrow().get_elem(j));
*out.elem_mut([i, j]) = self.elem(i) * rhs.borrow().elem(j);
}
}
out
Expand All @@ -378,7 +389,7 @@ where
let mut v = SVec::<DualBatchScalar<BATCH, DM, DN>, ROWS>::zeros();
let other = other.borrow();
for i in 0..ROWS {
v[i] = self.get_elem(i).select(mask, other.get_elem(i));
v[i] = self.elem(i).select(mask, other.elem(i));
}

Self { inner: v }
Expand Down
18 changes: 9 additions & 9 deletions crates/sophus_autodiff/src/dual/dual_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use num_traits::Zero;
use super::matrix::MatrixValuedDerivative;
use crate::{
dual::{
matrix::IsScalarFieldDualMatrix,
matrix::IsDualMatrixFromCurve,
DualScalar,
DualVector,
},
Expand Down Expand Up @@ -79,15 +79,15 @@ impl<const ROWS: usize, const COLS: usize, const DM: usize, const DN: usize>
}
}

impl<const ROWS: usize, const COLS: usize> IsScalarFieldDualMatrix<DualScalar<1, 1>, ROWS, COLS, 1>
impl<const ROWS: usize, const COLS: usize> IsDualMatrixFromCurve<DualScalar<1, 1>, ROWS, COLS, 1>
for DualMatrix<ROWS, COLS, 1, 1>
{
fn scalarfield_derivative(&self) -> MatF64<ROWS, COLS> {
fn curve_derivative(&self) -> MatF64<ROWS, COLS> {
let mut out = MatF64::<ROWS, COLS>::zeros();

for i in 0..ROWS {
for j in 0..COLS {
out.set_elem([i, j], self.inner[(i, j)].derivative()[(0, 0)]);
*out.elem_mut([i, j]) = self.inner[(i, j)].derivative()[(0, 0)];
}
}
out
Expand Down Expand Up @@ -193,10 +193,14 @@ impl<const ROWS: usize, const COLS: usize, const DM: usize, const DN: usize>
DualMatrix::from_real_matrix(MatF64::<ROWS, COLS>::identity())
}

fn get_elem(&self, idx: [usize; 2]) -> DualScalar<DM, DN> {
fn elem(&self, idx: [usize; 2]) -> DualScalar<DM, DN> {
self.inner[(idx[0], idx[1])]
}

fn elem_mut(&mut self, idx: [usize; 2]) -> &mut DualScalar<DM, DN> {
&mut self.inner[(idx[0], idx[1])]
}

fn from_array2<A>(duals: A) -> Self
where
A: Borrow<[[DualScalar<DM, DN>; COLS]; ROWS]>,
Expand Down Expand Up @@ -354,10 +358,6 @@ impl<const ROWS: usize, const COLS: usize, const DM: usize, const DN: usize>
}
}

fn set_elem(&mut self, idx: [usize; 2], val: DualScalar<DM, DN>) {
self.inner[(idx[0], idx[1])] = val;
}

fn transposed(&self) -> <DualScalar<DM, DN> as IsScalar<1, DM, DN>>::Matrix<COLS, ROWS> {
DualMatrix {
inner: self.inner.transpose(),
Expand Down
7 changes: 7 additions & 0 deletions crates/sophus_autodiff/src/dual/dual_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use num_traits::{
use super::{
dual_matrix::DualMatrix,
dual_vector::DualVector,
scalar::IsDualScalarFromCurve,
};
use crate::{
linalg::{
Expand Down Expand Up @@ -152,6 +153,12 @@ impl<const DM: usize, const DN: usize> IsDualScalar<1, DM, DN> for DualScalar<DM
}
}

impl IsDualScalarFromCurve<DualScalar<1, 1>, 1> for DualScalar<1, 1> {
fn curve_derivative(&self) -> f64 {
self.derivative()[0]
}
}

impl<const DM: usize, const DN: usize> DualScalar<DM, DN> {
fn binary_dij(
lhs_dx: &Option<MatF64<DM, DN>>,
Expand Down
28 changes: 18 additions & 10 deletions crates/sophus_autodiff/src/dual/dual_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::{
dual_scalar::DualScalar,
vector::{
HasJacobian,
IsDualVectorFromCurve,
VectorValuedDerivative,
},
};
Expand Down Expand Up @@ -61,6 +62,14 @@ impl<const ROWS: usize, const DM: usize, const DN: usize>
}
}

impl<const ROWS: usize> IsDualVectorFromCurve<DualScalar<1, 1>, ROWS, 1>
for DualVector<ROWS, 1, 1>
{
fn curve_derivative(&self) -> VecF64<ROWS> {
self.jacobian()
}
}

impl<const ROWS: usize, const DM: usize> HasJacobian<DualScalar<DM, 1>, ROWS, 1, DM>
for DualVector<ROWS, DM, 1>
{
Expand Down Expand Up @@ -171,14 +180,14 @@ impl<const ROWS: usize, const DM: usize, const DN: usize>
}

fn norm(&self) -> DualScalar<DM, DN> {
self.clone().dot(*self).sqrt()
self.dot(*self).sqrt()
}

fn squared_norm(&self) -> DualScalar<DM, DN> {
self.clone().dot(*self)
self.dot(*self)
}

fn get_elem(&self, idx: usize) -> DualScalar<DM, DN> {
fn elem(&self, idx: usize) -> DualScalar<DM, DN> {
self.inner[idx]
}

Expand Down Expand Up @@ -220,7 +229,7 @@ impl<const ROWS: usize, const DM: usize, const DN: usize>
fn real_vector(&self) -> VecF64<ROWS> {
let mut r = VecF64::<ROWS>::zeros();
for i in 0..ROWS {
r[i] = self.get_elem(i).real_part;
r[i] = self.elem(i).real_part;
}
r
}
Expand Down Expand Up @@ -263,15 +272,14 @@ impl<const ROWS: usize, const DM: usize, const DN: usize>
let mut sum = <DualScalar<DM, DN>>::from_f64(0.0);

for i in 0..ROWS {
sum += self.get_elem(i) * rhs.borrow().get_elem(i);
sum += self.elem(i) * rhs.borrow().elem(i);
}

sum
}

fn normalized(&self) -> Self {
self.clone()
.scaled(<DualScalar<DM, DN>>::from_f64(1.0) / self.norm())
self.scaled(<DualScalar<DM, DN>>::from_f64(1.0) / self.norm())
}

fn from_f64_array<A>(vals: A) -> Self
Expand All @@ -296,8 +304,8 @@ impl<const ROWS: usize, const DM: usize, const DN: usize>
}
}

fn set_elem(&mut self, idx: usize, v: DualScalar<DM, DN>) {
self.inner[idx] = v;
fn elem_mut(&mut self, idx: usize) -> &mut DualScalar<DM, DN> {
&mut self.inner[idx]
}

fn to_dual_const<const M: usize, const N: usize>(
Expand All @@ -316,7 +324,7 @@ impl<const ROWS: usize, const DM: usize, const DN: usize>
let mut out = DualMatrix::<ROWS, R2, DM, DN>::zeros();
for i in 0..ROWS {
for j in 0..R2 {
out.set_elem([i, j], self.get_elem(i) * rhs.borrow().get_elem(j));
*out.elem_mut([i, j]) = self.elem(i) * rhs.borrow().elem(j);
}
}
out
Expand Down
8 changes: 4 additions & 4 deletions crates/sophus_autodiff/src/dual/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<
}
}

/// Trait for scalar dual numbers
/// Trait for dual matrix
pub trait IsDualMatrix<
S: IsDualScalar<BATCH, DM, DN>,
const ROWS: usize,
Expand All @@ -54,16 +54,16 @@ pub trait IsDualMatrix<
fn derivative(self) -> MatrixValuedDerivative<S::RealScalar, ROWS, COLS, BATCH, DM, DN>;
}

/// Trait for scalar dual numbers
pub trait IsScalarFieldDualMatrix<
/// Trait for dual matrix as an output of a scalar field
pub trait IsDualMatrixFromCurve<
S: IsDualScalar<BATCH, 1, 1>,
const ROWS: usize,
const COLS: usize,
const BATCH: usize,
>: IsDualMatrix<S, ROWS, COLS, BATCH, 1, 1>
{
/// Get the derivative
fn scalarfield_derivative(&self) -> S::RealMatrix<ROWS, COLS>;
fn curve_derivative(&self) -> S::RealMatrix<ROWS, COLS>;
}

#[test]
Expand Down
Loading

0 comments on commit eb4088e

Please sign in to comment.