Debian Codegraph @

src/Diagrams/Parametric/Adjust.hs

raw symbols SCIP
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE TemplateHaskell #-}
3 {-# LANGUAGE TypeFamilies #-}
4 {-# LANGUAGE TypeOperators #-}
5 {-# LANGUAGE UndecidableInstances #-}
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : Diagrams.Parametric.Adjust
9 -- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
10 -- License : BSD-style (see LICENSE)
11 -- Maintainer : [email protected]
12 --
13 -- Tools for adjusting the length of parametric objects such as
14 -- segments and trails.
15 --
16 -----------------------------------------------------------------------------
17 module Diagrams.Parametric.Adjust
18 ( adjust
19 , AdjustOpts(_adjMethod, _adjSide, _adjEps)
20 , adjMethod, adjSide, adjEps
21 , AdjustMethod(..), AdjustSide(..)
22
23 ) where
24
25 import Control.Lens (Lens', generateSignatures, lensRules, makeLensesWith, (&),
26 (.~), (^.))
27 import Data.Proxy
28
29 import Data.Default
30
31 import Diagrams.Core.V
32 import Diagrams.Parametric
33
34 -- | What method should be used for adjusting a segment, trail, or
35 -- path?
36 data AdjustMethod n = ByParam n -- ^ Extend by the given parameter value
37 -- (use a negative parameter to shrink)
38 | ByAbsolute n -- ^ Extend by the given arc length
39 -- (use a negative length to shrink)
40 | ToAbsolute n -- ^ Extend or shrink to the given
41 -- arc length
42
43 -- | Which side of a segment, trail, or path should be adjusted?
44 data AdjustSide = Start -- ^ Adjust only the beginning
45 | End -- ^ Adjust only the end
46 | Both -- ^ Adjust both sides equally
47 deriving (Show, Read, Eq, Ord, Bounded, Enum)
48
49 -- | How should a segment, trail, or path be adjusted?
50 data AdjustOpts n = AO { _adjMethod :: AdjustMethod n
51 , _adjSide :: AdjustSide
52 , _adjEps :: n
53 , adjOptsvProxy :: Proxy n
54 }
55
56 -- See Diagrams.Combinators for reasoning behind 'Proxy'.
57
58 makeLensesWith (lensRules & generateSignatures .~ False) ''AdjustOpts
59
60 -- | Which method should be used for adjusting?
61 adjMethod :: Lens' (AdjustOpts n) (AdjustMethod n)
62
63 -- | Which end(s) of the object should be adjusted?
64 adjSide :: Lens' (AdjustOpts n) AdjustSide
65
66 -- | Tolerance to use when doing adjustment.
67 adjEps :: Lens' (AdjustOpts n) n
68
69 instance Fractional n => Default (AdjustMethod n) where
70 def = ByParam 0.2
71
72 instance Default AdjustSide where
73 def = Both
74
75 instance Fractional n => Default (AdjustOpts n) where
76 def = AO { _adjMethod = def
77 , _adjSide = def
78 , _adjEps = stdTolerance
79 , adjOptsvProxy = Proxy
80 }
81
82 -- | Adjust the length of a parametric object such as a segment or
83 -- trail. The second parameter is an option record which controls how
84 -- the adjustment should be performed; see 'AdjustOpts'.
85 adjust :: (N t ~ n, Sectionable t, HasArcLength t, Fractional n)
86 => t -> AdjustOpts n -> t
87 adjust s opts = section s
88 (if opts^.adjSide == End then domainLower s else getParam s)
89 (if opts^.adjSide == Start then domainUpper s else domainUpper s - getParam (reverseDomain s))
90 where
91 getParam seg = case opts^.adjMethod of
92 ByParam p -> -p * bothCoef
93 ByAbsolute len -> param (-len * bothCoef)
94 ToAbsolute len -> param (absDelta len * bothCoef)
95 where
96 param = arcLengthToParam eps seg
97 absDelta len = arcLength eps s - len
98 bothCoef = if opts^.adjSide == Both then 0.5 else 1
99 eps = opts^.adjEps