Skip to content

Commit

Permalink
Move bilinear texture filtering to a trait
Browse files Browse the repository at this point in the history
Allows data types, where bilinear filtering doesnt make sense, to be
stored in the texture
  • Loading branch information
Dave Poulter committed Aug 2, 2018
1 parent 2ceaad8 commit ef28185
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
use std::cmp;
use std::ops::{Add, Mul};

pub trait Bilinear {
type Item;
fn bilinear(&self, x: f64, y: f64) -> Self::Item;
}

#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct Tile {
pub x: usize,
Expand Down Expand Up @@ -93,7 +98,7 @@ impl Iterator for Tile {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Texture<T>
where
T: Send + Sync + Mul<f64, Output = T> + Add<Output = T> + Copy + Default,
T: Copy + Clone + Default,
{
pub width: usize,
pub height: usize,
Expand All @@ -102,7 +107,7 @@ where

impl<T> Texture<T>
where
T: Send + Sync + Mul<f64, Output = T> + Add<Output = T> + Copy + Default,
T: Copy + Clone + Default,
{
pub fn blank(width: usize, height: usize) -> Texture<T> {
Texture {
Expand Down Expand Up @@ -185,8 +190,26 @@ where
}
}

/// Iterate over the image in fixed size square tiles
pub fn tiles(&mut self, size: usize) -> TileIterator {
TileIterator {
width: self.width,
height: self.height,
size,
x: 0,
y: 0,
}
}
}

impl<T> Bilinear for Texture<T>
where
T: Mul<f64, Output = T> + Add<Output = T> + Copy + Default,
{
type Item = T;

/// Return a bilinearly filtered value from the texture
pub fn bilinear(&self, x: f64, y: f64) -> T {
fn bilinear(&self, x: f64, y: f64) -> T {
if x < 0.0
|| x + 1.0 >= self.width as f64
|| y < 0.0
Expand All @@ -210,17 +233,6 @@ where

a + b + c + d
}

/// Iterate over the image in fixed size square tiles
pub fn tiles(&mut self, size: usize) -> TileIterator {
TileIterator {
width: self.width,
height: self.height,
size,
x: 0,
y: 0,
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit ef28185

Please sign in to comment.