-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNode.cpp
178 lines (151 loc) · 3.45 KB
/
Node.cpp
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
172
173
174
175
176
177
178
/*
* (C) 2014 Douglas Sievers
*
* Node.cpp
*/
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <cstdlib>
#include "Node.h"
#include "Edge.h"
using namespace std;
Node::Node(const string& id, const float latitude, const float longitude) : nodeID(id)
{
setStatus(UNEXPLORED);
setPathCost(0.0f);
setHeuristic(0.0f);
setLatitude(latitude);
setLongitude(longitude);
}
// Because pointers to other objects, namely edges and the parent node, are
// managed by weak pointers, there is no requirement to explicitly delete
// them in the destructor.
Node::~Node()
{
// This is only for debug purposes
//cout << "Destroy node " << getNodeID() << endl;
//system("PAUSE");
}
string Node::getNodeID() const
{
return nodeID;
}
void Node::setStatus(const Node::ExploredStatus s)
{
status = s;
}
Node::ExploredStatus Node::getStatus() const
{
return status;
}
void Node::setPathCost(const float c)
{
if (c >= 0.0f)
pathCost = c;
else
pathCost = 0.0f;
}
float Node::getPathCost() const
{
return pathCost;
}
void Node::setHeuristic(const float h)
{
if (h >= 0.0f)
heuristic = h;
else
heuristic = 0.0f;
}
float Node::getHeuristic() const
{
return heuristic;
}
void Node::setLatitude(const float lat)
{
if ((lat < -90.0f) || (lat > 90.0f))
{
throw out_of_range("Latitude must be between -90 and 90\n");
}
else
{
latitude = lat;
}
}
float Node::getLatitude() const
{
return latitude;
}
void Node::setLongitude(const float lon)
{
if ((lon < -180.0f) || (lon > 180.0f))
{
throw out_of_range("Longitude must be between -180 and 180\n");
}
else
{
longitude = lon;
}
}
float Node::getLongitude() const
{
return longitude;
}
void Node::setParentNode(boost::shared_ptr<Node> p)
{
parentNode = p;
}
boost::weak_ptr<Node> Node::getParentNode() const
{
return parentNode;
}
void Node::setParentAction(edgeWeakPtr e)
{
parentAction = e;
}
edgeWeakPtr Node::getParentAction() const
{
return parentAction;
}
void Node::setSearchState(const ExploredStatus status, const float cost, boost::shared_ptr<Node> parent, edgeWeakPtr action)
{
// convenience method to set multiple values in a single call
setStatus(status);
setPathCost(cost);
setParentNode(parent);
setParentAction(action);
}
void Node::addEdge(edgePtr edge)
{
leavingEdges.push_back(edge);
}
float Node::linearDistanceTo(nodePtr b) const
{
// This calculates the distance in km between two latitude/longitude coordinates
double toRad = 0.01745329251994329;
double dLat = (getLatitude() - b->getLatitude())*toRad;
double dLon = (getLongitude() - b->getLongitude())*toRad;
double a = sin(dLat/2.0) * sin(dLat/2.0) +
sin(dLon/2.0) * sin(dLon/2.0) * cos(toRad*b->getLatitude()) * cos(toRad*getLatitude());
double c = 2.0 * atan2(sqrt(a), sqrt(1-a));
return float(6371.0f * c);
}
void Node::printEdges() const
{
// iterate over leaving edges, and call each one's toString() method
for (edgeWeakPtrConstIterator it = leavingEdges.cbegin(); it != leavingEdges.cend(); ++it)
{
boost::shared_ptr<Edge> ptrSharedEdge(*it);
cout << " " << ptrSharedEdge->toString() << endl;
}
}
void Node::clearSearchState()
{
// set all values back to initial states
setStatus(UNEXPLORED);
setPathCost(0.0f);
setHeuristic(0.0f);
// clear object pointers to point to nothing
parentNode.reset();
parentAction.reset();
}