--------------------------------------------------------------------------------
-- |
-- Module      :  Algorithms.Geometry.LineSegmentIntersection
-- Copyright   :  (C) Frank Staals
-- License     :  see the LICENSE file
-- Maintainer  :  Frank Staals
--------------------------------------------------------------------------------
module Algorithms.Geometry.LineSegmentIntersection
  ( BooleanSweep.hasIntersections
  , BO.intersections
  , BO.interiorIntersections
  , Intersections
  , Associated(..)
  , IntersectionPoint(..), mkIntersectionPoint
  -- , isInteriorIntersection
  , hasSelfIntersections
  ) where

import qualified Algorithms.Geometry.LineSegmentIntersection.BentleyOttmann as BO
import qualified Algorithms.Geometry.LineSegmentIntersection.BooleanSweep as BooleanSweep
import           Algorithms.Geometry.LineSegmentIntersection.Types
import           Data.Ext (ext)
import           Data.Geometry.LineSegment
import           Data.Geometry.Polygon

import qualified Data.Map as Map

-- | Test if the polygon has self intersections.
--
-- \(O(n \log n)\)
hasSelfIntersections :: (Ord r, Fractional r) => Polygon t p r -> Bool
hasSelfIntersections :: Polygon t p r -> Bool
hasSelfIntersections = Bool -> Bool
not (Bool -> Bool) -> (Polygon t p r -> Bool) -> Polygon t p r -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map (Point 2 r) (Associated p r ()) -> Bool
forall k a. Map k a -> Bool
Map.null (Map (Point 2 r) (Associated p r ()) -> Bool)
-> (Polygon t p r -> Map (Point 2 r) (Associated p r ()))
-> Polygon t p r
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [LineSegment 2 p r :+ ()] -> Map (Point 2 r) (Associated p r ())
forall r p e.
(Ord r, Fractional r) =>
[LineSegment 2 p r :+ e] -> Intersections p r e
BO.interiorIntersections ([LineSegment 2 p r :+ ()] -> Map (Point 2 r) (Associated p r ()))
-> (Polygon t p r -> [LineSegment 2 p r :+ ()])
-> Polygon t p r
-> Map (Point 2 r) (Associated p r ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LineSegment 2 p r -> LineSegment 2 p r :+ ())
-> [LineSegment 2 p r] -> [LineSegment 2 p r :+ ()]
forall a b. (a -> b) -> [a] -> [b]
map LineSegment 2 p r -> LineSegment 2 p r :+ ()
forall a. a -> a :+ ()
ext ([LineSegment 2 p r] -> [LineSegment 2 p r :+ ()])
-> (Polygon t p r -> [LineSegment 2 p r])
-> Polygon t p r
-> [LineSegment 2 p r :+ ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Polygon t p r -> [LineSegment 2 p r]
forall (t :: PolygonType) p r. Polygon t p r -> [LineSegment 2 p r]
listEdges
-- hasSelfIntersections :: (Ord r, Num r) => Polygon t p r -> Bool
-- hasSelfIntersections = BooleanSweep.hasIntersections . listEdges
-- FIXME: fix the open/closed bug, then switch to a boolean sweep based version