Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measured diagrams #246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions diagrams-lib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Library
Diagrams.LinearMap,
Diagrams.Located,
Diagrams.Names,
Diagrams.Measured,
Diagrams.Parametric,
Diagrams.Parametric.Adjust,
Diagrams.Path,
Expand Down
88 changes: 88 additions & 0 deletions src/Diagrams/Measured.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
-----------------------------------------------------------------------------
-- |
-- Module : Diagrams.Measured
-- Copyright : (c) 2015 diagrams-lib team (see LICENSE)
-- License : BSD-style (see LICENSE)
-- Maintainer : [email protected]
--
-- 'Measured' diagrams allow access to 'global', 'normalized, 'output'
-- and 'local' units when making a diagram (normal diagrams are in
-- 'local' units). The downside is we can't make use of the 'Envelope'
-- or 'Trace'.
--
-----------------------------------------------------------------------------
module Diagrams.Measured
(
-- * Measured diagrams
MDiagram
, measuredDiagram

-- * Internals
, measuredLeaf
)
where

import Data.Semigroup
import Data.Monoid.Coproduct
import Data.Typeable
import Data.Traversable (Traversable)

import Diagrams.Core
import Diagrams.Core.Measure
import Diagrams.Core.Types
import Diagrams.Util

import Linear.Metric

type MDiagram b = Measured b (Diagram b)

-- | Turn a measured diagram into a 'Diagram' with a 'DelayedLeaf'. The
-- resulting diagram has no 'Envelope' or 'Trace'.
--
-- Units are accesable by the 'Functor' instance of 'Measured':
--
-- @
-- outputCircle = fc blue . circle <$> output 10 :: MDiagram B
-- @
--
-- Or the 'Monad' instance:
--
-- @
-- topRight :: Diagram B
-- topRight = measuredDiagram $ do
-- o <- output 10
-- return $ circle o
-- @
--
-- The envelope of the resulting diagram will have no 'Envelope' or
-- 'Trace'. You can either get another trace by combining it with
-- another diagram or set the envelope explicitly with the 'envelope'
-- lens.
--
-- The non-nscaling part of transforms are applied as normal, but only
-- 'local' units get scaled. Other sizes depend on the final size and
-- output of the diagram.
measuredDiagram :: (Metric v, Traversable v, OrderedField n, Typeable n, Monoid' m)
=> Measured n (QDiagram b v n m) -> QDiagram b v n m
measuredDiagram md
= mkQD' (measuredLeaf md)
mempty -- envelope
mempty -- trace
mempty -- submap
mempty -- query

-- | Turn a measured diagram into a 'DelayedLeaf'.
measuredLeaf :: (Metric v, Traversable v, OrderedField n, Typeable n, Monoid' m)
=> Measured n (QDiagram b v n m) -> QDiaLeaf b v n m
measuredLeaf md = DelayedLeaf delayedPrim
where
delayedPrim da g n =
unmeasure md (l,g,n)
# transform tr'
# applyStyle sty
where
tr' = tr <> scaling (1/l)
(tr, sty) = option mempty untangle . fst $ da
l = avgScale tr