Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Grid.Internal.Convolution
Synopsis
- autoConvolute :: forall window dims f a b. (IsGrid dims, IsGrid window, Functor f) => (Grid window (Coord dims) -> f (Coord dims)) -> (f a -> b) -> Grid dims a -> Grid dims b
- convolute :: forall dims f a b. (Functor f, IsGrid dims) => (Coord dims -> f (Coord dims)) -> (f a -> b) -> Grid dims a -> Grid dims b
- clampBounds :: (IsGrid dims, Functor f) => f (Coord dims) -> f (Coord dims)
- wrapBounds :: (IsGrid dims, Functor f) => f (Coord dims) -> f (Coord dims)
- omitBounds :: (IsGrid dims, Functor f) => f (Coord dims) -> Compose f Maybe (Coord dims)
- window :: forall window dims. IsGrid window => Coord dims -> Grid window (Coord dims)
- class Neighboring dims
Documentation
Arguments
:: (IsGrid dims, IsGrid window, Functor f) | |
=> (Grid window (Coord dims) -> f (Coord dims)) | Restrict out of bounds coordinates in some way. Use |
-> (f a -> b) | Collapse the context down to a value |
-> Grid dims a | Starting grid |
-> Grid dims b |
Perform a computation based on the context surrounding a cell Good for doing things like Linear Image Filters (e.g. gaussian blur) or simulating Cellular Automata (e.g. Conway's game of life)
This function accepts a function which indicates what to do with
'out-of-bounds' indexes, clampWindow
, wrapWindow
and safeWindow
are examples.
It also acccepts a transformation function which operates over the functor created by the first parameter and collapses it down to a new value for the cell at that position.
This function is best used with Type Applications to denote the desired window size; the Grid passed to the given function contains the current cell (in the middle) and all the surrounding cells.
Here's an example of computing the average of all neighboring cells,
repeating values at the edge of the grid when indexes are out of bounds
(using clampWindow
)
gaussian :: (IsGrid dims) => Grid dims Double -> Grid dims Double gaussian = autoConvolute clampBounds avg where avg :: Grid '[3, 3] Double -> Double avg g = sum g / fromIntegral (length g)
Arguments
:: (Functor f, IsGrid dims) | |
=> (Coord dims -> f (Coord dims)) | Build a neighboring context within a functor from the current coord |
-> (f a -> b) | Collapse the context to a single value |
-> Grid dims a | Starting grid |
-> Grid dims b |
This is a fully generic version of autoConvolute
which allows
the user to provide a function which builds a context from the current
coord, then provides a collapsing function over the same functor.
clampBounds :: (IsGrid dims, Functor f) => f (Coord dims) -> f (Coord dims) Source #
Use with autoConvolute
; Clamp out-of-bounds coordinates to the nearest in-bounds coord.
wrapBounds :: (IsGrid dims, Functor f) => f (Coord dims) -> f (Coord dims) Source #
Use with autoConvolute
; Wrap out-of-bounds coordinates pac-man style to the other side of the grid
omitBounds :: (IsGrid dims, Functor f) => f (Coord dims) -> Compose f Maybe (Coord dims) Source #
Use with autoConvolute
; Out of bounds coords become Nothing
window :: forall window dims. IsGrid window => Coord dims -> Grid window (Coord dims) Source #
Given a coordinate generate a grid of size window
filled with
coordinates surrounding the given coord. Mostly used internally
class Neighboring dims Source #
Minimal complete definition
Instances
(KnownNat n, Neighboring ns) => Neighboring (n ': ns) Source # | |
Defined in Data.Grid.Internal.Grid Methods neighborCoords :: Grid (n ': ns) (Coord (n ': ns)) Source # | |
IsGrid (n ': ([] :: [Nat])) => Neighboring (n ': ([] :: [Nat])) Source # | |
Defined in Data.Grid.Internal.Grid Methods neighborCoords :: Grid (n ': []) (Coord (n ': [])) Source # |