-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added(core): Introducing the ["findPath"](https://heremaps.github.io/…
…xyz-maps/docs/classes/core.featureprovider.html#findpath) method for optimal client-side path finding on a GeoJSON road network supporting advanced options such as custom turn restrictions and weights. Signed-off-by: Tim Deubler <tim.deubler@here.com>
- Loading branch information
1 parent
ded5ccc
commit 73929ae
Showing
7 changed files
with
559 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright (C) 2019-2023 HERE Europe B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
|
||
import {BinaryHeap} from './BinaryHeap'; | ||
import {distance} from './geotools'; | ||
|
||
export type AStarNode = { point: number[], data?: any }; | ||
|
||
class Node implements AStarNode { | ||
point: number[]; | ||
data?: any; | ||
// "real" cost to reach the node (start->node) | ||
g: number; | ||
// approximate cost to reach the goal node (node->goal) | ||
h: number; | ||
// total cost (g+h) | ||
f: number; | ||
parent: Node | null; | ||
|
||
constructor(point: number[], g: number, h: number, parent: Node | null = null) { | ||
this.point = point; | ||
this.g = g; | ||
this.h = h; | ||
this.f = g + h; | ||
this.parent = parent; | ||
} | ||
} | ||
|
||
export class AStar { | ||
static precision = 1e5; | ||
static calculateDistance(point1: number[], point2: number[]): number { | ||
// const dx = point2[0] - point1[0]; | ||
// const dy = point2[1] - point1[1]; | ||
// return Math.sqrt(dx * dx + dy * dy); | ||
return distance(point1, point2); | ||
} | ||
|
||
private static weight(nodeA: AStarNode, nodeB: AStarNode): number { | ||
return AStar.calculateDistance(nodeA.point, nodeB.point); | ||
} | ||
|
||
static isPointEqual(point1: number[], point2: number[]): boolean { | ||
const precision = AStar.precision; | ||
return (point1[0] * precision ^ 0) === (point2[0] * precision ^ 0) && (point1[1] * precision ^ 0) === (point2[1] * precision ^ 0); | ||
// return point1[0] === point2[0] && point1[1] === point2[1]; | ||
} | ||
|
||
private static pointKey(node: AStarNode): number { | ||
const precision = AStar.precision; | ||
const point = node.point; | ||
return (point[0] * precision ^ 0) * 1e7 + (point[1] * precision ^ 0); | ||
} | ||
|
||
public static findPath( | ||
from: AStarNode, | ||
endNode: AStarNode, | ||
getNeighbors: (node: AStarNode) => AStarNode[], | ||
weight: (nodeA: AStarNode, nodeB: AStarNode) => number = AStar.weight | ||
): AStarNode[] | null { | ||
const start = from.point; | ||
const endCoordinate = endNode.point; | ||
const openList = new BinaryHeap<Node>((a, b) => a.f - b.f); | ||
const closedList = new Set<number>(); | ||
// const startNode = new NavNode(start, 0, AStar.calculateDistance(start, endCoordinate)); | ||
const startNode = new Node(start, 0, Infinity); | ||
startNode.data = from.data; | ||
|
||
openList.push(startNode); | ||
|
||
while (openList.size() > 0) { | ||
const currentNode = openList.pop()!; | ||
|
||
if (AStar.isPointEqual(currentNode.point, endCoordinate)) { | ||
// reconstruct the path | ||
const path: AStarNode[] = []; | ||
let current: Node | null = currentNode; | ||
while (current !== null) { | ||
path.unshift(current); | ||
current = current.parent; | ||
} | ||
return path; | ||
} | ||
|
||
const pointKey = AStar.pointKey(currentNode); | ||
closedList.add(pointKey); | ||
for (const neighborNode of getNeighbors(currentNode)) { | ||
const {point: neighbor, data} = neighborNode; | ||
const neighborKey = AStar.pointKey(neighborNode); | ||
if (closedList.has(neighborKey)) { | ||
continue; | ||
} | ||
const g = currentNode.g + weight(currentNode, neighborNode); | ||
const h = weight(neighborNode, endNode); | ||
// const h = AStar.calculateDistance(neighborNode.point, endNode.point); | ||
const existingNode = openList.find((node) => AStar.isPointEqual(node.point, neighbor)); | ||
if (existingNode) { | ||
if (g < existingNode.g) { | ||
existingNode.g = g; | ||
existingNode.h = h; | ||
existingNode.f = g + h; | ||
existingNode.parent = currentNode; | ||
existingNode.data = data; | ||
} | ||
} else { | ||
const newNode = new Node(neighbor, g, h, currentNode); | ||
newNode.data = data; | ||
openList.push(newNode); | ||
} | ||
} | ||
} | ||
// no path found | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (C) 2019-2023 HERE Europe B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* License-Filename: LICENSE | ||
*/ | ||
export class BinaryHeap<T> { | ||
private heap: T[]; | ||
private compare: (a: T, b: T) => number; | ||
|
||
constructor(compare: (a: T, b: T) => number) { | ||
this.heap = []; | ||
this.compare = compare; | ||
} | ||
|
||
find(predicate: (this: void, value: T, index: number, obj: T[]) => boolean) { | ||
return this.heap.find(predicate); | ||
} | ||
|
||
includes(value: T) { | ||
return this.heap.includes(value); | ||
} | ||
|
||
push(value: T): void { | ||
this.heap.push(value); | ||
this.bubbleUp(this.heap.length - 1); | ||
} | ||
|
||
pop(): T | undefined { | ||
const {heap} = this; | ||
|
||
if (!heap.length) return; | ||
|
||
const result = heap[0]; | ||
const end = heap.pop()!; | ||
if (heap.length) { | ||
heap[0] = end; | ||
this.sinkDown(0); | ||
} | ||
return result; | ||
} | ||
|
||
size(): number { | ||
return this.heap.length; | ||
} | ||
|
||
private bubbleUp(index: number): void { | ||
const element = this.heap[index]; | ||
while (index > 0) { | ||
const parentIndex = Math.floor((index - 1) / 2); | ||
const parent = this.heap[parentIndex]; | ||
if (this.compare(element, parent) >= 0) break; | ||
this.heap[parentIndex] = element; | ||
this.heap[index] = parent; | ||
index = parentIndex; | ||
} | ||
} | ||
|
||
private sinkDown(index: number): void { | ||
const length = this.heap.length; | ||
const element = this.heap[index]; | ||
while (true) { | ||
let leftChildIndex = 2 * index + 1; | ||
let rightChildIndex = 2 * index + 2; | ||
let swap = null; | ||
let leftChild; | ||
let rightChild; | ||
|
||
if (leftChildIndex < length) { | ||
leftChild = this.heap[leftChildIndex]; | ||
if (this.compare(leftChild, element) < 0) { | ||
swap = leftChildIndex; | ||
} | ||
} | ||
if (rightChildIndex < length) { | ||
rightChild = this.heap[rightChildIndex]; | ||
if ((swap === null && this.compare(rightChild, element) < 0) || | ||
(swap !== null && this.compare(rightChild, leftChild!) < 0)) { | ||
swap = rightChildIndex; | ||
} | ||
} | ||
if (swap === null) break; | ||
this.heap[index] = this.heap[swap]; | ||
this.heap[swap] = element; | ||
index = swap; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.