|
| 1 | +from copy import deepcopy |
| 2 | +from typing import Dict, Set, List, Tuple |
| 3 | +from queue import PriorityQueue |
| 4 | +from Blobtory.Scripts.planet_former.Nodes cimport NodeRef, NodeKey |
| 5 | +cdef extern class Node: |
| 6 | + cdef extern float* point |
| 7 | + cdef extern char* hashKey |
| 8 | + |
| 9 | +cdef extern class NodeRef(Node): |
| 10 | + cdef extern float relativeDist |
| 11 | + |
| 12 | +cdef extern class NodeKey(Node): |
| 13 | + cdef extern float weight |
| 14 | + |
| 15 | +from libcpp.map cimport map |
| 16 | +from libcpp.string cimport string |
| 17 | +from libcpp.vector cimport vector |
| 18 | + |
| 19 | +def GetDist(pFrom: Tuple[float, float, float, float], |
| 20 | + pTo: Tuple[float, float, float, float]): |
| 21 | + return (abs(pFrom[0] - pTo[0]) + |
| 22 | + abs(pFrom[1] - pTo[1]) + |
| 23 | + abs(pFrom[2] - pTo[2])) |
| 24 | + |
| 25 | + |
| 26 | +def ReconstructPath(cameFrom: Dict[NodeRef, NodeKey], current: NodeKey): |
| 27 | + cdef list totalPath = [current.point] |
| 28 | + while current in cameFrom.keys(): |
| 29 | + current = cameFrom[current] |
| 30 | + totalPath.insert(0, current.point) |
| 31 | + |
| 32 | + return totalPath |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +class AStar: |
| 37 | + def __init__(self, nodeDict: Dict): |
| 38 | + self.nodeDict = nodeDict |
| 39 | + self.keyList = list(self.nodeDict.keys()) |
| 40 | + |
| 41 | + def GetKeyNodeFromRef(self, p: NodeRef) -> NodeKey: |
| 42 | + return self.keyList[self.keyList.index(p.point)] |
| 43 | + |
| 44 | + def GetKeyNodeFromPoint(self, p: Tuple[float, float, float, float]) -> NodeKey: |
| 45 | + return self.keyList[self.keyList.index(p)] |
| 46 | + |
| 47 | + def GetH(self, p: NodeKey) -> float: |
| 48 | + return 1/p.weight |
| 49 | + |
| 50 | + def GetPathFromTo(self, |
| 51 | + pFrom: Tuple[float, float, float, float], |
| 52 | + pTo: Tuple[float, float, float, float] |
| 53 | + ) -> List[Tuple[float, float, float, float]]: |
| 54 | + startNode = self.GetKeyNodeFromPoint(pFrom) |
| 55 | + openSet = PriorityQueue() |
| 56 | + openSet.put((0, startNode)) |
| 57 | + cdef map[NodeRef, NodeKey] cameFrom = {} |
| 58 | + |
| 59 | + gScore = {nodeKey: float("inf") for nodeKey in self.nodeDict.keys()} |
| 60 | + gScore[pFrom] = 0 |
| 61 | + |
| 62 | + fScore = deepcopy(gScore) |
| 63 | + fScore[pFrom] = self.GetH(startNode) |
| 64 | + |
| 65 | + openSetTable = {startNode} |
| 66 | + |
| 67 | + while not openSet.empty(): |
| 68 | + setVal = openSet.get() |
| 69 | + current = setVal[1] |
| 70 | + openSetTable.remove(current) |
| 71 | + |
| 72 | + if current == pTo: |
| 73 | + return ReconstructPath(cameFrom, current) |
| 74 | + |
| 75 | + neighbourPoints = self.nodeDict[current] |
| 76 | + for neighbourPoint in neighbourPoints: |
| 77 | + tentativeGScore = gScore[current] + GetDist(current.point, neighbourPoint.point) |
| 78 | + |
| 79 | + if tentativeGScore < gScore[neighbourPoint]: |
| 80 | + cameFrom[neighbourPoint] = current |
| 81 | + gScore[neighbourPoint] = tentativeGScore |
| 82 | + fScore[neighbourPoint] = gScore[neighbourPoint] + self.GetH(self.GetKeyNodeFromRef(neighbourPoint)) |
| 83 | + if neighbourPoint not in openSetTable: |
| 84 | + openSet.put((fScore[neighbourPoint], neighbourPoint)) |
| 85 | + openSetTable.add(neighbourPoint) |
| 86 | + |
| 87 | + return [] |
0 commit comments