module Algorithms.Geometry.ConvexHull.GrahamScan( convexHull
, upperHull, upperHull'
, lowerHull, lowerHull'
, upperHullFromSorted, upperHullFromSorted'
) where
import Control.Lens ((^.))
import Data.Ext
import Data.Geometry.Point
import Data.Geometry.Polygon
import Data.Geometry.Polygon.Convex (ConvexPolygon(..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.List.NonEmpty (NonEmpty(..))
convexHull :: (Ord r, Num r)
=> NonEmpty (Point 2 r :+ p) -> ConvexPolygon p r
convexHull (p :| []) = ConvexPolygon . fromPoints $ [p]
convexHull ps = let ps' = NonEmpty.toList . NonEmpty.sortBy incXdecY $ ps
uh = NonEmpty.tail . hull' $ ps'
lh = NonEmpty.tail . hull' $ reverse ps'
in ConvexPolygon . fromPoints . reverse $ lh ++ uh
upperHull :: (Ord r, Num r) => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
upperHull = NonEmpty.reverse . hull id
upperHull' :: (Ord r, Num r) => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
upperHull' = NonEmpty.reverse . dropVertical . hull id
dropVertical :: Eq r => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
dropVertical = \case
h@(_ :| []) -> h
h@(p :| (q : rest)) | p^.core.xCoord == q^.core.xCoord -> q :| rest
| otherwise -> h
lowerHull :: (Ord r, Num r) => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
lowerHull = hull reverse
lowerHull' :: (Ord r, Num r) => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
lowerHull' = dropVertical . hull reverse
hull :: (Ord r, Num r)
=> ([Point 2 r :+ p] -> [Point 2 r :+ p])
-> NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
hull _ h@(_ :| []) = h
hull f pts = hull' . f
. NonEmpty.toList . NonEmpty.sortBy incXdecY $ pts
incXdecY :: Ord r => (Point 2 r) :+ p -> (Point 2 r) :+ q -> Ordering
incXdecY (Point2 px py :+ _) (Point2 qx qy :+ _) =
compare px qx <> compare qy py
upperHullFromSorted :: (Ord r, Num r) => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
upperHullFromSorted = \case
h@(_ :| []) -> h
pts -> hull' $ NonEmpty.toList pts
upperHullFromSorted' :: (Ord r, Num r) => NonEmpty (Point 2 r :+ p) -> NonEmpty (Point 2 r :+ p)
upperHullFromSorted' = dropVertical . upperHullFromSorted
hull' :: (Ord r, Num r) => [Point 2 r :+ p] -> NonEmpty (Point 2 r :+ p)
hull' (a:b:ps) = NonEmpty.fromList $ hull'' [b,a] ps
where
hull'' h [] = h
hull'' h (p:ps') = hull'' (cleanMiddle (p:h)) ps'
cleanMiddle h@[_,_] = h
cleanMiddle h@(z:y:x:rest)
| rightTurn (x^.core) (y^.core) (z^.core) = h
| otherwise = cleanMiddle (z:x:rest)
cleanMiddle _ = error "cleanMiddle: too few points"
rightTurn :: (Ord r, Num r) => Point 2 r -> Point 2 r -> Point 2 r -> Bool
rightTurn a b c = ccw a b c == CW