2014-12-03 21:02:42 +00:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
|
|
|
module Graphics.Diagram.AlgoDiags where
|
|
|
|
|
2014-12-17 02:35:33 +00:00
|
|
|
import Algebra.Vector(PT,Square,dimToSquare)
|
2014-12-03 21:02:42 +00:00
|
|
|
import Algorithms.GrahamScan
|
|
|
|
import Algorithms.QuadTree
|
|
|
|
import Algorithms.KDTree
|
|
|
|
import Algorithms.PolygonIntersection
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Monoid
|
|
|
|
import Data.Tree
|
|
|
|
import Diagrams.Backend.Cairo
|
|
|
|
import Diagrams.Prelude hiding ((<>))
|
|
|
|
import Diagrams.TwoD.Layout.Tree
|
|
|
|
import Graphics.Diagram.Core
|
|
|
|
import Parser.PathParser
|
2014-12-07 17:55:49 +00:00
|
|
|
import Safe
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Draw the lines of the polygon.
|
|
|
|
polyLines :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
polyLines = Diag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f _ = foldl (\x y -> x <> strokePoly y) mempty
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
strokePoly x' = fromVertices $ x' ++ (maybeToList . headMay $ x')
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Show the intersection points of two polygons as red dots.
|
|
|
|
polyIntersection :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
polyIntersection = Diag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p [x, y] = drawP vtpi (dotSize p) # fc red # lc red
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
|
|
|
vtpi = intersectionPoints . sortLexPolys $ (sortLexPoly x, sortLexPoly y)
|
2014-12-07 03:41:45 +00:00
|
|
|
f _ _ = mempty
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Show the coordinate text of the intersection points of two polygons.
|
|
|
|
polyIntersectionText :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
polyIntersectionText = Diag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p [x, y]
|
2014-12-03 21:02:42 +00:00
|
|
|
| showCoordText p = position . zip vtpi $ (pointToTextCoord # fc red <$> vtpi)
|
|
|
|
# translate (r2 (0, 10))
|
|
|
|
| otherwise = mempty
|
|
|
|
where
|
|
|
|
vtpi = intersectionPoints
|
|
|
|
. sortLexPolys
|
|
|
|
$ (sortLexPoly x,
|
|
|
|
sortLexPoly y)
|
2014-12-07 03:41:45 +00:00
|
|
|
f _ _ = mempty
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create a diagram which shows the points of the convex hull.
|
|
|
|
convexHP :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
convexHP = Diag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts = drawP (grahamCH (concat vts)) (dotSize p) # fc red # lc red
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Show coordinates as text above the convex hull points.
|
|
|
|
convexHPText :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
convexHPText = Diag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts
|
2014-12-03 21:02:42 +00:00
|
|
|
| showCoordText p =
|
2014-12-07 17:55:49 +00:00
|
|
|
(position . zip vtch $ (pointToTextCoord <$> vtch))
|
|
|
|
# translate (r2 (0, 10))
|
2014-12-03 21:02:42 +00:00
|
|
|
| otherwise = mempty
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
vtch = grahamCH (concat vts)
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create a diagram which shows the lines along the convex hull
|
|
|
|
-- points.
|
|
|
|
convexHLs :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
convexHLs = Diag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f _ vts =
|
|
|
|
(fromVertices
|
|
|
|
. flip (++) (maybeToList . headMay . grahamCH $ vt)
|
|
|
|
. grahamCH
|
|
|
|
$ vt
|
|
|
|
) # lc red
|
|
|
|
where
|
|
|
|
vt = mconcat vts
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create list of diagrama which describe the lines along points of a half
|
|
|
|
-- convex hull, for each iteration of the algorithm. Which half is chosen
|
|
|
|
-- depends on the input.
|
|
|
|
convexHStepsLs :: Diag
|
2014-12-07 03:41:45 +00:00
|
|
|
convexHStepsLs = GifDiag f
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f _ col g vt = fmap (\x -> fromVertices x # lc col) (g vt)
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create a diagram that shows all squares of the RangeSearch algorithm
|
|
|
|
-- from the quad tree.
|
|
|
|
squares :: Diag
|
|
|
|
squares = Diag f
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts =
|
2014-12-03 21:02:42 +00:00
|
|
|
mconcat
|
|
|
|
$ (uncurry rectByDiagonal # lw ultraThin)
|
|
|
|
<$>
|
2014-12-17 02:35:33 +00:00
|
|
|
(quadTreeSquares (diagDimSquare p)
|
2014-12-07 17:55:49 +00:00
|
|
|
. quadTree (mconcat vts)
|
2014-12-17 02:35:33 +00:00
|
|
|
$ diagDimSquare p)
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Draw the squares of the kd-tree.
|
|
|
|
kdSquares :: Diag
|
|
|
|
kdSquares = Diag f
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts =
|
2014-12-03 21:02:42 +00:00
|
|
|
mconcat
|
|
|
|
. fmap (uncurry (~~))
|
2014-12-17 02:35:33 +00:00
|
|
|
$ kdLines (kdTree (mconcat vts) Horizontal)
|
|
|
|
(diagDimSquare p)
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
|
|
|
-- Gets all lines that make up the kdSquares. Every line is
|
|
|
|
-- described by two points, start and end respectively.
|
|
|
|
kdLines :: KDTree PT -> Square -> [(PT, PT)]
|
2014-12-17 02:35:33 +00:00
|
|
|
kdLines (KTNode ln pt Horizontal rn) ((xmin, ymin), (xmax, ymax)) =
|
2014-12-03 21:02:42 +00:00
|
|
|
(\(x, _) -> [(p2 (x, ymin), p2 (x, ymax))])
|
|
|
|
(unp2 pt)
|
2014-12-17 02:35:33 +00:00
|
|
|
++ kdLines ln ((xmin, ymin), (x', ymax))
|
|
|
|
++ kdLines rn ((x', ymin), (xmax, ymax))
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
|
|
|
(x', _) = unp2 pt
|
2014-12-17 02:35:33 +00:00
|
|
|
kdLines (KTNode ln pt Vertical rn) ((xmin, ymin), (xmax, ymax)) =
|
2014-12-03 21:02:42 +00:00
|
|
|
(\(_, y) -> [(p2 (xmin, y), p2 (xmax, y))])
|
|
|
|
(unp2 pt)
|
2014-12-17 02:35:33 +00:00
|
|
|
++ kdLines ln ((xmin, ymin), (xmax, y'))
|
|
|
|
++ kdLines rn ((xmin, y'), (xmax, ymax))
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
|
|
|
(_, y') = unp2 pt
|
|
|
|
kdLines _ _ = []
|
|
|
|
|
|
|
|
|
|
|
|
-- |Draw the range rectangle and highlight the points inside that range.
|
|
|
|
kdRange :: Diag
|
|
|
|
kdRange = Diag f
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts =
|
2014-12-03 21:02:42 +00:00
|
|
|
(uncurry rectByDiagonal # lc red) (rangeSquare p)
|
|
|
|
<> drawP ptsInRange (dotSize p) # fc red # lc red
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
ptsInRange = fst
|
|
|
|
. rangeSearch (kdTree (mconcat vts) Vertical)
|
|
|
|
$ rangeSquare p
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |The kd-tree visualized as binary tree.
|
|
|
|
kdTreeDiag :: Diag
|
|
|
|
kdTreeDiag = Diag f
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts =
|
2014-12-03 21:02:42 +00:00
|
|
|
-- HACK: in order to give specific nodes a specific color
|
|
|
|
renderTree (\n -> case n of
|
|
|
|
'*':'*':_ -> (text n # fontSizeL 5.0)
|
|
|
|
<> rect 50.0 20.0 # fc green
|
|
|
|
'*':_ -> (text n # fontSizeL 5.0)
|
|
|
|
<> rect 50.0 20.0 # fc red
|
|
|
|
_ -> (text n # fontSizeL 5.0)
|
|
|
|
<> rect 50.0 20.0 # fc white)
|
|
|
|
(~~)
|
|
|
|
(symmLayout' (with & slHSep .~ 60 & slVSep .~ 40) roseTree)
|
|
|
|
# scale 2 # alignT # bg white
|
|
|
|
where
|
|
|
|
roseTree = snd
|
2014-12-07 17:55:49 +00:00
|
|
|
. rangeSearch (kdTree (mconcat vts) Vertical)
|
2014-12-03 21:02:42 +00:00
|
|
|
$ rangeSquare p
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- |Get the quad tree corresponding to the given points and diagram properties.
|
|
|
|
qt :: [PT] -> DiagProp -> QuadTree PT
|
2014-12-17 02:35:33 +00:00
|
|
|
qt vt p = quadTree vt (diagDimSquare p)
|
2014-12-03 21:02:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create a diagram that shows a single square of the RangeSearch algorithm
|
|
|
|
-- from the quad tree in red, according to the given path in quadPath.
|
|
|
|
quadPathSquare :: Diag
|
|
|
|
quadPathSquare = Diag f
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts =
|
2014-12-03 21:02:42 +00:00
|
|
|
(uncurry rectByDiagonal # lw thin # lc red)
|
2014-12-07 17:55:49 +00:00
|
|
|
(getSquare (stringToQuads (quadPath p)) (qt (mconcat vts) p, []))
|
2014-12-03 21:02:42 +00:00
|
|
|
where
|
|
|
|
getSquare :: [Either Quad Orient] -> QTZipper PT -> Square
|
2014-12-17 02:35:33 +00:00
|
|
|
getSquare [] z = getSquareByZipper (diagDimSquare p) z
|
2014-12-03 21:02:42 +00:00
|
|
|
getSquare (q:qs) z = case q of
|
|
|
|
Right x -> getSquare qs (fromMaybe z (findNeighbor x z))
|
|
|
|
Left x -> getSquare qs (fromMaybe z (goQuad x z))
|
|
|
|
|
|
|
|
|
|
|
|
-- |Create a list of diagrams that show the walk along the given path
|
|
|
|
-- through the quad tree.
|
|
|
|
gifQuadPath :: Diag
|
|
|
|
gifQuadPath = GifDiag f
|
|
|
|
where
|
|
|
|
f p col _ vt =
|
|
|
|
(uncurry rectByDiagonal # lw thick # lc col)
|
|
|
|
<$> getSquares (stringToQuads (quadPath p)) (qt vt p, [])
|
|
|
|
where
|
|
|
|
getSquares :: [Either Quad Orient] -> QTZipper PT -> [Square]
|
2014-12-17 02:35:33 +00:00
|
|
|
getSquares [] z = [getSquareByZipper (diagDimSquare p) z]
|
2014-12-03 21:02:42 +00:00
|
|
|
getSquares (q:qs) z = case q of
|
2014-12-17 02:35:33 +00:00
|
|
|
Right x -> getSquareByZipper (diagDimSquare p) z :
|
2014-12-03 21:02:42 +00:00
|
|
|
getSquares qs (fromMaybe z (findNeighbor x z))
|
2014-12-17 02:35:33 +00:00
|
|
|
Left x -> getSquareByZipper (diagDimSquare p) z :
|
2014-12-03 21:02:42 +00:00
|
|
|
getSquares qs (fromMaybe z (goQuad x z))
|
|
|
|
|
|
|
|
|
|
|
|
-- |A diagram that shows the full Quad Tree with nodes.
|
|
|
|
treePretty :: Diag
|
|
|
|
treePretty = Diag f
|
|
|
|
where
|
2014-12-07 17:55:49 +00:00
|
|
|
f p vts =
|
2014-12-03 21:02:42 +00:00
|
|
|
prettyRoseTree (quadTreeToRoseTree
|
2014-12-07 17:55:49 +00:00
|
|
|
. flip getCurQT (qt (mconcat vts) p, [])
|
2014-12-03 21:02:42 +00:00
|
|
|
. stringToQuads
|
|
|
|
. quadPath
|
|
|
|
$ p)
|
|
|
|
where
|
|
|
|
getCurQT :: [Either Quad Orient] -> QTZipper PT -> QTZipper PT
|
|
|
|
getCurQT [] z = z
|
|
|
|
getCurQT (q:qs) z = case q of
|
|
|
|
Right x -> getCurQT qs (fromMaybe z (findNeighbor x z))
|
|
|
|
Left x -> getCurQT qs (fromMaybe z (goQuad x z))
|
|
|
|
prettyRoseTree :: Tree String -> Diagram Cairo R2
|
|
|
|
prettyRoseTree tree =
|
|
|
|
-- HACK: in order to give specific nodes a specific color
|
|
|
|
renderTree (\n -> case head n of
|
|
|
|
'*' -> (text n # fontSizeL 5.0)
|
|
|
|
<> rect 50.0 20.0 # fc red
|
|
|
|
_ -> (text n # fontSizeL 5.0)
|
|
|
|
<> rect 50.0 20.0 # fc white)
|
|
|
|
(~~)
|
|
|
|
(symmLayout' (with & slHSep .~ 60 & slVSep .~ 40) tree)
|
|
|
|
# scale 2 # alignT # bg white
|