{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Diagrams.Core.Compile
(
RNode(..)
, RTree
, toRTree
, renderDia
, renderDiaT
, toDTree
, fromDTree
)
where
import qualified Data.List.NonEmpty as NEL
import Data.Maybe (fromMaybe)
import Data.Monoid.Coproduct
import Data.Monoid.MList
import Data.Monoid.WithSemigroup (Monoid')
import Data.Semigroup
import Data.Tree
import Data.Tree.DUAL
import Data.Typeable
import Diagrams.Core.Envelope (OrderedField, diameter)
import Diagrams.Core.Style
import Diagrams.Core.Transform
import Diagrams.Core.Types
import Linear.Metric hiding (qd)
#if __GLASGOW_HASKELL__ >= 707
#define Typeable1 Typeable
#endif
emptyDTree :: Tree (DNode b v n a)
emptyDTree :: forall b (v :: * -> *) n a. Tree (DNode b v n a)
emptyDTree = forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. DNode b v n a
DEmpty []
uncurry3 :: (a -> b -> c -> r) -> (a, b, c) -> r
uncurry3 :: forall a b c r. (a -> b -> c -> r) -> (a, b, c) -> r
uncurry3 a -> b -> c -> r
f (a
x, b
y, c
z) = a -> b -> c -> r
f a
x b
y c
z
toDTree :: (HasLinearMap v, Floating n, Typeable n)
=> n -> n -> QDiagram b v n m -> Maybe (DTree b v n Annotation)
toDTree :: forall (v :: * -> *) n b m.
(HasLinearMap v, Floating n, Typeable n) =>
n -> n -> QDiagram b v n m -> Maybe (DTree b v n Annotation)
toDTree n
g n
n (QD DUALTree
(DownAnnots v n) (UpAnnots b v n m) Annotation (QDiaLeaf b v n m)
qd)
= forall d l r a u.
(Semigroup d, Monoid d) =>
(d -> l -> r)
-> r
-> (NonEmpty r -> r)
-> (d -> r -> r)
-> (a -> r -> r)
-> DUALTree d u a l
-> Maybe r
foldDUAL
(\DownAnnots v n
d -> forall b (v :: * -> *) n r m.
(Prim b v n -> r)
-> ((DownAnnots v n -> n -> n -> QDiagram b v n m) -> r)
-> QDiaLeaf b v n m
-> r
withQDiaLeaf
(\Prim b v n
p -> forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. Prim b v n -> DNode b v n a
DPrim Prim b v n
p) [])
(forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. DNode b v n a
DDelay forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe forall b (v :: * -> *) n a. Tree (DNode b v n a)
emptyDTree forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) n b m.
(HasLinearMap v, Floating n, Typeable n) =>
n -> n -> QDiagram b v n m -> Maybe (DTree b v n Annotation)
toDTree n
g n
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. (a -> b) -> a -> b
$ (DownAnnots v n
d, n
g, n
n)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c r. (a -> b -> c -> r) -> (a, b, c) -> r
uncurry3)
)
forall b (v :: * -> *) n a. Tree (DNode b v n a)
emptyDTree
(\NonEmpty (DTree b v n Annotation)
ts -> case forall a. NonEmpty a -> [a]
NEL.toList NonEmpty (DTree b v n Annotation)
ts of
[DTree b v n Annotation
t] -> DTree b v n Annotation
t
[DTree b v n Annotation]
ts' -> forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. DNode b v n a
DEmpty [DTree b v n Annotation]
ts'
)
(\DownAnnots v n
d DTree b v n Annotation
t -> case forall l a. (l :>: a) => l -> Maybe a
get DownAnnots v n
d of
Maybe (Transformation v n :+: Style v n)
Nothing -> DTree b v n Annotation
t
Just Transformation v n :+: Style v n
d' ->
let (Transformation v n
tr,Style v n
sty) = forall m n. (Action m n, Monoid m, Monoid n) => (m :+: n) -> (m, n)
untangle Transformation v n :+: Style v n
d'
in forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. Style v n -> DNode b v n a
DStyle Style v n
sty) [forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. Transformation v n -> DNode b v n a
DTransform Transformation v n
tr) [DTree b v n Annotation
t]]
)
(\Annotation
a DTree b v n Annotation
t -> forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. a -> DNode b v n a
DAnnot Annotation
a) [DTree b v n Annotation
t])
DUALTree
(DownAnnots v n) (UpAnnots b v n m) Annotation (QDiaLeaf b v n m)
qd
fromDTree :: forall b v n. (Floating n, HasLinearMap v)
=> DTree b v n Annotation -> RTree b v n Annotation
fromDTree :: forall b (v :: * -> *) n.
(Floating n, HasLinearMap v) =>
DTree b v n Annotation -> RTree b v n Annotation
fromDTree = Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' forall a. Monoid a => a
mempty
where
fromDTree' :: Transformation v n -> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' :: Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' Transformation v n
accTr (Node (DPrim Prim b v n
p) [DTree b v n Annotation]
_)
= forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. Prim b v n -> RNode b v n a
RPrim (forall t. Transformable t => Transformation (V t) (N t) -> t -> t
transform Transformation v n
accTr Prim b v n
p)) []
fromDTree' Transformation v n
accTr (Node (DStyle Style v n
s) [DTree b v n Annotation]
ts)
= forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. Style v n -> RNode b v n a
RStyle (forall t. Transformable t => Transformation (V t) (N t) -> t -> t
transform Transformation v n
accTr Style v n
s)) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' Transformation v n
accTr) [DTree b v n Annotation]
ts)
fromDTree' Transformation v n
accTr (Node (DTransform Transformation v n
tr) [DTree b v n Annotation]
ts)
= forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. RNode b v n a
REmpty (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' (Transformation v n
accTr forall a. Semigroup a => a -> a -> a
<> Transformation v n
tr)) [DTree b v n Annotation]
ts)
fromDTree' Transformation v n
accTr (Node (DAnnot Annotation
a) [DTree b v n Annotation]
ts)
= forall a. a -> [Tree a] -> Tree a
Node (forall b (v :: * -> *) n a. a -> RNode b v n a
RAnnot Annotation
a) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' Transformation v n
accTr) [DTree b v n Annotation]
ts)
fromDTree' Transformation v n
_ (Node DNode b v n Annotation
DDelay [DTree b v n Annotation]
ts)
= forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. RNode b v n a
REmpty (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' forall a. Monoid a => a
mempty) [DTree b v n Annotation]
ts)
fromDTree' Transformation v n
accTr (Node DNode b v n Annotation
_ [DTree b v n Annotation]
ts)
= forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. RNode b v n a
REmpty (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Transformation v n
-> DTree b v n Annotation -> RTree b v n Annotation
fromDTree' Transformation v n
accTr) [DTree b v n Annotation]
ts)
toRTree
:: (HasLinearMap v, Metric v, Typeable n,
OrderedField n, Monoid m, Semigroup m)
=> Transformation v n -> QDiagram b v n m -> RTree b v n Annotation
toRTree :: forall (v :: * -> *) n m b.
(HasLinearMap v, Metric v, Typeable n, OrderedField n, Monoid m,
Semigroup m) =>
Transformation v n -> QDiagram b v n m -> RTree b v n Annotation
toRTree Transformation v n
globalToOutput QDiagram b v n m
d
= (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) n b a.
(Style v n -> Style v n) -> RNode b v n a -> RNode b v n a
onRStyle) (forall n (v :: * -> *). Num n => n -> n -> Style v n -> Style v n
unmeasureAttrs n
gToO n
nToO)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b (v :: * -> *) n.
(Floating n, HasLinearMap v) =>
DTree b v n Annotation -> RTree b v n Annotation
fromDTree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe (forall a. a -> [Tree a] -> Tree a
Node forall b (v :: * -> *) n a. DNode b v n a
DEmpty [])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) n b m.
(HasLinearMap v, Floating n, Typeable n) =>
n -> n -> QDiagram b v n m -> Maybe (DTree b v n Annotation)
toDTree n
gToO n
nToO
forall a b. (a -> b) -> a -> b
$ QDiagram b v n m
d
where
gToO :: n
gToO = forall (v :: * -> *) n.
(Additive v, Traversable v, Floating n) =>
Transformation v n -> n
avgScale Transformation v n
globalToOutput
nToO :: n
nToO = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product (forall a b. (a -> b) -> [a] -> [b]
map (forall a (v :: * -> *) n.
(V a ~ v, N a ~ n, Enveloped a) =>
v n -> a -> n
`diameter` QDiagram b v n m
d) forall (t :: * -> *) a. (Additive t, Traversable t, Num a) => [t a]
basis) forall a. Floating a => a -> a -> a
** (n
1 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. (Additive (V a), Traversable (V a)) => a -> Int
dimension QDiagram b v n m
d))
onRStyle :: (Style v n -> Style v n) -> RNode b v n a -> RNode b v n a
onRStyle :: forall (v :: * -> *) n b a.
(Style v n -> Style v n) -> RNode b v n a -> RNode b v n a
onRStyle Style v n -> Style v n
f (RStyle Style v n
s) = forall b (v :: * -> *) n a. Style v n -> RNode b v n a
RStyle (Style v n -> Style v n
f Style v n
s)
onRStyle Style v n -> Style v n
_ RNode b v n a
n = RNode b v n a
n
renderDiaT
:: (Backend b v n , HasLinearMap v, Metric v,
Typeable n, OrderedField n, Monoid' m)
=> b -> Options b v n -> QDiagram b v n m -> (Transformation v n, Result b v n)
renderDiaT :: forall b (v :: * -> *) n m.
(Backend b v n, HasLinearMap v, Metric v, Typeable n,
OrderedField n, Monoid' m) =>
b
-> Options b v n
-> QDiagram b v n m
-> (Transformation v n, Result b v n)
renderDiaT b
b Options b v n
opts QDiagram b v n m
d = (Transformation v n
g2o, forall b (v :: * -> *) n.
Backend b v n =>
b -> Options b v n -> RTree b v n Annotation -> Result b v n
renderRTree b
b Options b v n
opts' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> *) n m b.
(HasLinearMap v, Metric v, Typeable n, OrderedField n, Monoid m,
Semigroup m) =>
Transformation v n -> QDiagram b v n m -> RTree b v n Annotation
toRTree Transformation v n
g2o forall a b. (a -> b) -> a -> b
$ QDiagram b v n m
d')
where (Options b v n
opts', Transformation v n
g2o, QDiagram b v n m
d') = forall b (v :: * -> *) n m.
(Backend b v n, Additive v, Monoid' m, Num n) =>
b
-> Options b v n
-> QDiagram b v n m
-> (Options b v n, Transformation v n, QDiagram b v n m)
adjustDia b
b Options b v n
opts QDiagram b v n m
d
renderDia
:: (Backend b v n , HasLinearMap v, Metric v,
Typeable n, OrderedField n, Monoid' m)
=> b -> Options b v n -> QDiagram b v n m -> Result b v n
renderDia :: forall b (v :: * -> *) n m.
(Backend b v n, HasLinearMap v, Metric v, Typeable n,
OrderedField n, Monoid' m) =>
b -> Options b v n -> QDiagram b v n m -> Result b v n
renderDia b
b Options b v n
opts QDiagram b v n m
d = forall a b. (a, b) -> b
snd (forall b (v :: * -> *) n m.
(Backend b v n, HasLinearMap v, Metric v, Typeable n,
OrderedField n, Monoid' m) =>
b
-> Options b v n
-> QDiagram b v n m
-> (Transformation v n, Result b v n)
renderDiaT b
b Options b v n
opts QDiagram b v n m
d)