{-# 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 (NonEmpty (Path v n) -> Path v n
Path v n -> Path v n -> 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 :: forall b. Integral b => 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, 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
Monoid, 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' :: Iso' (Path v n) (Unwrapped (Path v n))
_Wrapped' = 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) 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 :: Traversal
  (Path v n)
  (Path v' n')
  (Located (Trail v n))
  (Located (Trail v' n'))
each = forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: Prism' (Path v n) ()
_Empty = forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped' forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: Prism
  (Path v n)
  (Path v' n')
  (Located (Trail v n), Path v n)
  (Located (Trail v' n'), Path v' n')
_Cons = forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. Cons s t a b => Prism s t (a, s) (b, t)
_Cons forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 forall a. a -> a
id 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 :: Prism
  (Path v n)
  (Path v' n')
  (Path v n, Located (Trail v n))
  (Path v' n', Located (Trail v' n'))
_Snoc = forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t a b. Snoc s t a b => Prism s t (s, a) (t, b)
_Snoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 forall s t. Rewrapping s t => Iso (Unwrapped t) (Unwrapped s) t s
_Unwrapped forall a. a -> a
id
  {-# INLINE _Snoc #-}

-- | Extract the located trails making up a 'Path'.
pathTrails :: Path v n -> [Located (Trail v n)]
pathTrails :: forall (v :: * -> *) n. Path v n -> [Located (Trail v n)]
pathTrails = forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 = forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (v :: * -> *) n. [Located (Trail v n)] -> Path v n
Path forall b c a. (b -> c) -> (a -> b) -> a -> c
. (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 = forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap Located (Trail v n) -> Envelope v n
trailEnvelope forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 (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)) = forall t. HasOrigin t => Point (V t) (N t) -> t -> t
moveOriginTo ((-n
1) forall (v :: * -> *) n.
(Functor v, Num n) =>
n -> Point v n -> Point v n
*. Point (V (Trail v n)) (N (Trail v n))
p) (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 = forall a. (Enveloped a, HasOrigin a) => Vn a -> a -> a -> a
juxtaposeDefault

instance (Metric v, OrderedField n) => Alignable (Path v n) where
  defaultBoundary :: forall (v :: * -> *) n.
(V (Path v n) ~ v, N (Path v n) ~ n) =>
v n -> Path v n -> Point v n
defaultBoundary = 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
_ = 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 :: (Metric (V (Path v n)), OrderedField (N (Path v n))) =>
Path v n -> Path (V (Path v n)) (N (Path v n))
toPath = forall a. a -> a
id

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

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

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

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

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

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

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

instance ToPath a => ToPath [a] where
  toPath :: (Metric (V [a]), OrderedField (N [a])) =>
[a] -> Path (V [a]) (N [a])
toPath = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail v n -> Path v n
pathFromTrail = forall t. TrailLike t => Located (Trail (V t) (N t)) -> t
trailLike forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> Point (V a) (N a) -> Located a
`at` 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail v n -> Point v n -> Path v n
pathFromTrailAt Trail v n
t Point v n
p = forall t. TrailLike t => Located (Trail (V t) (N t)) -> t
trailLike (Trail v n
t forall a. a -> Point (V a) (N a) -> Located a
`at` Point 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> Path v n
pathFromLocTrail = 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' :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
n -> Path v n -> [[Point v n]]
pathVertices' n
toler = forall a b. (a -> b) -> [a] -> [b]
map (forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
n -> Located (Trail v n) -> [Point v n]
trailVertices' n
toler) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> [[Point v n]]
pathVertices = forall a b. (a -> b) -> [a] -> [b]
map forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [Point v n]
trailVertices forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> [[Point v n]]
pathPoints = forall a b. (a -> b) -> [a] -> [b]
map forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [Point v n]
trailPoints forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> [v n]
pathOffsets = forall a b. (a -> b) -> [a] -> [b]
map (forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Trail v n -> v n
trailOffset forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Located a -> a
unLoc) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> Point v n
pathCentroid = forall (v :: * -> *) n.
(Additive v, Fractional n) =>
[Point v n] -> Point v n
centroid forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> [[Located (Segment Closed v n)]]
pathLocSegments = forall a b. (a -> b) -> [a] -> [b]
map forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [Located (Segment Closed v n)]
trailLocSegments forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> [[FixedSegment v n]]
fixPath = forall a b. (a -> b) -> [a] -> [b]
map forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Located (Trail v n) -> [FixedSegment v n]
fixTrail forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall t (v :: * -> *) n.
(V t ~ v, N t ~ n, TrailLike t) =>
Path v n -> [[t]]
explodePath = forall a b. (a -> b) -> [a] -> [b]
map forall t (v :: * -> *) n.
(V t ~ v, N t ~ n, TrailLike t) =>
Located (Trail v n) -> [t]
explodeTrail forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(Located (Trail v n) -> Bool) -> Path v n -> (Path v n, Path v n)
partitionPath Located (Trail v n) -> Bool
p = (forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s. Wrapped s => Iso' (Unwrapped s) s
_Unwrapped' forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view forall s. Wrapped s => Iso' (Unwrapped s) s
_Unwrapped') forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> ([a], [a])
partition Located (Trail v n) -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Wrapped s => (Unwrapped s -> s) -> s -> Unwrapped s
op 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 :: forall (v :: * -> *) n.
(HasLinearMap v, Metric v, OrderedField n) =>
n -> Path v n -> Path v n
scalePath n
d Path v n
p = forall s t a b. AnIso s t a b -> (t -> s) -> b -> a
under (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 (forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> Point v n
pathCentroid Path v n
p)) (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 :: forall (v :: * -> *) n.
(Metric v, OrderedField n) =>
Path v n -> Path v n
reversePath = forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => Setter (f a) (f b) a b
mapped forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ 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 = forall s. Wrapped s => Iso' s (Unwrapped s)
_Wrapped' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => Setter (f a) (f b) a b
mapped forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ forall t. Reversing t => t -> t
reversing