use crate::{gaussian, utils, DataType, Properties, RenderPolicy};
use crate::patch::{PatchFactory, SigmaFactory};
use imp::{ImageMut, OwnedImage};
use locs::{UncertainLocalisation};
fn render_patch<I: ImageMut<Data=DataType>>(localisation: (f64, f64), pixel: (usize, usize), patch: (usize, usize), sigma_nm: f64, mut image: I, properties: &Properties) -> Result<(), String>
{
let (n_rows, n_cols) = image.shape();
let (x, y) = localisation;
let (row, col) = pixel;
let (patch_height, patch_width) = patch;
let patch_rows = (patch_height / 2) + 1;
let patch_cols = (patch_width / 2) + 1;
let image_row_pos = row + patch_rows;
let image_row_neg = row.checked_sub(patch_rows);
let image_col_pos = col + patch_cols;
let image_col_neg = col.checked_sub(patch_cols);
let row_start = if image_row_neg.is_some() {image_row_neg.unwrap()} else{0};
let row_end = std::cmp::min(image_row_pos, n_rows);
let col_start = if image_col_neg.is_some() {image_col_neg.unwrap()} else{0};
let col_end = std::cmp::min(image_col_pos, n_cols);
for row in row_start..row_end
{
for col in col_start..col_end
{
let ((x_lower, x_upper), (y_lower, y_upper)) = utils::pixel_boundaries((row, col), properties.pixel_size());
let x_lower = x - x_lower;
let x_upper = x_upper - x;
let y_lower = y - y_lower;
let y_upper = y_upper - y;
let value = gaussian::integral_over(x_lower, x_upper, y_lower, y_upper, sigma_nm);
*image.get_at_mut(row, col) += value;
}
}
Ok(())
}
#[derive(Debug, Clone)]
pub struct IntegralPatchRenderer<F>
{
factory: F
}
impl IntegralPatchRenderer<SigmaFactory>
{
pub fn with_sigma(sigma: f64) -> Self
{
Self::new(SigmaFactory::new(sigma))
}
}
impl<F> IntegralPatchRenderer<F>
{
pub fn new(factory: F) -> Self
{
Self{factory}
}
}
impl<F: PatchFactory> IntegralPatchRenderer<F>
{
fn render_as_patch<L: UncertainLocalisation, I: ImageMut<Data=DataType>>(&self, localisation: L, image: I, properties: &Properties) -> Result<(), String>
{
let col = properties.get_column(localisation.x());
let row = properties.get_row(localisation.y());
let sigma_nm = localisation.uncertainty();
let patch_size = self.factory.get_patch(sigma_nm, &properties)?;
let l = (localisation.x(), localisation.y());
let pixel = (row, col);
let patch = (patch_size.height(), patch_size.width());
let _ = render_patch(l, pixel, patch, sigma_nm, image, properties);
Ok(())
}
}
impl<F: PatchFactory, L: UncertainLocalisation> RenderPolicy<L> for IntegralPatchRenderer<F>
{
type Output = ();
type Error = String;
fn render(&self, localisation: L, image: &mut OwnedImage<DataType>, properties: &Properties) -> Result<Self::Output, Self::Error>
{
self.render_as_patch(localisation, image, properties)
}
}
#[cfg(test)]
mod tests
{
}