| 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 |
|
| 35 |
|
| 36 |
data AdjustMethod n = ByParam n |
| 37 |
-- (use a negative parameter to shrink) |
| 38 |
| ByAbsolute n |
| 39 |
-- (use a negative length to shrink) |
| 40 |
| ToAbsolute n |
| 41 |
-- arc length |
| 42 |
|
| 43 |
|
| 44 |
data AdjustSide = Start |
| 45 |
| End |
| 46 |
| Both |
| 47 |
deriving (Show, Read, Eq, Ord, Bounded, Enum) |
| 48 |
|
| 49 |
|
| 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 |
|
| 61 |
adjMethod :: Lens' (AdjustOpts n) (AdjustMethod n) |
| 62 |
|
| 63 |
|
| 64 |
adjSide :: Lens' (AdjustOpts n) AdjustSide |
| 65 |
|
| 66 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 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 |