HOG2
Map2DConstrainedEnvironment.cpp
Go to the documentation of this file.
1 //
2 // Map2DConstrainedEnvironment.cpp
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 8/3/12.
6 // Copyright (c) 2012 University of Denver. All rights reserved.
7 //
8 
10 
11 //std::vector<constraint> constraints;
12 //MapEnvironment *mapEnv;
13 
14 bool operator==(const xytLoc &l1, const xytLoc &l2)
15 {
16  return (l1.t == l2.t) && (l1.l == l2.l);
17 }
18 
20 {
21  mapEnv = new MapEnvironment(m);
23 }
24 
26 {
27  constraints.push_back(c);
28 }
29 
31 {
33 }
34 
36 {
37  constraint c;
38  c.loc = loc;
39  c.dir = dir;
40  constraints.push_back(c);
41 }
42 
44 {
45  constraints.resize(0);
46 }
47 
48 void Map2DConstrainedEnvironment::GetSuccessors(const xytLoc &nodeID, std::vector<xytLoc> &neighbors) const
49 {
50  std::vector<xyLoc> n;
51  mapEnv->GetSuccessors(nodeID.l, n);
52 
53  // TODO: remove illegal successors
54  for (unsigned int x = 0; x < n.size(); x++)
55  {
56  if (!ViolatesConstraint(nodeID.l, n[x], nodeID.t))
57  {
58  xytLoc newLoc;
59  newLoc.l = n[x];
60  newLoc.t = nodeID.t+1;
61  neighbors.push_back(newLoc);
62  }
63  }
64  // TODO: Add kStay
65  if (!ViolatesConstraint(nodeID.l, nodeID.l, nodeID.t))
66  {
67  xytLoc newLoc;
68  newLoc.l = nodeID.l;
69  newLoc.t = nodeID.t+1;
70  neighbors.push_back(newLoc);
71  }
72 // std::cout << "Successors of " << nodeID << " are:" << std::endl;
73 // for (unsigned int x = 0; x < neighbors.size(); x++)
74 // std::cout << neighbors[x] << " ";
75 // std::cout << std::endl;
76 
77 }
78 
79 bool Map2DConstrainedEnvironment::ViolatesConstraint(const xyLoc &from, const xyLoc &to, int time) const
80 {
81  xyLoc tmp;
82  for (unsigned int x = 0; x < constraints.size(); x++)
83  {
84  if ((constraints[x].dir == kTeleport) && time+1 == constraints[x].loc.t && (to == constraints[x].loc.l))
85  return true;
86  if ((constraints[x].dir != kTeleport) && time+1 == constraints[x].loc.t && (constraints[x].loc.l == to))
87  {
88  tmp = to;
89  mapEnv->UndoAction(tmp, constraints[x].dir);
90  if (tmp == from)
91  return true;
92  }
93  }
94  return false;
95 }
96 
97 void Map2DConstrainedEnvironment::GetActions(const xytLoc &nodeID, std::vector<tDirection> &actions) const
98 {
99  mapEnv->GetActions(nodeID.l, actions);
100 
101  // TODO: remove illegal actions
102 }
103 
105 {
106  return mapEnv->GetAction(s1.l, s2.l);
107 }
108 
110 {
111  mapEnv->ApplyAction(s.l, a);
112  s.t+=1;
113 }
114 
116 {
117  mapEnv->UndoAction(s.l, a);
118  s.t-=1;
119 }
120 
121 
123 {
124  return mapEnv->InvertAction(a);
125 }
126 
127 
129 double Map2DConstrainedEnvironment::HCost(const xytLoc &node1, const xytLoc &node2) const
130 {
131  double res1 = mapEnv->HCost(node1.l, node2.l);
132  double res2 = (node2.t>node1.t)?(node2.t-node1.t):0;
133  //std::cout << "h(" << node1 << ", " << node2 << ") = " << res1 << " " << res2 << std::endl;
134  return max(res1, res2);
135 }
136 
138 {
139  return (node.l == goal.l && node.t >= goal.t);
140 }
141 
142 
144 {
145  uint64_t hash;
146  hash = node.l.x;
147  hash <<= 16;
148  hash |= node.l.y;
149  hash <<= 16;
150  hash |= node.t;
151  return hash;
152 }
153 
155 {
156  return act;
157 }
158 
159 
161 {
162  mapEnv->OpenGLDraw();
163  // draw constrains
164  Map *map = mapEnv->GetMap();
165  for (unsigned int x = 0; x < constraints.size(); x++)
166  {
167  GLdouble xx, yy, zz, rad;
168  map->GetOpenGLCoord(constraints[x].loc.l.x, constraints[x].loc.l.y, xx, yy, zz, rad);
169  glColor4f(1.0, 0.0, 0.0, 0.5);
170  //glColor3f(0.5, 0.5, 0.5);
171  DrawSphere(xx, yy, zz-constraints[x].loc.t*rad, rad);
172  }
173 }
174 
176 {
177  GLfloat r, g, b, t;
178  GetColor(r, g, b, t);
179  Map *map = mapEnv->GetMap();
180  GLdouble xx, yy, zz, rad;
181  map->GetOpenGLCoord(l.l.x, l.l.y, xx, yy, zz, rad);
182  glColor4f(r, g, b, t);
183  DrawSphere(xx, yy, zz-l.t*rad, rad); // zz-l.t*2*rad
184 }
185 
187 {
188 
189 }
190 
192 {
193  GLdouble xx, yy, zz, rad;
194  Map *map = mapEnv->GetMap();
195  map->GetOpenGLCoord(x.l.x, x.l.y, xx, yy, zz, rad);
196 
197  GLfloat r, g, b, t;
198  GetColor(r, g, b, t);
199  glColor4f(r, g, b, t);
200  glBegin(GL_LINES);
201  glVertex3f(xx, yy, zz-x.t*rad);
202  map->GetOpenGLCoord(y.l.x, y.l.y, xx, yy, zz, rad);
203  glVertex3f(xx, yy, zz-y.t*rad);
204  glEnd();
205 }
206 
loc::x
int x
Definition: MapGenerators.cpp:296
MapEnvironment::InvertAction
virtual bool InvertAction(tDirection &a) const
Definition: Map2DEnvironment.cpp:415
Map2DConstrainedEnvironment::ApplyAction
virtual void ApplyAction(xytLoc &s, tDirection a) const
Definition: Map2DConstrainedEnvironment.cpp:109
Map2DConstrainedEnvironment::GetStateHash
virtual uint64_t GetStateHash(const xytLoc &node) const
Definition: Map2DConstrainedEnvironment.cpp:143
MapEnvironment::SetFourConnected
void SetFourConnected()
Definition: Map2DEnvironment.h:211
kTeleport
@ kTeleport
Definition: Map2DEnvironment.h:79
operator==
bool operator==(const xytLoc &l1, const xytLoc &l2)
Definition: Map2DConstrainedEnvironment.cpp:14
xyLoc::y
uint16_t y
Definition: Map2DEnvironment.h:42
MapEnvironment::GetMap
Map * GetMap() const
Definition: Map2DEnvironment.h:200
constraint::loc
xytLoc loc
Definition: Map2DConstrainedEnvironment.h:24
Map2DConstrainedEnvironment::UndoAction
virtual void UndoAction(xytLoc &s, tDirection a) const
Definition: Map2DConstrainedEnvironment.cpp:115
Map2DConstrainedEnvironment::GLDrawLine
virtual void GLDrawLine(const xytLoc &x, const xytLoc &y) const
Definition: Map2DConstrainedEnvironment.cpp:191
SearchEnvironment::UndoAction
virtual void UndoAction(state &s, action a) const
Definition: SearchEnvironment.h:40
Map2DConstrainedEnvironment::ViolatesConstraint
bool ViolatesConstraint(const xyLoc &from, const xyLoc &to, int time) const
Definition: Map2DConstrainedEnvironment.cpp:79
xytLoc::t
uint32_t t
Definition: Map2DConstrainedEnvironment.h:20
DrawSphere
void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
Definition: GLUtil.cpp:433
xyLoc
Definition: Map2DEnvironment.h:37
Map2DConstrainedEnvironment::ClearConstraints
void ClearConstraints()
Definition: Map2DConstrainedEnvironment.cpp:43
MapEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: Map2DEnvironment.cpp:552
Map2DConstrainedEnvironment::AddConstraint
void AddConstraint(constraint c)
Definition: Map2DConstrainedEnvironment.cpp:25
xyLoc::x
uint16_t x
Definition: Map2DEnvironment.h:41
Map2DConstrainedEnvironment::GoalTest
virtual bool GoalTest(const xytLoc &node, const xytLoc &goal) const
Definition: Map2DConstrainedEnvironment.cpp:137
MapEnvironment::ApplyAction
virtual void ApplyAction(xyLoc &s, tDirection dir) const
Definition: Map2DEnvironment.cpp:432
constraint
Definition: Map2DConstrainedEnvironment.h:23
Map2DConstrainedEnvironment::HCost
virtual double HCost(const xytLoc &node1, const xytLoc &node2) const
Heuristic value between two arbitrary nodes.
Definition: Map2DConstrainedEnvironment.cpp:129
MapEnvironment
Definition: Map2DEnvironment.h:133
Map2DConstrainedEnvironment::InvertAction
virtual bool InvertAction(tDirection &a) const
Definition: Map2DConstrainedEnvironment.cpp:122
loc
Definition: MapGenerators.cpp:296
Map2DConstrainedEnvironment::GetAction
virtual tDirection GetAction(const xytLoc &s1, const xytLoc &s2) const
Definition: Map2DConstrainedEnvironment.cpp:104
tDirection
tDirection
Definition: Map2DEnvironment.h:77
xytLoc::l
xyLoc l
Definition: Map2DConstrainedEnvironment.h:19
SearchEnvironment< xytLoc, tDirection >::GetColor
virtual rgbColor GetColor() const
Definition: SearchEnvironment.h:105
Map2DConstrainedEnvironment::GetActionHash
virtual uint64_t GetActionHash(tDirection act) const
Definition: Map2DConstrainedEnvironment.cpp:154
MapEnvironment::HCost
virtual double HCost(const xyLoc &) const
Heuristic value between node and the stored goal.
Definition: Map2DEnvironment.h:154
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
MapEnvironment::GetSuccessors
virtual void GetSuccessors(const xyLoc &nodeID, std::vector< xyLoc > &neighbors) const
Definition: Map2DEnvironment.cpp:59
xytLoc
Definition: Map2DConstrainedEnvironment.h:16
MapEnvironment::GetActions
void GetActions(const xyLoc &nodeID, std::vector< tDirection > &actions) const
Definition: Map2DEnvironment.cpp:355
Map2DConstrainedEnvironment::constraints
std::vector< constraint > constraints
Definition: Map2DConstrainedEnvironment.h:70
Map2DConstrainedEnvironment.h
Map::GetOpenGLCoord
bool GetOpenGLCoord(int _x, int _y, GLdouble &x, GLdouble &y, GLdouble &z, GLdouble &radius) const
Get the openGL coordinates of a given tile.
Definition: Map.cpp:1826
Map2DConstrainedEnvironment::GetActions
virtual void GetActions(const xytLoc &nodeID, std::vector< tDirection > &actions) const
Definition: Map2DConstrainedEnvironment.cpp:97
Map2DConstrainedEnvironment::mapEnv
MapEnvironment * mapEnv
Definition: Map2DConstrainedEnvironment.h:71
Map2DConstrainedEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: Map2DConstrainedEnvironment.cpp:160
Map2DConstrainedEnvironment::Map2DConstrainedEnvironment
Map2DConstrainedEnvironment(Map *m)
Definition: Map2DConstrainedEnvironment.cpp:19
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
MapEnvironment::GetAction
tDirection GetAction(const xyLoc &s1, const xyLoc &s2) const
Definition: Map2DEnvironment.cpp:392
Map
A tile-based representation of the world.
Definition: Map.h:142
constraint::dir
tDirection dir
Definition: Map2DConstrainedEnvironment.h:25
Map2DConstrainedEnvironment::GetSuccessors
virtual void GetSuccessors(const xytLoc &nodeID, std::vector< xytLoc > &neighbors) const
Definition: Map2DConstrainedEnvironment.cpp:48