Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTate committed Sep 9, 2021
0 parents commit 8c97b96
Show file tree
Hide file tree
Showing 17 changed files with 1,570 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
out/

### Java template
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@


# Jump Point Search
Jump Point Search algorithm implemented in Java. This is forked from [jps](https://github.com/kevinsheehan/jps) which started as a port of the JPS algorithm implemented in the [PathFinding.js](https://github.com/qiao/PathFinding.js) project.

## Requirements
This library has been built using Java 8 up to this point. Making it compatible with other versions of Java is possible but at this point unlikely.

## Usage
There are four different rules that pathing Diagonally can follow.
- Always
- One Obstacle
- No Obstacles
- Never

You can check out the code within the Test directory for examples on how to use this within your own projects. But in the end it more or less comes down to these three lines:
```java
JPS<Tile> jps = JPS.JPSFactory.getJPS(new Graph<>(tileList), Graph.Diagonal.NO_OBSTACLES);
Future<Queue<Tile>> futurePath = jps.findPath(start, end);
Queue<Tile> path = futurePath.get();
```

### Distance and Heuristic
The code comes with four different distance algorithms already included but you are free to set your own as well.
The four you can choose from are: Manhattan, Euclidean, Octile, and Chebyshev.
```java
// Tile Map, Distance Calculation, Heuristic Calculation
new Graph<>(tiles, Graph.DistanceAlgo.MANHATTAN, Graph.DistanceAlgo.EUCLIDEAN);
```

If you would like to set your own:
```java
BiFunction<Node, Node, Double> minDistance = (a, b) -> (double) Math.min(Math.abs(a.x - b.x), Math.abs(a.y - b.y));
graph.setDistanceAlgo(minDistance);
graph.setHeuristicAlgo(minDistance);
```

### Adjacent Stop
When finding a path you can have the goal include adjacent nodes. If you want all of the goal node's neighbors to be considered good endpoints:
```java
Queue<Tile> futurePath = jps.findPathSync(start, end, true);
```

If you want to have neighbors be goal nodes but to exclude the diagonal ones:
```java
Queue<Tile> futurePath = jps.findPathSync(start, end, true, false);
```

## License
The MIT License (MIT)

Copyright (c) 2015 Kevin Sheehan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>JavaJPS</groupId>
<artifactId>JavaJPS</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
221 changes: 221 additions & 0 deletions src/main/java/jps/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package jps;


import java.util.*;
import java.util.function.BiFunction;

/**
* @author Kevin
*/
public class Graph<T extends Node> {
public enum Diagonal {
ALWAYS,
NO_OBSTACLES,
ONE_OBSTACLE,
NEVER
}

private List<T> nodes;
private int width;

private BiFunction<Node, Node, Double> distance = euclidean;
private BiFunction<Node, Node, Double> heuristic = euclidean;

public Graph(List<List<T>> map, DistanceAlgo distance, DistanceAlgo heuristic) {
width = map.get(0).size();
nodes = new ArrayList<>(map.size() * map.get(0).size());

map.forEach(nodes::addAll);

this.distance = distance.algo;
this.heuristic = heuristic.algo;
}

public Graph(T[][] map, DistanceAlgo distance, DistanceAlgo heuristic) {
width = map[0].length;
nodes = new ArrayList<>(map.length * map[0].length);

for (T[] row : map) {
Collections.addAll(nodes, row);
}

this.distance = distance.algo;
this.heuristic = heuristic.algo;
}

/**
* By default, will use EUCLIDEAN for distance and heuristic calculations. You can set your own algorithm
* if you would like to change this.
*/
public Graph(List<List<T>> map) {
width = map.get(0).size();
nodes = new ArrayList<>(map.size() * map.get(0).size());

map.forEach(nodes::addAll);
}

/**
* By default, will use EUCLIDEAN for distance and heuristic calculations. You can set your own algorithm
* if you would like to change this.
*/
public Graph(T[][] map) {
width = map[0].length;
nodes = new ArrayList<>(map.length * map[0].length);

for (T[] row : map) {
Collections.addAll(nodes, row);
}
}

/**
* @return List of all nodes in the graph.
*/
public Collection<T> getNodes() { return nodes; }

public T getNode(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= nodes.size() / width) {
return null;
}
return nodes.get(x + y*width);
}

/**
* Given two adjacent nodes, returns the distance between them.
* @return The distance between the two nodes given.
*/
public double getDistance(Node a, Node b) { return distance.apply(a, b); }

/**
* Given two nodes, returns the estimated distance between them. Optimizing this is the best way to improve
* performance of your search time.
* @return Estimated distance between the two given nodes.
*/
public double getHeuristicDistance(Node a, Node b) { return heuristic.apply(a, b); }

/**
* By default, we return all reachable diagonal neighbors that have no obstacles blocking us.
* i.e.
* O O G
* O C X
* O O O
*
* In this above example, we could not go diagonally from our (C)urrent position to our (G)oal due to the obstacle (X).
*
* Please use {@link #getNeighborsOf(Node, Diagonal)} method if you would like to specify different diagonal functionality.
*
* @return All reachable neighboring nodes of the given node.
*/
public Collection<T> getNeighborsOf(T node) {
return getNeighborsOf(node, Diagonal.NO_OBSTACLES);
}

/**
* @return All reachable neighboring nodes of the given node.
*/
public Set<T> getNeighborsOf(T node, Diagonal diagonal) {
int x = node.x;
int y = node.y;
Set<T> neighbors = new HashSet<>();

boolean n = false, s = false, e = false, w = false, ne = false, nw = false, se = false, sw = false;

// ?
if (isWalkable(x, y - 1)) {
neighbors.add(getNode(x, y - 1));
n = true;
}
// ?
if (isWalkable(x + 1, y)) {
neighbors.add(getNode(x + 1, y));
e = true;
}
// ?
if (isWalkable(x, y + 1)) {
neighbors.add(getNode(x, y+1));
s = true;
}
// ?
if (isWalkable(x - 1, y)) {
neighbors.add(getNode(x-1, y));
w = true;
}

switch (diagonal) {
case NEVER:
return neighbors;
case NO_OBSTACLES:
ne = n && e;
nw = n && w;
se = s && e;
sw = s && w;
break;
case ONE_OBSTACLE:
ne = n || e;
nw = n || w;
se = s || e;
sw = s || w;
break;
case ALWAYS:
ne = nw = se = sw = true;
}

// ?
if (nw && isWalkable(x - 1, y - 1)) {
neighbors.add(getNode(x - 1, y - 1));
}
// ?
if (ne && isWalkable(x + 1, y - 1)) {
neighbors.add(getNode(x + 1, y - 1));
}
// ?
if (se && isWalkable(x + 1, y + 1)) {
neighbors.add(getNode(x + 1, y + 1));
}
// ?
if (sw && isWalkable(x - 1, y + 1)) {
neighbors.add(getNode(x - 1, y + 1));
}

return neighbors;
}

public boolean isWalkable(int x, int y) {
return x >= 0 && x < width && y >= 0 && y < nodes.size() / width && getNode(x, y).walkable;
}

/**
* If you would like to define your own Distance Algorithm not included.
*/
public void setDistanceAlgo(BiFunction<Node, Node, Double> distance) {
this.distance = distance;
}

/**
* If you would like to define your own Heuristic Algorithm not included.
*/
public void setHeuristicAlgo(BiFunction<Node, Node, Double> heuristic) {
this.heuristic = heuristic;
}

public enum DistanceAlgo {
MANHATTAN(manhattan),
EUCLIDEAN(euclidean),
OCTILE(octile),
CHEBYSHEV(chebyshev);

BiFunction<Node, Node, Double> algo;

DistanceAlgo(BiFunction<Node, Node, Double> algo) {
this.algo = algo;
}
}
private static BiFunction<Node, Node, Double> manhattan = (a, b) -> (double) Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
private static BiFunction<Node, Node, Double> euclidean = (a, b) -> Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
private static BiFunction<Node, Node, Double> octile = (a, b) -> {
double F = Math.sqrt(2) - 1;
double dx = Math.abs(a.x - b.x);
double dy = Math.abs(a.y - b.y);
return (dx < dy) ? F * dx + dy : F * dy + dx;
};
private static BiFunction<Node, Node, Double> chebyshev = (a, b) -> (double) Math.max(Math.abs(a.x - b.x), Math.abs(a.y - b.y));
}
Loading

0 comments on commit 8c97b96

Please sign in to comment.