{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Diagrams.Combinators
(
withEnvelope, withTrace
, phantom, strut
, pad, frame
, extrudeEnvelope, intrudeEnvelope
, atop
, beneath
, beside
, atDirection
, appends
, position, atPoints
, cat, cat'
, CatOpts(_catMethod, _sep), catMethod, sep
, CatMethod(..)
, composeAligned
) where
import Control.Lens hiding (beside, ( # ))
import Data.Default.Class
import Data.Maybe (fromJust)
import Data.Monoid.Deletable (toDeletable)
import Data.Monoid.MList (inj)
import Data.Proxy
import Data.Semigroup
import qualified Data.Tree.DUAL as D
import Diagrams.Core
import Diagrams.Core.Types (QDiagram (QD))
import Diagrams.Direction
import Diagrams.Names (named)
import Diagrams.Segment (straight)
import Diagrams.Util
import Linear.Affine
import Linear.Metric
import Linear.Vector
withEnvelope :: (InSpace v n a, Monoid' m, Enveloped a)
=> a -> QDiagram b v n m -> QDiagram b v n m
withEnvelope :: forall (v :: * -> *) n a m b.
(InSpace v n a, Monoid' m, Enveloped a) =>
a -> QDiagram b v n m -> QDiagram b v n m
withEnvelope = forall b (v :: * -> *) n m.
(OrderedField n, Metric v, Monoid' m) =>
Envelope v n -> QDiagram b v n m -> QDiagram b v n m
setEnvelope forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enveloped a => a -> Envelope (V a) (N a)
getEnvelope
withTrace :: (InSpace v n a, Metric v, OrderedField n, Monoid' m, Traced a)
=> a -> QDiagram b v n m -> QDiagram b v n m
withTrace :: forall (v :: * -> *) n a m b.
(InSpace v n a, Metric v, OrderedField n, Monoid' m, Traced a) =>
a -> QDiagram b v n m -> QDiagram b v n m
withTrace = forall b (v :: * -> *) n m.
(OrderedField n, Metric v, Semigroup m) =>
Trace v n -> QDiagram b v n m -> QDiagram b v n m
setTrace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Traced a => a -> Trace (V a) (N a)
getTrace
phantom :: (InSpace v n a, Monoid' m, Enveloped a, Traced a) => a -> QDiagram b v n m
phantom :: forall (v :: * -> *) n a m b.
(InSpace v n a, Monoid' m, Enveloped a, Traced a) =>
a -> QDiagram b v n m
phantom a
a = forall b (v :: * -> *) n m.
DUALTree
(DownAnnots v n) (UpAnnots b v n m) Annotation (QDiaLeaf b v n m)
-> QDiagram b v n m
QD forall a b. (a -> b) -> a -> b
$ forall u d a l. u -> DUALTree d u a l
D.leafU ((forall l a. (l :>: a) => a -> l
inj forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. m -> Deletable m
toDeletable forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enveloped a => a -> Envelope (V a) (N a)
getEnvelope forall a b. (a -> b) -> a -> b
$ a
a) forall a. Semigroup a => a -> a -> a
<> (forall l a. (l :>: a) => a -> l
inj forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. m -> Deletable m
toDeletable forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Traced a => a -> Trace (V a) (N a)
getTrace forall a b. (a -> b) -> a -> b
$ a
a))
pad :: (Metric v, OrderedField n, Monoid' m)
=> n -> QDiagram b v n m -> QDiagram b v n m
pad :: forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
n -> QDiagram b v n m -> QDiagram b v n m
pad n
s QDiagram b v n m
d = forall (v :: * -> *) n a m b.
(InSpace v n a, Monoid' m, Enveloped a) =>
a -> QDiagram b v n m -> QDiagram b v n m
withEnvelope (QDiagram b v n m
d forall a b. a -> (a -> b) -> b
# forall (v :: * -> *) n a.
(InSpace v n a, Eq n, Fractional n, Transformable a) =>
n -> a -> a
scale n
s) QDiagram b v n m
d
frame :: (Metric v, OrderedField n, Monoid' m)
=> n -> QDiagram b v n m -> QDiagram b v n m
frame :: forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
n -> QDiagram b v n m -> QDiagram b v n m
frame n
s = forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall n (v :: * -> *) m b.
(OrderedField n, Metric v, Monoid' m) =>
Lens' (QDiagram b v n m) (Envelope v n)
envelope (forall (v :: * -> *) n.
((v n -> n) -> v n -> n) -> Envelope v n -> Envelope v n
onEnvelope forall a b. (a -> b) -> a -> b
$ \v n -> n
f v n
x -> v n -> n
f v n
x forall a. Num a => a -> a -> a
+ n
s)
strut :: (Metric v, OrderedField n)
=> v n -> QDiagram b v n m
strut :: forall (v :: * -> *) n b m.
(Metric v, OrderedField n) =>
v n -> QDiagram b v n m
strut v n
v = forall b (v :: * -> *) n m.
DUALTree
(DownAnnots v n) (UpAnnots b v n m) Annotation (QDiaLeaf b v n m)
-> QDiagram b v n m
QD forall a b. (a -> b) -> a -> b
$ forall u d a l. u -> DUALTree d u a l
D.leafU (forall l a. (l :>: a) => a -> l
inj forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. m -> Deletable m
toDeletable forall a b. (a -> b) -> a -> b
$ Envelope v n
env)
where env :: Envelope v n
env = forall t. Transformable t => Vn t -> t -> t
translate ((-n
0.5) forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ v n
v) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enveloped a => a -> Envelope (V a) (N a)
getEnvelope forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) n. v n -> Segment Closed v n
straight v n
v
extrudeEnvelope
:: (Metric v, OrderedField n, Monoid' m)
=> v n -> QDiagram b v n m -> QDiagram b v n m
extrudeEnvelope :: forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
v n -> QDiagram b v n m -> QDiagram b v n m
extrudeEnvelope = forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
n -> v n -> QDiagram b v n m -> QDiagram b v n m
deformEnvelope n
1
intrudeEnvelope
:: (Metric v, OrderedField n, Monoid' m)
=> v n -> QDiagram b v n m -> QDiagram b v n m
intrudeEnvelope :: forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
v n -> QDiagram b v n m -> QDiagram b v n m
intrudeEnvelope = forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
n -> v n -> QDiagram b v n m -> QDiagram b v n m
deformEnvelope (-n
1)
deformEnvelope
:: (Metric v, OrderedField n, Monoid' m)
=> n -> v n -> QDiagram b v n m -> QDiagram b v n m
deformEnvelope :: forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
n -> v n -> QDiagram b v n m -> QDiagram b v n m
deformEnvelope n
s v n
v = forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over (forall n (v :: * -> *) m b.
(OrderedField n, Metric v, Monoid' m) =>
Lens' (QDiagram b v n m) (Envelope v n)
envelope forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t.
Rewrapping s t =>
(Unwrapped s -> s) -> Iso s t (Unwrapped s) (Unwrapped t)
_Wrapping forall (v :: * -> *) n. Maybe (v n -> Max n) -> Envelope v n
Envelope) Maybe (v n -> Max n) -> Maybe (v n -> Max n)
deformE
where
deformE :: Maybe (v n -> Max n) -> Maybe (v n -> Max n)
deformE = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (v n -> Max n) -> v n -> Max n
deformE'
deformE' :: (v n -> Max n) -> v n -> Max n
deformE' v n -> Max n
env v n
v'
| n
dp forall a. Ord a => a -> a -> Bool
> n
0 = forall a. a -> Max a
Max forall a b. (a -> b) -> a -> b
$ forall a. Max a -> a
getMax (v n -> Max n
env v n
v') forall a. Num a => a -> a -> a
+ (n
dp forall a. Num a => a -> a -> a
* n
s) forall a. Fractional a => a -> a -> a
/ forall (f :: * -> *) a. (Metric f, Num a) => f a -> a
quadrance v n
v'
| Bool
otherwise = v n -> Max n
env v n
v'
where
dp :: n
dp = v n
v' forall (f :: * -> *) a. (Metric f, Num a) => f a -> f a -> a
`dot` v n
v
beneath :: (Metric v, OrderedField n, Monoid' m)
=> QDiagram b v n m -> QDiagram b v n m -> QDiagram b v n m
beneath :: forall (v :: * -> *) n m b.
(Metric v, OrderedField n, Monoid' m) =>
QDiagram b v n m -> QDiagram b v n m -> QDiagram b v n m
beneath = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall n (v :: * -> *) m b.
(OrderedField n, Metric v, Semigroup m) =>
QDiagram b v n m -> QDiagram b v n m -> QDiagram b v n m
atop
infixl 6 `beneath`
beside :: (Juxtaposable a, Semigroup a) => Vn a -> a -> a -> a
beside :: forall a. (Juxtaposable a, Semigroup a) => Vn a -> a -> a -> a
beside Vn a
v a
d1 a
d2 = a
d1 forall a. Semigroup a => a -> a -> a
<> forall a. Juxtaposable a => Vn a -> a -> a -> a
juxtapose Vn a
v a
d1 a
d2
atDirection :: (InSpace v n a, Metric v, Floating n, Juxtaposable a, Semigroup a)
=> Direction v n -> a -> a -> a
atDirection :: forall (v :: * -> *) n a.
(InSpace v n a, Metric v, Floating n, Juxtaposable a,
Semigroup a) =>
Direction v n -> a -> a -> a
atDirection = forall a. (Juxtaposable a, Semigroup a) => Vn a -> a -> a -> a
beside forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) n.
(Metric v, Floating n) =>
Direction v n -> v n
fromDirection
appends :: (Juxtaposable a, Monoid' a) => a -> [(Vn a,a)] -> a
appends :: forall a. (Juxtaposable a, Monoid' a) => a -> [(Vn a, a)] -> a
appends a
d1 [(Vn a, a)]
apps = a
d1 forall a. Semigroup a => a -> a -> a
<> forall a. Monoid a => [a] -> a
mconcat (forall a b. (a -> b) -> [a] -> [b]
map (\(Vn a
v,a
d) -> forall a. Juxtaposable a => Vn a -> a -> a -> a
juxtapose Vn a
v a
d1 a
d) [(Vn a, a)]
apps)
position :: (InSpace v n a, HasOrigin a, Monoid' a) => [(Point v n, a)] -> a
position :: forall (v :: * -> *) n a.
(InSpace v n a, HasOrigin a, Monoid' a) =>
[(Point v n, a)] -> a
position = forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall (v :: * -> *) n t.
(InSpace v n t, HasOrigin t) =>
Point v n -> t -> t
moveTo)
atPoints :: (InSpace v n a, HasOrigin a, Monoid' a) => [Point v n] -> [a] -> a
atPoints :: forall (v :: * -> *) n a.
(InSpace v n a, HasOrigin a, Monoid' a) =>
[Point v n] -> [a] -> a
atPoints [Point v n]
ps [a]
as = forall (v :: * -> *) n a.
(InSpace v n a, HasOrigin a, Monoid' a) =>
[(Point v n, a)] -> a
position forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip [Point v n]
ps [a]
as
data CatMethod = Cat
| Distrib
data CatOpts n = CatOpts { forall n. CatOpts n -> CatMethod
_catMethod :: CatMethod
, forall n. CatOpts n -> n
_sep :: n
, forall n. CatOpts n -> Proxy n
catOptsvProxy :: Proxy n
}
makeLensesWith (lensRules & generateSignatures .~ False) ''CatOpts
catMethod :: Lens' (CatOpts n) CatMethod
sep :: Lens' (CatOpts n) n
instance Num n => Default (CatOpts n) where
def :: CatOpts n
def = CatOpts { _catMethod :: CatMethod
_catMethod = CatMethod
Cat
, _sep :: n
_sep = n
0
, catOptsvProxy :: Proxy n
catOptsvProxy = forall {k} (t :: k). Proxy t
Proxy
}
cat :: (InSpace v n a, Metric v, Floating n, Juxtaposable a, Monoid' a, HasOrigin a)
=> v n -> [a] -> a
cat :: forall (v :: * -> *) n a.
(InSpace v n a, Metric v, Floating n, Juxtaposable a, Monoid' a,
HasOrigin a) =>
v n -> [a] -> a
cat v n
v = forall (v :: * -> *) n a.
(InSpace v n a, Metric v, Floating n, Juxtaposable a, Monoid' a,
HasOrigin a) =>
v n -> CatOpts n -> [a] -> a
cat' v n
v forall a. Default a => a
def
cat' :: (InSpace v n a, Metric v, Floating n, Juxtaposable a, Monoid' a, HasOrigin a)
=> v n -> CatOpts n -> [a] -> a
cat' :: forall (v :: * -> *) n a.
(InSpace v n a, Metric v, Floating n, Juxtaposable a, Monoid' a,
HasOrigin a) =>
v n -> CatOpts n -> [a] -> a
cat' v n
v (CatOpts { _catMethod :: forall n. CatOpts n -> CatMethod
_catMethod = CatMethod
Cat, _sep :: forall n. CatOpts n -> n
_sep = n
s }) = forall a. (a -> a -> a) -> a -> [a] -> a
foldB a -> a -> a
comb forall a. Monoid a => a
mempty
where comb :: a -> a -> a
comb a
d1 a
d2 = a
d1 forall a. Semigroup a => a -> a -> a
<> (forall a. Juxtaposable a => Vn a -> a -> a -> a
juxtapose v n
v a
d1 a
d2 forall a b. a -> (a -> b) -> b
# forall t (v :: * -> *) n.
(V t ~ v, N t ~ n, HasOrigin t) =>
v n -> t -> t
moveOriginBy v n
vs)
vs :: v n
vs = n
s forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ forall (f :: * -> *) a. (Metric f, Floating a) => f a -> f a
signorm (forall (f :: * -> *) a. (Functor f, Num a) => f a -> f a
negated v n
v)
cat' v n
v (CatOpts { _catMethod :: forall n. CatOpts n -> CatMethod
_catMethod = CatMethod
Distrib, _sep :: forall n. CatOpts n -> n
_sep = n
s }) =
forall (v :: * -> *) n a.
(InSpace v n a, HasOrigin a, Monoid' a) =>
[(Point v n, a)] -> a
position forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [a] -> [b] -> [(a, b)]
zip (forall a. (a -> a) -> a -> [a]
iterate (forall (p :: * -> *) a. (Affine p, Num a) => p a -> Diff p a -> p a
.+^ (n
s forall (f :: * -> *) a. (Functor f, Num a) => a -> f a -> f a
*^ forall (f :: * -> *) a. (Metric f, Floating a) => f a -> f a
signorm v n
v)) forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin)
composeAligned
:: (Monoid' m, Floating n, Ord n, Metric v)
=> (QDiagram b v n m -> QDiagram b v n m)
-> ([QDiagram b v n m] -> QDiagram b v n m)
-> ([QDiagram b v n m] -> QDiagram b v n m)
composeAligned :: forall m n (v :: * -> *) b.
(Monoid' m, Floating n, Ord n, Metric v) =>
(QDiagram b v n m -> QDiagram b v n m)
-> ([QDiagram b v n m] -> QDiagram b v n m)
-> [QDiagram b v n m]
-> QDiagram b v n m
composeAligned QDiagram b v n m -> QDiagram b v n m
_ [QDiagram b v n m] -> QDiagram b v n m
combine [] = [QDiagram b v n m] -> QDiagram b v n m
combine []
composeAligned QDiagram b v n m -> QDiagram b v n m
algn [QDiagram b v n m] -> QDiagram b v n m
comb (QDiagram b v n m
d:[QDiagram b v n m]
ds) = ([QDiagram b v n m] -> QDiagram b v n m
comb forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map QDiagram b v n m -> QDiagram b v n m
algn (QDiagram b v n m
dforall a. a -> [a] -> [a]
:[QDiagram b v n m]
ds)) forall a b. a -> (a -> b) -> b
# forall t. HasOrigin t => Point (V t) (N t) -> t -> t
moveOriginTo Point v n
l
where
mss :: Maybe [Subdiagram b v n m]
mss = ( (() forall q a. (Qualifiable q, IsName a) => a -> q -> q
.>> QDiagram b v n m
d)
# named () -- Mark the origin
# algn -- Apply the alignment function
)
forall s a. s -> Getting a s a -> a
^. forall (v :: * -> *) m n b.
(Metric v, Semigroup m, OrderedField n) =>
Lens' (QDiagram b v n m) (SubMap b v n m)
subMap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
Control.Lens.at (forall a. IsName a => a -> Name
toName ())
l :: Point v n
l = forall (v :: * -> *) n b m.
(Additive v, Num n) =>
Subdiagram b v n m -> Point v n
location forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ Maybe [Subdiagram b v n m]
mss