Skip to content

Commit

Permalink
Addressed Cargo clippy comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
zlogic committed Aug 22, 2023
1 parent 7a3f8f7 commit 3244949
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 80 deletions.
68 changes: 42 additions & 26 deletions src/correlation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,15 @@ struct CorrelationStep<'a> {
img2_data: ImagePointData,
}

impl PointCorrelations {
pub fn new(
img1_dimensions: (u32, u32),
img2_dimensions: (u32, u32),
fundamental_matrix: Matrix3<f64>,
projection_mode: ProjectionMode,
hardware_mode: HardwareMode,
) -> PointCorrelations {
struct CorrelationParameters {
min_stdev: f32,
corridor_size: usize,
correlation_threshold: f32,
corridor_extend_range: f64,
}

impl CorrelationParameters {
fn for_projection(projection_mode: &ProjectionMode) -> CorrelationParameters {
let (min_stdev, correlation_threshold, corridor_size, corridor_extend_range) =
match projection_mode {
ProjectionMode::Affine => (
Expand All @@ -114,17 +115,31 @@ impl PointCorrelations {
CORRIDOR_EXTEND_RANGE_PERSPECTIVE,
),
};
CorrelationParameters {
min_stdev,
corridor_size,
correlation_threshold,
corridor_extend_range,
}
}
}

impl PointCorrelations {
pub fn new(
img1_dimensions: (u32, u32),
img2_dimensions: (u32, u32),
fundamental_matrix: Matrix3<f64>,
projection_mode: ProjectionMode,
hardware_mode: HardwareMode,
) -> PointCorrelations {
let selected_hardware;
let gpu_context = if matches!(hardware_mode, HardwareMode::Gpu | HardwareMode::GpuLowPower)
{
let low_power = matches!(hardware_mode, HardwareMode::GpuLowPower);
match gpu::GpuContext::new(
(img1_dimensions.0 as usize, img1_dimensions.1 as usize),
(img2_dimensions.0 as usize, img2_dimensions.1 as usize),
min_stdev,
correlation_threshold,
corridor_size,
corridor_extend_range,
&projection_mode,
fundamental_matrix,
low_power,
) {
Expand Down Expand Up @@ -153,14 +168,16 @@ impl PointCorrelations {
DMatrix::from_element(img2_dimensions.1 as usize, img2_dimensions.0 as usize, None),
),
};

let params = CorrelationParameters::for_projection(&projection_mode);
PointCorrelations {
correlated_points,
correlated_points_reverse,
first_pass: true,
min_stdev,
corridor_size,
correlation_threshold,
corridor_extend_range,
min_stdev: params.min_stdev,
corridor_size: params.corridor_size,
correlation_threshold: params.correlation_threshold,
corridor_extend_range: params.corridor_extend_range,
fundamental_matrix,
gpu_context,
selected_hardware,
Expand Down Expand Up @@ -742,8 +759,9 @@ mod gpu {
use rayon::prelude::*;

use super::{
CORRIDOR_MIN_RANGE, CORRIDOR_SEGMENT_LENGTH_HIGHPERFORMANCE,
CORRIDOR_SEGMENT_LENGTH_LOWPOWER, CROSS_CHECK_SEARCH_AREA, KERNEL_SIZE, NEIGHBOR_DISTANCE,
CorrelationParameters, ProjectionMode, CORRIDOR_MIN_RANGE,
CORRIDOR_SEGMENT_LENGTH_HIGHPERFORMANCE, CORRIDOR_SEGMENT_LENGTH_LOWPOWER,
CROSS_CHECK_SEARCH_AREA, KERNEL_SIZE, NEIGHBOR_DISTANCE,
SEARCH_AREA_SEGMENT_LENGTH_HIGHPERFORMANCE, SEARCH_AREA_SEGMENT_LENGTH_LOWPOWER,
};

Expand Down Expand Up @@ -814,10 +832,7 @@ mod gpu {
pub fn new(
img1_dimensions: (usize, usize),
img2_dimensions: (usize, usize),
min_stdev: f32,
correlation_threshold: f32,
corridor_size: usize,
corridor_extend_range: f64,
projection_mode: &ProjectionMode,
fundamental_matrix: Matrix3<f64>,
low_power: bool,
) -> Result<GpuContext, Box<dyn error::Error>> {
Expand Down Expand Up @@ -931,11 +946,12 @@ mod gpu {

let correlation_values = DMatrix::from_element(img1_shape.0, img1_shape.1, None);

let params = CorrelationParameters::for_projection(projection_mode);
let result = GpuContext {
min_stdev,
correlation_threshold,
corridor_size,
corridor_extend_range,
min_stdev: params.min_stdev,
correlation_threshold: params.correlation_threshold,
corridor_size: params.corridor_size,
corridor_extend_range: params.corridor_extend_range,
fundamental_matrix,
img1_shape,
img2_shape,
Expand Down
37 changes: 16 additions & 21 deletions src/fundamentalmatrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct FundamentalMatrix {
impl FundamentalMatrix {
pub fn new<PL: ProgressListener>(
projection: ProjectionMode,
point_matches: &Vec<Match>,
point_matches: &[Match],
progress_listener: Option<&PL>,
) -> Result<FundamentalMatrix, RansacError> {
let ransac_k = match projection {
Expand Down Expand Up @@ -101,7 +101,7 @@ impl FundamentalMatrix {

fn find_ransac<PL: ProgressListener>(
&self,
point_matches: &Vec<Match>,
point_matches: &[Match],
progress_listener: Option<&PL>,
) -> Result<RansacIterationResult, RansacError> {
if point_matches.len() < RANSAC_D + self.ransac_n {
Expand Down Expand Up @@ -151,7 +151,7 @@ impl FundamentalMatrix {
}

#[inline]
fn choose_inliers(&self, point_matches: &Vec<Match>) -> Vec<Match> {
fn choose_inliers(&self, point_matches: &[Match]) -> Vec<Match> {
let rng = &mut SmallRng::from_rng(rand::thread_rng()).unwrap();
let mut inliers: Vec<Match> = vec![];

Expand All @@ -174,7 +174,7 @@ impl FundamentalMatrix {
inliers
}

fn ransac_iteration(&self, point_matches: &Vec<Match>) -> Vec<RansacIterationResult> {
fn ransac_iteration(&self, point_matches: &[Match]) -> Vec<RansacIterationResult> {
let inliers = self.choose_inliers(point_matches);
if inliers.len() < self.ransac_n {
return vec![];
Expand All @@ -192,8 +192,8 @@ impl FundamentalMatrix {
fn validate_f(
&self,
f: Matrix3<f64>,
inliers: &Vec<Match>,
point_matches: &Vec<Match>,
inliers: &[Match],
point_matches: &[Match],
) -> Option<RansacIterationResult> {
if !f.iter().all(|v| v.is_finite()) {
return None;
Expand All @@ -203,20 +203,15 @@ impl FundamentalMatrix {
} else {
f
};
let inliers_pass = inliers
.into_iter()
.all(|m| self.fits_model(&f, &m).is_some());
let inliers_pass = inliers.iter().all(|m| self.fits_model(&f, m).is_some());
if !inliers_pass {
return None;
}
let all_inliers: (usize, f64) = point_matches
.iter()
.filter_map(|point_match| {
if let Some(match_error) = self.fits_model(&f, point_match) {
Some((1, match_error))
} else {
None
}
self.fits_model(&f, point_match)
.map(|match_error| (1, match_error))
})
.fold((0, 0.0), |acc, err| (acc.0 + err.0, acc.1 + err.1));

Expand Down Expand Up @@ -263,7 +258,7 @@ impl FundamentalMatrix {
}

#[inline]
fn calculate_model_perspective(inliers: &Vec<Match>) -> Vec<Matrix3<f64>> {
fn calculate_model_perspective(inliers: &[Match]) -> Vec<Matrix3<f64>> {
let mut a = SMatrix::<f64, RANSAC_N_PERSPECTIVE, 9>::zeros();
let mut x1 = SMatrix::<f64, 3, RANSAC_N_PERSPECTIVE>::zeros();
let mut x2 = SMatrix::<f64, 3, RANSAC_N_PERSPECTIVE>::zeros();
Expand Down Expand Up @@ -365,11 +360,11 @@ impl FundamentalMatrix {
.collect::<Vec<_>>()
}

fn optimize_perspective_f(f: &Matrix3<f64>, inliers: &Vec<Match>) -> Option<Matrix3<f64>> {
fn optimize_perspective_f(f: &Matrix3<f64>, inliers: &[Match]) -> Option<Matrix3<f64>> {
const JACOBIAN_H: f64 = 0.001;
let f_params = FundamentalMatrix::params_from_perspective_f(&f);
let f_params = FundamentalMatrix::params_from_perspective_f(f);
let f_residuals = |p: &OVector<f64, U7>| {
let f = FundamentalMatrix::f_from_perspective_params(&p);
let f = FundamentalMatrix::f_from_perspective_params(p);
let mut residuals = SVector::<f64, 7>::zeros();
for i in 0..RANSAC_N_PERSPECTIVE {
residuals[i] = FundamentalMatrix::reprojection_error(&f, &inliers[i]);
Expand All @@ -382,10 +377,10 @@ impl FundamentalMatrix {
let f_plus;
let f_minus;
{
let mut p_plus = p.clone();
let mut p_plus = *p;
p_plus[i] += JACOBIAN_H;
f_plus = FundamentalMatrix::f_from_perspective_params(&p_plus);
let mut p_minus = p.clone();
let mut p_minus = *p;
p_minus[i] -= JACOBIAN_H;
f_minus = FundamentalMatrix::f_from_perspective_params(&p_minus);
}
Expand Down Expand Up @@ -499,7 +494,7 @@ where
mu = TAU
* (0..jt_j.nrows())
.map(|i| jt_j[(i, i)])
.max_by(|a, b| a.total_cmp(&b))
.max_by(|a, b| a.total_cmp(b))
.unwrap_or(1.0);
}
let mut nu = 2.0;
Expand Down
20 changes: 10 additions & 10 deletions src/orb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn extract_points<PL: ProgressListener>(
}
Some((
*point,
harris_response(&img, &harris_gaussian_kernel, *point)?,
harris_response(img, &harris_gaussian_kernel, *point)?,
))
})
.collect::<Vec<_>>();
Expand All @@ -78,7 +78,7 @@ pub fn extract_points<PL: ProgressListener>(
.map(|(keypoint, _)| *keypoint)
.collect();

extract_brief_descriptors(&img, keypoints, progress_listener)
extract_brief_descriptors(img, keypoints, progress_listener)
}

fn find_fast_keypoints<PL: ProgressListener>(
Expand All @@ -98,7 +98,7 @@ fn find_fast_keypoints<PL: ProgressListener>(
}
let kp: Vec<(usize, usize)> = (FAST_KERNEL_SIZE..(img.nrows() - FAST_KERNEL_SIZE))
.filter_map(
|row| match is_keypoint(&img, FAST_THRESHOLD.into(), row, col) {
|row| match is_keypoint(img, FAST_THRESHOLD.into(), row, col) {
true => Some((row, col)),
false => None,
},
Expand All @@ -123,7 +123,7 @@ fn find_fast_keypoints<PL: ProgressListener>(
let mut threshold_max = std::u8::MAX as i16;
let mut threshold = (threshold_max + threshold_min) / 2;
while threshold_max > threshold_min + 1 {
if is_keypoint(&img, threshold, p.0, p.1) {
if is_keypoint(img, threshold, p.0, p.1) {
threshold_min = threshold;
} else {
threshold_max = threshold;
Expand Down Expand Up @@ -388,7 +388,7 @@ fn extract_brief_descriptors<PL: ProgressListener>(
let angle = get_brief_orientation(&img, *coords)?;
let angle_sin = angle.sin();
let angle_cos = angle.cos();
let mut orb_descriptor = [0 as u32; 8];
let mut orb_descriptor = [0_u32; 8];
for i in 0..ORB_MATCH_PATTERN.len() {
let offset1 = ORB_MATCH_PATTERN[i].0;
let offset2 = ORB_MATCH_PATTERN[i].1;
Expand All @@ -401,12 +401,12 @@ fn extract_brief_descriptors<PL: ProgressListener>(
(offset2.1 as f64 * angle_sin + offset2.0 as f64 * angle_cos).round() as isize,
);
let p1_coords = (
coords.0.saturating_add_signed(offset1.0 as isize),
coords.1.saturating_add_signed(offset1.1 as isize),
coords.0.saturating_add_signed(offset1.0),
coords.1.saturating_add_signed(offset1.1),
);
let p2_coords = (
coords.0.saturating_add_signed(offset2.0 as isize),
coords.1.saturating_add_signed(offset2.1 as isize),
coords.0.saturating_add_signed(offset2.0),
coords.1.saturating_add_signed(offset2.1),
);
if p1_coords.0 == 0
|| p2_coords.0 == 0
Expand All @@ -421,7 +421,7 @@ fn extract_brief_descriptors<PL: ProgressListener>(
let p2 = img[p2_coords]?;
let dst_block = &mut orb_descriptor[i / 32];
let tau = if p1 < p2 { 1 } else { 0 };
*dst_block = *dst_block | (tau << (i % 32));
*dst_block |= tau << (i % 32);
}
Some((*coords, orb_descriptor))
})
Expand Down
4 changes: 2 additions & 2 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl MeshWriter for PlyWriter {
let first_image = track.range().start;
if let Some(point2d) = track.get(first_image) {
let img = &self.images[first_image];
img.get_pixel_checked(point2d.1 as u32, point2d.0 as u32)
img.get_pixel_checked(point2d.1, point2d.0)
.map(|pixel| pixel.0)
} else {
return Err(OutputError::new("Track has no image").into());
Expand Down Expand Up @@ -668,7 +668,7 @@ impl MeshWriter for ObjWriter {
let first_image = track.range().start;
if let Some(point2d) = track.get(first_image) {
let img = &self.images[first_image];
img.get_pixel_checked(point2d.1 as u32, point2d.0 as u32)
img.get_pixel_checked(point2d.1, point2d.0)
.map(|pixel| pixel.0)
} else {
return Err(OutputError::new("Track has no image").into());
Expand Down
8 changes: 4 additions & 4 deletions src/pointmatching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ where

impl KeypointMatching {
pub fn new<PL: ProgressListener>(
points1: &Vec<Keypoint>,
points2: &Vec<Keypoint>,
points1: &[Keypoint],
points2: &[Keypoint],
projection_mode: ProjectionMode,
progress_listener: Option<&PL>,
) -> KeypointMatching {
Expand All @@ -42,8 +42,8 @@ impl KeypointMatching {
}

fn match_points<PL: ProgressListener>(
points1: &Vec<Keypoint>,
points2: &Vec<Keypoint>,
points1: &[Keypoint],
points2: &[Keypoint],
threshold: u32,
progress_listener: Option<&PL>,
) -> Vec<(Point, Point)> {
Expand Down
Loading

0 comments on commit 3244949

Please sign in to comment.