Skip to content

Commit

Permalink
Add mmAnimCurveFilterPops command.
Browse files Browse the repository at this point in the history
A maya command plug-in that is used to manipulate an animation curve.

This also adds C++ bindings via CXX.

GitHub issue #268
  • Loading branch information
david-cattermole committed Dec 2, 2024
1 parent 78fbc47 commit 9d5ba41
Show file tree
Hide file tree
Showing 15 changed files with 642 additions and 34 deletions.
4 changes: 4 additions & 0 deletions lib/cppbind/mmscenegraph/include/mmscenegraph/_cxxbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -1202,4 +1202,8 @@ MMSCENEGRAPH_API_EXPORT bool shim_fit_straight_line_to_ordered_points(::rust::Sl
MMSCENEGRAPH_API_EXPORT bool shim_line_point_intersection(::mmscenegraph::Point3 point, ::mmscenegraph::Point3 line_a, ::mmscenegraph::Point3 line_b, ::mmscenegraph::Point3 &out_point) noexcept;

MMSCENEGRAPH_API_EXPORT bool shim_fit_plane_to_points(::rust::Slice<const double> points_xyz, double &out_point_x, double &out_point_y, double &out_point_z, double &out_dir_x, double &out_dir_y, double &out_dir_z, double &out_scale, double &out_rms_error) noexcept;

MMSCENEGRAPH_API_EXPORT bool shim_detect_curve_pops(::rust::Slice<const double> x_values, ::rust::Slice<const double> y_values, double threshold, ::rust::Vec<double> &out_x_values, ::rust::Vec<double> &out_y_values) noexcept;

MMSCENEGRAPH_API_EXPORT bool shim_filter_curve_pops(::rust::Slice<const double> x_values, ::rust::Slice<const double> y_values, double threshold, ::rust::Vec<double> &out_x_values, ::rust::Vec<double> &out_y_values) noexcept;
} // namespace mmscenegraph
46 changes: 46 additions & 0 deletions lib/cppbind/mmscenegraph/include/mmscenegraph/curve_detect_pops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2024 David Cattermole.
*
* This file is part of mmSolver.
*
* mmSolver is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* mmSolver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mmSolver. If not, see <https://www.gnu.org/licenses/>.
* ====================================================================
*
*/

#ifndef MM_SOLVER_MM_SCENE_GRAPH_CURVE_DETECT_POPS_H
#define MM_SOLVER_MM_SCENE_GRAPH_CURVE_DETECT_POPS_H

#include "_cxx.h"
#include "_cxxbridge.h"
#include "_symbol_export.h"
#include "_types.h"

namespace mmscenegraph {

MMSCENEGRAPH_API_EXPORT
bool detect_curve_pops(rust::Slice<const Real> &x_values,
rust::Slice<const Real> &y_values, const Real threshold,
rust::Vec<Real> &out_x_values,
rust::Vec<Real> &out_y_values) noexcept;

MMSCENEGRAPH_API_EXPORT
bool filter_curve_pops(rust::Slice<const Real> &x_values,
rust::Slice<const Real> &y_values, const Real threshold,
rust::Vec<Real> &out_x_values,
rust::Vec<Real> &out_y_values) noexcept;

} // namespace mmscenegraph

#endif // MM_SOLVER_MM_SCENE_GRAPH_CURVE_DETECT_POPS_H
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "_cxxbridge.h"
#include "_types.h"
#include "attrdatablock.h"
#include "curve_detect_pops.h"
#include "fit_plane.h"
#include "flatscene.h"
#include "line.h"
Expand Down
12 changes: 12 additions & 0 deletions lib/cppbind/mmscenegraph/src/_cxxbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,10 @@ bool mmscenegraph$cxxbridge1$shim_fit_straight_line_to_ordered_points(::rust::Sl
bool mmscenegraph$cxxbridge1$shim_line_point_intersection(::mmscenegraph::Point3 point, ::mmscenegraph::Point3 line_a, ::mmscenegraph::Point3 line_b, ::mmscenegraph::Point3 &out_point) noexcept;

bool mmscenegraph$cxxbridge1$shim_fit_plane_to_points(::rust::Slice<const double> points_xyz, double &out_point_x, double &out_point_y, double &out_point_z, double &out_dir_x, double &out_dir_y, double &out_dir_z, double &out_scale, double &out_rms_error) noexcept;

bool mmscenegraph$cxxbridge1$shim_detect_curve_pops(::rust::Slice<const double> x_values, ::rust::Slice<const double> y_values, double threshold, ::rust::Vec<double> &out_x_values, ::rust::Vec<double> &out_y_values) noexcept;

bool mmscenegraph$cxxbridge1$shim_filter_curve_pops(::rust::Slice<const double> x_values, ::rust::Slice<const double> y_values, double threshold, ::rust::Vec<double> &out_x_values, ::rust::Vec<double> &out_y_values) noexcept;
} // extern "C"
} // namespace mmscenegraph

Expand Down Expand Up @@ -1774,6 +1778,14 @@ MMSCENEGRAPH_API_EXPORT bool shim_line_point_intersection(::mmscenegraph::Point3
MMSCENEGRAPH_API_EXPORT bool shim_fit_plane_to_points(::rust::Slice<const double> points_xyz, double &out_point_x, double &out_point_y, double &out_point_z, double &out_dir_x, double &out_dir_y, double &out_dir_z, double &out_scale, double &out_rms_error) noexcept {
return mmscenegraph$cxxbridge1$shim_fit_plane_to_points(points_xyz, out_point_x, out_point_y, out_point_z, out_dir_x, out_dir_y, out_dir_z, out_scale, out_rms_error);
}

MMSCENEGRAPH_API_EXPORT bool shim_detect_curve_pops(::rust::Slice<const double> x_values, ::rust::Slice<const double> y_values, double threshold, ::rust::Vec<double> &out_x_values, ::rust::Vec<double> &out_y_values) noexcept {
return mmscenegraph$cxxbridge1$shim_detect_curve_pops(x_values, y_values, threshold, out_x_values, out_y_values);
}

MMSCENEGRAPH_API_EXPORT bool shim_filter_curve_pops(::rust::Slice<const double> x_values, ::rust::Slice<const double> y_values, double threshold, ::rust::Vec<double> &out_x_values, ::rust::Vec<double> &out_y_values) noexcept {
return mmscenegraph$cxxbridge1$shim_filter_curve_pops(x_values, y_values, threshold, out_x_values, out_y_values);
}
} // namespace mmscenegraph

extern "C" {
Expand Down
46 changes: 46 additions & 0 deletions lib/cppbind/mmscenegraph/src/curve_detect_pops.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2024 David Cattermole.
*
* This file is part of mmSolver.
*
* mmSolver is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* mmSolver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mmSolver. If not, see <https://www.gnu.org/licenses/>.
* ====================================================================
*
*/

#include <mmscenegraph/_cxx.h>
#include <mmscenegraph/_cxxbridge.h>
#include <mmscenegraph/curve_detect_pops.h>

namespace mmscenegraph {

MMSCENEGRAPH_API_EXPORT
bool detect_curve_pops(rust::Slice<const Real> &x_values,
rust::Slice<const Real> &y_values, const Real threshold,
rust::Vec<Real> &out_x_values,
rust::Vec<Real> &out_y_values) noexcept {
return shim_detect_curve_pops(x_values, y_values, threshold, out_x_values,
out_y_values);
}

MMSCENEGRAPH_API_EXPORT
bool filter_curve_pops(rust::Slice<const Real> &x_values,
rust::Slice<const Real> &y_values, const Real threshold,
rust::Vec<Real> &out_x_values,
rust::Vec<Real> &out_y_values) noexcept {
return shim_filter_curve_pops(x_values, y_values, threshold, out_x_values,
out_y_values);
}

} // namespace mmscenegraph
89 changes: 89 additions & 0 deletions lib/cppbind/mmscenegraph/src/curve_detect_pops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Copyright (C) 2024 David Cattermole.
//
// This file is part of mmSolver.
//
// mmSolver is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// mmSolver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mmSolver. If not, see <https://www.gnu.org/licenses/>.
// ====================================================================
//

use mmscenegraph_rust::constant::Real as CoreReal;
use mmscenegraph_rust::curve::detect::pops::detect_curve_pops as core_detect_curve_pops;
use mmscenegraph_rust::curve::detect::pops::filter_curve_pops as core_filter_curve_pops;

pub fn shim_detect_curve_pops(
// xy_values: &[(CoreReal, CoreReal)],
x_values: &[CoreReal],
y_values: &[CoreReal],
threshold: CoreReal,
out_x_values: &mut Vec<CoreReal>,
out_y_values: &mut Vec<CoreReal>,
) -> bool {
let result = core_detect_curve_pops(&x_values, &y_values, threshold);
match result {
Ok(values) => {
let new_x_capacity =
values.len().saturating_sub(out_x_values.capacity());
let new_y_capacity =
values.len().saturating_sub(out_y_values.capacity());
out_x_values.reserve_exact(new_x_capacity);
out_y_values.reserve_exact(new_y_capacity);
out_x_values.clear();
out_y_values.clear();

for value in values.iter() {
let x = value.0;
let y = value.1;
out_x_values.push(x);
out_y_values.push(y);
}

true
}
Err(_) => false,
}
}

pub fn shim_filter_curve_pops(
// xy_values: &[(CoreReal, CoreReal)],
x_values: &[CoreReal],
y_values: &[CoreReal],
threshold: CoreReal,
out_x_values: &mut Vec<CoreReal>,
out_y_values: &mut Vec<CoreReal>,
) -> bool {
let result = core_filter_curve_pops(&x_values, &y_values, threshold);
match result {
Ok(values) => {
let new_x_capacity =
values.len().saturating_sub(out_x_values.capacity());
let new_y_capacity =
values.len().saturating_sub(out_y_values.capacity());
out_x_values.reserve_exact(new_x_capacity);
out_y_values.reserve_exact(new_y_capacity);
out_x_values.clear();
out_y_values.clear();

for value in values.iter() {
let x = value.0;
let y = value.1;
out_x_values.push(x);
out_y_values.push(y);
}

true
}
Err(_) => false,
}
}
21 changes: 21 additions & 0 deletions lib/cppbind/mmscenegraph/src/cxxbridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

use crate::attrdatablock::shim_create_attr_data_block_box;
use crate::attrdatablock::ShimAttrDataBlock;
use crate::curve_detect_pops::shim_detect_curve_pops;
use crate::curve_detect_pops::shim_filter_curve_pops;
use crate::evaluationobjects::shim_create_evaluation_objects_box;
use crate::evaluationobjects::ShimEvaluationObjects;
use crate::fit_plane::shim_fit_plane_to_points;
Expand Down Expand Up @@ -431,4 +433,23 @@ pub mod ffi {
out_rms_error: &mut f64,
) -> bool;
}

// Fit Plane
extern "Rust" {
fn shim_detect_curve_pops(
x_values: &[f64],
y_values: &[f64],
threshold: f64,
out_x_values: &mut Vec<f64>,
out_y_values: &mut Vec<f64>,
) -> bool;

fn shim_filter_curve_pops(
x_values: &[f64],
y_values: &[f64],
threshold: f64,
out_x_values: &mut Vec<f64>,
out_y_values: &mut Vec<f64>,
) -> bool;
}
}
1 change: 1 addition & 0 deletions lib/cppbind/mmscenegraph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

pub mod attr;
pub mod attrdatablock;
pub mod curve_detect_pops;
pub mod cxxbridge;
pub mod evaluationobjects;
pub mod fit_plane;
Expand Down
1 change: 1 addition & 0 deletions lib/mmsolverlibs/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(lib_source_files

${mmscenegraph_source_dir}/_cxxbridge.cpp
${mmscenegraph_source_dir}/attrdatablock.cpp
${mmscenegraph_source_dir}/curve_detect_pops.cpp
${mmscenegraph_source_dir}/evaluationobjects.cpp
${mmscenegraph_source_dir}/fit_plane.cpp
${mmscenegraph_source_dir}/flatscene.cpp
Expand Down
37 changes: 3 additions & 34 deletions lib/rust/mmscenegraph/src/curve/detect/pops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use anyhow::bail;
use anyhow::Result;
use log::debug;
use std::fmt;

use crate::constant::Real;
use crate::curve::derivatives::allocate_derivatives_order_2;
Expand All @@ -32,24 +31,6 @@ use crate::math::statistics::calc_z_score;
use crate::math::statistics::SortedDataSlice;
use crate::math::statistics::SortedDataSliceOps;

/// Represents a point that was classified as a pop
#[derive(Debug)]
pub struct PopPoint {
pub time: Real,
pub value: Real,
pub score: Real,
}

impl fmt::Display for PopPoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"PopPoint [ t={:.2}, v={:.2} (score={:.2}) ]",
self.time, self.value, self.score
)
}
}

fn calculate_per_frame_pop_score(
times: &[Real],
values: &[Real],
Expand Down Expand Up @@ -109,7 +90,7 @@ pub fn detect_curve_pops(
times: &[Real],
values: &[Real],
threshold: Real,
) -> Result<Vec<PopPoint>> {
) -> Result<Vec<(Real, Real)>> {
if times.len() != values.len() {
bail!("Times and values must have the same length.");
}
Expand Down Expand Up @@ -147,13 +128,7 @@ pub fn detect_curve_pops(
if pop_prev || pop_current || pop_next {
let t = times[i];
let v = values[i];

let point = PopPoint {
time: t,
value: v,
score: score_current,
};
out_values.push(point);
out_values.push((t, v));
}
}
} else {
Expand All @@ -162,13 +137,7 @@ pub fn detect_curve_pops(
if score > threshold {
let t = times[i];
let v = values[i];

let point = PopPoint {
time: t,
value: v,
score: score,
};
out_values.push(point);
out_values.push((t, v));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(SOURCE_FILES
mmSolver/adjust/adjust_solveFunc.cpp
mmSolver/calibrate/calibrate_common.cpp
mmSolver/calibrate/vanishing_point.cpp
mmSolver/cmd/MMAnimCurveFilterPopsCmd.cpp
mmSolver/cmd/MMBestFitPlaneCmd.cpp
mmSolver/cmd/MMCameraPoseFromPointsCmd.cpp
mmSolver/cmd/MMCameraRelativePoseCmd.cpp
Expand Down
Loading

0 comments on commit 9d5ba41

Please sign in to comment.