{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE QuantifiedConstraints #-}
--------------------------------------------------------------------------------
-- |
-- Module      :  HGeometry.Polygon.WithHoles
-- Copyright   :  (C) Frank Staals
-- License     :  see the LICENSE file
-- Maintainer  :  Frank Staals
--
-- A simple type for representing polygonswith holes
--
--------------------------------------------------------------------------------
module HGeometry.Polygon.WithHoles
  ( PolygonalDomainF(PolygonalDomain)
  , PolygonalDomain
  , asSimplePolygon
  , outerBoundaryPolygon
  , theHoles
  , HoleContainer
  ) where

import Data.Semigroup
import Control.DeepSeq (NFData)
import Control.Lens hiding (holes)
import Data.Foldable1
import Data.Functor.Apply ((<.*>), MaybeApply(..))
import Data.Kind (Type)
import Data.Semigroup.Traversable
import Data.Vector (Vector)
import Data.Vector.NonEmpty.Internal (NonEmptyVector(..))
import GHC.Generics (Generic)
import HGeometry.Box
import HGeometry.Cyclic
import HGeometry.Foldable.Util
import HGeometry.Point
import Data.Ord (comparing)
import HGeometry.Vector (lerp)
import HGeometry.Triangle
import HGeometry.Line.PointAndVector
import HGeometry.Polygon.Class
import HGeometry.Polygon.Simple
import HGeometry.Properties
import HGeometry.Transformation
import HGeometry.Boundary
import HGeometry.Vector.NonEmpty.Util ()

--------------------------------------------------------------------------------

-- | Simple data type modelling polygons with holes
data PolygonalDomainF (h     :: Type -> Type)
                      (f     :: Type -> Type)
                      (point :: Type) =
    PolygonalDomain (SimplePolygonF f point) -- ^ the outer boundary
                    (h (SimplePolygonF f point)) -- ^ the holes
  deriving stock ((forall x.
 PolygonalDomainF h f point -> Rep (PolygonalDomainF h f point) x)
-> (forall x.
    Rep (PolygonalDomainF h f point) x -> PolygonalDomainF h f point)
-> Generic (PolygonalDomainF h f point)
forall x.
Rep (PolygonalDomainF h f point) x -> PolygonalDomainF h f point
forall x.
PolygonalDomainF h f point -> Rep (PolygonalDomainF h f point) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (h :: * -> *) (f :: * -> *) point x.
Rep (PolygonalDomainF h f point) x -> PolygonalDomainF h f point
forall (h :: * -> *) (f :: * -> *) point x.
PolygonalDomainF h f point -> Rep (PolygonalDomainF h f point) x
$cfrom :: forall (h :: * -> *) (f :: * -> *) point x.
PolygonalDomainF h f point -> Rep (PolygonalDomainF h f point) x
from :: forall x.
PolygonalDomainF h f point -> Rep (PolygonalDomainF h f point) x
$cto :: forall (h :: * -> *) (f :: * -> *) point x.
Rep (PolygonalDomainF h f point) x -> PolygonalDomainF h f point
to :: forall x.
Rep (PolygonalDomainF h f point) x -> PolygonalDomainF h f point
Generic)

instance ( NFData    (SimplePolygonF f point)
         , NFData (h (SimplePolygonF f point))
         ) => NFData (PolygonalDomainF h f point)

deriving instance ( Show    (SimplePolygonF f point)
                  , Show (h (SimplePolygonF f point))
                  ) => Show (PolygonalDomainF h f point)
deriving instance ( Read    (SimplePolygonF f point)
                  , Read (h (SimplePolygonF f point))
                  ) => Read (PolygonalDomainF h f point)
deriving instance ( Eq    (SimplePolygonF f point)
                  , Eq (h (SimplePolygonF f point))
                  ) => Eq (PolygonalDomainF h f point)

-- | The simple polygon representing the outer boundary
outerBoundaryPolygon :: Lens' (PolygonalDomainF h f point) (SimplePolygonF f point)
outerBoundaryPolygon :: forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon = (PolygonalDomainF h f point -> SimplePolygonF f point)
-> (PolygonalDomainF h f point
    -> SimplePolygonF f point -> PolygonalDomainF h f point)
-> Lens
     (PolygonalDomainF h f point)
     (PolygonalDomainF h f point)
     (SimplePolygonF f point)
     (SimplePolygonF f point)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (\(PolygonalDomain SimplePolygonF f point
outer h (SimplePolygonF f point)
_)    -> SimplePolygonF f point
outer)
                            (\(PolygonalDomain SimplePolygonF f point
_ h (SimplePolygonF f point)
hs) SimplePolygonF f point
outer -> SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain SimplePolygonF f point
outer h (SimplePolygonF f point)
hs)

-- | Lens to access the holes
theHoles :: Lens (PolygonalDomainF h f point) (PolygonalDomainF h' f point)
                 (h (SimplePolygonF f point)) (h' (SimplePolygonF f point))
theHoles :: forall (h :: * -> *) (f :: * -> *) point (h' :: * -> *)
       (f :: * -> *).
Functor f =>
(h (SimplePolygonF f point) -> f (h' (SimplePolygonF f point)))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h' f point)
theHoles = (PolygonalDomainF h f point -> h (SimplePolygonF f point))
-> (PolygonalDomainF h f point
    -> h' (SimplePolygonF f point) -> PolygonalDomainF h' f point)
-> Lens
     (PolygonalDomainF h f point)
     (PolygonalDomainF h' f point)
     (h (SimplePolygonF f point))
     (h' (SimplePolygonF f point))
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (\(PolygonalDomain SimplePolygonF f point
_     h (SimplePolygonF f point)
hs)    -> h (SimplePolygonF f point)
hs)
                (\(PolygonalDomain SimplePolygonF f point
outer h (SimplePolygonF f point)
_)  h' (SimplePolygonF f point)
hs -> SimplePolygonF f point
-> h' (SimplePolygonF f point) -> PolygonalDomainF h' f point
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain SimplePolygonF f point
outer h' (SimplePolygonF f point)
hs)

-- | Polygonal domain implemented using Vectors
type PolygonalDomain point = PolygonalDomainF Vector (Cyclic NonEmptyVector) point

type instance Dimension (PolygonalDomainF h f point) = 2
type instance NumType   (PolygonalDomainF h f point) = NumType point

instance (Functor h, Functor f) => Functor (PolygonalDomainF h f) where
  fmap :: forall a b.
(a -> b) -> PolygonalDomainF h f a -> PolygonalDomainF h f b
fmap a -> b
f (PolygonalDomain SimplePolygonF f a
outer h (SimplePolygonF f a)
hs) =
    SimplePolygonF f b
-> h (SimplePolygonF f b) -> PolygonalDomainF h f b
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain ((a -> b) -> SimplePolygonF f a -> SimplePolygonF f b
forall a b. (a -> b) -> SimplePolygonF f a -> SimplePolygonF f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f SimplePolygonF f a
outer) ((SimplePolygonF f a -> SimplePolygonF f b)
-> h (SimplePolygonF f a) -> h (SimplePolygonF f b)
forall a b. (a -> b) -> h a -> h b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> SimplePolygonF f a -> SimplePolygonF f b
forall a b. (a -> b) -> SimplePolygonF f a -> SimplePolygonF f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) h (SimplePolygonF f a)
hs)

instance (Foldable h, Foldable f) => Foldable (PolygonalDomainF h f) where
  foldMap :: forall m a. Monoid m => (a -> m) -> PolygonalDomainF h f a -> m
foldMap a -> m
f (PolygonalDomain SimplePolygonF f a
outer h (SimplePolygonF f a)
hs) = (a -> m) -> SimplePolygonF f a -> m
forall m a. Monoid m => (a -> m) -> SimplePolygonF f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f SimplePolygonF f a
outer m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (SimplePolygonF f a -> m) -> h (SimplePolygonF f a) -> m
forall m a. Monoid m => (a -> m) -> h a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> m) -> SimplePolygonF f a -> m
forall m a. Monoid m => (a -> m) -> SimplePolygonF f a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) h (SimplePolygonF f a)
hs

instance (Traversable h, Traversable f) => Traversable (PolygonalDomainF h f) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> PolygonalDomainF h f a -> f (PolygonalDomainF h f b)
traverse a -> f b
f (PolygonalDomain SimplePolygonF f a
outer h (SimplePolygonF f a)
hs) =
    SimplePolygonF f b
-> h (SimplePolygonF f b) -> PolygonalDomainF h f b
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain (SimplePolygonF f b
 -> h (SimplePolygonF f b) -> PolygonalDomainF h f b)
-> f (SimplePolygonF f b)
-> f (h (SimplePolygonF f b) -> PolygonalDomainF h f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
traverse a -> f b
f SimplePolygonF f a
outer f (h (SimplePolygonF f b) -> PolygonalDomainF h f b)
-> f (h (SimplePolygonF f b)) -> f (PolygonalDomainF h f b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (SimplePolygonF f a -> f (SimplePolygonF f b))
-> h (SimplePolygonF f a) -> f (h (SimplePolygonF f b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> h a -> f (h b)
traverse ((a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
traverse a -> f b
f) h (SimplePolygonF f a)
hs

instance (Foldable h, Foldable1 f) => Foldable1 (PolygonalDomainF h f) where
  foldMap1 :: forall m a. Semigroup m => (a -> m) -> PolygonalDomainF h f a -> m
foldMap1 a -> m
f (PolygonalDomain SimplePolygonF f a
outer h (SimplePolygonF f a)
hs) =
      (a -> m) -> SimplePolygonF f a -> m
forall m a. Semigroup m => (a -> m) -> SimplePolygonF f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f SimplePolygonF f a
outer m -> Maybe m -> m
forall {b}. Semigroup b => b -> Maybe b -> b
`combine` (SimplePolygonF f a -> Maybe m)
-> h (SimplePolygonF f a) -> Maybe m
forall m a. Monoid m => (a -> m) -> h a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (m -> Maybe m
forall a. a -> Maybe a
Just (m -> Maybe m)
-> (SimplePolygonF f a -> m) -> SimplePolygonF f a -> Maybe m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m) -> SimplePolygonF f a -> m
forall m a. Semigroup m => (a -> m) -> SimplePolygonF f a -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 a -> m
f) h (SimplePolygonF f a)
hs
    where
      combine :: b -> Maybe b -> b
combine b
x0 = b -> (b -> b) -> Maybe b -> b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
x0 (b
x0 b -> b -> b
forall a. Semigroup a => a -> a -> a
<>)

instance (Traversable h, Traversable1 f) => Traversable1 (PolygonalDomainF h f) where
  traverse1 :: forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> PolygonalDomainF h f a -> f (PolygonalDomainF h f b)
traverse1 a -> f b
f (PolygonalDomain SimplePolygonF f a
outer h (SimplePolygonF f a)
hs) =
    SimplePolygonF f b
-> h (SimplePolygonF f b) -> PolygonalDomainF h f b
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain (SimplePolygonF f b
 -> h (SimplePolygonF f b) -> PolygonalDomainF h f b)
-> f (SimplePolygonF f b)
-> f (h (SimplePolygonF f b) -> PolygonalDomainF h f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable1 t, Apply f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
traverse1 a -> f b
f SimplePolygonF f a
outer f (h (SimplePolygonF f b) -> PolygonalDomainF h f b)
-> MaybeApply f (h (SimplePolygonF f b))
-> f (PolygonalDomainF h f b)
forall (f :: * -> *) a b.
Apply f =>
f (a -> b) -> MaybeApply f a -> f b
<.*> (SimplePolygonF f a -> f (SimplePolygonF f b))
-> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Apply f) =>
(a -> f b) -> t a -> MaybeApply f (t b)
traverse1Maybe ((a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable1 t, Apply f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
traverse1 a -> f b
f) h (SimplePolygonF f a)
hs

instance ( VertexContainer f point, Traversable h
         ) => HasPoints (PolygonalDomainF h f point)
                        (PolygonalDomainF h f point') point point' where
  allPoints :: forall (d :: Nat) r r'.
(Point_ point d r, Point_ point' d r',
 NumType (PolygonalDomainF h f point) ~ r,
 NumType (PolygonalDomainF h f point') ~ r',
 Dimension (PolygonalDomainF h f point) ~ d,
 Dimension (PolygonalDomainF h f point') ~ d) =>
Traversal1
  (PolygonalDomainF h f point)
  (PolygonalDomainF h f point')
  point
  point'
allPoints = (point -> f point')
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point')
forall (f :: * -> *) a b.
Traversable1 f =>
IndexedTraversal1 Int (f a) (f b) a b
IndexedTraversal1
  Int
  (PolygonalDomainF h f point)
  (PolygonalDomainF h f point')
  point
  point'
traversed1

instance ( VertexContainer f point, Traversable h
         , DefaultTransformByConstraints (PolygonalDomainF h f point) 2 r
         , Point_ point 2 r
         ) => IsTransformable (PolygonalDomainF h f point)

instance ( VertexContainer f point
         , Point_ point 2 r
         ) => IsBoxable (PolygonalDomainF h  f point) where
  boundingBox :: forall (d :: Nat) r.
(d ~ Dimension (PolygonalDomainF h f point),
 r ~ NumType (PolygonalDomainF h f point), Ord r) =>
PolygonalDomainF h f point -> Box (Point d r)
boundingBox = SimplePolygonF f point -> Box (Point d r)
forall (d :: Nat) r.
(d ~ Dimension (SimplePolygonF f point),
 r ~ NumType (SimplePolygonF f point), Ord r) =>
SimplePolygonF f point -> Box (Point d r)
forall g (d :: Nat) r.
(IsBoxable g, d ~ Dimension g, r ~ NumType g, Ord r) =>
g -> Box (Point d r)
boundingBox (SimplePolygonF f point -> Box (Point d r))
-> (PolygonalDomainF h f point -> SimplePolygonF f point)
-> PolygonalDomainF h f point
-> Box (Point d r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting
  (SimplePolygonF f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
-> PolygonalDomainF h f point -> SimplePolygonF f point
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting
  (SimplePolygonF f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon


instance ( HoleContainer h f point, VertexContainer f point
         ) => HasVertices (PolygonalDomainF h f point) (PolygonalDomainF h f point') where
  vertices :: IndexedTraversal1
  (VertexIx (PolygonalDomainF h f point))
  (PolygonalDomainF h f point)
  (PolygonalDomainF h f point')
  (Vertex (PolygonalDomainF h f point))
  (Vertex (PolygonalDomainF h f point'))
vertices = ((p ~ (->)) =>
 (point -> f point')
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point'))
-> (p point (f point')
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point'))
-> p point (f point')
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point')
forall (p :: * -> * -> *) (q :: * -> * -> *) a b r.
Conjoined p =>
((p ~ (->)) => q (a -> b) r) -> q (p a b) r -> q (p a b) r
forall (q :: * -> * -> *) a b r.
((p ~ (->)) => q (a -> b) r) -> q (p a b) r -> q (p a b) r
conjoined (p ~ (->)) =>
(point -> f point')
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point')
(point -> f point')
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point')
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable1 t, Apply f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Apply f =>
(a -> f b) -> PolygonalDomainF h f a -> f (PolygonalDomainF h f b)
traverse1 ((VtxIx -> point -> f point')
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point')
forall {f :: * -> *} {a} {f :: * -> *} {b} {f :: * -> *}
       {h :: * -> *}.
(IxValue (f a) ~ a, Index (f a) ~ Int,
 HasVertices (SimplePolygonF f a) (SimplePolygonF f b), Apply f,
 TraversableWithIndex Int h, TraversableWithIndex Int f,
 Traversable1 f, Ixed (f a), HasDirectedTraversals f) =>
(VtxIx -> a -> f b)
-> PolygonalDomainF h f a -> f (PolygonalDomainF h f b)
itraverse1 ((VtxIx -> point -> f point')
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point'))
-> (p point (f point') -> VtxIx -> point -> f point')
-> p point (f point')
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p point (f point') -> VtxIx -> point -> f point'
forall a b. p a b -> VtxIx -> a -> b
forall i (p :: * -> * -> *) a b.
Indexable i p =>
p a b -> i -> a -> b
indexed)
    where
      itraverse1 :: (VtxIx -> a -> f b)
-> PolygonalDomainF h f a -> f (PolygonalDomainF h f b)
itraverse1 VtxIx -> a -> f b
f (PolygonalDomain SimplePolygonF f a
outer h (SimplePolygonF f a)
hs) =
        SimplePolygonF f b
-> h (SimplePolygonF f b) -> PolygonalDomainF h f b
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain (SimplePolygonF f b
 -> h (SimplePolygonF f b) -> PolygonalDomainF h f b)
-> f (SimplePolygonF f b)
-> f (h (SimplePolygonF f b) -> PolygonalDomainF h f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>  (Indexed VtxIx a (f b)
 -> SimplePolygonF f a -> f (SimplePolygonF f b))
-> (VtxIx -> a -> f b)
-> SimplePolygonF f a
-> f (SimplePolygonF f b)
forall i a (f :: * -> *) b s t.
(Indexed i a (f b) -> s -> f t) -> (i -> a -> f b) -> s -> f t
itraverseOf ((Int -> VtxIx)
-> (Indexed Int a (f b)
    -> SimplePolygonF f a -> f (SimplePolygonF f b))
-> Indexed VtxIx a (f b)
-> SimplePolygonF f a
-> f (SimplePolygonF f b)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer Indexed Int a (f b) -> SimplePolygonF f a -> f (SimplePolygonF f b)
Indexed
  Int (Vertex (SimplePolygonF f a)) (f (Vertex (SimplePolygonF f b)))
-> SimplePolygonF f a -> f (SimplePolygonF f b)
forall graph graph'.
HasVertices graph graph' =>
IndexedTraversal1
  (VertexIx graph) graph graph' (Vertex graph) (Vertex graph')
IndexedTraversal1
  (VertexIx (SimplePolygonF f a))
  (SimplePolygonF f a)
  (SimplePolygonF f b)
  (Vertex (SimplePolygonF f a))
  (Vertex (SimplePolygonF f b))
vertices) VtxIx -> a -> f b
f SimplePolygonF f a
outer
                        f (h (SimplePolygonF f b) -> PolygonalDomainF h f b)
-> MaybeApply f (h (SimplePolygonF f b))
-> f (PolygonalDomainF h f b)
forall (f :: * -> *) a b.
Apply f =>
f (a -> b) -> MaybeApply f a -> f b
<.*> (Indexed VtxIx a (MaybeApply f b)
 -> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b)))
-> (VtxIx -> a -> MaybeApply f b)
-> h (SimplePolygonF f a)
-> MaybeApply f (h (SimplePolygonF f b))
forall i a (f :: * -> *) b s t.
(Indexed i a (f b) -> s -> f t) -> (i -> a -> f b) -> s -> f t
itraverseOf (((Int, Int) -> VtxIx)
-> (Indexed (Int, Int) a (MaybeApply f b)
    -> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b)))
-> Indexed VtxIx a (MaybeApply f b)
-> h (SimplePolygonF f a)
-> MaybeApply f (h (SimplePolygonF f b))
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed (Int, Int) -> VtxIx
inner ((Indexed (Int, Int) a (MaybeApply f b)
  -> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b)))
 -> Indexed VtxIx a (MaybeApply f b)
 -> h (SimplePolygonF f a)
 -> MaybeApply f (h (SimplePolygonF f b)))
-> (Indexed (Int, Int) a (MaybeApply f b)
    -> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b)))
-> Indexed VtxIx a (MaybeApply f b)
-> h (SimplePolygonF f a)
-> MaybeApply f (h (SimplePolygonF f b))
forall a b. (a -> b) -> a -> b
$ Indexed
  Int (SimplePolygonF f a) (MaybeApply f (SimplePolygonF f b))
-> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b))
forall i (t :: * -> *) a b.
TraversableWithIndex i t =>
IndexedTraversal i (t a) (t b) a b
IndexedTraversal
  Int
  (h (SimplePolygonF f a))
  (h (SimplePolygonF f b))
  (SimplePolygonF f a)
  (SimplePolygonF f b)
itraversed (Indexed
   Int (SimplePolygonF f a) (MaybeApply f (SimplePolygonF f b))
 -> h (SimplePolygonF f a) -> MaybeApply f (h (SimplePolygonF f b)))
-> (Indexed Int a (MaybeApply f b)
    -> SimplePolygonF f a -> MaybeApply f (SimplePolygonF f b))
-> Indexed (Int, Int) a (MaybeApply f b)
-> h (SimplePolygonF f a)
-> MaybeApply f (h (SimplePolygonF f b))
forall i j (p :: * -> * -> *) s t r a b.
Indexable (i, j) p =>
(Indexed i s t -> r) -> (Indexed j a b -> s -> t) -> p a b -> r
<.> Indexed Int a (MaybeApply f b)
-> SimplePolygonF f a -> MaybeApply f (SimplePolygonF f b)
Indexed
  Int
  (Vertex (SimplePolygonF f a))
  (MaybeApply f (Vertex (SimplePolygonF f b)))
-> SimplePolygonF f a -> MaybeApply f (SimplePolygonF f b)
forall graph graph'.
HasVertices graph graph' =>
IndexedTraversal1
  (VertexIx graph) graph graph' (Vertex graph) (Vertex graph')
IndexedTraversal1
  (VertexIx (SimplePolygonF f a))
  (SimplePolygonF f a)
  (SimplePolygonF f b)
  (Vertex (SimplePolygonF f a))
  (Vertex (SimplePolygonF f b))
vertices)
                                         (\VtxIx
i -> Either (f b) b -> MaybeApply f b
forall (f :: * -> *) a. Either (f a) a -> MaybeApply f a
MaybeApply (Either (f b) b -> MaybeApply f b)
-> (a -> Either (f b) b) -> a -> MaybeApply f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f b -> Either (f b) b
forall a b. a -> Either a b
Left (f b -> Either (f b) b) -> (a -> f b) -> a -> Either (f b) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VtxIx -> a -> f b
f VtxIx
i)
                                         h (SimplePolygonF f a)
hs

-- | Containers that stores holes must satisfy the following constraints:
type HoleContainer h f point =
  ( TraversableWithIndex Int h
  , Index   (h (SimplePolygonF f point)) ~ Int
  , IxValue (h (SimplePolygonF f point)) ~ SimplePolygonF f point
  , Ixed    (h (SimplePolygonF f point))
  , VertexContainer f point
  )

instance ( HoleContainer h f point
         ) => HasHoles (PolygonalDomainF h f point) where
  type HoleIx (PolygonalDomainF h f point) = Int
  type HoleF  (PolygonalDomainF h f point) = f

  holes :: IndexedTraversal'
  (HoleIx (PolygonalDomainF h f point))
  (PolygonalDomainF h f point)
  (Hole (PolygonalDomainF h f point))
holes    = (h (SimplePolygonF f point) -> f (h (SimplePolygonF f point)))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (h' :: * -> *)
       (f :: * -> *).
Functor f =>
(h (SimplePolygonF f point) -> f (h' (SimplePolygonF f point)))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h' f point)
theHoles ((h (SimplePolygonF f point) -> f (h (SimplePolygonF f point)))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (p (SimplePolygonF f point) (f (SimplePolygonF f point))
    -> h (SimplePolygonF f point) -> f (h (SimplePolygonF f point)))
-> p (SimplePolygonF f point) (f (SimplePolygonF f point))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> p (SimplePolygonF f point) (f (SimplePolygonF f point))
-> h (SimplePolygonF f point) -> f (h (SimplePolygonF f point))
forall i (t :: * -> *) a b.
TraversableWithIndex i t =>
IndexedTraversal i (t a) (t b) a b
IndexedTraversal
  Int
  (h (SimplePolygonF f point))
  (h (SimplePolygonF f point))
  (SimplePolygonF f point)
  (SimplePolygonF f point)
itraversed
  holeAt :: HoleIx (PolygonalDomainF h f point)
-> IndexedTraversal'
     (HoleIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Hole (PolygonalDomainF h f point))
holeAt HoleIx (PolygonalDomainF h f point)
i = (h (SimplePolygonF f point) -> f (h (SimplePolygonF f point)))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (h' :: * -> *)
       (f :: * -> *).
Functor f =>
(h (SimplePolygonF f point) -> f (h' (SimplePolygonF f point)))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h' f point)
theHoles ((h (SimplePolygonF f point) -> f (h (SimplePolygonF f point)))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (p (SimplePolygonF f point) (f (SimplePolygonF f point))
    -> h (SimplePolygonF f point) -> f (h (SimplePolygonF f point)))
-> p (SimplePolygonF f point) (f (SimplePolygonF f point))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> Index (h (SimplePolygonF f point))
-> IndexedTraversal'
     (Index (h (SimplePolygonF f point)))
     (h (SimplePolygonF f point))
     (IxValue (h (SimplePolygonF f point)))
forall m.
Ixed m =>
Index m -> IndexedTraversal' (Index m) m (IxValue m)
iix Index (h (SimplePolygonF f point))
HoleIx (PolygonalDomainF h f point)
i

-- | The indices we use to identify vertices
data VtxIx = Outer {-#UNPACK#-}!Int
           | Inner {-#UNPACK#-}!Int {-#UNPACK#-}!Int
           deriving (Int -> VtxIx -> ShowS
[VtxIx] -> ShowS
VtxIx -> String
(Int -> VtxIx -> ShowS)
-> (VtxIx -> String) -> ([VtxIx] -> ShowS) -> Show VtxIx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VtxIx -> ShowS
showsPrec :: Int -> VtxIx -> ShowS
$cshow :: VtxIx -> String
show :: VtxIx -> String
$cshowList :: [VtxIx] -> ShowS
showList :: [VtxIx] -> ShowS
Show,ReadPrec [VtxIx]
ReadPrec VtxIx
Int -> ReadS VtxIx
ReadS [VtxIx]
(Int -> ReadS VtxIx)
-> ReadS [VtxIx]
-> ReadPrec VtxIx
-> ReadPrec [VtxIx]
-> Read VtxIx
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS VtxIx
readsPrec :: Int -> ReadS VtxIx
$creadList :: ReadS [VtxIx]
readList :: ReadS [VtxIx]
$creadPrec :: ReadPrec VtxIx
readPrec :: ReadPrec VtxIx
$creadListPrec :: ReadPrec [VtxIx]
readListPrec :: ReadPrec [VtxIx]
Read,VtxIx -> VtxIx -> Bool
(VtxIx -> VtxIx -> Bool) -> (VtxIx -> VtxIx -> Bool) -> Eq VtxIx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VtxIx -> VtxIx -> Bool
== :: VtxIx -> VtxIx -> Bool
$c/= :: VtxIx -> VtxIx -> Bool
/= :: VtxIx -> VtxIx -> Bool
Eq,Eq VtxIx
Eq VtxIx =>
(VtxIx -> VtxIx -> Ordering)
-> (VtxIx -> VtxIx -> Bool)
-> (VtxIx -> VtxIx -> Bool)
-> (VtxIx -> VtxIx -> Bool)
-> (VtxIx -> VtxIx -> Bool)
-> (VtxIx -> VtxIx -> VtxIx)
-> (VtxIx -> VtxIx -> VtxIx)
-> Ord VtxIx
VtxIx -> VtxIx -> Bool
VtxIx -> VtxIx -> Ordering
VtxIx -> VtxIx -> VtxIx
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: VtxIx -> VtxIx -> Ordering
compare :: VtxIx -> VtxIx -> Ordering
$c< :: VtxIx -> VtxIx -> Bool
< :: VtxIx -> VtxIx -> Bool
$c<= :: VtxIx -> VtxIx -> Bool
<= :: VtxIx -> VtxIx -> Bool
$c> :: VtxIx -> VtxIx -> Bool
> :: VtxIx -> VtxIx -> Bool
$c>= :: VtxIx -> VtxIx -> Bool
>= :: VtxIx -> VtxIx -> Bool
$cmax :: VtxIx -> VtxIx -> VtxIx
max :: VtxIx -> VtxIx -> VtxIx
$cmin :: VtxIx -> VtxIx -> VtxIx
min :: VtxIx -> VtxIx -> VtxIx
Ord)

-- | Construct an index of an inner vertex
inner :: (Int,Int) -> VtxIx
inner :: (Int, Int) -> VtxIx
inner = (Int -> Int -> VtxIx) -> (Int, Int) -> VtxIx
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> VtxIx
Inner

instance HoleContainer h f point => HasVertices' (PolygonalDomainF h f point) where
  type Vertex   (PolygonalDomainF h f point) = point
  type VertexIx (PolygonalDomainF h f point) = VtxIx

  vertexAt :: VertexIx (PolygonalDomainF h f point)
-> IndexedTraversal'
     (VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point))
vertexAt = \case
    Outer   Int
j -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedTraversal'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall graph.
HasVertices' graph =>
VertexIx graph
-> IndexedTraversal' (VertexIx graph) graph (Vertex graph)
vertexAt Int
VertexIx (SimplePolygonF f point)
j
    Inner Int
i Int
j -> ((Int, Int) -> VtxIx)
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed (Int, Int) -> VtxIx
inner ((Indexed
    (Int, Int)
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ HoleIx (PolygonalDomainF h f point)
-> IndexedTraversal'
     (HoleIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Hole (PolygonalDomainF h f point))
forall polygon.
HasHoles polygon =>
HoleIx polygon
-> IndexedTraversal' (HoleIx polygon) polygon (Hole polygon)
holeAt Int
HoleIx (PolygonalDomainF h f point)
i (Indexed Int (SimplePolygonF f point) (f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     (Int, Int)
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall i j (p :: * -> * -> *) s t r a b.
Indexable (i, j) p =>
(Indexed i s t -> r) -> (Indexed j a b -> s -> t) -> p a b -> r
<.> VertexIx (SimplePolygonF f point)
-> IndexedTraversal'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall graph.
HasVertices' graph =>
VertexIx graph
-> IndexedTraversal' (VertexIx graph) graph (Vertex graph)
vertexAt Int
VertexIx (SimplePolygonF f point)
j

  numVertices :: PolygonalDomainF h f point -> Int
numVertices PolygonalDomainF h f point
pg = SimplePolygonF f point -> Int
forall graph. HasVertices' graph => graph -> Int
numVertices (PolygonalDomainF h f point
pgPolygonalDomainF h f point
-> Getting
     (SimplePolygonF f point)
     (PolygonalDomainF h f point)
     (SimplePolygonF f point)
-> SimplePolygonF f point
forall s a. s -> Getting a s a -> a
^.Getting
  (SimplePolygonF f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Getting (Endo (Endo Int)) (PolygonalDomainF h f point) Int
-> PolygonalDomainF h f point -> Int
forall a s. Num a => Getting (Endo (Endo a)) s a -> s -> a
sumOf ((SimplePolygonF f point
 -> Const (Endo (Endo Int)) (SimplePolygonF f point))
-> PolygonalDomainF h f point
-> Const (Endo (Endo Int)) (PolygonalDomainF h f point)
(Hole (PolygonalDomainF h f point)
 -> Const (Endo (Endo Int)) (Hole (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> Const (Endo (Endo Int)) (PolygonalDomainF h f point)
forall polygon.
HasHoles polygon =>
IndexedTraversal' (HoleIx polygon) polygon (Hole polygon)
IndexedTraversal'
  (HoleIx (PolygonalDomainF h f point))
  (PolygonalDomainF h f point)
  (Hole (PolygonalDomainF h f point))
holes((SimplePolygonF f point
  -> Const (Endo (Endo Int)) (SimplePolygonF f point))
 -> PolygonalDomainF h f point
 -> Const (Endo (Endo Int)) (PolygonalDomainF h f point))
-> ((Int -> Const (Endo (Endo Int)) Int)
    -> SimplePolygonF f point
    -> Const (Endo (Endo Int)) (SimplePolygonF f point))
-> Getting (Endo (Endo Int)) (PolygonalDomainF h f point) Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SimplePolygonF f point -> Int)
-> (Int -> Const (Endo (Endo Int)) Int)
-> SimplePolygonF f point
-> Const (Endo (Endo Int)) (SimplePolygonF f point)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to SimplePolygonF f point -> Int
forall graph. HasVertices' graph => graph -> Int
numVertices) PolygonalDomainF h f point
pg

instance ( HoleContainer h f point
         ) => HasOuterBoundary (PolygonalDomainF h f point) where
  outerBoundary :: IndexedTraversal1'
  (VertexIx (PolygonalDomainF h f point))
  (PolygonalDomainF h f point)
  (Vertex (PolygonalDomainF h f point))
outerBoundary        = (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> Indexed
  Int
  (Vertex (SimplePolygonF f point))
  (f (Vertex (SimplePolygonF f point)))
-> SimplePolygonF f point -> f (SimplePolygonF f point)
Indexed
  Int
  (Vertex (PolygonalDomainF h f point))
  (f (Vertex (PolygonalDomainF h f point)))
-> SimplePolygonF f point -> f (SimplePolygonF f point)
forall polygon.
HasOuterBoundary polygon =>
IndexedTraversal1' (VertexIx polygon) polygon (Vertex polygon)
IndexedTraversal1'
  (VertexIx (SimplePolygonF f point))
  (SimplePolygonF f point)
  (Vertex (SimplePolygonF f point))
outerBoundary
  ccwOuterBoundaryFrom :: VertexIx (PolygonalDomainF h f point)
-> IndexedTraversal1'
     (VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point))
ccwOuterBoundaryFrom = \case
    Outer Int
u -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedTraversal1'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon.
HasOuterBoundary polygon =>
VertexIx polygon
-> IndexedTraversal1' (VertexIx polygon) polygon (Vertex polygon)
ccwOuterBoundaryFrom Int
VertexIx (SimplePolygonF f point)
u
    VertexIx (PolygonalDomainF h f point)
u       -> String
-> VtxIx
-> p point (f point)
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a. String -> VtxIx -> a
error' String
"ccwOuterBoundaryFrom" VertexIx (PolygonalDomainF h f point)
VtxIx
u

  cwOuterBoundaryFrom :: VertexIx (PolygonalDomainF h f point)
-> IndexedTraversal1'
     (VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point))
cwOuterBoundaryFrom  = \case
    Outer Int
u -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedTraversal1'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon.
HasOuterBoundary polygon =>
VertexIx polygon
-> IndexedTraversal1' (VertexIx polygon) polygon (Vertex polygon)
cwOuterBoundaryFrom Int
VertexIx (SimplePolygonF f point)
u
    VertexIx (PolygonalDomainF h f point)
u       -> String
-> VtxIx
-> p point (f point)
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a. String -> VtxIx -> a
error' String
"cwOuterBoundaryFrom" VertexIx (PolygonalDomainF h f point)
VtxIx
u

  outerBoundaryVertexAt :: VertexIx (PolygonalDomainF h f point)
-> IndexedGetter
     (VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point))
outerBoundaryVertexAt = \case
    Outer Int
u -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedGetter
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon.
HasOuterBoundary polygon =>
VertexIx polygon
-> IndexedGetter (VertexIx polygon) polygon (Vertex polygon)
outerBoundaryVertexAt Int
VertexIx (SimplePolygonF f point)
u
    VertexIx (PolygonalDomainF h f point)
u       -> String
-> VtxIx
-> p point (f point)
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a. String -> VtxIx -> a
error' String
"outerBoundaryVertexAt" VertexIx (PolygonalDomainF h f point)
VtxIx
u

  outerBoundaryEdges :: IndexedFold1
  (VertexIx (PolygonalDomainF h f point),
   VertexIx (PolygonalDomainF h f point))
  (PolygonalDomainF h f point)
  (Vertex (PolygonalDomainF h f point),
   Vertex (PolygonalDomainF h f point))
outerBoundaryEdges  = ((Int, Int) -> (VtxIx, VtxIx))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point),
         Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed (Int, Int) -> (VtxIx, VtxIx)
mapEdge ((Indexed
    (Int, Int)
    (Vertex (PolygonalDomainF h f point),
     Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point),
        Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point),
         Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     (Int, Int)
     (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point),
         Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> Indexed
  (Int, Int)
  (Vertex (SimplePolygonF f point), Vertex (SimplePolygonF f point))
  (f (Vertex (SimplePolygonF f point),
      Vertex (SimplePolygonF f point)))
-> SimplePolygonF f point -> f (SimplePolygonF f point)
Indexed
  (Int, Int)
  (Vertex (PolygonalDomainF h f point),
   Vertex (PolygonalDomainF h f point))
  (f (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point)))
-> SimplePolygonF f point -> f (SimplePolygonF f point)
forall polygon.
HasOuterBoundary polygon =>
IndexedFold1
  (VertexIx polygon, VertexIx polygon)
  polygon
  (Vertex polygon, Vertex polygon)
IndexedFold1
  (VertexIx (SimplePolygonF f point),
   VertexIx (SimplePolygonF f point))
  (SimplePolygonF f point)
  (Vertex (SimplePolygonF f point), Vertex (SimplePolygonF f point))
outerBoundaryEdges
  outerBoundaryEdgeAt :: VertexIx (PolygonalDomainF h f point)
-> IndexedGetter
     (VertexIx (PolygonalDomainF h f point),
      VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
outerBoundaryEdgeAt = \case
    Outer Int
u -> ((Int, Int) -> (VtxIx, VtxIx))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point),
         Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed (Int, Int) -> (VtxIx, VtxIx)
mapEdge ((Indexed
    (Int, Int)
    (Vertex (PolygonalDomainF h f point),
     Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point),
        Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point),
         Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      (Int, Int)
      (Vertex (PolygonalDomainF h f point),
       Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point),
          Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     (Int, Int)
     (Vertex (PolygonalDomainF h f point),
      Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point),
         Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedGetter
     (VertexIx (SimplePolygonF f point),
      VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point), Vertex (SimplePolygonF f point))
forall polygon.
HasOuterBoundary polygon =>
VertexIx polygon
-> IndexedGetter
     (VertexIx polygon, VertexIx polygon)
     polygon
     (Vertex polygon, Vertex polygon)
outerBoundaryEdgeAt Int
VertexIx (SimplePolygonF f point)
u
    VertexIx (PolygonalDomainF h f point)
u       -> String
-> VtxIx
-> p (point, point) (f (point, point))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a. String -> VtxIx -> a
error' String
"outerBoundaryVertexAt" VertexIx (PolygonalDomainF h f point)
VtxIx
u

-- | Helper to produce a more useful error message
error'     :: String -> VtxIx -> a
error' :: forall a. String -> VtxIx -> a
error' String
s VtxIx
u = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
s String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", vertex " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> VtxIx -> String
forall a. Show a => a -> String
show VtxIx
u String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" not on outer boundary"

-- | remap an edge on the outer boundary
mapEdge       :: (Int,Int) -> (VtxIx,VtxIx)
mapEdge :: (Int, Int) -> (VtxIx, VtxIx)
mapEdge (Int
u,Int
v) = (Int -> VtxIx
Outer Int
u, Int -> VtxIx
Outer Int
v)

instance ( Point_ point 2 r
         , HasFromFoldable1 f
         , HoleContainer h f point
         ) => Polygon_ (PolygonalDomainF h f point) point r where
  extremes :: (Num r, Ord r, Point_ point 2 r) =>
Vector 2 r -> PolygonalDomainF h f point -> (point, point)
extremes Vector 2 r
u = Vector 2 r -> SimplePolygonF f point -> (point, point)
forall polygon point r.
(Polygon_ polygon point r, Num r, Ord r, Point_ point 2 r) =>
Vector 2 r -> polygon -> (point, point)
extremes Vector 2 r
u (SimplePolygonF f point -> (point, point))
-> (PolygonalDomainF h f point -> SimplePolygonF f point)
-> PolygonalDomainF h f point
-> (point, point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting
  (SimplePolygonF f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
-> PolygonalDomainF h f point -> SimplePolygonF f point
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting
  (SimplePolygonF f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon
  ccwPredecessorOf :: VertexIx (PolygonalDomainF h f point)
-> IndexedLens'
     (VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point))
ccwPredecessorOf = \case
    Outer Int
u   -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer     ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedLens'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon point r.
Polygon_ polygon point r =>
VertexIx polygon
-> IndexedLens' (VertexIx polygon) polygon (Vertex polygon)
ccwPredecessorOf Int
VertexIx (SimplePolygonF f point)
u
    Inner Int
i Int
j -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed (Int -> Int -> VtxIx
Inner Int
i) ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ Traversing
  (->)
  f
  (PolygonalDomainF h f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
  (SimplePolygonF f point)
-> (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall (p :: * -> * -> *) (f :: * -> *) s t a.
(HasCallStack, Conjoined p, Functor f) =>
Traversing p f s t a a -> Over p f s t a a
singular (HoleIx (PolygonalDomainF h f point)
-> IndexedTraversal'
     (HoleIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Hole (PolygonalDomainF h f point))
forall polygon.
HasHoles polygon =>
HoleIx polygon
-> IndexedTraversal' (HoleIx polygon) polygon (Hole polygon)
holeAt Int
HoleIx (PolygonalDomainF h f point)
i)  ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedLens'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon point r.
Polygon_ polygon point r =>
VertexIx polygon
-> IndexedLens' (VertexIx polygon) polygon (Vertex polygon)
ccwPredecessorOf Int
VertexIx (SimplePolygonF f point)
j
  ccwSuccessorOf :: VertexIx (PolygonalDomainF h f point)
-> IndexedLens'
     (VertexIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Vertex (PolygonalDomainF h f point))
ccwSuccessorOf = \case
    Outer Int
u   -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed Int -> VtxIx
Outer     ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedLens'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon point r.
Polygon_ polygon point r =>
VertexIx polygon
-> IndexedLens' (VertexIx polygon) polygon (Vertex polygon)
ccwSuccessorOf Int
VertexIx (SimplePolygonF f point)
u
    Inner Int
i Int
j -> (Int -> VtxIx)
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall j (p :: * -> * -> *) i a b r.
Indexable j p =>
(i -> j) -> (Indexed i a b -> r) -> p a b -> r
reindexed (Int -> Int -> VtxIx
Inner Int
i) ((Indexed
    Int
    (Vertex (PolygonalDomainF h f point))
    (f (Vertex (PolygonalDomainF h f point)))
  -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
 -> p (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
 -> PolygonalDomainF h f point
 -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> p (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall a b. (a -> b) -> a -> b
$ Traversing
  (->)
  f
  (PolygonalDomainF h f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
  (SimplePolygonF f point)
-> (SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall (p :: * -> * -> *) (f :: * -> *) s t a.
(HasCallStack, Conjoined p, Functor f) =>
Traversing p f s t a a -> Over p f s t a a
singular (HoleIx (PolygonalDomainF h f point)
-> IndexedTraversal'
     (HoleIx (PolygonalDomainF h f point))
     (PolygonalDomainF h f point)
     (Hole (PolygonalDomainF h f point))
forall polygon.
HasHoles polygon =>
HoleIx polygon
-> IndexedTraversal' (HoleIx polygon) polygon (Hole polygon)
holeAt Int
HoleIx (PolygonalDomainF h f point)
i)  ((SimplePolygonF f point -> f (SimplePolygonF f point))
 -> PolygonalDomainF h f point -> f (PolygonalDomainF h f point))
-> (Indexed
      Int
      (Vertex (PolygonalDomainF h f point))
      (f (Vertex (PolygonalDomainF h f point)))
    -> SimplePolygonF f point -> f (SimplePolygonF f point))
-> Indexed
     Int
     (Vertex (PolygonalDomainF h f point))
     (f (Vertex (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> f (PolygonalDomainF h f point)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.> VertexIx (SimplePolygonF f point)
-> IndexedLens'
     (VertexIx (SimplePolygonF f point))
     (SimplePolygonF f point)
     (Vertex (SimplePolygonF f point))
forall polygon point r.
Polygon_ polygon point r =>
VertexIx polygon
-> IndexedLens' (VertexIx polygon) polygon (Vertex polygon)
ccwSuccessorOf Int
VertexIx (SimplePolygonF f point)
j

{-

instance ( SimplePolygon_ (SimplePolygonF f point) point r
         , HolesContainer h f point
         )
         => HasInPolygon (PolygonalDomainF h f point) point r where
  inPolygon q pg = case q `inPolygon` (pg^.outerBoundaryPolygon) of
                     Inside  -> case getResult $ ifoldMapOf holes (testInHole q) pg of
                       Outside -> Inside
                       res     -> res
                     res     -> Outer <$> res
    where
      testInHole q i pg = Intersect $ Inner i <$> q `inPolygon` (pg^.outerBoundaryPolygon)


newtype Intersect = Intersect { getResult :: PointLocationResultWith Vtx }

instance Semigroup Intersect where
  (Intersect Outside) <> r = r
  (Intersect Inside)


instance Semigroup Monoid where
  mempty = Intersect Outside

-}


instance ( HoleContainer h nonEmpty vertex
         , VertexContainer nonEmpty vertex, HasFromFoldable1 nonEmpty, Point_ vertex 2 r
         , Fractional r, Ord r
         ) => HasPickPoint (PolygonalDomainF h nonEmpty vertex) r where
  pointInteriorTo :: PolygonalDomainF h nonEmpty vertex -> Point 2 r
pointInteriorTo PolygonalDomainF h nonEmpty vertex
pg = case Getting
  (Maybe (Max (Arg r vertex)))
  (PolygonalDomainF h nonEmpty vertex)
  vertex
-> (vertex -> Maybe (Max (Arg r vertex)))
-> PolygonalDomainF h nonEmpty vertex
-> Maybe (Max (Arg r vertex))
forall r s a. Getting r s a -> (a -> r) -> s -> r
foldMapOf Getting
  (Maybe (Max (Arg r vertex)))
  (PolygonalDomainF h nonEmpty vertex)
  vertex
(Vertex (PolygonalDomainF h nonEmpty vertex)
 -> Const
      (Maybe (Max (Arg r vertex)))
      (Vertex (PolygonalDomainF h nonEmpty vertex)))
-> PolygonalDomainF h nonEmpty vertex
-> Const
     (Maybe (Max (Arg r vertex))) (PolygonalDomainF h nonEmpty vertex)
forall graph graph'.
HasVertices graph graph' =>
IndexedTraversal1
  (VertexIx graph) graph graph' (Vertex graph) (Vertex graph')
IndexedTraversal1
  (VertexIx (PolygonalDomainF h nonEmpty vertex))
  (PolygonalDomainF h nonEmpty vertex)
  (PolygonalDomainF h nonEmpty vertex)
  (Vertex (PolygonalDomainF h nonEmpty vertex))
  (Vertex (PolygonalDomainF h nonEmpty vertex))
vertices vertex -> Maybe (Max (Arg r vertex))
farthest PolygonalDomainF h nonEmpty vertex
pg of
      Maybe (Max (Arg r vertex))
Nothing              -> Triangle vertex -> Point 2 r
forall polygon r. HasPickPoint polygon r => polygon -> Point 2 r
pointInteriorTo Triangle vertex
tri
      Just (Max (Arg r
_ vertex
p)) -> Vector 2 r -> Point 2 r
forall v. v -> PointF v
Point (Vector 2 r -> Point 2 r) -> Vector 2 r -> Point 2 r
forall a b. (a -> b) -> a -> b
$ r -> Vector 2 r -> Vector 2 r -> Vector 2 r
forall r vector (d :: Nat).
(Num r, Additive_ vector d r) =>
r -> vector -> vector -> vector
lerp (r
1r -> r -> r
forall a. Fractional a => a -> a -> a
/r
2) (vertex
vvertex -> Getting (Vector 2 r) vertex (Vector 2 r) -> Vector 2 r
forall s a. s -> Getting a s a -> a
^.Getting (Vector 2 r) vertex (Vector 2 r)
forall (d :: Nat) r s.
(Dimension vertex ~ d, NumType vertex ~ r, Dimension vertex ~ d,
 NumType vertex ~ s) =>
Lens vertex vertex (Vector d r) (Vector d s)
forall point point' (d :: Nat) r s.
(HasVector point point', Dimension point ~ d, NumType point ~ r,
 Dimension point' ~ d, NumType point' ~ s) =>
Lens point point' (Vector d r) (Vector d s)
Lens vertex vertex (Vector 2 r) (Vector 2 r)
vector) (vertex
pvertex -> Getting (Vector 2 r) vertex (Vector 2 r) -> Vector 2 r
forall s a. s -> Getting a s a -> a
^.Getting (Vector 2 r) vertex (Vector 2 r)
forall (d :: Nat) r s.
(Dimension vertex ~ d, NumType vertex ~ r, Dimension vertex ~ d,
 NumType vertex ~ s) =>
Lens vertex vertex (Vector d r) (Vector d s)
forall point point' (d :: Nat) r s.
(HasVector point point', Dimension point ~ d, NumType point ~ r,
 Dimension point' ~ d, NumType point' ~ s) =>
Lens point point' (Vector d r) (Vector d s)
Lens vertex vertex (Vector 2 r) (Vector 2 r)
vector)
    where
      outer :: SimplePolygonF nonEmpty vertex
outer  = PolygonalDomainF h nonEmpty vertex
pgPolygonalDomainF h nonEmpty vertex
-> Getting
     (SimplePolygonF nonEmpty vertex)
     (PolygonalDomainF h nonEmpty vertex)
     (SimplePolygonF nonEmpty vertex)
-> SimplePolygonF nonEmpty vertex
forall s a. s -> Getting a s a -> a
^.Getting
  (SimplePolygonF nonEmpty vertex)
  (PolygonalDomainF h nonEmpty vertex)
  (SimplePolygonF nonEmpty vertex)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon
      (Int
i, vertex
v) = Getting
  (First (Int, vertex))
  (SimplePolygonF nonEmpty vertex)
  (Int, vertex)
-> SimplePolygonF nonEmpty vertex -> (Int, vertex)
forall a s. Getting (First a) s a -> s -> a
first1Of ((Vertex (SimplePolygonF nonEmpty vertex)
 -> Vertex (SimplePolygonF nonEmpty vertex) -> Ordering)
-> IndexedFold1
     (VertexIx (SimplePolygonF nonEmpty vertex))
     (SimplePolygonF nonEmpty vertex)
     (Vertex (SimplePolygonF nonEmpty vertex))
forall polygon.
HasOuterBoundary polygon =>
(Vertex polygon -> Vertex polygon -> Ordering)
-> IndexedFold1 (VertexIx polygon) polygon (Vertex polygon)
minimumVertexBy ((vertex -> r) -> vertex -> vertex -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (vertex -> Getting r vertex r -> r
forall s a. s -> Getting a s a -> a
^.Getting r vertex r
forall (d :: Nat) point r.
(1 <= d, Point_ point d r) =>
IndexedLens' Int point r
IndexedLens' Int vertex r
xCoord))(Indexed Int vertex (Const (First (Int, vertex)) vertex)
 -> SimplePolygonF nonEmpty vertex
 -> Const (First (Int, vertex)) (SimplePolygonF nonEmpty vertex))
-> (((Int, vertex) -> Const (First (Int, vertex)) (Int, vertex))
    -> Indexed Int vertex (Const (First (Int, vertex)) vertex))
-> Getting
     (First (Int, vertex))
     (SimplePolygonF nonEmpty vertex)
     (Int, vertex)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((Int, vertex) -> Const (First (Int, vertex)) (Int, vertex))
-> Indexed Int vertex (Const (First (Int, vertex)) vertex)
forall i (p :: * -> * -> *) (f :: * -> *) s j t.
(Indexable i p, Functor f) =>
p (i, s) (f (j, t)) -> Indexed i s (f t)
withIndex) SimplePolygonF nonEmpty vertex
outer
      u :: vertex
u      = SimplePolygonF nonEmpty vertex
outerSimplePolygonF nonEmpty vertex
-> Getting vertex (SimplePolygonF nonEmpty vertex) vertex -> vertex
forall s a. s -> Getting a s a -> a
^.VertexIx (SimplePolygonF nonEmpty vertex)
-> IndexedGetter
     (VertexIx (SimplePolygonF nonEmpty vertex))
     (SimplePolygonF nonEmpty vertex)
     (Vertex (SimplePolygonF nonEmpty vertex))
forall polygon.
HasOuterBoundary polygon =>
VertexIx polygon
-> IndexedGetter (VertexIx polygon) polygon (Vertex polygon)
outerBoundaryVertexAt (Int -> Int
forall a. Enum a => a -> a
succ Int
i)
      w :: vertex
w      = SimplePolygonF nonEmpty vertex
outerSimplePolygonF nonEmpty vertex
-> Getting vertex (SimplePolygonF nonEmpty vertex) vertex -> vertex
forall s a. s -> Getting a s a -> a
^.VertexIx (SimplePolygonF nonEmpty vertex)
-> IndexedGetter
     (VertexIx (SimplePolygonF nonEmpty vertex))
     (SimplePolygonF nonEmpty vertex)
     (Vertex (SimplePolygonF nonEmpty vertex))
forall polygon.
HasOuterBoundary polygon =>
VertexIx polygon
-> IndexedGetter (VertexIx polygon) polygon (Vertex polygon)
outerBoundaryVertexAt (Int -> Int
forall a. Enum a => a -> a
pred Int
i)

      tri :: Triangle vertex
tri    = vertex -> vertex -> vertex -> Triangle vertex
forall point. point -> point -> point -> Triangle point
Triangle vertex
u vertex
v vertex
w
      diag :: LinePV 2 r
diag   = Point 2 r -> Vector 2 r -> LinePV 2 r
forall (d :: Nat) r. Point d r -> Vector d r -> LinePV d r
LinePV (vertex
uvertex -> Getting (Point 2 r) vertex (Point 2 r) -> Point 2 r
forall s a. s -> Getting a s a -> a
^.Getting (Point 2 r) vertex (Point 2 r)
forall point (d :: Nat) r.
Point_ point d r =>
Lens' point (Point d r)
Lens' vertex (Point 2 r)
asPoint) (vertex
w vertex -> vertex -> Vector 2 r
forall point (d :: Nat) r.
(Affine_ point d r, Num r) =>
point -> point -> Vector d r
.-. vertex
u)
      farthest :: vertex -> Maybe (Max (Arg r vertex))
farthest vertex
p
        | vertex
p vertex -> Triangle vertex -> PointLocationResultWith Int
forall {point} {queryPoint} {f :: * -> *}.
(Dimension point ~ 2, Dimension queryPoint ~ 2,
 NumType queryPoint ~ NumType point, Num (NumType point),
 Ord (NumType point), Foldable1 f,
 Point_ queryPoint 2 (NumType point),
 Point_ point 2 (NumType point)) =>
queryPoint -> f point -> PointLocationResultWith Int
`inTriangle` Triangle vertex
tri PointLocationResultWith Int -> PointLocationResultWith Int -> Bool
forall a. Eq a => a -> a -> Bool
== PointLocationResultWith Int
forall edge. PointLocationResultWith edge
StrictlyInside =
            Max (Arg r vertex) -> Maybe (Max (Arg r vertex))
forall a. a -> Maybe a
Just (Arg r vertex -> Max (Arg r vertex)
forall a. a -> Max a
Max (r -> vertex -> Arg r vertex
forall a b. a -> b -> Arg a b
Arg (vertex
p vertex -> LinePV 2 r -> r
forall g r (d :: Nat) point.
(HasSquaredEuclideanDistance g, r ~ NumType g, d ~ Dimension g,
 Num r, Point_ point d r) =>
point -> g -> r
forall r (d :: Nat) point.
(r ~ NumType (LinePV 2 r), d ~ Dimension (LinePV 2 r), Num r,
 Point_ point d r) =>
point -> LinePV 2 r -> r
`squaredEuclideanDistTo` LinePV 2 r
diag) vertex
p))
        | Bool
otherwise         = Maybe (Max (Arg r vertex))
forall a. Maybe a
Nothing
  -- The overall strategy follows the "a polygon can be triangulated
  -- argument". I.e. the idea is as follows: we find the leftmost
  -- vertex v, and its two neighbours u and w. If uwv is a triangle of
  -- the polygon (i.e. uw is a diagonal), then one can just pick a
  -- point sticly inside the triangle. If uwv is not a valid triangle
  -- of the polgyon, then there must be an other vertex inside the
  -- triangle [*], we find the vertex farthest from uw then it follows
  -- that the linesegment vp is a diagonal of the polygon, and hence
  -- we can just return the midpoint of v and p.
  --
  -- [*] it could be that there is a vertex on the interior of the
  -- segment uw (and no vertices strictly in the interior). However, then
  -- picking a point inside the triangle is also fine.
      inTriangle :: queryPoint
-> f point
-> PointLocationResultWith (VertexIx (SimplePolygon point))
inTriangle queryPoint
q f point
tr = queryPoint
q queryPoint
-> SimplePolygon point
-> PointLocationResultWith (VertexIx (SimplePolygon point))
forall queryPoint.
(Num (NumType queryPoint), Ord (NumType queryPoint),
 Point_ queryPoint 2 (NumType queryPoint)) =>
queryPoint
-> SimplePolygon point
-> PointLocationResultWith (VertexIx (SimplePolygon point))
forall polygon point r queryPoint.
(HasInPolygon polygon point r, Num r, Ord r,
 Point_ queryPoint 2 r) =>
queryPoint -> polygon -> PointLocationResultWith (VertexIx polygon)
`inPolygon` (f point -> SimplePolygon point
forall simplePolygon point r (f :: * -> *).
(SimplePolygon_ simplePolygon point r, Foldable1 f) =>
f point -> simplePolygon
forall (f :: * -> *). Foldable1 f => f point -> SimplePolygon point
uncheckedFromCCWPoints f point
tr :: SimplePolygon _)
      -- TOOD: just make triangle an instance of SimplePolygon_



--------------------------------------------------------------------------------


-- | interpret a simple polygon as a Polygonal domain.
asSimplePolygon :: (HasFromFoldable h, HoleContainer h f point)
                 => Prism' (PolygonalDomainF h f point) (SimplePolygonF f point)
asSimplePolygon :: forall (h :: * -> *) (f :: * -> *) point.
(HasFromFoldable h, HoleContainer h f point) =>
Prism' (PolygonalDomainF h f point) (SimplePolygonF f point)
asSimplePolygon = (SimplePolygonF f point -> PolygonalDomainF h f point)
-> (PolygonalDomainF h f point -> Maybe (SimplePolygonF f point))
-> Prism
     (PolygonalDomainF h f point)
     (PolygonalDomainF h f point)
     (SimplePolygonF f point)
     (SimplePolygonF f point)
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' ((SimplePolygonF f point
 -> h (SimplePolygonF f point) -> PolygonalDomainF h f point)
-> h (SimplePolygonF f point)
-> SimplePolygonF f point
-> PolygonalDomainF h f point
forall a b c. (a -> b -> c) -> b -> a -> c
flip SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
forall (h :: * -> *) (f :: * -> *) point.
SimplePolygonF f point
-> h (SimplePolygonF f point) -> PolygonalDomainF h f point
PolygonalDomain ([SimplePolygonF f point] -> h (SimplePolygonF f point)
forall a. [a] -> h a
forall (f :: * -> *) a. HasFromFoldable f => [a] -> f a
fromList []))
                         (\PolygonalDomainF h f point
pd -> if Getting All (PolygonalDomainF h f point) (SimplePolygonF f point)
-> PolygonalDomainF h f point -> Bool
forall s a. Getting All s a -> s -> Bool
nullOf Getting All (PolygonalDomainF h f point) (SimplePolygonF f point)
(Hole (PolygonalDomainF h f point)
 -> Const All (Hole (PolygonalDomainF h f point)))
-> PolygonalDomainF h f point
-> Const All (PolygonalDomainF h f point)
forall polygon.
HasHoles polygon =>
IndexedTraversal' (HoleIx polygon) polygon (Hole polygon)
IndexedTraversal'
  (HoleIx (PolygonalDomainF h f point))
  (PolygonalDomainF h f point)
  (Hole (PolygonalDomainF h f point))
holes PolygonalDomainF h f point
pd then SimplePolygonF f point -> Maybe (SimplePolygonF f point)
forall a. a -> Maybe a
Just (PolygonalDomainF h f point
pdPolygonalDomainF h f point
-> Getting
     (SimplePolygonF f point)
     (PolygonalDomainF h f point)
     (SimplePolygonF f point)
-> SimplePolygonF f point
forall s a. s -> Getting a s a -> a
^.Getting
  (SimplePolygonF f point)
  (PolygonalDomainF h f point)
  (SimplePolygonF f point)
forall (h :: * -> *) (f :: * -> *) point (f :: * -> *).
Functor f =>
(SimplePolygonF f point -> f (SimplePolygonF f point))
-> PolygonalDomainF h f point -> f (PolygonalDomainF h f point)
outerBoundaryPolygon)
                                 else Maybe (SimplePolygonF f point)
forall a. Maybe a
Nothing
                         )