2015-01-07 17:55:16 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
|
|
|
module Algorithms.PolygonTriangulation where
|
|
|
|
|
2015-01-08 00:44:47 +00:00
|
|
|
import Algebra.Polygon
|
2015-01-07 17:55:16 +00:00
|
|
|
import Algebra.Vector
|
2015-01-09 02:24:09 +00:00
|
|
|
import qualified Control.Arrow as A
|
2015-01-08 00:44:47 +00:00
|
|
|
import Data.Maybe
|
2015-01-14 17:17:35 +00:00
|
|
|
import Diagrams.TwoD.Types
|
2015-01-09 02:24:09 +00:00
|
|
|
import Safe
|
2015-01-07 17:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
data VCategory = VStart
|
|
|
|
| VEnd
|
|
|
|
| VRegular
|
|
|
|
| VSplit
|
|
|
|
| VMerge
|
2015-01-07 18:16:12 +00:00
|
|
|
deriving (Show, Eq)
|
2015-01-07 17:55:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Classify all vertices on a polygon into five categories (see VCategory).
|
2015-05-21 00:14:15 +00:00
|
|
|
classifyList :: [P2 Double] -> [(P2 Double, VCategory)]
|
2015-01-07 17:55:16 +00:00
|
|
|
classifyList p@(x:y:_:_) =
|
|
|
|
-- need to handle the first and last element separately
|
|
|
|
[classify (last p) x y] ++ go p ++ [classify (last . init $ p) (last p) x]
|
|
|
|
where
|
2015-05-21 00:14:15 +00:00
|
|
|
go :: [P2 Double] -> [(P2 Double, VCategory)]
|
2015-01-07 17:55:16 +00:00
|
|
|
go (x':y':z':xs) = classify x' y' z' : go (y':z':xs)
|
|
|
|
go _ = []
|
|
|
|
classifyList _ = []
|
|
|
|
|
|
|
|
|
|
|
|
-- |Classify a vertex on a polygon given it's next and previous vertex
|
|
|
|
-- into five categories (see VCategory).
|
2015-05-21 00:14:15 +00:00
|
|
|
classify :: P2 Double -- ^ prev vertex
|
|
|
|
-> P2 Double -- ^ classify this one
|
|
|
|
-> P2 Double -- ^ next vertex
|
|
|
|
-> (P2 Double, VCategory)
|
2015-01-07 17:55:16 +00:00
|
|
|
classify prev v next
|
|
|
|
| isVStart prev v next = (v, VStart)
|
|
|
|
| isVSplit prev v next = (v, VSplit)
|
|
|
|
| isVEnd prev v next = (v, VEnd)
|
|
|
|
| isVMerge prev v next = (v, VMerge)
|
|
|
|
| otherwise = (v, VRegular)
|
|
|
|
|
|
|
|
|
2015-01-07 18:15:51 +00:00
|
|
|
-- |Whether the vertex, given it's next and previous vertex,
|
2015-01-07 17:55:16 +00:00
|
|
|
-- is a start vertex.
|
2015-05-21 00:14:15 +00:00
|
|
|
isVStart :: P2 Double -- ^ previous vertex
|
|
|
|
-> P2 Double -- ^ vertice to check
|
|
|
|
-> P2 Double -- ^ next vertex
|
2015-01-07 17:55:16 +00:00
|
|
|
-> Bool
|
|
|
|
isVStart prev v next =
|
2015-01-09 02:24:44 +00:00
|
|
|
ptCmpY next v == LT && ptCmpY prev v == LT && cw next v prev
|
2015-01-07 17:55:16 +00:00
|
|
|
|
|
|
|
|
2015-01-07 18:15:51 +00:00
|
|
|
-- |Whether the vertex, given it's next and previous vertex,
|
2015-01-07 17:55:16 +00:00
|
|
|
-- is a split vertex.
|
2015-05-21 00:14:15 +00:00
|
|
|
isVSplit :: P2 Double -- ^ previous vertex
|
|
|
|
-> P2 Double -- ^ vertice to check
|
|
|
|
-> P2 Double -- ^ next vertex
|
2015-01-07 17:55:16 +00:00
|
|
|
-> Bool
|
|
|
|
isVSplit prev v next =
|
2015-01-09 02:24:44 +00:00
|
|
|
ptCmpY prev v == LT && ptCmpY next v == LT && cw prev v next
|
2015-01-07 17:55:16 +00:00
|
|
|
|
|
|
|
|
2015-01-07 18:15:51 +00:00
|
|
|
-- |Whether the vertex, given it's next and previous vertex,
|
2015-01-07 17:55:16 +00:00
|
|
|
-- is an end vertex.
|
2015-05-21 00:14:15 +00:00
|
|
|
isVEnd :: P2 Double -- ^ previous vertex
|
|
|
|
-> P2 Double -- ^ vertice to check
|
|
|
|
-> P2 Double -- ^ next vertex
|
2015-01-07 17:55:16 +00:00
|
|
|
-> Bool
|
|
|
|
isVEnd prev v next =
|
2015-01-09 02:24:44 +00:00
|
|
|
ptCmpY prev v == GT && ptCmpY next v == GT && cw next v prev
|
2015-01-07 17:55:16 +00:00
|
|
|
|
|
|
|
|
2015-01-07 18:15:51 +00:00
|
|
|
-- |Whether the vertex, given it's next and previous vertex,
|
2015-01-07 17:55:16 +00:00
|
|
|
-- is a merge vertex.
|
2015-05-21 00:14:15 +00:00
|
|
|
isVMerge :: P2 Double -- ^ previous vertex
|
|
|
|
-> P2 Double -- ^ vertice to check
|
|
|
|
-> P2 Double -- ^ next vertex
|
2015-01-07 17:55:16 +00:00
|
|
|
-> Bool
|
|
|
|
isVMerge prev v next =
|
2015-01-09 02:24:44 +00:00
|
|
|
ptCmpY next v == GT && ptCmpY prev v == GT && cw prev v next
|
2015-01-07 17:55:16 +00:00
|
|
|
|
|
|
|
|
2015-01-07 18:15:51 +00:00
|
|
|
-- |Whether the vertex, given it's next and previous vertex,
|
2015-01-07 17:55:16 +00:00
|
|
|
-- is a regular vertex.
|
2015-05-21 00:14:15 +00:00
|
|
|
isVRegular :: P2 Double -- ^ previous vertex
|
|
|
|
-> P2 Double -- ^ vertice to check
|
|
|
|
-> P2 Double -- ^ next vertex
|
2015-01-07 17:55:16 +00:00
|
|
|
-> Bool
|
|
|
|
isVRegular prev v next =
|
|
|
|
(not . isVStart prev v $ next)
|
|
|
|
&& (not . isVSplit prev v $ next)
|
|
|
|
&& (not . isVEnd prev v $ next)
|
|
|
|
&& (not . isVMerge prev v $ next)
|
|
|
|
|
|
|
|
|
2015-01-07 18:16:12 +00:00
|
|
|
|
2015-01-09 02:58:24 +00:00
|
|
|
-- |A polygon P is y-monotone, if it has no split and merge vertices.
|
2015-05-21 00:14:15 +00:00
|
|
|
isYmonotone :: [P2 Double] -> Bool
|
2015-01-07 18:16:12 +00:00
|
|
|
isYmonotone poly =
|
|
|
|
not
|
|
|
|
. any (\x -> x == VSplit || x == VMerge)
|
|
|
|
. fmap snd
|
|
|
|
$ classifyList poly
|
2015-01-08 00:44:47 +00:00
|
|
|
|
|
|
|
|
2015-01-12 21:44:23 +00:00
|
|
|
-- |Partition P into y-monotone pieces.
|
2015-05-21 00:14:15 +00:00
|
|
|
monotonePartitioning :: [P2 Double] -> [[P2 Double]]
|
2015-01-09 02:58:05 +00:00
|
|
|
monotonePartitioning pts
|
|
|
|
| isYmonotone pts = [pts]
|
2015-01-12 21:27:17 +00:00
|
|
|
| otherwise = go (monotoneDiagonals pts) pts
|
2015-01-08 00:44:47 +00:00
|
|
|
where
|
2015-05-21 00:14:15 +00:00
|
|
|
go :: [(P2 Double, P2 Double)] -> [P2 Double] -> [[P2 Double]]
|
2015-01-12 21:40:39 +00:00
|
|
|
go (x:xs) pts'@(_:_)
|
2015-01-12 21:27:17 +00:00
|
|
|
| isYmonotone a && isYmonotone b = [a, b]
|
|
|
|
| isYmonotone b = b : go xs a
|
|
|
|
| otherwise = a : go xs b
|
|
|
|
where
|
|
|
|
[a, b] = splitPoly pts' x
|
2015-01-13 00:05:02 +00:00
|
|
|
go _ _ = []
|
2015-01-08 00:44:47 +00:00
|
|
|
|
|
|
|
|
2015-01-09 02:58:24 +00:00
|
|
|
-- |Try to eliminate the merge and split vertices by computing the
|
2015-01-12 21:44:04 +00:00
|
|
|
-- diagonals we have to use for splitting the polygon.
|
2015-05-21 00:14:15 +00:00
|
|
|
monotoneDiagonals :: [P2 Double] -> [(P2 Double, P2 Double)]
|
2015-01-08 00:44:47 +00:00
|
|
|
monotoneDiagonals pts = catMaybes . go $ classifyList pts
|
|
|
|
where
|
2015-05-21 00:14:15 +00:00
|
|
|
go :: [(P2 Double, VCategory)] -> [Maybe (P2 Double, P2 Double)]
|
2015-01-08 00:44:47 +00:00
|
|
|
go (x:xs) = case snd x of
|
2015-01-12 21:27:43 +00:00
|
|
|
VMerge -> getSeg (belowS . fst $ x) (fst x) : go xs
|
|
|
|
VSplit -> getSeg (aboveS . fst $ x) (fst x) : go xs
|
2015-01-08 00:44:47 +00:00
|
|
|
_ -> [] ++ go xs
|
|
|
|
go [] = []
|
2015-05-21 00:14:15 +00:00
|
|
|
getSeg :: [P2 Double] -- all points above/below the current point
|
|
|
|
-> P2 Double -- current point
|
|
|
|
-> Maybe (P2 Double, P2 Double)
|
2015-01-12 21:27:43 +00:00
|
|
|
getSeg [] _ = Nothing
|
|
|
|
getSeg (z:zs) pt
|
2015-01-09 02:20:13 +00:00
|
|
|
| isInsidePoly pts (z, pt) = Just (z, pt)
|
2015-01-12 21:27:43 +00:00
|
|
|
| otherwise = getSeg zs pt
|
2015-05-21 00:14:15 +00:00
|
|
|
aboveS :: P2 Double -> [P2 Double]
|
2015-01-12 21:27:43 +00:00
|
|
|
aboveS pt = tail . dropWhile (/= pt) $ sortedYX pts
|
2015-05-21 00:14:15 +00:00
|
|
|
belowS :: P2 Double -> [P2 Double]
|
2015-01-12 21:27:43 +00:00
|
|
|
belowS pt = reverse . takeWhile (/= pt) $ sortedYX pts
|
2015-01-08 00:44:47 +00:00
|
|
|
|
2015-01-09 02:24:09 +00:00
|
|
|
|
2015-01-09 03:08:19 +00:00
|
|
|
-- |Triangulate a y-monotone polygon.
|
2015-05-21 00:14:15 +00:00
|
|
|
triangulate :: [P2 Double] -> [[P2 Double]]
|
2015-01-09 02:58:05 +00:00
|
|
|
triangulate pts =
|
2015-01-09 02:24:09 +00:00
|
|
|
go pts . A.first reverse . splitAt 3 . reverse . sortedYX $ pts
|
|
|
|
where
|
2015-05-21 00:14:15 +00:00
|
|
|
go :: [P2 Double] -- current polygon
|
|
|
|
-> ([P2 Double], [P2 Double]) -- (stack of visited vertices, rest)
|
2015-01-12 21:37:10 +00:00
|
|
|
-- sorted by Y-coordinate
|
2015-05-21 00:14:15 +00:00
|
|
|
-> [[P2 Double]]
|
2015-01-09 02:24:09 +00:00
|
|
|
go xs (p@[_, _], r:rs) = go xs (r:p, rs)
|
|
|
|
go xs (p@(u:vi:vi1:ys), rs)
|
|
|
|
-- case 1 and 3
|
|
|
|
| adjacent u (last p) xs =
|
2015-01-13 00:05:02 +00:00
|
|
|
(triangleOnly . splitPoly xs $ (u, (last . init) p))
|
2015-01-09 02:24:09 +00:00
|
|
|
++ go (fromMaybe []
|
|
|
|
. headMay
|
|
|
|
. nonTriangleOnly
|
|
|
|
. splitPoly xs
|
|
|
|
$ (u, (last . init) p))
|
|
|
|
(init p, rs)
|
|
|
|
-- case 2
|
|
|
|
| adjacent u vi xs && (not . null) rs =
|
|
|
|
if getAngle (vp2 vi u) (vp2 vi vi1) < pi / 2
|
2015-01-13 00:05:02 +00:00
|
|
|
then (triangleOnly . splitPoly xs $ (u, vi1))
|
2015-01-09 02:24:09 +00:00
|
|
|
++ go (fromMaybe []
|
|
|
|
. headMay
|
|
|
|
. nonTriangleOnly
|
|
|
|
. splitPoly xs
|
|
|
|
$ (u, vi1))
|
|
|
|
(u:vi1:ys, rs)
|
|
|
|
else go xs (head rs:p, tail rs)
|
2015-01-13 00:05:02 +00:00
|
|
|
| otherwise = []
|
|
|
|
go _ _ = []
|