\section{RSAGL.Interpolation}
\begin{code}
module RSAGL.Interpolation
(lerp,
lerpBetween,
lerpBetweenMutated,
lerpBetweenClamped,
lerpBetweenClampedMutated,
lerp_mutator_continuous_1st,
lerpMap)
where
import RSAGL.AbstractVector
import Data.Ord
import Data.Map as Map
import Data.Maybe
\end{code}
\subsection{The Lerpable typeclass}
Implements linear interpolation.
\begin{code}
lerp :: (AbstractScale v,AbstractSubtract p v,AbstractAdd p v,Real r) => r -> (p,p) -> p
lerp u (a,b) = a `add` scalarMultiply (realToFrac u) (b `sub` a)
\end{code}
\subsection{Nonlinear interpolations}
The mutated versions of the lerp functions takes an arbitrary ``mutator function'' to define nonlinear interpolation curves.
The ``between'' versions of the lerp functions allow the uvalue to lie between any two numbers, as opposed to between 0 and 1.
The ``clamped'' versions of the lerp functions clamp the uvalue to lie between its boundaries. Otherwise, with nonclamped
interpolations, the uvalue may lie outside of its boundaries.
\begin{code}
lerpBetween :: (AbstractScale v,AbstractSubtract p v,AbstractAdd p v,Real r,Fractional r) => (r,r,r) -> (p,p) -> p
lerpBetween = lerpBetweenMutated id
lerpBetweenMutated :: (AbstractScale v,AbstractSubtract p v,AbstractAdd p v,Real r,Fractional r) => (r -> r) -> (r,r,r) -> (p,p) -> p
lerpBetweenMutated _ (l,_,r) | l == r = lerp 0.5
lerpBetweenMutated mutator (l,u,r) = lerp $ mutator $ (ul) / (rl)
lerpBetweenClamped :: (AbstractScale v,AbstractSubtract p v,AbstractAdd p v,Real r,Fractional r,Ord r) => (r,r,r) -> (p,p) -> p
lerpBetweenClamped = lerpBetweenClampedMutated id
lerpBetweenClampedMutated :: (AbstractScale v,AbstractSubtract p v,AbstractAdd p v,Real r,Fractional r,Ord r) => (r -> r) -> (r,r,r) -> (p,p) -> p
lerpBetweenClampedMutated mutator (l,u,r) = lerpBetweenMutated (lerp_mutator_clamp . mutator) (l,u,r)
\end{code}
\subsection{Lerp mutators}
\texttt{lerp\_mutator\_clamp} implements clamping between 0 and 1.
\begin{code}
lerp_mutator_clamp :: (Real r) => r -> r
lerp_mutator_clamp = min 1 . max 0
\end{code}
\texttt{lerp\_mutator\_continuous\_1st} implements clamping between 0 and 1, but such that the 1st derivative of the result is continuous.
\begin{code}
lerp_mutator_continuous_1st :: (Real r,Fractional r) => r -> r
lerp_mutator_continuous_1st x | x < 0 = 0
lerp_mutator_continuous_1st x | x > 1 = 1
lerp_mutator_continuous_1st x | x <= 0.5 = 2*x^2
lerp_mutator_continuous_1st x = 4*x 2*x^2 1
\end{code}
\subsection{lerpMap}
Given many entities, lerp between the two entities closest to the given point
on either side. For example, if we wanted to lerp between colors on a rainbow,
we might use the map [(0,red),(1,orange),(2,yellow),(3,green),(4,blue),(5,indigo),(6,violet)].
lerpMap 3.5 would result in a bluegreen color.
\begin{code}
lerpMap :: (Real r,Fractional r,AbstractScale v,AbstractSubtract p v,AbstractAdd p v) => [(r,p)] -> r -> p
lerpMap pts = lerpMapSorted $ Map.fromList pts
lerpMapSorted :: (Real r,Fractional r,AbstractScale v,AbstractSubtract p v,AbstractAdd p v) => Map r p -> r -> p
lerpMapSorted m _ | Map.null m = error "lerpMapSorted: empty map"
lerpMapSorted m u = case () of
() | isJust exactly -> fromJust exactly
() | Map.null lowers -> higher_a
() | Map.null highers -> lower_a
() -> lerpBetween (lower_r,u,higher_r) (lower_a,higher_a)
where (lowers, exactly, highers) = splitLookup u m
(lower_r,lower_a) = findMax lowers
(higher_r,higher_a) = findMin highers
\end{code}