File size: 3,638 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
public class SearchPath {
/* private PriorityQueue<Node> pqueue;
private HashSet<Node> pqueueHash;
SearchPath(Node start){
pqueue = new PriorityQueue<>();
pqueueHash = new HashSet<>();
//Set initial condition
start.setCost(0);
pqueue.offer(start);
}
private boolean dijkstra(Centroid centroid, ArrayList<ArrayList<Node>> nodes){
if(!pqueue.isEmpty()){
//Set current to best possible node in priority queue, comparison can be found i Node class
Node current = pqueue.poll();
pqueueHash.remove(current);
if(current.isClosed()){
return true;
}
current.setClosed(true);
centroid.addNode(current);
current.setNeighbours(addNeighbours(current, nodes));
current.setBelongsToCentroid(centroid); //OBS! This may be wrong
for (int i = 0; i < current.getNeighbours().size(); i++) {
Node neighbour = current.getNeighbours().get(i);
double tentative_g_score = (Helpers.PlanarEuclideanDistance(centroid.getX(), centroid.getY(), neighbour.getX(), neighbour.getY())) + Helpers.ColorEuclideanDistance(centroid.getColor(), neighbour.getColor());
//If neighbours is in closed list; ignore it
if(neighbour.isClosed() && tentative_g_score > neighbour.getCost()){
continue;
}
//If open list contains neighbour OR it is better, set parent to current node.
if(!pqueueHash.contains(neighbour) || tentative_g_score < neighbour.getCost()){
current.setBelongsToCentroid(centroid);
centroid.addNode(current);
neighbour.setParent(current);
neighbour.setCost(tentative_g_score);
neighbour.setBelongsToCentroid(centroid); //OBS! This may be wrong
}
// Add to open list if not already there
if(!pqueueHash.contains(neighbour)){
pqueue.offer(neighbour);
pqueueHash.add(neighbour);
}
}
return true;
}
return false;
}
public boolean runOneStep(ArrayList<Node> closed, BufferedImage img, Centroid centroid, ArrayList<ArrayList<Node>> nodes){
return dijkstra(centroid, nodes);
}
private ArrayList<Node> addNeighbours(Node node, ArrayList<ArrayList<Node>> nodes){
ArrayList<Node> neighbours = new ArrayList<>();
try{
Node neighbour = nodes.get((int) node.getX()).get((int) (node.getY()-1));
if(!neighbour.isClosed()){
neighbours.add(neighbour);
}
}
catch(Exception ignored){
}
try{
Node neighbour = nodes.get((int) node.getX()).get((int) (node.getY()+1));
if(!neighbour.isClosed()){
neighbours.add(neighbour);
}
}
catch(Exception ignored){
}
try{
Node neighbour = nodes.get((int) node.getX()-1).get((int) (node.getY()));
if(!neighbour.isClosed()){
neighbours.add(neighbour);
}
}
catch(Exception ignored){
}
try{
Node neighbour = nodes.get((int) node.getX()+1).get((int) (node.getY()));
if(!neighbour.isClosed()){
neighbours.add(neighbour);
}
}
catch(Exception ignored){
}
return neighbours;
}
*/
} |