{-# LANGUAGE ConstraintKinds       #-}
{-# LANGUAGE CPP                   #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE MonoLocalBinds        #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Diagrams.Core.Compile
-- Copyright   :  (c) 2013-2015 diagrams-core team (see LICENSE)
-- License     :  BSD-style (see LICENSE)
-- Maintainer  :  diagrams-discuss@googlegroups.com
--
-- This module provides tools for compiling @QDiagrams@ into a more
-- convenient and optimized tree form, suitable for use by backends.
--
-----------------------------------------------------------------------------

module Diagrams.Core.Compile
  ( -- * Tools for backends
    RNode(..)
  , RTree
  , toRTree

    -- * Backend API

  , renderDia
  , renderDiaT

    -- * Internals

  , 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)

-- Typeable1 is a depreciated synonym in ghc > 707
#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

-- | Convert a @QDiagram@ into a raw tree.
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

      -- Prims at the leaves.  We ignore the accumulated d-annotations
      -- for prims (since we instead distribute them incrementally
      -- throughout the tree as they occur), or pass them to the
      -- continuation in the case of a delayed node.
      (\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: make a leaf node
               (\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) [])

               -- Delayed tree: pass the accumulated d-annotations to
               -- the continuation, convert the result to a DTree, and
               -- splice it in, adding a DDelay node to mark the point
               -- of the splice.
               (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)
      )

      -- u-only leaves --> empty DTree. We don't care about the
      -- u-annotations.
      forall b (v :: * -> *) n a. Tree (DNode b v n a)
emptyDTree

      -- a non-empty list of child trees.
      (\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'
      )

      -- Internal d-annotations.  We untangle the interleaved
      -- transformations and style, and carefully place the style
      -- /above/ the transform in the tree (since by calling
      -- 'untangle' we have already performed the action of the
      -- transform on the style).
      (\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]]
      )

      -- Internal a-annotations.
      (\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

-- | Convert a @DTree@ to an @RTree@ which can be used directly by backends.
--   A @DTree@ includes nodes of type @DTransform (Transformation v)@;
--   in the @RTree@ transform is pushed down until it reaches a primitive node.
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
    -- We put the accumulated transformation (accTr) and the prim
    -- into an RPrim node.
    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)) []

    -- Styles are transformed then stored in their own node
    -- and accTr is push down the tree.
    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)

    -- Transformations are accumulated and pushed down as well.
    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)

    -- Drop accumulated transformations upon encountering a DDelay
    -- node --- the tree unfolded beneath it already took into account
    -- any transformation at this point.
    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)

    -- DEmpty nodes become REmpties, again accTr flows through.
    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)

-- | Compile a @QDiagram@ into an 'RTree', rewriting styles with the
--   given function along the way.  Suitable for use by backends when
--   implementing 'renderData'.  The first argument is the
--   transformation used to convert the diagram from local to output
--   units.
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

    -- Scaling factor from normalized units to output units: nth root
    -- of product of diameters along each basis direction.  Note at
    -- this point the diagram has already had the globalToOutput
    -- transformation applied, so output = global = local units.
    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))

-- | Apply a style transformation on 'RStyle' nodes; the identity for
--   other 'RNode's.
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

--------------------------------------------------

-- | Render a diagram, returning also the transformation which was
--   used to convert the diagram from its (\"global\") coordinate
--   system into the output coordinate system.  The inverse of this
--   transformation can be used, for example, to convert output/screen
--   coordinates back into diagram coordinates.  See also 'adjustDia'.
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

-- | Render a diagram.
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)