| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE FlexibleContexts #-} |
| 3 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 4 |
{-# LANGUAGE TemplateHaskell #-} |
| 5 |
{-# LANGUAGE TypeFamilies #-} |
| 6 |
----------------------------------------------------------------------------- |
| 7 |
-- | |
| 8 |
-- Module : Diagrams.TwoD.Model |
| 9 |
-- Copyright : (c) 2011 diagrams-lib team (see LICENSE) |
| 10 |
-- License : BSD-style (see LICENSE) |
| 11 |
-- Maintainer : [email protected] |
| 12 |
-- |
| 13 |
-- Tools for visualizing diagrams' internal model: local origins, |
| 14 |
-- envelopes, traces, /etc./ |
| 15 |
-- |
| 16 |
----------------------------------------------------------------------------- |
| 17 |
module Diagrams.TwoD.Model |
| 18 |
( -- * Showing the local origin |
| 19 |
showOrigin |
| 20 |
, showOrigin' |
| 21 |
, OriginOpts(..), oColor, oScale, oMinSize |
| 22 |
|
| 23 |
-- * Showing an approximation of the envelope |
| 24 |
, showEnvelope |
| 25 |
, showEnvelope' |
| 26 |
, EnvelopeOpts(..), eColor, eLineWidth, ePoints |
| 27 |
|
| 28 |
-- * Showing an approximation of the trace |
| 29 |
, showTrace |
| 30 |
, showTrace' |
| 31 |
, TraceOpts(..), tColor, tScale, tMinSize, tPoints |
| 32 |
|
| 33 |
-- * Showing labels of all named subdiagrams |
| 34 |
, showLabels |
| 35 |
) where |
| 36 |
|
| 37 |
import Control.Arrow (second) |
| 38 |
import Control.Lens (makeLenses, (^.)) |
| 39 |
import Data.Colour (Colour) |
| 40 |
import Data.Colour.Names |
| 41 |
import Data.Default |
| 42 |
import Data.List (intercalate) |
| 43 |
import qualified Data.Map as M |
| 44 |
import Data.Maybe (catMaybes) |
| 45 |
import Data.Semigroup |
| 46 |
|
| 47 |
import Diagrams.Attributes |
| 48 |
import Diagrams.Combinators (atPoints) |
| 49 |
import Diagrams.Core |
| 50 |
import Diagrams.Core.Names |
| 51 |
import Diagrams.CubicSpline |
| 52 |
import Diagrams.Path |
| 53 |
import Diagrams.TwoD.Attributes |
| 54 |
import Diagrams.TwoD.Ellipse |
| 55 |
import Diagrams.TwoD.Path |
| 56 |
import Diagrams.TwoD.Text |
| 57 |
import Diagrams.TwoD.Transform (rotateBy) |
| 58 |
import Diagrams.TwoD.Types |
| 59 |
import Diagrams.TwoD.Vector (unitX) |
| 60 |
import Diagrams.Util |
| 61 |
|
| 62 |
import Linear.Affine |
| 63 |
import Linear.Vector |
| 64 |
|
| 65 |
------------------------------------------------------------ |
| 66 |
-- Marking the origin |
| 67 |
------------------------------------------------------------ |
| 68 |
|
| 69 |
data OriginOpts n = OriginOpts |
| 70 |
{ _oColor :: Colour Double |
| 71 |
, _oScale :: n |
| 72 |
, _oMinSize :: n |
| 73 |
} |
| 74 |
|
| 75 |
makeLenses ''OriginOpts |
| 76 |
|
| 77 |
instance Fractional n => Default (OriginOpts n) where |
| 78 |
def = OriginOpts red (1/50) 0.001 |
| 79 |
|
| 80 |
data EnvelopeOpts n = EnvelopeOpts |
| 81 |
{ _eColor :: Colour Double |
| 82 |
, _eLineWidth :: Measure n |
| 83 |
, _ePoints :: Int |
| 84 |
} |
| 85 |
|
| 86 |
makeLenses ''EnvelopeOpts |
| 87 |
|
| 88 |
instance OrderedField n => Default (EnvelopeOpts n) where |
| 89 |
def = EnvelopeOpts red medium 32 |
| 90 |
|
| 91 |
data TraceOpts n = TraceOpts |
| 92 |
{ _tColor :: Colour Double |
| 93 |
, _tScale :: n |
| 94 |
, _tMinSize :: n |
| 95 |
, _tPoints :: Int |
| 96 |
} |
| 97 |
|
| 98 |
makeLenses ''TraceOpts |
| 99 |
|
| 100 |
instance Floating n => Default (TraceOpts n) where |
| 101 |
def = TraceOpts red (1/100) 0.001 64 |
| 102 |
|
| 103 |
|
| 104 |
showOrigin :: (TypeableFloat n, Renderable (Path V2 n) b, Monoid' m) |
| 105 |
=> QDiagram b V2 n m -> QDiagram b V2 n m |
| 106 |
showOrigin = showOrigin' def |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
showOrigin' :: (TypeableFloat n, Renderable (Path V2 n) b, Monoid' m) |
| 111 |
=> OriginOpts n -> QDiagram b V2 n m -> QDiagram b V2 n m |
| 112 |
showOrigin' oo d = o <> d |
| 113 |
where o = strokeP (circle sz) |
| 114 |
# fc (oo^.oColor) |
| 115 |
# lw none |
| 116 |
# fmap (const mempty) |
| 117 |
V2 w h = oo^.oScale *^ size d |
| 118 |
sz = maximum [w, h, oo^.oMinSize] |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
showEnvelope' :: (Enum n, TypeableFloat n, Renderable (Path V2 n) b) |
| 123 |
=> EnvelopeOpts n -> QDiagram b V2 n Any -> QDiagram b V2 n Any |
| 124 |
showEnvelope' opts d = cubicSpline True pts # lc (opts^.eColor) |
| 125 |
# lw w <> d |
| 126 |
where |
| 127 |
pts = catMaybes [envelopePMay v d | v <- map (`rotateBy` unitX) [0,inc..top]] |
| 128 |
w = opts ^. eLineWidth |
| 129 |
inc = 1 / fromIntegral (opts^.ePoints) |
| 130 |
top = 1 - inc |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
showEnvelope :: (Enum n, TypeableFloat n, Renderable (Path V2 n) b) |
| 136 |
=> QDiagram b V2 n Any -> QDiagram b V2 n Any |
| 137 |
showEnvelope = showEnvelope' def |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
showTrace' :: (Enum n, TypeableFloat n, Renderable (Path V2 n) b) |
| 142 |
=> TraceOpts n -> QDiagram b V2 n Any -> QDiagram b V2 n Any |
| 143 |
showTrace' opts d = atPoints ps (repeat pt) <> d |
| 144 |
where |
| 145 |
ps = concatMap p ts |
| 146 |
ts = zip rs vs |
| 147 |
p (r, v) = [origin .+^ (s *^ v) | s <- r] |
| 148 |
vs = map (`rotateBy` unitX) [0, inc..top] |
| 149 |
rs = [getSortedList $ (appTrace . getTrace) d origin v | v <- vs] |
| 150 |
pt = circle sz # fc (opts^.tColor) # lw none |
| 151 |
V2 w h = opts^.tScale *^ size d |
| 152 |
sz = maximum [w, h, opts^.tMinSize] |
| 153 |
inc = 1 / fromIntegral (opts^.tPoints) |
| 154 |
top = 1 - inc |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
showTrace :: (Enum n, TypeableFloat n, Renderable (Path V2 n) b) |
| 159 |
=> QDiagram b V2 n Any -> QDiagram b V2 n Any |
| 160 |
showTrace = showTrace' def |
| 161 |
|
| 162 |
------------------------------------------------------------ |
| 163 |
-- Labeling named points |
| 164 |
------------------------------------------------------------ |
| 165 |
|
| 166 |
showLabels :: (TypeableFloat n, Renderable (Text n) b, Semigroup m) |
| 167 |
=> QDiagram b V2 n m -> QDiagram b V2 n Any |
| 168 |
showLabels d = |
| 169 |
( mconcat |
| 170 |
. map (\(n,p) -> text (simpleName n) # translate (p .-. origin)) |
| 171 |
. concatMap (\(n,ps) -> zip (repeat n) ps) |
| 172 |
. (map . second . map) location |
| 173 |
. M.assocs |
| 174 |
$ m |
| 175 |
) <> |
| 176 |
fmap (const (Any False)) d |
| 177 |
where |
| 178 |
SubMap m = d^.subMap |
| 179 |
simpleName (Name ns) = intercalate " .> " $ map simpleAName ns |
| 180 |
simpleAName (AName n) = show n |