|
|
@ -26,7 +26,7 @@ public class AStarPathPlanner { |
|
|
PriorityQueue<AStarNodeState> openSet = new PriorityQueue<>(); |
|
|
PriorityQueue<AStarNodeState> openSet = new PriorityQueue<>(); |
|
|
|
|
|
|
|
|
// 状态管理
|
|
|
// 状态管理
|
|
|
Map<String, Double> gScoreMap = new HashMap<>(); |
|
|
Map<String, Float> gScoreMap = new HashMap<>(); |
|
|
Map<String, AStarNodeState> cameFrom = new HashMap<>(); |
|
|
Map<String, AStarNodeState> cameFrom = new HashMap<>(); |
|
|
|
|
|
|
|
|
// 初始化起点
|
|
|
// 初始化起点
|
|
|
@ -39,7 +39,7 @@ public class AStarPathPlanner { |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
openSet.add(start); |
|
|
openSet.add(start); |
|
|
gScoreMap.put(start.stateKey(), 0.0); |
|
|
gScoreMap.put(start.stateKey(), 0.0f); |
|
|
|
|
|
|
|
|
while (!openSet.isEmpty()) { |
|
|
while (!openSet.isEmpty()) { |
|
|
AStarNodeState current = openSet.poll(); |
|
|
AStarNodeState current = openSet.poll(); |
|
|
@ -55,12 +55,12 @@ public class AStarPathPlanner { |
|
|
PathSegment segment = graph.getPathSegment(current.nodeId(), neighbor.id()); |
|
|
PathSegment segment = graph.getPathSegment(current.nodeId(), neighbor.id()); |
|
|
if (segment == null) continue; |
|
|
if (segment == null) continue; |
|
|
|
|
|
|
|
|
double moveCost = segment.distance(); |
|
|
float moveCost = segment.distance(); |
|
|
double tentativeGCost = current.gCost() + moveCost; |
|
|
float tentativeGCost = current.gCost() + moveCost; |
|
|
String neighborKey = neighbor.id() + ":" + current.direction(); |
|
|
String neighborKey = neighbor.id() + ":" + current.direction(); |
|
|
|
|
|
|
|
|
// 发现更好路径
|
|
|
// 发现更好路径
|
|
|
if (tentativeGCost < gScoreMap.getOrDefault(neighborKey, Double.MAX_VALUE)) { |
|
|
if (tentativeGCost < gScoreMap.getOrDefault(neighborKey, Float.MAX_VALUE)) { |
|
|
AStarNodeState neighborState = new AStarNodeState( |
|
|
AStarNodeState neighborState = new AStarNodeState( |
|
|
neighbor.id(), |
|
|
neighbor.id(), |
|
|
current.direction(), |
|
|
current.direction(), |
|
|
@ -80,12 +80,12 @@ public class AStarPathPlanner { |
|
|
if (currentNode != null && currentNode.rotatable()) { |
|
|
if (currentNode != null && currentNode.rotatable()) { |
|
|
for (int rotation : new int[]{90, -90}) { |
|
|
for (int rotation : new int[]{90, -90}) { |
|
|
int newDirection = (current.direction() + rotation + 360) % 360; |
|
|
int newDirection = (current.direction() + rotation + 360) % 360; |
|
|
double rotateCost = 1.0; // 旋转代价
|
|
|
float rotateCost = 1.0f; // 旋转代价
|
|
|
double tentativeGCost = current.gCost() + rotateCost; |
|
|
float tentativeGCost = current.gCost() + rotateCost; |
|
|
String rotatedKey = current.nodeId() + ":" + newDirection; |
|
|
String rotatedKey = current.nodeId() + ":" + newDirection; |
|
|
|
|
|
|
|
|
// 发现更好路径
|
|
|
// 发现更好路径
|
|
|
if (tentativeGCost < gScoreMap.getOrDefault(rotatedKey, Double.MAX_VALUE)) { |
|
|
if (tentativeGCost < gScoreMap.getOrDefault(rotatedKey, Float.MAX_VALUE)) { |
|
|
AStarNodeState rotatedState = new AStarNodeState( |
|
|
AStarNodeState rotatedState = new AStarNodeState( |
|
|
current.nodeId(), |
|
|
current.nodeId(), |
|
|
newDirection, |
|
|
newDirection, |
|
|
|