Skip to content

Commit

Permalink
Merge branch 'rafaelcastrocouto-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
imor committed Jun 5, 2014
2 parents ec7161f + 6419f2a commit a6c9019
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src/PathFinding.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module.exports = {
'Heap' : require('heap'),
'Node' : require('./core/Node'),
'Grid' : require('./core/Grid'),
'Util' : require('./core/Util'),
'Heuristic' : require('./core/Heuristic'),
'AStarFinder' : require('./finders/AStarFinder'),
'BestFirstFinder' : require('./finders/BestFirstFinder'),
'BreadthFirstFinder' : require('./finders/BreadthFirstFinder'),
'DijkstraFinder' : require('./finders/DijkstraFinder'),
'BiAStarFinder' : require('./finders/BiAStarFinder'),
'BiBestFirstFinder' : require('./finders/BiBestFirstFinder'),
'BiBreadthFirstFinder' : require('./finders/BiBreadthFirstFinder'),
'BiDijkstraFinder' : require('./finders/BiDijkstraFinder'),
'JumpPointFinder' : require('./finders/JumpPointFinder'),
'IDAStarFinder' : require('./finders/IDAStarFinder'),
'OrthogonalJumpPointFinder' : require('./finders/OrthogonalJumpPointFinder')
'Heap' : require('heap'),
'Node' : require('./core/Node'),
'Grid' : require('./core/Grid'),
'Util' : require('./core/Util'),
'Heuristic' : require('./core/Heuristic'),
'AStarFinder' : require('./finders/AStarFinder'),
'BestFirstFinder' : require('./finders/BestFirstFinder'),
'BreadthFirstFinder' : require('./finders/BreadthFirstFinder'),
'DijkstraFinder' : require('./finders/DijkstraFinder'),
'BiAStarFinder' : require('./finders/BiAStarFinder'),
'BiBestFirstFinder' : require('./finders/BiBestFirstFinder'),
'BiBreadthFirstFinder' : require('./finders/BiBreadthFirstFinder'),
'BiDijkstraFinder' : require('./finders/BiDijkstraFinder'),
'IDAStarFinder' : require('./finders/IDAStarFinder'),
'JumpPointFinder' : require('./finders/JumpPointFinder'),
'OrthogonalJumpPointFinder' : require('./finders/OrthogonalJumpPointFinder'),
'TraceFinder' : require('./finders/TraceFinder')
};
108 changes: 108 additions & 0 deletions src/finders/TraceFinder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var Heap = require('heap');
var Util = require('../core/Util');
var Heuristic = require('../core/Heuristic');

/**
* A* path-finder.
* based upon https://github.com/bgrins/javascript-astar
* @constructor
* @param {object} opt
* @param {boolean} opt.allowDiagonal Whether diagonal movement is allowed.
* @param {boolean} opt.dontCrossCorners Disallow diagonal movement touching block corners.
* @param {function} opt.heuristic Heuristic function to estimate the distance
* (defaults to manhattan).
* @param {integer} opt.weight Weight to apply to the heuristic to allow for suboptimal paths,
* in order to speed up the search.
*/
function TraceFinder(opt) {
opt = opt || {};
this.allowDiagonal = opt.allowDiagonal;
this.dontCrossCorners = opt.dontCrossCorners;
this.heuristic = opt.heuristic || Heuristic.manhattan;
}

/**
* Find and return the the path.
* @return {Array.<[number, number]>} The path, including both start and
* end positions.
*/
TraceFinder.prototype.findPath = function(startX, startY, endX, endY, grid) {

var openList = new Heap(function(nodeA, nodeB) {
return nodeA.f - nodeB.f;
}),
startNode = grid.getNodeAt(startX, startY),
endNode = grid.getNodeAt(endX, endY),
heuristic = this.heuristic,
allowDiagonal = this.allowDiagonal,
dontCrossCorners = this.dontCrossCorners,
abs = Math.abs, SQRT2 = Math.SQRT2,
node, neighbors, neighbor, i, l, x, y, ng;

// set the `g` and `f` value of the start node to be 0
startNode.g = 0;
startNode.f = 0;

// push the start node into the open list
openList.push(startNode);
startNode.opened = true;

// while the open list is not empty
while (!openList.empty()) {
// pop the position of node which has the minimum `f` value.
node = openList.pop();
node.closed = true;

// if reached the end position, construct the path and return it
if (node === endNode) {
return Util.backtrace(endNode);
}

// get neigbours of the current node
neighbors = grid.getNeighbors(node, allowDiagonal, dontCrossCorners);

var ar = neighbors.length;

for (i = 0, l = neighbors.length; i < l; ++i) {
neighbor = neighbors[i];

if (neighbor.closed) {
continue;
}

x = neighbor.x;
y = neighbor.y;

// get the distance between current node and the neighbor
// and calculate the next g score
ng = node.g + ((x - node.x === 0 || y - node.y === 0) ? 1 : SQRT2);

// check if the neighbor has not been inspected yet, or
// can be reached with smaller cost from the current node
if (!neighbor.opened || ng < neighbor.g) {
neighbor.g = ng * ar/9; //the trace magic
neighbor.h = neighbor.h || heuristic(abs(x - endX), abs(y - endY));
neighbor.f = neighbor.g + neighbor.h;
neighbor.parent = node;

if (!neighbor.opened) {
//openList.push(neighbor);
openList.push(neighbor);
neighbor.opened = true;
} else {
// the neighbor can be reached with smaller cost.
// Since its f value has been updated, we have to
// update its position in the open list

//openList.updateItem(neighbor);
openList.updateItem(neighbor);
}
}
} // end for each neighbor
} // end while not open list empty

// fail to find the path
return [];
};

module.exports = TraceFinder;
27 changes: 26 additions & 1 deletion visual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ <h3>Options</h3>
</div>
</div>

<h3 id="orth_jump_point_header"><a href="#">Orthogonal Jump Point Search</a></h3>
<h3 id="orth_jump_point_header"><a href="#">Orthogonal Jump Point Search</a></h3>
<div id="orth_jump_point_section" class="finder_section">
<header class="option_header">
<h3>Heuristic</h3>
Expand All @@ -206,6 +206,31 @@ <h3>Options</h3>
</div>
</div>

<h3 id="trace_header"><a href="#">Trace</a></h3>
<div id="trace_section" class="finder_section">
<header class="option_header">
<h3>Heuristic</h3>
</header>
<div id="trace_heuristic" class="sub_options">
<input type="radio" name="trace_heuristic" value="manhattan" checked />
<label class="option_label">Manhattan</label> <br>
<input type="radio" name="trace_heuristic" value="euclidean"/>
<label class="option_label">Euclidean</label> <br>
<input type="radio" name="trace_heuristic" value="chebyshev"/>
<label class="option_label">Chebyshev</label> <br>
</div>

<header class="option_header">
<h3>Options</h3>
</header>
<div class="optional sub_options">
<input type="checkbox" class="allow_diagonal" checked>
<label class="option_label">Allow Diagonal</label> <br>
<input type="checkbox" class="dont_cross_corners">
<label class="option_label">Don't Cross Corners</label> <br>
</div>
</div>

</div><!-- .accordion -->
</div><!-- #algorithm_panel -->

Expand Down
19 changes: 19 additions & 0 deletions visual/js/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ var Panel = {
});

break;

case 'trace_header':
allowDiagonal = typeof $('#trace_section ' +
'.allow_diagonal:checked').val() !== 'undefined';
biDirectional = typeof $('#trace_section ' +
'.bi-directional:checked').val() !=='undefined';
dontCrossCorners = typeof $('#trace_section ' +
'.dont_cross_corners:checked').val() !=='undefined';

heuristic = $('input[name=trace_heuristic]:checked').val();

finder = new PF.TraceFinder({
allowDiagonal: allowDiagonal,
dontCrossCorners: dontCrossCorners,
heuristic: PF.Heuristic[heuristic]
});

break;

}

return finder;
Expand Down
Loading

0 comments on commit a6c9019

Please sign in to comment.