2014-12-03 20:26:35 +00:00
|
|
|
module Algorithms.PolygonIntersection where
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
import Algebra.Vector
|
2014-10-25 18:23:34 +00:00
|
|
|
import Control.Applicative
|
2014-10-24 23:44:13 +00:00
|
|
|
import Data.Dequeue (BankersDequeue)
|
|
|
|
import qualified Data.Dequeue as Q
|
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
|
|
|
import Diagrams.TwoD.Types
|
|
|
|
import MyPrelude
|
|
|
|
import QueueEx
|
|
|
|
|
|
|
|
|
2014-11-16 03:49:34 +00:00
|
|
|
-- TODO: probably use a zipper.
|
2014-10-24 23:44:13 +00:00
|
|
|
-- |Describes a point on the convex hull of the polygon.
|
|
|
|
-- In addition to the point itself, both it's predecessor and
|
|
|
|
-- successor are saved for convenience.
|
|
|
|
data PolyPT =
|
|
|
|
PolyA {
|
2015-05-21 00:14:15 +00:00
|
|
|
id' :: P2 Double
|
|
|
|
, pre :: P2 Double
|
|
|
|
, suc :: P2 Double
|
2014-10-24 23:44:13 +00:00
|
|
|
}
|
|
|
|
| PolyB {
|
2015-05-21 00:14:15 +00:00
|
|
|
id' :: P2 Double
|
|
|
|
, pre :: P2 Double
|
|
|
|
, suc :: P2 Double
|
2014-10-24 23:44:13 +00:00
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
|
2014-10-25 18:23:34 +00:00
|
|
|
isPolyA :: PolyPT -> Bool
|
|
|
|
isPolyA PolyA {} = True
|
|
|
|
isPolyA _ = False
|
|
|
|
|
|
|
|
|
|
|
|
isPolyB :: PolyPT -> Bool
|
|
|
|
isPolyB = not . isPolyA
|
|
|
|
|
|
|
|
|
2014-10-24 23:44:13 +00:00
|
|
|
-- |Shift a list of sorted convex hull points of a polygon so that
|
|
|
|
-- the first element in the list is the one with the highest y-coordinate.
|
|
|
|
-- This is done in O(n).
|
2015-05-21 00:14:15 +00:00
|
|
|
sortLexPoly :: [P2 Double] -> [P2 Double]
|
2014-10-24 23:44:13 +00:00
|
|
|
sortLexPoly ps = maybe [] (`shiftM` ps) (elemIndex (yMax ps) ps)
|
|
|
|
where
|
|
|
|
yMax = foldl1 (\x y -> if ptCmpY x y == GT then x else y)
|
|
|
|
|
|
|
|
|
2014-10-26 03:22:05 +00:00
|
|
|
-- |Make a PolyPT list out of a regular list of points, so
|
|
|
|
-- the predecessor and successors are all saved.
|
2015-05-21 00:14:15 +00:00
|
|
|
mkPolyPTList :: (P2 Double -> P2 Double -> P2 Double -> PolyPT) -- ^ PolyA or PolyB function
|
|
|
|
-> [P2 Double] -- ^ polygon points
|
2014-10-26 13:58:43 +00:00
|
|
|
-> [PolyPT]
|
2014-10-26 03:22:05 +00:00
|
|
|
mkPolyPTList f' pts@(x':y':_:_) =
|
|
|
|
f' x' (last pts) y' : go f' pts
|
|
|
|
where
|
|
|
|
go f (x:y:z:xs) = f y x z : go f (y:z:xs)
|
|
|
|
go f [x, y] = [f y x x']
|
|
|
|
go _ _ = []
|
|
|
|
mkPolyPTList _ _ = []
|
|
|
|
|
|
|
|
|
2014-10-25 18:23:34 +00:00
|
|
|
-- |Sort the points of two polygons according to their y-coordinates,
|
2014-10-24 23:44:13 +00:00
|
|
|
-- while saving the origin of that point. This is done in O(n).
|
2015-05-21 00:14:15 +00:00
|
|
|
sortLexPolys :: ([P2 Double], [P2 Double]) -> [PolyPT]
|
2014-10-24 23:44:13 +00:00
|
|
|
sortLexPolys (pA'@(_:_), pB'@(_:_)) =
|
2014-10-26 03:22:05 +00:00
|
|
|
queueToList $ go (Q.fromList . mkPolyPTList PolyA . sortLexPoly $ pA')
|
|
|
|
(Q.fromList . mkPolyPTList PolyB . sortLexPoly $ pB')
|
2014-10-24 23:44:13 +00:00
|
|
|
where
|
|
|
|
-- Start recursive algorithm, each polygon is represented by a Queue.
|
|
|
|
-- Traverse predecessor and successor and insert them in the right
|
|
|
|
-- order into the resulting queue.
|
|
|
|
-- We start at the max y-coordinates of both polygons.
|
2014-10-26 13:58:43 +00:00
|
|
|
go :: BankersDequeue PolyPT -- polyA
|
|
|
|
-> BankersDequeue PolyPT -- polyB
|
|
|
|
-> BankersDequeue PolyPT -- sorted queue
|
2014-10-24 23:44:13 +00:00
|
|
|
go pA pB
|
|
|
|
-- Nothing to sort.
|
2014-10-25 18:23:34 +00:00
|
|
|
| Q.null pA && Q.null pB = Q.empty
|
2014-10-24 23:44:13 +00:00
|
|
|
-- Current point of polygon A is higher on the y-axis than the
|
|
|
|
-- current point of polygon B, so insert it into the resulting
|
|
|
|
-- queue and traverse the rest.
|
2014-10-26 03:22:05 +00:00
|
|
|
| ptCmpY (fromMaybe negInfPT (id' <$> Q.first pA))
|
2014-10-28 17:51:11 +00:00
|
|
|
(fromMaybe negInfPT (id' <$> Q.first pB)) == GT
|
2015-09-04 22:51:12 +00:00
|
|
|
= Q.pushFront (go (maybeShift . snd . fromJust . Q.popFront $ pA) pB)
|
2014-10-26 03:22:05 +00:00
|
|
|
(fromJust . Q.first $ pA)
|
2014-10-24 23:44:13 +00:00
|
|
|
-- Same as above, except that the current point of polygon B
|
|
|
|
-- is higher.
|
2015-09-04 22:51:12 +00:00
|
|
|
| otherwise = Q.pushFront (go pA (maybeShift . snd . fromJust . Q.popFront $ pB))
|
2014-10-26 03:22:05 +00:00
|
|
|
(fromJust . Q.first $ pB)
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
-- Compare the first and the last element of the queue according
|
|
|
|
-- to their y-coordinate and shift the queue (if necessary) so that
|
|
|
|
-- the element with the highest value is at the front.
|
2014-10-26 03:22:05 +00:00
|
|
|
maybeShift :: BankersDequeue PolyPT -> BankersDequeue PolyPT
|
|
|
|
maybeShift q = if ptCmpY (fromMaybe posInfPT (id' <$> Q.first q))
|
|
|
|
(fromMaybe negInfPT (id' <$> Q.last q)) == GT
|
2014-10-24 23:44:13 +00:00
|
|
|
then q
|
|
|
|
else shiftQueueRight q
|
|
|
|
sortLexPolys _ = []
|
|
|
|
|
|
|
|
|
|
|
|
-- |Get all points that intersect between both polygons. This is done
|
|
|
|
-- in O(n).
|
2015-05-21 00:14:15 +00:00
|
|
|
intersectionPoints :: [PolyPT] -> [P2 Double]
|
2014-10-31 16:49:08 +00:00
|
|
|
intersectionPoints xs' = rmdups . go $ xs'
|
2014-10-24 23:44:13 +00:00
|
|
|
where
|
2014-10-31 16:49:08 +00:00
|
|
|
go [] = []
|
|
|
|
go xs = (++) (segIntersections . scanLine $ xs)
|
|
|
|
(go (tail xs))
|
|
|
|
|
2014-10-24 23:44:13 +00:00
|
|
|
-- Get the scan line or in other words the
|
|
|
|
-- Segment pairs we are going to check for intersection.
|
2015-05-21 00:14:15 +00:00
|
|
|
scanLine :: [PolyPT] -> ([(P2 Double, P2 Double)], [(P2 Double, P2 Double)])
|
2014-10-25 18:39:34 +00:00
|
|
|
scanLine sp@(_:_) = (,) (getSegment isPolyA) (getSegment isPolyB)
|
2014-10-25 18:23:34 +00:00
|
|
|
where
|
|
|
|
getSegment f = fromMaybe []
|
|
|
|
((\x -> [(id' x, suc x), (id' x, pre x)])
|
|
|
|
<$> (listToMaybe . filter f $ sp))
|
|
|
|
scanLine _ = ([], [])
|
2014-10-24 23:44:13 +00:00
|
|
|
|
|
|
|
-- Gets the actual intersections between the segments of
|
|
|
|
-- both polygons we currently examine. This is done in O(1)
|
|
|
|
-- since we have max 4 segments.
|
2015-05-21 00:14:15 +00:00
|
|
|
segIntersections :: ([(P2 Double, P2 Double)], [(P2 Double, P2 Double)]) -> [P2 Double]
|
2014-10-25 18:39:34 +00:00
|
|
|
segIntersections (a@(_:_), b@(_:_)) =
|
|
|
|
catMaybes
|
2015-11-25 20:49:48 +00:00
|
|
|
. fmap (\[(x1, y1), (x2, y2)] -> intersectSegSeg x1 y1 x2 y2)
|
2014-11-16 03:49:13 +00:00
|
|
|
$ combinations a b
|
2014-10-24 23:44:13 +00:00
|
|
|
segIntersections _ = []
|
|
|
|
|
|
|
|
-- Gets all unique(!) combinations of two arrays. Both arrays
|
|
|
|
-- are max 2, so this is actually O(1) for this algorithm.
|
|
|
|
combinations :: [a] -> [a] -> [[a]]
|
|
|
|
combinations xs ys = concat . fmap (\y -> fmap (\x -> [y, x]) xs) $ ys
|
|
|
|
|