Skip to content

Commit

Permalink
Fix pyramid downsampling interpolation, and more.
Browse files Browse the repository at this point in the history
Changes to:
- Curve interpolation.
- Resample interpolation.
- Downsampling algorthim to maintain key features.
- Downsampling, maintaining end-points.
- Curve fitting with interpolation method.
- Test programs simplfied to reduce line count.
- Test image output file names.
- f64 to 'Real' type.

This is a big change, but basically this change is a simplification of
test code, fixing of the pyramid down-sampling algorithm, and
unification around new, reuseable interpolation code.
  • Loading branch information
david-cattermole committed Nov 29, 2024
1 parent d56da4d commit 78fbc47
Show file tree
Hide file tree
Showing 116 changed files with 1,767 additions and 1,485 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ argmin = { version = "0.10.0", default-features = true, features = ["serde1"] }
argmin-math = { version = "0.4", default-features = true, features = ["primitives", "vec", "ndarray_latest"] }
criterion = { version = "0.5.1", default-features = false, features = ["html_reports"] }
exr = "1.72.0"
enum_dispatch = "0.3.13"
fastapprox = "0.3.1"
finitediff = { version = "0.1.4", features = ["ndarray"] }
half = { version = "2.4.1", default-features = false, features = ["std", "num-traits"] }
Expand Down
1 change: 1 addition & 0 deletions lib/rust/mmscenegraph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ anyhow = { workspace = true }
approx = { workspace = true }
argmin = { workspace = true }
argmin-math = { workspace = true }
enum_dispatch = { workspace = true }
fastapprox = { workspace = true }
finitediff = { workspace = true }
log = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions lib/rust/mmscenegraph/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ pub const MM_TO_CM: Real = 0.1;
pub const _CM_TO_MM: Real = 10.0;
pub const INCH_TO_MM: Real = 25.4;
pub const _INCH_TO_CM: Real = 2.54;

// Number constants.
pub const REAL_E: f64 = std::f64::consts::E;
126 changes: 89 additions & 37 deletions lib/rust/mmscenegraph/src/curve/detect/keypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,28 @@ fn compute_velocity_importance(velocity: Real, acceleration: Real) -> Real {
}

/// Detect keypoints specific to animation curves
fn detect_level_keypoints(
level: &PyramidLevel,
existing_keypoints: &[RankedKeypoint],
pub fn detect_level_keypoints(
times: &[Real],
values: &[Real],
velocity: &[Real],
acceleration: &[Real],
curvature: &[Real],
current_level: usize,
) -> Vec<RankedKeypoint> {
debug!("detect_level_keypoints: level.level={:?}", level.level);
debug!("detect_level_keypoints: level.scale={:?}", level.scale());
debug!("detect_level_keypoints: times.len()={}", times.len());
debug!("detect_level_keypoints: values.len()={}", values.len());
debug!("detect_level_keypoints: velocity.len()={}", velocity.len());
debug!(
"detect_level_keypoints: existing_keypoints.len()={:?}",
existing_keypoints.len()
"detect_level_keypoints: acceleration.len()={}",
acceleration.len()
);
debug!(
"detect_level_keypoints: existing_keypoints={:?}",
existing_keypoints
"detect_level_keypoints: curvature.len()={}",
curvature.len()
);
debug!("detect_level_keypoints: current_level={:?}", current_level);

let mut keypoints = Vec::new();
let times = &level.times;
let values = &level.values;
let velocity = &level.velocity;
let acceleration = &level.acceleration;
let curvature = &level.curvature;
debug!("detect_level_keypoints: current_level={}", current_level);

let mut keypoints = Vec::with_capacity(times.len() / 2);
for i in 1..times.len() - 1 {
let curr_time = times[i];
let _prev_time = times[i - 1];
Expand Down Expand Up @@ -190,24 +188,14 @@ fn detect_level_keypoints(
keypoint_type = Some(KeypointType::VelocityPeak);
}

// If point is important and not near existing keypoints
if let Some(kp_type) = keypoint_type {
let level_scale = level.scale();
// let level_scale = level.inverse_scale();

if !is_near_existing_keypoint(
curr_time,
level_scale,
existing_keypoints,
) {
keypoints.push(RankedKeypoint {
time: curr_time,
value: curr_value,
importance,
level_detected: current_level,
keypoint_type: kp_type,
});
}
keypoints.push(RankedKeypoint {
time: curr_time,
value: curr_value,
importance,
level_detected: current_level,
keypoint_type: kp_type,
});
}
}

Expand All @@ -222,6 +210,67 @@ fn detect_level_keypoints(
keypoints
}

/// Detect keypoints specific to animation curves
fn detect_level_and_combine_keypoints(
level: &PyramidLevel,
existing_keypoints: &[RankedKeypoint],
current_level: usize,
) -> Vec<RankedKeypoint> {
debug!(
"detect_level_and_combine_keypoints: level.level={:?}",
level.level
);
debug!(
"detect_level_and_combine_keypoints: level.scale={:?}",
level.scale()
);
debug!(
"detect_level_and_combine_keypoints: existing_keypoints.len()={:?}",
existing_keypoints.len()
);
debug!(
"detect_level_and_combine_keypoints: existing_keypoints={:?}",
existing_keypoints
);
debug!(
"detect_level_and_combine_keypoints: current_level={:?}",
current_level
);

let level_keypoints = detect_level_keypoints(
&level.times,
&level.values,
&level.velocity,
&level.acceleration,
&level.curvature,
current_level,
);

let mut keypoints = Vec::new();
for keypoint in level_keypoints {
if !is_near_existing_keypoint(
keypoint.time,
level.scale(),
existing_keypoints,
) {
keypoints.push(keypoint);
}
}

debug!(
"detect_level_and_combine_keypoints: keypoints.len()={:?}",
keypoints.len()
);
for keypoint in &keypoints {
debug!(
"detect_level_and_combine_keypoints: keypoint={:?}",
keypoint
);
}

keypoints
}

/// Check if point is near any existing keypoint.
fn is_near_existing_keypoint(
time: Real,
Expand Down Expand Up @@ -308,8 +357,11 @@ fn process_pyramid_levels(
// Process levels from coarse to fine.
let mut max_level = 0;
for pyramid in pyramids.iter().rev() {
let pyramid_keypoints =
detect_level_keypoints(pyramid, &all_keypoints, pyramid.level);
let pyramid_keypoints = detect_level_and_combine_keypoints(
pyramid,
&all_keypoints,
pyramid.level,
);

for keypoint in pyramid_keypoints {
all_keypoints.push(keypoint);
Expand All @@ -328,7 +380,7 @@ fn process_pyramid_levels(
Ok(selected)
}

fn filter_keypoints_by_type_and_level(
pub fn filter_keypoints_by_type_and_level(
only_keypoint_type: KeypointType,
only_level_detected: usize,
min_spacing: Real,
Expand Down
31 changes: 16 additions & 15 deletions lib/rust/mmscenegraph/src/curve/detect/pops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use anyhow::Result;
use log::debug;
use std::fmt;

use crate::constant::Real;
use crate::curve::derivatives::allocate_derivatives_order_2;
use crate::curve::derivatives::calculate_derivatives_order_2;
use crate::math::statistics::calc_median_absolute_deviation_sigma;
Expand All @@ -34,9 +35,9 @@ use crate::math::statistics::SortedDataSliceOps;
/// Represents a point that was classified as a pop
#[derive(Debug)]
pub struct PopPoint {
pub time: f64,
pub value: f64,
pub score: f64,
pub time: Real,
pub value: Real,
pub score: Real,
}

impl fmt::Display for PopPoint {
Expand All @@ -50,11 +51,11 @@ impl fmt::Display for PopPoint {
}

fn calculate_per_frame_pop_score(
times: &[f64],
values: &[f64],
out_velocity: &mut [f64],
out_acceleration: &mut [f64],
out_scores: &mut [f64],
times: &[Real],
values: &[Real],
out_velocity: &mut [Real],
out_acceleration: &mut [Real],
out_scores: &mut [Real],
) -> Result<()> {
if (times.len() != values.len()) && (times.len() != out_scores.len()) {
bail!("Times, values and output arrays must have the same length.");
Expand Down Expand Up @@ -105,9 +106,9 @@ fn calculate_per_frame_pop_score(

/// Find pops in the data.
pub fn detect_curve_pops(
times: &[f64],
values: &[f64],
threshold: f64,
times: &[Real],
values: &[Real],
threshold: Real,
) -> Result<Vec<PopPoint>> {
if times.len() != values.len() {
bail!("Times and values must have the same length.");
Expand Down Expand Up @@ -176,10 +177,10 @@ pub fn detect_curve_pops(
}

pub fn filter_curve_pops(
times: &[f64],
values: &[f64],
threshold: f64,
) -> Result<Vec<(f64, f64)>> {
times: &[Real],
values: &[Real],
threshold: Real,
) -> Result<Vec<(Real, Real)>> {
if times.len() != values.len() {
bail!("Times and values must have the same length.");
}
Expand Down
Loading

0 comments on commit 78fbc47

Please sign in to comment.