use super::{RipsInputKind, ThresholdRips, exact};
use crate::Result;
use crate::execution::{Execution, WorkBudget};
use crate::filtration::SimplicialFiltration;
use crate::geometry::{DissimilarityMatrixView, PointCloudView, euclidean_distance};
#[derive(Clone, Copy, Debug)]
pub(crate) enum Input<'a> {
Points(PointCloudView<'a>),
Matrix(DissimilarityMatrixView<'a>),
}
impl Input<'_> {
pub(crate) fn len(self) -> usize {
match self {
Self::Points(p) => p.len(),
Self::Matrix(m) => m.len(),
}
}
pub(crate) fn kind(self) -> RipsInputKind {
match self {
Self::Points(_) => RipsInputKind::Euclidean,
Self::Matrix(_) => RipsInputKind::Dissimilarities,
}
}
pub(crate) fn distance(self, a: usize, b: usize) -> Result<f64> {
match self {
Self::Points(p) => euclidean_distance(p, a, b),
Self::Matrix(m) => Ok(m.get(a, b).unwrap()),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct RipsBuilder<'a> {
pub(crate) input: Input<'a>,
pub(crate) max_edge: Option<f64>,
}
impl<'a> RipsBuilder<'a> {
pub fn from_points(points: PointCloudView<'a>) -> Self {
Self {
input: Input::Points(points),
max_edge: None,
}
}
pub fn from_distance_matrix(matrix: DissimilarityMatrixView<'a>) -> Self {
Self {
input: Input::Matrix(matrix),
max_edge: None,
}
}
pub fn from_distance_fn<P, F>(items: &'a [P], distance: F) -> RipsCallbackBuilder<'a, P, F>
where
F: FnMut(&P, &P) -> Result<f64>,
{
RipsCallbackBuilder {
items,
distance,
max_edge: None,
}
}
pub fn max_edge_length(mut self, value: f64) -> Self {
self.max_edge = Some(value);
self
}
pub fn full_range(mut self) -> Self {
self.max_edge = None;
self
}
pub fn prepare(&self) -> Result<ThresholdRips> {
self.prepare_with(&Execution::default())
}
pub fn prepare_with(&self, execution: &Execution<'_>) -> Result<ThresholdRips> {
self.validate()?;
self.prepare_budget(&mut WorkBudget::new(execution)?)
}
pub fn build_complex(&self, max_simplex_dimension: usize) -> Result<SimplicialFiltration> {
self.build_complex_with(max_simplex_dimension, &Execution::default())
}
pub fn build_complex_with(
&self,
max_simplex_dimension: usize,
execution: &Execution<'_>,
) -> Result<SimplicialFiltration> {
self.validate()?;
let mut budget = WorkBudget::new(execution)?;
self.prepare_budget(&mut budget)?
.build_complex_budget(max_simplex_dimension, &mut budget)
}
pub(crate) fn validate(&self) -> Result<()> {
crate::geometry::distance::cutoff(self.max_edge).map(|_| ())
}
pub(crate) fn prepare_budget(&self, budget: &mut WorkBudget<'_>) -> Result<ThresholdRips> {
exact::build_with(
self.input.len(),
self.max_edge,
self.input.kind(),
|a, b| self.input.distance(a, b),
&mut || budget.step(),
)
}
}
pub struct RipsCallbackBuilder<'a, P, F> {
items: &'a [P],
distance: F,
max_edge: Option<f64>,
}
impl<P, F: FnMut(&P, &P) -> Result<f64>> RipsCallbackBuilder<'_, P, F> {
pub fn max_edge_length(mut self, value: f64) -> Self {
self.max_edge = Some(value);
self
}
pub fn full_range(mut self) -> Self {
self.max_edge = None;
self
}
pub fn prepare(self) -> Result<ThresholdRips> {
self.prepare_with(&Execution::default())
}
pub fn prepare_with(self, execution: &Execution<'_>) -> Result<ThresholdRips> {
crate::geometry::distance::cutoff(self.max_edge)?;
self.prepare_budget(&mut WorkBudget::new(execution)?)
}
pub fn build_complex(self, max_simplex_dimension: usize) -> Result<SimplicialFiltration> {
self.build_complex_with(max_simplex_dimension, &Execution::default())
}
pub fn build_complex_with(
self,
max_simplex_dimension: usize,
execution: &Execution<'_>,
) -> Result<SimplicialFiltration> {
crate::geometry::distance::cutoff(self.max_edge)?;
let mut budget = WorkBudget::new(execution)?;
self.prepare_budget(&mut budget)?
.build_complex_budget(max_simplex_dimension, &mut budget)
}
fn prepare_budget(mut self, budget: &mut WorkBudget<'_>) -> Result<ThresholdRips> {
exact::build_with(
self.items.len(),
self.max_edge,
RipsInputKind::Custom,
|a, b| (self.distance)(&self.items[a], &self.items[b]),
&mut || budget.step(),
)
}
}