Files
loop/src/Dodge/Path.hs
T

178 lines
6.9 KiB
Haskell

--{-# LANGUAGE TupleSections #-}
module Dodge.Path
( pointTowardsImpulse
, makePathBetween
, makePathBetweenPs
, pairsToGraph
, pairsToGraph''
, walkableNodeNear
, nodesNearL
, getNodePos
, addObstacleCrossing'
) where
import Dodge.Data
import Dodge.Base.Collide
import Dodge.Zone
import Geometry.Data
import Geometry
import Control.Lens
import Data.Maybe
import Data.List
--import qualified Data.IntMap.Strict as IM
import Data.Graph.Inductive hiding ((&))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Map (Map)
import qualified Data.Map.Strict as M
import qualified Streaming.Prelude as S
import StreamingHelp
--import Data.Graph.Inductive.PatriciaTree
--import Data.Graph.Inductive.Graph hiding ((&))
getNodePos :: Int -> World -> Maybe Point2
getNodePos i w = _pgGraph (_pathGraph w) `lab` i
makePathBetween :: Point2 -> Point2 -> World -> Maybe [Int]
makePathBetween a b w = do -- join $ sp <$> a' <*> b' <*> return (_pathGraph w)
(na,_) <- walkableNodeNear a w
(nb,_) <- walkableNodeNear b w
sp na nb (second pathDist $ efilter noobstacle $ _pgGraph $ _pathGraph w)
where
noobstacle (_,_,PathEdge _ _ s) = null s
pathDist :: PathEdge -> Float
pathDist pe = dist (_peStart pe) (_peEnd pe)
walkableNodeNear :: Point2 -> World -> Maybe (Int,Point2)
{-# INLINE walkableNodeNear #-}
walkableNodeNear p w = minStreamOn (dist p . snd)
. S.filter (flip (isWalkable p) w . snd) $ nodesNear p w
nodesNear :: Point2 -> World -> StreamOf (Int,Point2)
{-# INLINE nodesNear #-}
nodesNear = aroundPoint _pnZoning
nodesNearL :: Point2 -> World -> [(Int,Point2)]
nodesNearL p = runIdentity . S.toList_ . nodesNear p
makePathBetweenPs :: Point2 -> Point2 -> World -> Maybe [Point2]
makePathBetweenPs a b w = mapMaybe (lab $ _pgGraph $ _pathGraph w) <$> makePathBetween a b w
pointTowardsImpulse :: Point2 -> Point2 -> World -> Maybe Point2
pointTowardsImpulse a b w = find (flip (isWalkable a) w) =<< makePathBetweenPs a b w
------ continues a walk from a list of points, without repetitions
------ supposes that the list is non-empty
--randomGraphWalk :: RandomGen g => [Int] -> Gr a b -> State g [Int]
--randomGraphWalk (n:ns) g = do
-- next' <- randomGraphStepRestricted n ns g
-- case next' of
-- Nothing -> return (n:ns)
-- Just n' -> randomGraphWalk (n':n:ns) g
--randomGraphWalk _ _ = error "Trying to walk in an empty list"
--
--randomPointXStepsFrom :: Int -> Point2 -> World -> Point2
--randomPointXStepsFrom i p w =
-- let g = _pathGraph w
-- ns = labNodes g
-- mp = listToMaybe $ sortBy (compare `on` dist p . snd) $ filter (flip (isWalkable p) w . snd) ns
-- in case mp of
-- Nothing -> p
-- Just (n,_) -> fromJust
-- $ lab g (last $ take i $ randomGraphWalk [n] g Data.Function.& evalState $ _randGen w)
--
--randomPointsXStepsFrom :: Int -> Point2 -> World -> [Point2]
--randomPointsXStepsFrom i p w =
-- let g = _pathGraph w
-- ns = labNodes g
-- mp = listToMaybe $ sortBy (compare `on` dist p . snd) $ filter (flip (isWalkable p) w . snd) ns
-- in case mp of
-- Nothing -> [p]
-- Just (n,_) -> mapMaybe (lab g) (take i $ randomGraphWalk [n] g Data.Function.& evalState $ _randGen w)
--
--randomGraphStep :: RandomGen g => Int -> Gr a b -> State g (Maybe Int)
--randomGraphStep n g =
-- do let ns = neighbors g n
-- i <- state $ randomR (0,length ns - 1)
-- case ns of [] -> return Nothing
-- _ -> return $ Just $ ns !! i
--randomGraphStepRestricted :: RandomGen g => Int -> [Int] -> Gr a b -> State g (Maybe Int)
--randomGraphStepRestricted n notns g = do
-- let ns = neighbors g n \\ notns
-- i <- state $ randomR (0,length ns - 1)
-- case ns of
-- [] -> return Nothing
-- _ -> return $ Just $ ns !! i
--
pairsToGraph'' :: Set.Set (Point2,Point2) -> PathGraph
pairsToGraph'' pairset = PathGraph gr' nodemap ncount edgemap
where
(gr,nodemap,ncount) = insertNodes pairset
(gr',edgemap) = insertEdges toPathEdge pairset gr nodemap
toPathEdge :: Point2 -> Point2 -> PathEdge
toPathEdge sp' ep = PathEdge sp' ep mempty
insertEdges :: (Point2 -> Point2 -> b)
-> Set (Point2,Point2) -> Gr Point2 b -> Map Point2 Int -> (Gr Point2 b,Map (V2 Point2) Int2)
insertEdges efunc pairset gr nm = runIdentity $ S.fold_ insertedge (gr,mempty) id $ S.each pairset
where
insertedge (gr',em) (a,b) = (insEdge (f a,f b,efunc a b) gr'
, M.insert (V2 a b) (V2 (f a) (f b)) em)
f a = nm M.! a
insertNodes :: Set (Point2,Point2) -> (Gr Point2 b, Map Point2 Int, Int)
insertNodes pairset = runIdentity $ S.fold_ insertnode (Data.Graph.Inductive.empty,mempty,0) id nodestream
where
nodestream :: StreamOf Point2
nodestream = S.each . runIdentity $ S.fold_ getnode Set.empty id $ S.each pairset
getnode strm (x,y) = Set.insert x $ Set.insert y strm
insertnode :: (Gr Point2 b,Map Point2 Int,Int) -> Point2 -> (Gr Point2 b,Map Point2 Int,Int)
insertnode (gr,nm,i) n = case M.lookup n nm of
Nothing -> (insNode (j,n) gr, M.insert n j nm, j)
Just _ -> (gr,nm,i)
where
j = i + 1
--pairsToGraph' :: (Ord a) => (a -> a -> b) -> Set (a,a) -> (Gr a b, Map a Int, Map (V2 a) Int2)
--pairsToGraph' f pairset = ngr'
-- where
-- nodeset = S.each . runIdentity $ S.fold_ getnode Set.empty id $ S.each pairset
-- getnode nodeset' (x,y) = Set.insert x $ Set.insert y nodeset'
-- ngr = runIdentity $ S.fold_ insertnode (Data.Graph.Inductive.empty,new) id nodeset
-- ngr' = runIdentity $ S.fold_ insertedge ngr id $ S.each pairset
-- insertedge (gr,nm) (x,y) = (insMapEdge nm (x,y,f x y) gr, nm)
-- insertnode (gr,nm) n = fstsnd $ insMapNode nm n gr
-- fstsnd (a,b,_) = (a,b)
pairsToGraph :: (Ord a, Ord b) => (a -> a -> b) -> Set.Set (a,a) -> Gr a b
pairsToGraph f pairs = undir
$ run_ Data.Graph.Inductive.empty
$ insMapNodesM (Set.toList nodes') >> insMapEdgesM (Set.toList pairs')
where
nodes' = Set.map fst pairs `Set.union` Set.map snd pairs
pairs' = Set.map (\(x,y)->(x,y,f x y)) pairs
--findEdgesCrossing :: Point2 -> Point2 -> Gr Point2 PathEdge ->
--
addObstacleCrossing :: World -> EdgeObstacle -> Point2 -> Point2
-> Gr Point2 PathEdge
-> Gr Point2 PathEdge
addObstacleCrossing w eo a b gr = runIdentity $ S.fold_ updateedge gr id paths
where
updateedge gr' (xi,yi,PathEdge x y obs) = insEdge (xi,yi,PathEdge x y (Set.insert eo obs))
$ delEdge (xi,yi) gr'
paths = S.filter f $ nearSeg _peZoning a b w
f (_,_,PathEdge x y _) = isJust $ intersectSegSeg x y a b
addObstacleCrossing' :: EdgeObstacle -> Point2 -> Point2
-> World
-> World
addObstacleCrossing' eo a b w = w & pathGraph . pgGraph %~ addObstacleCrossing w eo a b
-- pg' = Set.filter (isNothing . uncurry (intersectSegSeg a b)) $ _pathGraphP w
-- -- insertPoint pp@(_,p) = insertInZoneWith (wlZoneOfPoint p) (++) [pp]
-- newGraph = pairsToGraph'' dist pg'