generative-art-0.1.0.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Geometry.LookupTable.Lookup2

Description

Two-dimensional lookup tables.

Synopsis

Function cache

data LookupTable2 a Source #

Lookup table for a two-dimensional function. Created with lookupTable2.

Instances

Instances details
Show a => Show (LookupTable2 a) Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

NFData a => NFData (LookupTable2 a) Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

rnf :: LookupTable2 a -> () #

Eq a => Eq (LookupTable2 a) Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Ord a => Ord (LookupTable2 a) Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

data Grid Source #

Specification of a discrete grid, used for sampling contour lines.

Subdivide the unit square with 50 squares (51 steps!) in x direction, and 30 (31 steps!) in y direction:

Grid (Vec2 0 0, Vec2 1 1) (50, 30)

Constructors

Grid 

Fields

Instances

Instances details
Show Grid Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

showsPrec :: Int -> Grid -> ShowS #

show :: Grid -> String #

showList :: [Grid] -> ShowS #

NFData Grid Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

rnf :: Grid -> () #

Eq Grid Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

(==) :: Grid -> Grid -> Bool #

(/=) :: Grid -> Grid -> Bool #

Ord Grid Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

compare :: Grid -> Grid -> Ordering #

(<) :: Grid -> Grid -> Bool #

(<=) :: Grid -> Grid -> Bool #

(>) :: Grid -> Grid -> Bool #

(>=) :: Grid -> Grid -> Bool #

max :: Grid -> Grid -> Grid #

min :: Grid -> Grid -> Grid #

createLookupTable2 :: Grid -> (Vec2 -> a) -> LookupTable2 a Source #

Build a 2D lookup table, suitable for caching function calls. Values are initialized lazily, so that only repeated computations are sped up.

Example: lookup table for \(f(x,y) = x\cdot y\)

grid = Grid (Vec2 (-10) (-10), Vec2 10 10), (100, 100)
f (Vec2 x y) = x*y
table = createLookupTable2 grid f

lookupNearest :: LookupTable2 Double -> Vec2 -> Double Source #

Nearest neigbour lookup in a two-dimensional lookup table. Lookup outside of the lookup table’s domain is clamped to the table’s edges.

Compared to lookupBilinear this function works on types that don’t support arithmetic on them, and is faster. The downside is of course that only the values on the grid points are accessible, without any interpolation.

lookupBilinear :: LookupTable2 Double -> Vec2 -> Double Source #

Bilinear lookup in a two-dimensional lookup table. Lookup outside of the lookup table’s domain is clamped to the table’s edges, so while it will not make the program crash, the values are not useful.

This lookup approximates a function in the sense that

lookupBilinear (createLookupTable2 grid f) x ≈ f x

forLookupTable2_ :: Monad f => LookupTable2 a -> (a -> Vec2 -> IVec2 -> f b) -> f () Source #

Perform an action for each entry in the lookup table. Can be handy for plotting its contents.

grid = Grid (Vec2 0 0, Vec2 100, 100) (100, 100)
f (Vec2 x y) = x + sin y
table = createLookupTable2 grid f

forLookupTable2_ table $ val pos _ ->
    moveToVec pos
    showTextAligned HCenter VCenter (show val)

Technical utilities

data IVec2 Source #

Discrete Vec2. Useful as coordinate in a Vector (Vector a).

Constructors

IVec2 !Int !Int 

Instances

Instances details
Show IVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

showsPrec :: Int -> IVec2 -> ShowS #

show :: IVec2 -> String #

showList :: [IVec2] -> ShowS #

NFData IVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

rnf :: IVec2 -> () #

Eq IVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

(==) :: IVec2 -> IVec2 -> Bool #

(/=) :: IVec2 -> IVec2 -> Bool #

Ord IVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

compare :: IVec2 -> IVec2 -> Ordering #

(<) :: IVec2 -> IVec2 -> Bool #

(<=) :: IVec2 -> IVec2 -> Bool #

(>) :: IVec2 -> IVec2 -> Bool #

(>=) :: IVec2 -> IVec2 -> Bool #

max :: IVec2 -> IVec2 -> IVec2 #

min :: IVec2 -> IVec2 -> IVec2 #

data CIVec2 Source #

Continuous version of IVec2. Type-wise the same as Vec2, but it shows fractional grid coodrdinates, so we can express the fact that our lookup might be »between i and i+1« and we can interpolate.

Constructors

CIVec2 !Double !Double 

Instances

Instances details
Show CIVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

NFData CIVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

rnf :: CIVec2 -> () #

Eq CIVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

Methods

(==) :: CIVec2 -> CIVec2 -> Bool #

(/=) :: CIVec2 -> CIVec2 -> Bool #

Ord CIVec2 Source # 
Instance details

Defined in Geometry.LookupTable.Lookup2

roundCIVec2 :: CIVec2 -> IVec2 Source #

Round a »continuous integral« coordinate to a »proper integral« coordinate.

fromGrid Source #

Arguments

:: Grid 
-> IVec2

Discrete coordinate

-> Vec2

Continuous coordinate

Map a coordinate from the discrete grid to continuous space.

toGrid Source #

Arguments

:: Grid 
-> Vec2

Continuous coordinate

-> CIVec2

Continuous coordinate, scaled and clamped to grid dimensions. Suitable to be rounded to an IVec with roundCIVec2.

valueTable :: Grid -> (Vec2 -> a) -> Vector (Vector a) Source #

A raw value table, filled (lazily) by a function applied to the underlying Grid.

We first index by i and then j, so that vec!i!j has the intuitive meaning of »go in ix direction and then in jy. The drawback is that this makes the table look like downward columns of y values, indexed by x. The more common picture for at least me is to have line numbers and then rows in each line.