File size: 3,789 Bytes
8be366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * PathFinder.cc
 *
 *  Created on: May 30, 2011
 *      Author: truonghuy
 */

#include <PathFinder.h>
#include <climits>
#include <cmath>
//#include <iostream>

PathFinder::PathFinder() {
  // TODO Auto-generated constructor stub

}

/**
 *
 * @param[in] freeSpace the 2 dimensional array of freeSpace. -1 = obstacle, >=0 free.
 * */
int PathFinder::aStar(int startX, int startY, int endX, int endY,
		  std::vector<std::vector<long> >& freeSpace)
{
	int i,j; // counter
	std::vector<Node*>::iterator it;

	std::vector<Node*> openedList;
	std::vector<Node*> closedList;

	Node *startNode, *endNode, *currNode;

	startNode = new Node();
	endNode = new Node();

	endNode->x = endX;
	endNode->y = endY;

	startNode->x = startX;
	startNode->y = startY;
	startNode->_g = 0;
	startNode->_h = manhattanDist(startNode, endNode);

	openedList.push_back(startNode);

	bool pathFound = false;

	while(!openedList.empty()){
		std::vector<Node*>::iterator minIndex;
		int currCost;
		int minCost = INT_MAX;

		for (it=openedList.begin(); it < openedList.end(); ++it){

			currCost = (*it)->_g + (*it)->_h;

			if (minCost > currCost){
				minCost = currCost;
				minIndex = it;
			}
		}

		// moves the node with min cost from openedList to closedList
		currNode = *minIndex;
		closedList.push_back(currNode);
		openedList.erase(minIndex);

		// if finished
		if (sameNodes(endNode, currNode)){
			endNode->parent = currNode->parent;

			pathFound = true;
			break;
		}

		// checks the neighbourhood
		int neighborX, neighborY;
		Node* node2check;
		for(i=-1; i<2; i++){
			for(j=-1; j<2; j++){
				// only four movement directions
				if (fabs(i)+fabs(j) != 1) continue;

				neighborX = currNode->x + i;
				neighborY = currNode->y + j;

				if (neighborX < 0 || neighborY < 0 || neighborX >= freeSpace.size() || neighborY >= freeSpace[0].size())
					continue;

				// obstacle
				if (freeSpace[neighborX][neighborY] < 0) continue;

				node2check = new Node();
				node2check->x = neighborX;
				node2check->y = neighborY;

				// if already checked
				if (containsNode(openedList, node2check) || containsNode(closedList, node2check)){
					delete node2check;
					continue;
				}

				// calculates cost
				node2check->_g = currNode->_g + manhattanDist(node2check, currNode);
				node2check->_h = manhattanDist(node2check, endNode);

				// keep track of path
				node2check->parent = currNode;

				openedList.push_back(node2check);

			}
		}
	}

	if (pathFound){
		// go back from endNode
		Node *nextNodeFromStart;
		nextNodeFromStart = endNode;
		int action;
		while (!sameNodes(nextNodeFromStart->parent, startNode)){
			getAction(startNode, nextNodeFromStart);
			nextNodeFromStart = nextNodeFromStart->parent;
		}

		return getAction(startNode, nextNodeFromStart);
	}
	else return -1;
};

int PathFinder::getAction(Node* startNode, Node* nextNodeFromStart){
	if (startNode->x > nextNodeFromStart->x){
		//std::cout << "~left~" << std::endl;
		return 3;
	}
	if (startNode->x < nextNodeFromStart->x){
		//std::cout << "~right~" << std::endl;
		return 1;
	}
	if (startNode->y > nextNodeFromStart->y){
		//std::cout << "~down~" << std::endl;
		return 2;
	}
	if (startNode->y < nextNodeFromStart->y){
		//std::cout << "~up~" << std::endl;
		return 4;
	}
};


int PathFinder::manhattanDist(Node* startNode, Node* endNode){
	return fabs(startNode->x - endNode->x) + fabs(startNode->y - endNode->y);
};

bool PathFinder::sameNodes(Node* node1, Node* node2){
	return ((node1->x == node2->x) && (node1->y == node2->y));
};

bool PathFinder::containsNode(std::vector<Node*>& nodeArray, Node* node){

	for (unsigned i=0; i<nodeArray.size(); i++){
		if (sameNodes(node, nodeArray[i]))
			return true;
	}
	return false;
};

PathFinder::~PathFinder() {
  // TODO Auto-generated destructor stub
}