HOG2
RoadMap.cpp
Go to the documentation of this file.
1 //
2 // RoadMap.cpp
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 8/25/17.
6 // Copyright © 2017 University of Denver. All rights reserved.
7 //
8 
9 #include "RoadMap.h"
10 
11 #include <algorithm>
12 
13 
14 RoadMap::RoadMap(const char *graph, const char *coordinates, bool timeGraph)
15 {
16  double xMin = DBL_MAX;
17  double xMax = -DBL_MAX;
18  double yMin = DBL_MAX;
19  double yMax = -DBL_MAX;
20 
21 // double scale = 0;
22 
23  std::vector<double> speed;
24  g = new Graph();
25  node *n = new node("");
26  g->AddNode(n);
27  FILE *f = fopen(coordinates, "r");
28  //FILE *f = fopen("/Users/nathanst/Downloads/USA-road-d.COL.co", "r");
29  std::vector<double> xloc, yloc;
30  double minx = DBL_MAX, maxx=-DBL_MAX, miny=DBL_MAX, maxy=-DBL_MAX;
31  int count=0;
32  while (!feof(f))
33  {
34  char line[255];
35  fgets(line, 255, f);
36  if (line[0] == 'v')
37  {
38  double x1, y1;
39  int id;
40  if (3 != sscanf(line, "v %d %lf %lf", &id, &x1, &y1))
41  continue;
42  if(count==0)
43  {
44 // std::cout<<"x1 and y1 after scanning\n"<<x1<<" "<<y1;
45  count++;
46  }
47  //assert(id == xloc.size()+1);
48  xloc.push_back(x1);
49  yloc.push_back(y1);
50  //printf("%d: (%f, %f) [%f, %f]\n", xloc.size(), x1, y1, minx, maxx);
51  if (x1 > maxx) maxx = x1;
52  if (x1 < minx) minx = x1;
53  if (y1 > maxy) maxy = y1;
54  if (y1 < miny) miny = y1;
55  //if (maxx > -1)
56  }
57  }
58  std::cout<<"Xloc and yloc before scaling\n"<<xloc[1]<<" "<<yloc[1];
59  fclose(f);
60  printf("x between (%f, %f), y between (%f, %f)\n",
61  minx, maxx, miny, maxy);
62  scale = std::max(maxx-minx,maxy-miny);
63  printf("Scale is %f\n", scale);
64  double xoff = (maxx-minx)-scale;
65  double yoff = (maxy-miny)-scale;
66 
67  for (unsigned int x = 0; x < xloc.size(); x++)
68  {
69  //printf("(%f, %f) -> ", xloc[x], yloc[x]);
70  xloc[x] -= (minx);
71  xloc[x] /= scale;
72  xloc[x] = xloc[x]*2-1+xoff/scale;
73 
74  yloc[x] -= (miny);
75  yloc[x] /= scale;
76  yloc[x] = yloc[x]*2-1;
77  yloc[x] = -yloc[x]+yoff/scale;
78 
79  if(xloc[x] < xMin)
80  xMin = xloc[x];
81  else if(xloc[x] > xMax)
82  xMax = xloc[x];
83 
84  if(yloc[x] < yMin)
85  yMin = yloc[x];
86  else if(yloc[x] > yMax)
87  yMax = yloc[x];
88 
89 
90  node *n = new node("");
91  int nodeNum = g->AddNode(n);
95 
96  //std::cout<<" grid y size is "<<grid.size();
97  //std::cout<<" grid x size is "<<grid[0].size();
98 
99  }
100  //a 1 2 1988
101  std::cout<<"xMin is "<<xMin<<" yMin is "<<yMin<< " xMax is "<<xMax<<" yMax is "<<yMax<<std::endl;
102  std::cout<<" min x is "<<minx/scale + xoff;
103  std::cout<<" max x is "<<maxx/scale + xoff;
104  std::cout<<" min y is "<<miny/scale + yoff;
105  std::cout<<" max y is "<<maxy/scale + yoff;
106  printf("\n");
107 
108 
109  f = fopen(graph, "r");
110  //f = fopen("/Users/nathanst/Downloads/USA-road-d.COL.gr", "r");
111  int dups = 0;
112  long double e=0;
113  double minE=DBL_MAX;
114  while (!feof(f))
115  {
116  // std::cout<<"reading file\n";
117  char line[255];
118  fgets(line, 255, f);
119  if (line[0] == 'a')
120  {
121  int x1, y1;
122  sscanf(line, "a %d %d %Lf", &x1, &y1,&e);
123  if (g->findDirectedEdge(x1, y1) == 0)
124  {
125  node* n1=g->GetNode(x1);
126  node* n2=g->GetNode(y1);
127  long double xCoord1 = n1->GetLabelF(GraphSearchConstants::kXCoordinate);
128  long double yCoord1 =n1->GetLabelF(GraphSearchConstants::kYCoordinate);
129 
130  long double xCoord2 = n2->GetLabelF(GraphSearchConstants::kXCoordinate);
131  long double yCoord2 =n2->GetLabelF(GraphSearchConstants::kYCoordinate);
132 
133  long double dist = scale*sqrtf((xCoord1-xCoord2)*(xCoord1-xCoord2) + (yCoord1-yCoord2)*(yCoord1-yCoord2))/2;
134 
135  if(e<minE)
136  minE=e;
137  if (fequal(e, 0))
138  printf("**FOUND ZERO-COST EDGE\n");
139  if (x1 == y1)
140  printf("**FOUND SELF EDGE\n");
141 
142 
143  g->AddEdge(new edge(x1, y1, e));
144 
145  speed.push_back(dist/e);
146  }
147 
148  else if(g->findDirectedEdge(x1,y1)->GetWeight() < e)
149  g->findDirectedEdge(x1,y1)->setWeight(e);
150  else{
151  dups++;
152  //printf("Not adding duplicate directed edge between %d and %d\n", x1, y1);
153  }
154  }
155  }
156 
157  std::cout<<"minE "<<minE << "\n";
158  //printf("%d dups ignored\n", dups);
159  fclose(f);
160  std::cout<<"closed the file\n";
161  ge = new GraphEnvironment(g);
162  ge->SetDirected(true);
163  std::cout<<"created graph environment\n";
164 
165 // double maxSpeed = 0;
166 
167  if(!timeGraph)
168  maxSpeed = *(std::max_element(speed.begin(),speed.end()));
169  else if(timeGraph)
170  maxSpeed = *(std::max_element(speed.begin(),speed.end()));
171  else
172  {
173  std::cout<<"\n Invalid graph type";
174  exit(1);
175  }
176 
177  printf(" max element %lf \n",maxSpeed);
178 
179  // create heuristic & pass max speed
180 //
181 // h1 = new myHeuristic(g,maxSpeed, scale);
182 // astar.SetHeuristic(h1);
183 //
184 // std::cout<<" Set heuristic for A* \n";
185 //
186 //
187 
188 }
189 
190 
192 {
193  delete ge;
194  ge = 0;
195 }
196 
197 void RoadMap::GetSuccessors(const intersection &nodeID, std::vector<intersection> &neighbors) const
198 {
199  ge->GetSuccessors(nodeID, neighbors);
200 }
201 
202 void RoadMap::GetActions(const intersection &nodeID, std::vector<neighbor> &actions) const
203 {
204  ge->GetActions(nodeID, actions);
205 }
206 
207 
209 {
210  return ge->GetAction(s1, s2);
211 }
212 
214 {
215  ge->ApplyAction(s, a);
216 }
217 
218 
220 {
221  return ge->InvertAction(a);
222 }
223 
224 
226 double RoadMap::HCost(const intersection &from, const intersection &to) const
227 {
228  node* a = g->GetNode(from);
229  node* b = g->GetNode(to);
230 
235 
236  return scale*sqrt((aX-bX)*(aX-bX)+(aY-bY)*(aY-bY))/(2*maxSpeed);
237 }
238 
239 double RoadMap::GCost(const intersection &node1, const intersection &node2) const
240 {
241  return ge->GCost(node1, node2);
242 }
243 
244 double RoadMap::GCost(const intersection &node, const neighbor &act) const
245 {
246  return ge->GCost(node, act);
247 }
248 
249 bool RoadMap::GoalTest(const intersection &node, const intersection &goal) const
250 {
251  return node == goal;
252 }
253 
254 
255 uint64_t RoadMap::GetMaxHash() const
256 {
257  return g->GetNumNodes();
258 }
259 
261 {
262  return node;
263 }
264 
265 void RoadMap::GetStateFromHash(uint64_t parent, intersection &s) const
266 {
267  s = parent;
268 }
269 
270 uint64_t RoadMap::GetActionHash(neighbor act) const
271 {
272  return ge->GetActionHash(act);
273 }
274 
275 
277 {
278  ge->OpenGLDraw();
279 }
280 
282 {
283  ge->OpenGLDraw(i);
284 }
285 
286 void RoadMap::OpenGLDraw(const intersection&i, const neighbor&n) const
287 {
288  ge->OpenGLDraw(i, n);
289 }
290 
291 void RoadMap::GLDrawPath(const std::vector<intersection> &x) const
292 {
293  ge->GLDrawPath(x);
294 }
295 
296 void RoadMap::SetColor(GLfloat rr, GLfloat g, GLfloat b, GLfloat t) const
297 {
298  ge->SetColor(rr, g, b, t);
299 }
300 
301 void RoadMap::GetColor(GLfloat& rr, GLfloat& g, GLfloat& b, GLfloat &t) const
302 {
303  ge->GetColor(rr, g, b, t);
304 }
305 
306 
307 //}
308 //
309 //double HCost(const unsigned long &from, const unsigned long &to) const{
310 //
311 // node* a = g->GetNode(from);
312 // node* b = g->GetNode(to);
313 //
314 // double aX=a->GetLabelF(GraphSearchConstants::kXCoordinate);
315 // double aY=a->GetLabelF(GraphSearchConstants::kYCoordinate);
316 // double bX=b->GetLabelF(GraphSearchConstants::kXCoordinate);
317 // double bY=b->GetLabelF(GraphSearchConstants::kYCoordinate);
318 //
319 // return scale*sqrt((aX-bX)*(aX-bX)+(aY-bY)*(aY-bY))/(2*maxSpeed);
RoadMap::maxSpeed
double maxSpeed
Definition: RoadMap.h:52
Graph::AddNode
int AddNode(node *)
Definition: Graph.cpp:136
edge::GetWeight
double GetWeight()
Definition: Graph.h:140
node::SetLabelF
void SetLabelF(unsigned int index, double val) const
Definition: Graph.cpp:687
graphMove
Definition: GraphEnvironment.h:34
SearchEnvironment::GLDrawPath
virtual void GLDrawPath(const std::vector< state > &x) const
Definition: SearchEnvironment.h:170
GraphEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: GraphEnvironment.cpp:206
edge::setWeight
void setWeight(double val)
Definition: Graph.h:141
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
GraphEnvironment::GetSuccessors
virtual void GetSuccessors(const graphState &stateID, std::vector< graphState > &neighbors) const
Definition: GraphEnvironment.cpp:75
RoadMap::GetActionHash
virtual uint64_t GetActionHash(neighbor act) const
Definition: RoadMap.cpp:270
RoadMap::GLDrawPath
virtual void GLDrawPath(const std::vector< intersection > &x) const
Definition: RoadMap.cpp:291
RoadMap::GetSuccessors
virtual void GetSuccessors(const intersection &nodeID, std::vector< intersection > &neighbors) const
Definition: RoadMap.cpp:197
RoadMap::scale
double scale
Definition: RoadMap.h:53
GraphSearchConstants::kXCoordinate
@ kXCoordinate
Definition: GraphEnvironment.h:51
RoadMap::GCost
virtual double GCost(const intersection &node1, const intersection &node2) const
Definition: RoadMap.cpp:239
Graph
A generic Graph class.
Definition: Graph.h:66
Graph::GetNode
node * GetNode(unsigned long num)
Definition: Graph.cpp:152
RoadMap::GoalTest
virtual bool GoalTest(const intersection &node, const intersection &goal) const
Definition: RoadMap.cpp:249
RoadMap::HCost
virtual double HCost(const intersection &node1, const intersection &node2) const
Heuristic value between two arbitrary nodes.
Definition: RoadMap.cpp:226
RoadMap::ApplyAction
virtual void ApplyAction(intersection &s, neighbor a) const
Definition: RoadMap.cpp:213
GraphEnvironment::SetDirected
void SetDirected(bool b)
Definition: GraphEnvironment.h:303
SearchEnvironment::SetColor
virtual void SetColor(const rgbColor &r) const
Definition: SearchEnvironment.h:102
RoadMap::GetAction
virtual neighbor GetAction(const intersection &s1, const intersection &s2) const
Definition: RoadMap.cpp:208
SearchEnvironment::GetColor
virtual void GetColor(GLfloat &rr, GLfloat &g, GLfloat &b, GLfloat &t) const
Definition: SearchEnvironment.h:104
RoadMap::SetColor
virtual void SetColor(GLfloat rr, GLfloat g, GLfloat b, GLfloat t=1.0) const
Definition: RoadMap.cpp:296
GraphEnvironment::InvertAction
virtual bool InvertAction(graphMove &a) const
Definition: GraphEnvironment.cpp:147
RoadMap::GetStateFromHash
virtual void GetStateFromHash(uint64_t parent, intersection &s) const
Definition: RoadMap.cpp:265
RoadMap::~RoadMap
virtual ~RoadMap()
Definition: RoadMap.cpp:191
GraphEnvironment::ApplyAction
virtual void ApplyAction(graphState &s, graphMove a) const
Definition: GraphEnvironment.cpp:141
RoadMap.h
intersection
graphState intersection
Definition: RoadMap.h:16
RoadMap::OpenGLDraw
virtual void OpenGLDraw() const
Definition: RoadMap.cpp:276
RoadMap::ge
GraphEnvironment * ge
Definition: RoadMap.h:50
SearchEnvironment< intersection, neighbor >::GetColor
virtual rgbColor GetColor() const
Definition: SearchEnvironment.h:105
GraphEnvironment::GCost
virtual double GCost(const graphState &state1, const graphState &state2) const
Definition: GraphEnvironment.cpp:173
GraphSearchConstants::kYCoordinate
@ kYCoordinate
Definition: GraphEnvironment.h:52
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
Graph::GetNumNodes
int GetNumNodes()
Definition: Graph.cpp:403
node::GetLabelF
double GetLabelF(unsigned int index) const
Definition: Graph.h:215
RoadMap::GetMaxHash
virtual uint64_t GetMaxHash() const
Definition: RoadMap.cpp:255
GraphEnvironment::GetActionHash
virtual uint64_t GetActionHash(graphMove act) const
Definition: GraphEnvironment.cpp:198
RoadMap::GetStateHash
virtual uint64_t GetStateHash(const intersection &node) const
Definition: RoadMap.cpp:260
RoadMap::InvertAction
virtual bool InvertAction(neighbor &a) const
Definition: RoadMap.cpp:219
GraphEnvironment::GetAction
virtual graphMove GetAction(const graphState &s1, const graphState &s2) const
Definition: GraphEnvironment.cpp:136
RoadMap::g
Graph * g
Definition: RoadMap.h:51
RoadMap::GetActions
virtual void GetActions(const intersection &nodeID, std::vector< neighbor > &actions) const
Definition: RoadMap.cpp:202
GraphEnvironment::GetActions
virtual void GetActions(const graphState &stateID, std::vector< graphMove > &actions) const
Definition: GraphEnvironment.cpp:105
GraphSearchConstants::kZCoordinate
@ kZCoordinate
Definition: GraphEnvironment.h:53
Graph::findDirectedEdge
edge * findDirectedEdge(unsigned int from, unsigned int to)
Definition: Graph.cpp:189
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
GraphEnvironment
Definition: GraphEnvironment.h:291
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
RoadMap::RoadMap
RoadMap(const char *graph, const char *coordinates, bool time)
Definition: RoadMap.cpp:14
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129