{-# LANGUAGE ConstraintKinds            #-}
{-# LANGUAGE DeriveDataTypeable         #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE StandaloneDeriving         #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE UndecidableInstances       #-}
{-# LANGUAGE ViewPatterns               #-}

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
  -- for Data.Semigroup

-----------------------------------------------------------------------------
-- |
-- Module      :  Diagrams.Path
-- Copyright   :  (c) 2011 diagrams-lib team (see LICENSE)
-- License     :  BSD-style (see LICENSE)
-- Maintainer  :  diagrams-discuss@googlegroups.com
--
-- This module defines /paths/, which are collections of concretely
-- located 'Trail's.  Many drawing systems (cairo, svg, ...) have a
-- similar notion of \"path\".  Note that paths with multiple trails
-- are necessary for being able to draw /e.g./ filled objects with
-- holes in them.
--
-----------------------------------------------------------------------------

module Diagrams.Path
       (

         -- * Paths

         Path(..), pathTrails

         -- * Constructing paths
         -- $construct

       , ToPath (..)
       , pathFromTrail
       , pathFromTrailAt
       , pathFromLocTrail

         -- * Eliminating paths

       , pathPoints
       , pathVertices'
       , pathVertices
       , pathOffsets
       , pathCentroid
       , pathLocSegments, fixPath

         -- * Modifying paths

       , scalePath
       , reversePath

         -- * Miscellaneous

       , explodePath
       , partitionPath

       ) where

import           Control.Arrow      ((***))
import           Control.Lens       hiding (at, transform, ( # ))
import qualified Data.Foldable      as F
import           Data.List          (partition)
import           Data.Semigroup
import           Data.Typeable

import           Diagrams.Align
import           Diagrams.Core
import           Diagrams.Located
import           Diagrams.Points
import           Diagrams.Segment
import           Diagrams.Trail
import           Diagrams.TrailLike
import           Diagrams.Transform

import           Linear.Metric
import           Linear.Vector

import           Data.Serialize     (Serialize)
import           GHC.Generics       (Generic)

------------------------------------------------------------
--  Paths  -------------------------------------------------
------------------------------------------------------------

-- | A /path/ is a (possibly empty) list of 'Located' 'Trail's.
--   Hence, unlike trails, paths are not translationally invariant,
--   and they form a monoid under /superposition/ (placing one path on
--   top of another) rather than concatenation.
newtype Path v n = Path [Located (Trail v n)]
  deriving (b -> Path v n -> Path v n
NonEmpty (Path v n) -> Path v n
Path v n -> Path v n -> Path v n
(Path v n -> Path v n -> Path v n)
-> (NonEmpty (Path v n) -> Path v n)
-> (forall b. Integral b => b -> Path v n -> Path v n)
-> Semigroup (Path v n)
forall b. Integral b => b -> Path v n -> Path v n
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall (v :: * -> *) n. NonEmpty (Path v n) -> Path v n
forall (v :: * -> *) n. Path v n -> Path v n -> Path v n
forall (v :: * -> *) n b. Integral b => b -> Path v n -> Path v n
stimes :: b -> Path v n -> Path v n
$cstimes :: forall (v :: * -> *) n b. Integral b => b -> Path v n -> Path v n
sconcat :: NonEmpty (Path v n) -> Path v n
$csconcat :: forall (v :: * -> *) n. NonEmpty (Path v n) -> Path v n
<> :: Path v n -> Path v n -> Path v n
$c<> :: forall (v :: * -> *) n. Path v n -> Path v n -> Path v n
Semigroup, Semigroup (Path v n)
Path v n
Semigroup (Path v n)
-> Path v n
-> (Path v n -> Path v n -> Path v n)
-> ([Path v n] -> Path v n)
-> Monoid (Path v n)
[Path v n] -> Path v n
Path v n -> Path v n -> Path v n
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall (v :: * -> *) n. Semigroup (Path v n)
forall (v :: * -> *) n. Path v n
forall (v :: * -> *) n. [Path v n] -> Path v n
forall (v :: * -> *) n. Path v n -> Path v n -> Path v n
mconcat :: [Path v n] -> Path v n
$cmconcat :: forall (v :: * -> *) n. [Path v n] -> Path v n
mappend :: Path v n -> Path v n -> Path v n
$cmappend :: forall (v :: * -> *) n. Path v n -> Path v n -> Path v n
mempty :: Path v n
$cmempty :: forall (v :: * -> *) n. Path v n
$cp1Monoid :: forall (v :: * -> *) n. Semigroup (Path v n)
Monoid, (forall x. Path v n -> Rep (Path v n) x)
-> (forall x. Rep (Path v n) x -> Path v n) -> Generic (Path v n)
forall x. Rep (Path v n) x -> Path v n
forall x. Path v n -> Rep (Path v n) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (v :: * -> *) n x. Rep (Path v n) x -> Path v n
forall (v :: * -> *) n x. Path v n -> Rep (Path v n) x
$cto :: forall (v :: * -> *) n x. Rep (Path v n) x -> Path v n
$cfrom :: forall (v :: * -> *) n x. Path v n -> Rep (Path v n) x
Generic
  , Typeable
  )

-- instance (OrderedField n, Metric v, Serialize (v n), Serialize (V n (N n))) =>
instance (OrderedField n, Metric v, Serialize (v n), Serialize (V (v n) (N (v n)))) =>
  Serialize (Path v n)

instance Wrapped (Path v n) where
  type Unwrapped (Path v n) = [Located (Trail v n)]
  _Wrapped' :: p (Unwrapped (Path v n)) (f (Unwrapped (Path v n)))
-> p (Path v n) (f (Path v n))
_Wrapped' = (Path v n -> [Located (Trail v n)])
-> ([Located (Trail v n)] -> Path v n)
-> Iso
     (Path v n) (Path v n) [Located (Trail v n)] [Located (Trail v n)]
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(Path [Located (Trail v n)]
x) -> [Located (Trail v n)]
x) [Located (Trail v n)] -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

instance Rewrapped (Path v n) (Path v' n')

instance Each (Path v n) (Path v' n') (Located (Trail v n)) (Located (Trail v' n')) where
  each :: (Located (Trail v n) -> f (Located (Trail v' n')))
-> Path v n -> f (Path v' n')
each = ([Located (Trail v n)] -> f [Located (Trail v' n')])
-> Path v n -> f (Path v' n')
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (([Located (Trail v n)] -> f [Located (Trail v' n')])
 -> Path v n -> f (Path v' n'))
-> ((Located (Trail v n) -> f (Located (Trail v' n')))
    -> [Located (Trail v n)] -> f [Located (Trail v' n')])
-> (Located (Trail v n) -> f (Located (Trail v' n')))
-> Path v n
-> f (Path v' n')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n) -> f (Located (Trail v' n')))
-> [Located (Trail v n)] -> f [Located (Trail v' n')]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse

instance AsEmpty (Path v n) where
  _Empty :: p () (f ()) -> p (Path v n) (f (Path v n))
_Empty = p [Located (Trail v n)] (f [Located (Trail v n)])
-> p (Path v n) (f (Path v n))
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped' (p [Located (Trail v n)] (f [Located (Trail v n)])
 -> p (Path v n) (f (Path v n)))
-> (p () (f ())
    -> p [Located (Trail v n)] (f [Located (Trail v n)]))
-> p () (f ())
-> p (Path v n) (f (Path v n))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p () (f ()) -> p [Located (Trail v n)] (f [Located (Trail v n)])
forall a. AsEmpty a => Prism' a ()
_Empty

instance Cons (Path v n) (Path v' n') (Located (Trail v n)) (Located (Trail v' n')) where
  _Cons :: p (Located (Trail v n), Path v n)
  (f (Located (Trail v' n'), Path v' n'))
-> p (Path v n) (f (Path v' n'))
_Cons = p [Located (Trail v n)] (f [Located (Trail v' n')])
-> p (Path v n) (f (Path v' n'))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (p [Located (Trail v n)] (f [Located (Trail v' n')])
 -> p (Path v n) (f (Path v' n')))
-> (p (Located (Trail v n), Path v n)
      (f (Located (Trail v' n'), Path v' n'))
    -> p [Located (Trail v n)] (f [Located (Trail v' n')]))
-> p (Located (Trail v n), Path v n)
     (f (Located (Trail v' n'), Path v' n'))
-> p (Path v n) (f (Path v' n'))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p (Located (Trail v n), [Located (Trail v n)])
  (f (Located (Trail v' n'), [Located (Trail v' n')]))
-> p [Located (Trail v n)] (f [Located (Trail v' n')])
forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons (p (Located (Trail v n), [Located (Trail v n)])
   (f (Located (Trail v' n'), [Located (Trail v' n')]))
 -> p [Located (Trail v n)] (f [Located (Trail v' n')]))
-> (p (Located (Trail v n), Path v n)
      (f (Located (Trail v' n'), Path v' n'))
    -> p (Located (Trail v n), [Located (Trail v n)])
         (f (Located (Trail v' n'), [Located (Trail v' n')])))
-> p (Located (Trail v n), Path v n)
     (f (Located (Trail v' n'), Path v' n'))
-> p [Located (Trail v n)] (f [Located (Trail v' n')])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso
  (Located (Trail v n))
  (Located (Trail v' n'))
  (Located (Trail v n))
  (Located (Trail v' n'))
-> AnIso
     [Located (Trail v n)]
     [Located (Trail v' n')]
     (Path v n)
     (Path v' n')
-> Iso
     (Located (Trail v n), [Located (Trail v n)])
     (Located (Trail v' n'), [Located (Trail v' n')])
     (Located (Trail v n), Path v n)
     (Located (Trail v' n'), Path v' n')
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b s' t' a' b'.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b
-> AnIso s' t' a' b' -> Iso (f s s') (g t t') (f a a') (g b b')
bimapping AnIso
  (Located (Trail v n))
  (Located (Trail v' n'))
  (Located (Trail v n))
  (Located (Trail v' n'))
forall a. a -> a
id AnIso
  [Located (Trail v n)]
  [Located (Trail v' n')]
  (Path v n)
  (Path v' n')
forall s t. Rewrapping s t => Iso (Unwrapped t) (Unwrapped s) t s
_Unwrapped
  {-# INLINE _Cons #-}

instance Snoc (Path v n) (Path v' n') (Located (Trail v n)) (Located (Trail v' n')) where
  _Snoc :: p (Path v n, Located (Trail v n))
  (f (Path v' n', Located (Trail v' n')))
-> p (Path v n) (f (Path v' n'))
_Snoc = p [Located (Trail v n)] (f [Located (Trail v' n')])
-> p (Path v n) (f (Path v' n'))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (p [Located (Trail v n)] (f [Located (Trail v' n')])
 -> p (Path v n) (f (Path v' n')))
-> (p (Path v n, Located (Trail v n))
      (f (Path v' n', Located (Trail v' n')))
    -> p [Located (Trail v n)] (f [Located (Trail v' n')]))
-> p (Path v n, Located (Trail v n))
     (f (Path v' n', Located (Trail v' n')))
-> p (Path v n) (f (Path v' n'))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p ([Located (Trail v n)], Located (Trail v n))
  (f ([Located (Trail v' n')], Located (Trail v' n')))
-> p [Located (Trail v n)] (f [Located (Trail v' n')])
forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc (p ([Located (Trail v n)], Located (Trail v n))
   (f ([Located (Trail v' n')], Located (Trail v' n')))
 -> p [Located (Trail v n)] (f [Located (Trail v' n')]))
-> (p (Path v n, Located (Trail v n))
      (f (Path v' n', Located (Trail v' n')))
    -> p ([Located (Trail v n)], Located (Trail v n))
         (f ([Located (Trail v' n')], Located (Trail v' n'))))
-> p (Path v n, Located (Trail v n))
     (f (Path v' n', Located (Trail v' n')))
-> p [Located (Trail v n)] (f [Located (Trail v' n')])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnIso
  [Located (Trail v n)]
  [Located (Trail v' n')]
  (Path v n)
  (Path v' n')
-> AnIso
     (Located (Trail v n))
     (Located (Trail v' n'))
     (Located (Trail v n))
     (Located (Trail v' n'))
-> Iso
     ([Located (Trail v n)], Located (Trail v n))
     ([Located (Trail v' n')], Located (Trail v' n'))
     (Path v n, Located (Trail v n))
     (Path v' n', Located (Trail v' n'))
forall (f :: * -> * -> *) (g :: * -> * -> *) s t a b s' t' a' b'.
(Bifunctor f, Bifunctor g) =>
AnIso s t a b
-> AnIso s' t' a' b' -> Iso (f s s') (g t t') (f a a') (g b b')
bimapping AnIso
  [Located (Trail v n)]
  [Located (Trail v' n')]
  (Path v n)
  (Path v' n')
forall s t. Rewrapping s t => Iso (Unwrapped t) (Unwrapped s) t s
_Unwrapped AnIso
  (Located (Trail v n))
  (Located (Trail v' n'))
  (Located (Trail v n))
  (Located (Trail v' n'))
forall a. a -> a
id
  {-# INLINE _Snoc #-}

-- | Extract the located trails making up a 'Path'.
pathTrails :: Path v n -> [Located (Trail v n)]
pathTrails :: Path v n -> [Located (Trail v n)]
pathTrails = (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

deriving instance Show (v n) => Show (Path v n)
deriving instance Eq   (v n) => Eq   (Path v n)
deriving instance Ord  (v n) => Ord  (Path v n)

type instance V (Path v n) = v
type instance N (Path v n) = n

instance (Additive v, Num n) => HasOrigin (Path v n) where
  moveOriginTo :: Point (V (Path v n)) (N (Path v n)) -> Path v n -> Path v n
moveOriginTo = ASetter
  (Path v n) (Path v n) [Located (Trail v n)] [Located (Trail v n)]
-> ([Located (Trail v n)] -> [Located (Trail v n)])
-> Path v n
-> Path v n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (Path v n) (Path v n) [Located (Trail v n)] [Located (Trail v n)]
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped' (([Located (Trail v n)] -> [Located (Trail v n)])
 -> Path v n -> Path v n)
-> (Point v n -> [Located (Trail v n)] -> [Located (Trail v n)])
-> Point v n
-> Path v n
-> Path v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n) -> Located (Trail v n))
-> [Located (Trail v n)] -> [Located (Trail v n)]
forall a b. (a -> b) -> [a] -> [b]
map ((Located (Trail v n) -> Located (Trail v n))
 -> [Located (Trail v n)] -> [Located (Trail v n)])
-> (Point v n -> Located (Trail v n) -> Located (Trail v n))
-> Point v n
-> [Located (Trail v n)]
-> [Located (Trail v n)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Point v n -> Located (Trail v n) -> Located (Trail v n)
forall t. HasOrigin t => Point (V t) (N t) -> t -> t
moveOriginTo

-- | Paths are trail-like; a trail can be used to construct a
--   singleton path.
instance (Metric v, OrderedField n) => TrailLike (Path v n) where
  trailLike :: Located (Trail (V (Path v n)) (N (Path v n))) -> Path v n
trailLike = [Located (Trail v n)] -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path ([Located (Trail v n)] -> Path v n)
-> (Located (Trail v n) -> [Located (Trail v n)])
-> Located (Trail v n)
-> Path v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n)
-> [Located (Trail v n)] -> [Located (Trail v n)]
forall a. a -> [a] -> [a]
:[])

-- See Note [Transforming paths]
instance (HasLinearMap v, Metric v, OrderedField n)
    => Transformable (Path v n) where
  transform :: Transformation (V (Path v n)) (N (Path v n))
-> Path v n -> Path v n
transform = ASetter
  (Path v n) (Path v n) [Located (Trail v n)] [Located (Trail v n)]
-> ([Located (Trail v n)] -> [Located (Trail v n)])
-> Path v n
-> Path v n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (Path v n) (Path v n) [Located (Trail v n)] [Located (Trail v n)]
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (([Located (Trail v n)] -> [Located (Trail v n)])
 -> Path v n -> Path v n)
-> (Transformation v n
    -> [Located (Trail v n)] -> [Located (Trail v n)])
-> Transformation v n
-> Path v n
-> Path v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n) -> Located (Trail v n))
-> [Located (Trail v n)] -> [Located (Trail v n)]
forall a b. (a -> b) -> [a] -> [b]
map ((Located (Trail v n) -> Located (Trail v n))
 -> [Located (Trail v n)] -> [Located (Trail v n)])
-> (Transformation v n
    -> Located (Trail v n) -> Located (Trail v n))
-> Transformation v n
-> [Located (Trail v n)]
-> [Located (Trail v n)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transformation v n -> Located (Trail v n) -> Located (Trail v n)
forall t. Transformable t => Transformation (V t) (N t) -> t -> t
transform

instance (Metric v, OrderedField n) => Enveloped (Path v n) where
  getEnvelope :: Path v n -> Envelope (V (Path v n)) (N (Path v n))
getEnvelope = (Located (Trail v n) -> Envelope v n)
-> [Located (Trail v n)] -> Envelope v n
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap Located (Trail v n) -> Envelope v n
trailEnvelope ([Located (Trail v n)] -> Envelope v n)
-> (Path v n -> [Located (Trail v n)]) -> Path v n -> Envelope v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path
          -- this type signature is necessary to work around an apparent bug in ghc 6.12.1
    where trailEnvelope :: Located (Trail v n) -> Envelope v n
          trailEnvelope :: Located (Trail v n) -> Envelope v n
trailEnvelope (Located (Trail v n)
-> (Point (V (Trail v n)) (N (Trail v n)), Trail v n)
forall a. Located a -> (Point (V a) (N a), a)
viewLoc -> (Point (V (Trail v n)) (N (Trail v n))
p, Trail v n
t)) = Point (V (Envelope v n)) (N (Envelope v n))
-> Envelope v n -> Envelope v n
forall t. HasOrigin t => Point (V t) (N t) -> t -> t
moveOriginTo ((-n
1) n -> Point v n -> Point v n
forall (v :: * -> *) n.
(Functor v, Num n) =>
n -> Point v n -> Point v n
*. Point v n
Point (V (Trail v n)) (N (Trail v n))
p) (Trail v n -> Envelope (V (Trail v n)) (N (Trail v n))
forall a. Enveloped a => a -> Envelope (V a) (N a)
getEnvelope Trail v n
t)

instance (Metric v, OrderedField n) => Juxtaposable (Path v n) where
  juxtapose :: Vn (Path v n) -> Path v n -> Path v n -> Path v n
juxtapose = Vn (Path v n) -> Path v n -> Path v n -> Path v n
forall a. (Enveloped a, HasOrigin a) => Vn a -> a -> a -> a
juxtaposeDefault

instance (Metric v, OrderedField n) => Alignable (Path v n) where
  defaultBoundary :: v n -> Path v n -> Point v n
defaultBoundary = v n -> Path v n -> Point v n
forall a (v :: * -> *) n.
(V a ~ v, N a ~ n, Enveloped a) =>
v n -> a -> Point v n
envelopeBoundary

instance (HasLinearMap v, Metric v, OrderedField n)
    => Renderable (Path v n) NullBackend where
  render :: NullBackend
-> Path v n -> Render NullBackend (V (Path v n)) (N (Path v n))
render NullBackend
_ Path v n
_ = Render NullBackend (V (Path v n)) (N (Path v n))
forall a. Monoid a => a
mempty

------------------------------------------------------------
--  Constructing paths  ------------------------------------
------------------------------------------------------------

-- | Type class for things that can be converted to a 'Path'.
--
--   Note that this class is very different from 'TrailLike'. 'TrailLike' is
--   usually the result of a library function to give you a convenient,
--   polymorphic result ('Path', 'Diagram' etc.).
--
class ToPath t where
  -- | 'toPath' takes something that can be converted to 'Path' and returns
  --    the 'Path'.
  toPath :: (Metric (V t), OrderedField (N t)) => t -> Path (V t) (N t)

instance ToPath (Path v n) where
  toPath :: Path v n -> Path (V (Path v n)) (N (Path v n))
toPath = Path v n -> Path (V (Path v n)) (N (Path v n))
forall a. a -> a
id

instance ToPath (Trail v n) where
  toPath :: Trail v n -> Path (V (Trail v n)) (N (Trail v n))
toPath = Trail v n -> Path (V (Trail v n)) (N (Trail v n))
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail v n -> Path v n
pathFromTrail

instance ToPath (Trail' l v n) where
  toPath :: Trail' l v n -> Path (V (Trail' l v n)) (N (Trail' l v n))
toPath Trail' l v n
t = [Located (Trail v n)] -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path [Trail' l v n -> Trail v n
forall l (v :: * -> *) n. Trail' l v n -> Trail v n
Trail Trail' l v n
t Trail v n
-> Point (V (Trail v n)) (N (Trail v n)) -> Located (Trail v n)
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V (Trail v n)) (N (Trail v n))
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin]

instance ToPath (Located (Trail v n)) where
  toPath :: Located (Trail v n)
-> Path (V (Located (Trail v n))) (N (Located (Trail v n)))
toPath = Located (Trail v n)
-> Path (V (Located (Trail v n))) (N (Located (Trail v n)))
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> Path v n
pathFromLocTrail

instance ToPath (Located (Trail' l v n)) where
  toPath :: Located (Trail' l v n)
-> Path (V (Located (Trail' l v n))) (N (Located (Trail' l v n)))
toPath = Located (Trail v n) -> Path v n
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> Path v n
pathFromLocTrail (Located (Trail v n) -> Path v n)
-> (Located (Trail' l v n) -> Located (Trail v n))
-> Located (Trail' l v n)
-> Path v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Trail' l v n -> Trail v n)
-> Located (Trail' l v n) -> Located (Trail v n)
forall a b. SameSpace a b => (a -> b) -> Located a -> Located b
mapLoc Trail' l v n -> Trail v n
forall l (v :: * -> *) n. Trail' l v n -> Trail v n
Trail

instance ToPath (Located (Segment Closed v n)) where
  toPath :: Located (Segment Closed v n)
-> Path
     (V (Located (Segment Closed v n)))
     (N (Located (Segment Closed v n)))
toPath (Located (Segment Closed v n)
-> (Point (V (Segment Closed v n)) (N (Segment Closed v n)),
    Segment Closed v n)
forall a. Located a -> (Point (V a) (N a), a)
viewLoc -> (Point (V (Segment Closed v n)) (N (Segment Closed v n))
p,Segment Closed v n
seg))
    = [Located (Trail v n)] -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path [[Segment Closed v n] -> Trail v n
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
[Segment Closed v n] -> Trail v n
trailFromSegments [Segment Closed v n
seg] Trail v n
-> Point (V (Trail v n)) (N (Trail v n)) -> Located (Trail v n)
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V (Segment Closed v n)) (N (Segment Closed v n))
Point (V (Trail v n)) (N (Trail v n))
p]

instance ToPath (Located [Segment Closed v n]) where
  toPath :: Located [Segment Closed v n]
-> Path
     (V (Located [Segment Closed v n]))
     (N (Located [Segment Closed v n]))
toPath (Located [Segment Closed v n]
-> (Point (V [Segment Closed v n]) (N [Segment Closed v n]),
    [Segment Closed v n])
forall a. Located a -> (Point (V a) (N a), a)
viewLoc -> (Point (V [Segment Closed v n]) (N [Segment Closed v n])
p,[Segment Closed v n]
segs))
    = [Located (Trail v n)] -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path [[Segment Closed v n] -> Trail v n
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
[Segment Closed v n] -> Trail v n
trailFromSegments [Segment Closed v n]
segs Trail v n
-> Point (V (Trail v n)) (N (Trail v n)) -> Located (Trail v n)
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V [Segment Closed v n]) (N [Segment Closed v n])
Point (V (Trail v n)) (N (Trail v n))
p]

instance ToPath (FixedSegment v n) where
  toPath :: FixedSegment v n
-> Path (V (FixedSegment v n)) (N (FixedSegment v n))
toPath = Located (Segment Closed v n) -> Path v n
forall t.
(ToPath t, Metric (V t), OrderedField (N t)) =>
t -> Path (V t) (N t)
toPath (Located (Segment Closed v n) -> Path v n)
-> (FixedSegment v n -> Located (Segment Closed v n))
-> FixedSegment v n
-> Path v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FixedSegment v n -> Located (Segment Closed v n)
forall n (v :: * -> *).
(Num n, Additive v) =>
FixedSegment v n -> Located (Segment Closed v n)
fromFixedSeg

instance ToPath a => ToPath [a] where
  toPath :: [a] -> Path (V [a]) (N [a])
toPath = (a -> Path (V a) (N a)) -> [a] -> Path (V a) (N a)
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap a -> Path (V a) (N a)
forall t.
(ToPath t, Metric (V t), OrderedField (N t)) =>
t -> Path (V t) (N t)
toPath

-- $construct
-- Since paths are 'TrailLike', any function producing a 'TrailLike'
-- can be used to construct a (singleton) path.  The functions in this
-- section are provided for convenience.

-- | Convert a trail to a path beginning at the origin.
pathFromTrail :: (Metric v, OrderedField n) => Trail v n -> Path v n
pathFromTrail :: Trail v n -> Path v n
pathFromTrail = Located (Trail v n) -> Path v n
forall t. TrailLike t => Located (Trail (V t) (N t)) -> t
trailLike (Located (Trail v n) -> Path v n)
-> (Trail v n -> Located (Trail v n)) -> Trail v n -> Path v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Trail v n
-> Point (V (Trail v n)) (N (Trail v n)) -> Located (Trail v n)
forall a. a -> Point (V a) (N a) -> Located a
`at` Point (V (Trail v n)) (N (Trail v n))
forall (f :: * -> *) a. (Additive f, Num a) => Point f a
origin)

-- | Convert a trail to a path with a particular starting point.
pathFromTrailAt :: (Metric v, OrderedField n) => Trail v n -> Point v n -> Path v n
pathFromTrailAt :: Trail v n -> Point v n -> Path v n
pathFromTrailAt Trail v n
t Point v n
p = Located (Trail (V (Path v n)) (N (Path v n))) -> Path v n
forall t. TrailLike t => Located (Trail (V t) (N t)) -> t
trailLike (Trail v n
t Trail v n
-> Point (V (Trail v n)) (N (Trail v n)) -> Located (Trail v n)
forall a. a -> Point (V a) (N a) -> Located a
`at` Point v n
Point (V (Trail v n)) (N (Trail v n))
p)

-- | Convert a located trail to a singleton path.  This is equivalent
--   to 'trailLike', but provided with a more specific name and type
--   for convenience.
pathFromLocTrail :: (Metric v, OrderedField n) => Located (Trail v n) -> Path v n
pathFromLocTrail :: Located (Trail v n) -> Path v n
pathFromLocTrail = Located (Trail v n) -> Path v n
forall t. TrailLike t => Located (Trail (V t) (N t)) -> t
trailLike

------------------------------------------------------------
--  Eliminating paths  -------------------------------------
------------------------------------------------------------

-- | Extract the vertices of a path, resulting in a separate list of
--   vertices for each component trail.  Here a /vertex/ is defined as
--   a non-differentiable point on the trail, /i.e./ a sharp corner.
--   (Vertices are thus a subset of the places where segments join; if
--   you want all joins between segments, see 'pathPoints'.)  The
--   tolerance determines how close the tangents of two segments must be
--   at their endpoints to consider the transition point to be
--   differentiable.  See 'trailVertices' for more information.
pathVertices' :: (Metric v, OrderedField n) => n -> Path v n -> [[Point v n]]
pathVertices' :: n -> Path v n -> [[Point v n]]
pathVertices' n
toler = (Located (Trail v n) -> [Point v n])
-> [Located (Trail v n)] -> [[Point v n]]
forall a b. (a -> b) -> [a] -> [b]
map (n -> Located (Trail v n) -> [Point v n]
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
n -> Located (Trail v n) -> [Point v n]
trailVertices' n
toler) ([Located (Trail v n)] -> [[Point v n]])
-> (Path v n -> [Located (Trail v n)]) -> Path v n -> [[Point v n]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | Like 'pathVertices'', with a default tolerance.
pathVertices :: (Metric v, OrderedField n) => Path v n -> [[Point v n]]
pathVertices :: Path v n -> [[Point v n]]
pathVertices = (Located (Trail v n) -> [Point v n])
-> [Located (Trail v n)] -> [[Point v n]]
forall a b. (a -> b) -> [a] -> [b]
map Located (Trail v n) -> [Point v n]
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [Point v n]
trailVertices ([Located (Trail v n)] -> [[Point v n]])
-> (Path v n -> [Located (Trail v n)]) -> Path v n -> [[Point v n]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | Extract the points of a path, resulting in a separate list of
--   points for each component trail.  Here a /point/ is any place
--   where two segments join; see also 'pathVertices' and 'trailPoints'.
--
--   This function allows you "observe" the fact that trails are
--   implemented as lists of segments, which may be problematic if we
--   want to think of trails as parametric vector functions. This also
--   means that the behavior of this function may not be stable under
--   future changes to the implementation of trails and paths.  For an
--   unproblematic version which only yields vertices at which there
--   is a sharp corner, excluding points differentiable points, see
--   'pathVertices'.
--
--   This function is not re-exported from "Diagrams.Prelude"; to use
--   it, import "Diagrams.Path".
pathPoints :: (Metric v, OrderedField n) => Path v n -> [[Point v n]]
pathPoints :: Path v n -> [[Point v n]]
pathPoints = (Located (Trail v n) -> [Point v n])
-> [Located (Trail v n)] -> [[Point v n]]
forall a b. (a -> b) -> [a] -> [b]
map Located (Trail v n) -> [Point v n]
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [Point v n]
trailPoints ([Located (Trail v n)] -> [[Point v n]])
-> (Path v n -> [Located (Trail v n)]) -> Path v n -> [[Point v n]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | Compute the total offset of each trail comprising a path (see 'trailOffset').
pathOffsets :: (Metric v, OrderedField n) => Path v n -> [v n]
pathOffsets :: Path v n -> [v n]
pathOffsets = (Located (Trail v n) -> v n) -> [Located (Trail v n)] -> [v n]
forall a b. (a -> b) -> [a] -> [b]
map (Trail v n -> v n
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail v n -> v n
trailOffset (Trail v n -> v n)
-> (Located (Trail v n) -> Trail v n) -> Located (Trail v n) -> v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Located (Trail v n) -> Trail v n
forall a. Located a -> a
unLoc) ([Located (Trail v n)] -> [v n])
-> (Path v n -> [Located (Trail v n)]) -> Path v n -> [v n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | Compute the /centroid/ of a path (/i.e./ the average location of
--   its /vertices/; see 'pathVertices').
pathCentroid :: (Metric v, OrderedField n) => Path v n -> Point v n
pathCentroid :: Path v n -> Point v n
pathCentroid = [Point v n] -> Point v n
forall (v :: * -> *) n.
(Additive v, Fractional n) =>
[Point v n] -> Point v n
centroid ([Point v n] -> Point v n)
-> (Path v n -> [Point v n]) -> Path v n -> Point v n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Point v n]] -> [Point v n]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Point v n]] -> [Point v n])
-> (Path v n -> [[Point v n]]) -> Path v n -> [Point v n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path v n -> [[Point v n]]
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> [[Point v n]]
pathVertices

-- | Convert a path into a list of lists of located segments.
pathLocSegments :: (Metric v, OrderedField n) => Path v n -> [[Located (Segment Closed v n)]]
pathLocSegments :: Path v n -> [[Located (Segment Closed v n)]]
pathLocSegments = (Located (Trail v n) -> [Located (Segment Closed v n)])
-> [Located (Trail v n)] -> [[Located (Segment Closed v n)]]
forall a b. (a -> b) -> [a] -> [b]
map Located (Trail v n) -> [Located (Segment Closed v n)]
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [Located (Segment Closed v n)]
trailLocSegments ([Located (Trail v n)] -> [[Located (Segment Closed v n)]])
-> (Path v n -> [Located (Trail v n)])
-> Path v n
-> [[Located (Segment Closed v n)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | Convert a path into a list of lists of 'FixedSegment's.
fixPath :: (Metric v, OrderedField n) => Path v n -> [[FixedSegment v n]]
fixPath :: Path v n -> [[FixedSegment v n]]
fixPath = (Located (Trail v n) -> [FixedSegment v n])
-> [Located (Trail v n)] -> [[FixedSegment v n]]
forall a b. (a -> b) -> [a] -> [b]
map Located (Trail v n) -> [FixedSegment v n]
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [FixedSegment v n]
fixTrail ([Located (Trail v n)] -> [[FixedSegment v n]])
-> (Path v n -> [Located (Trail v n)])
-> Path v n
-> [[FixedSegment v n]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | \"Explode\" a path by exploding every component trail (see
--   'explodeTrail').
explodePath :: (V t ~ v, N t ~ n, TrailLike t) => Path v n -> [[t]]
explodePath :: Path v n -> [[t]]
explodePath = (Located (Trail v n) -> [t]) -> [Located (Trail v n)] -> [[t]]
forall a b. (a -> b) -> [a] -> [b]
map Located (Trail v n) -> [t]
forall t (v :: * -> *) n.
(V t ~ v, N t ~ n, TrailLike t) =>
Located (Trail v n) -> [t]
explodeTrail ([Located (Trail v n)] -> [[t]])
-> (Path v n -> [Located (Trail v n)]) -> Path v n -> [[t]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

-- | Partition a path into two paths based on a predicate on trails:
--   the first containing all the trails for which the predicate returns
--   @True@, and the second containing the remaining trails.
partitionPath :: (Located (Trail v n) -> Bool) -> Path v n -> (Path v n, Path v n)
partitionPath :: (Located (Trail v n) -> Bool) -> Path v n -> (Path v n, Path v n)
partitionPath Located (Trail v n) -> Bool
p = (Getting (Path v n) [Located (Trail v n)] (Path v n)
-> [Located (Trail v n)] -> Path v n
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Path v n) [Located (Trail v n)] (Path v n)
forall s. Wrapped s => Iso' (Unwrapped s) s
_Unwrapped' ([Located (Trail v n)] -> Path v n)
-> ([Located (Trail v n)] -> Path v n)
-> ([Located (Trail v n)], [Located (Trail v n)])
-> (Path v n, Path v n)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Getting (Path v n) [Located (Trail v n)] (Path v n)
-> [Located (Trail v n)] -> Path v n
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting (Path v n) [Located (Trail v n)] (Path v n)
forall s. Wrapped s => Iso' (Unwrapped s) s
_Unwrapped') (([Located (Trail v n)], [Located (Trail v n)])
 -> (Path v n, Path v n))
-> (Path v n -> ([Located (Trail v n)], [Located (Trail v n)]))
-> Path v n
-> (Path v n, Path v n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n) -> Bool)
-> [Located (Trail v n)]
-> ([Located (Trail v n)], [Located (Trail v n)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Located (Trail v n) -> Bool
p ([Located (Trail v n)]
 -> ([Located (Trail v n)], [Located (Trail v n)]))
-> (Path v n -> [Located (Trail v n)])
-> Path v n
-> ([Located (Trail v n)], [Located (Trail v n)])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (Path v n) -> Path v n)
-> Path v n -> Unwrapped (Path v n)
forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op Unwrapped (Path v n) -> Path v n
forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path

------------------------------------------------------------
--  Modifying paths  ---------------------------------------
------------------------------------------------------------

-- | Scale a path using its centroid (see 'pathCentroid') as the base
--   point for the scale.
scalePath :: (HasLinearMap v, Metric v, OrderedField n) => n -> Path v n -> Path v n
scalePath :: n -> Path v n -> Path v n
scalePath n
d Path v n
p = AnIso (Path v n) (Path v n) (Path v n) (Path v n)
-> (Path v n -> Path v n) -> Path v n -> Path v n
forall s t a b. AnIso s t a b -> (t -> s) -> b -> a
under (Point v n -> Iso (Path v n) (Path v n) (Path v n) (Path v n)
forall (v :: * -> *) n a b.
(InSpace v n a, SameSpace a b, HasOrigin a, HasOrigin b) =>
Point v n -> Iso a b a b
movedFrom (Path v n -> Point v n
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> Point v n
pathCentroid Path v n
p)) (n -> Path v n -> Path v n
forall (v :: * -> *) n a.
(InSpace v n a, Eq n, Fractional n, Transformable a) =>
n -> a -> a
scale n
d) Path v n
p

-- | Reverse all the component trails of a path.
reversePath :: (Metric v, OrderedField n) => Path v n -> Path v n
reversePath :: Path v n -> Path v n
reversePath = ([Located (Trail v n)] -> Identity [Located (Trail v n)])
-> Path v n -> Identity (Path v n)
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped (([Located (Trail v n)] -> Identity [Located (Trail v n)])
 -> Path v n -> Identity (Path v n))
-> ((Located (Trail v n) -> Identity (Located (Trail v n)))
    -> [Located (Trail v n)] -> Identity [Located (Trail v n)])
-> (Located (Trail v n) -> Identity (Located (Trail v n)))
-> Path v n
-> Identity (Path v n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n) -> Identity (Located (Trail v n)))
-> [Located (Trail v n)] -> Identity [Located (Trail v n)]
forall (f :: * -> *) a b. Functor f => Setter (f a) (f b) a b
mapped ((Located (Trail v n) -> Identity (Located (Trail v n)))
 -> Path v n -> Identity (Path v n))
-> (Located (Trail v n) -> Located (Trail v n))
-> Path v n
-> Path v n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Located (Trail v n) -> Located (Trail v n)
forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> Located (Trail v n)
reverseLocTrail

-- | Same as 'reversePath'.
instance (Metric v, OrderedField n) => Reversing (Path v n) where
  reversing :: Path v n -> Path v n
reversing = ([Located (Trail v n)] -> Identity [Located (Trail v n)])
-> Path v n -> Identity (Path v n)
forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped' (([Located (Trail v n)] -> Identity [Located (Trail v n)])
 -> Path v n -> Identity (Path v n))
-> ((Located (Trail v n) -> Identity (Located (Trail v n)))
    -> [Located (Trail v n)] -> Identity [Located (Trail v n)])
-> (Located (Trail v n) -> Identity (Located (Trail v n)))
-> Path v n
-> Identity (Path v n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Located (Trail v n) -> Identity (Located (Trail v n)))
-> [Located (Trail v n)] -> Identity [Located (Trail v n)]
forall (f :: * -> *) a b. Functor f => Setter (f a) (f b) a b
mapped ((Located (Trail v n) -> Identity (Located (Trail v n)))
 -> Path v n -> Identity (Path v n))
-> (Located (Trail v n) -> Located (Trail v n))
-> Path v n
-> Path v n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Located (Trail v n) -> Located (Trail v n)
forall t. Reversing t => t -> t
reversing