{-# LANGUAGE OverloadedStrings #-}
module Data.Spline.Key (
Key(..)
, keyValue
, interpolateKeys
, normalizeSampling
) where
import Data.Aeson
import Data.Text ( Text )
import Linear
data Key a
= Hold a
| Linear a
| Cosine a
| CubicHermite a
| Bezier a a a
deriving (Eq,Functor,Show)
instance (FromJSON a) => FromJSON (Key a) where
parseJSON = withObject "key" $ \o -> do
interpolation :: String <- o .: "interpolation"
value <- o .: "value"
if
| interpolation == "hold" -> pure (Hold value)
| interpolation == "linear" -> pure (Linear value)
| interpolation == "cosine" -> pure (Cosine value)
| interpolation == "cubic-hermite" -> pure (CubicHermite value)
| interpolation == "bezier" -> do
left <- o .: "left"
right <- o .: "right"
pure $ Bezier left value right
| otherwise -> fail "unknown interpolation mode"
instance (ToJSON a) => ToJSON (Key a) where
toJSON k = object $
["value" .= value,"interpolation" .= interpolation] ++ tangents
where
value = keyValue k
interpolation = keyInterpolation k
tangents = case k of
Bezier l _ r -> ["left" .= l,"right" .= r]
_ -> []
keyValue :: Key a -> a
keyValue k = case k of
Hold a -> a
Linear a -> a
Cosine a -> a
CubicHermite a -> a
Bezier _ a _ -> a
keyInterpolation :: Key a -> Text
keyInterpolation k = case k of
Hold{} -> "hold"
Linear{} -> "linear"
Cosine{} -> "cosine"
CubicHermite{} -> "cubic-hermite"
Bezier{} -> "bezier"
interpolateKeys :: (Additive a,Floating s) => s -> Key (a s) -> Key (a s) -> a s
interpolateKeys s start end = case start of
Hold k -> k
Linear k -> lerp s b k
Cosine k -> lerp ((1 - cos (s * pi)) * 0.5) b k
CubicHermite k -> lerp (s * s * (3 - 2 * s)) b k
Bezier _ k0 r0 -> case end of
Bezier l1 k1 _ -> interpolateBezier s k0 r0 l1 k1
_ -> interpolateBezier s k0 r0 r0 b
where
b = keyValue end
interpolateBezier :: (Additive a,Floating s)
=> s
-> a s
-> a s
-> a s
-> a s
-> a s
interpolateBezier s k0 r0 l1 k1 =
k0 ^* ms3 ^+^ r0 ^* (3 * ms2 * s) ^+^ l1 ^* (3 * ms * s2) ^+^ k1 ^* s3
where
ms = 1 - s
ms2 = ms * ms
ms3 = ms2 * ms
s2 = s * s
s3 = s2 * s
normalizeSampling :: (Fractional s)
=> (a s -> s)
-> s
-> Key (a s)
-> Key (a s)
-> s
normalizeSampling sampler s k0 k1 = (s - s0) / (s1 - s0)
where
s0 = sampler (keyValue k0)
s1 = sampler (keyValue k1)