| 1 | /* |
| 2 | Copyright (c) 2012, Sean Heber. All rights reserved. |
| 3 | Copyright (c) 2013, Cong Xu. All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | |
| 8 | 1. Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | this list of conditions and the following disclaimer in the documentation |
| 13 | and/or other materials provided with the distribution. |
| 14 | |
| 15 | 3. Neither the name of Sean Heber nor the names of its contributors may |
| 16 | be used to endorse or promote products derived from this software without |
| 17 | specific prior written permission. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | DISCLAIMED. IN NO EVENT SHALL SEAN HEBER BE LIABLE FOR ANY DIRECT, |
| 23 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef AStar_h |
| 32 | #define AStar_h |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | typedef struct __ASNeighborList *ASNeighborList; |
| 37 | typedef struct __ASPath *ASPath; |
| 38 | |
| 39 | typedef struct { |
| 40 | size_t nodeSize; // the size of the structure being used for the nodes - important since nodes are copied into the resulting path |
| 41 | void (*nodeNeighbors)(ASNeighborList neighbors, void *node, void *context); // add nodes to the neighbor list if they are connected to this node |
| 42 | float (*pathCostHeuristic)(void *fromNode, void *toNode, void *context); // estimated cost to transition from the first node to the second node -- optional, uses 0 if not specified |
| 43 | int (*earlyExit)(size_t visitedCount, void *visitingNode, void *goalNode, void *context); // early termination, return 1 for success, -1 for failure, 0 to continue searching -- optional |
| 44 | int (*nodeComparator)(void *node1, void *node2, void *context); // must return a sort order for the nodes (-1, 0, 1) -- optional, uses memcmp if not specified |
| 45 | } ASPathNodeSource; |
| 46 | |
| 47 | // use in the nodeNeighbors callback to return neighbors |
| 48 | void ASNeighborListAdd(ASNeighborList neighbors, void *node, float edgeCost); |
| 49 | |
| 50 | // if goalNode is NULL, it searches the entire graph and returns the cheapest deepest path |
| 51 | // context is optional and is simply passed through to the callback functions |
| 52 | // startNode and nodeSource is required |
| 53 | // as a path is created, the relevant nodes are copied into the path |
| 54 | ASPath ASPathCreate(const ASPathNodeSource *nodeSource, void *context, void *startNode, void *goalNode); |
| 55 | |
| 56 | // paths created with ASPathCreate() must be destroyed or else it will leak memory |
| 57 | void ASPathDestroy(ASPath path); |
| 58 | |
| 59 | // if you want to make a copy of a path result, this function will do the job |
| 60 | // you must call ASPathDestroy() with the resulting path to clean it up or it will cause a leak |
| 61 | ASPath ASPathCopy(ASPath path); |
| 62 | |
| 63 | // fetches the number of nodes in the path |
| 64 | size_t ASPathGetCount(ASPath path); |
| 65 | |
| 66 | // returns a pointer to the given node in the path |
| 67 | void *ASPathGetNode(ASPath path, size_t index); |
| 68 | |
| 69 | #endif |