HOG2
ConfigEnvironment.cpp
Go to the documentation of this file.
1 /*
2  * ConfigEnvironment.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 1/9/09.
6  * Copyright 2009 NS Software. All rights reserved.
7  *
8  */
9 
10 #include "ConfigEnvironment.h"
11 #include "FPUtil.h"
12 #include <iostream>
13 #include <math.h>
14 
16 {
17  goal_stored = false;
18 }
19 
21 {
22 }
23 
24 void ConfigEnvironment::GetSuccessors(const recVec &nodeID, std::vector<recVec> &neighbors) const
25 {
26  //[(-0.300000, -0.320000) to (0.000000, 0.000000)] does not cross [(-0.320000, -0.300000) to (0.320000, -0.300000)]
27 // line2d a(recVec(-0.300000, -0.320000, 0), recVec(0, -0.4, 0));
28 // line2d b(recVec(-0.320000, -0.300000, 0), recVec(0.320000, -0.300000, 0));
29 // line2d c(recVec(-0.300000, -0.320000, 0), recVec(-0.300000, 0.320000, 0));
30 // if (a.crosses(b))
31 // printf("1 They Cross!\n");
32 // else
33 // printf("1 No crossing here!\n");
34 // if (b.crosses(a))
35 // printf("2 They Cross!\n");
36 // else
37 // printf("2 No crossing here!\n");
38 // if (a.crosses(c))
39 // printf("3 They Cross!\n");
40 // else
41 // printf("3 No crossing here!\n");
42 // if (c.crosses(a))
43 // printf("4 They Cross!\n");
44 // else
45 // printf("4 No crossing here!\n");
46 
47  neighbors.resize(0);
48  neighbors.push_back(goal);
49  for (unsigned int x = 0; x < obstacles.size(); x++)
50  {
51  neighbors.push_back(obstacles[x].start);
52  neighbors.push_back(obstacles[x].end);
53  }
54  for (unsigned int x = 0; x < neighbors.size(); x++)
55  {
56  if (!Legal(nodeID, neighbors[x]))
57  {
58  neighbors[x] = neighbors.back();
59  neighbors.pop_back();
60  x--;
61  }
62  }
63 // printf("Returning:\n");
64 // for (unsigned int x = 0; x < neighbors.size(); x++)
65 // std::cout << neighbors[x] << " ";
66 // std::cout << std::endl;
67 }
68 
69 void ConfigEnvironment::GetActions(const recVec &nodeID, std::vector<line2d> &actions) const
70 {
71  actions.resize(0);
72  actions.push_back(line2d(nodeID, goal));
73  for (unsigned int x = 0; x < obstacles.size(); x++)
74  {
75  actions.push_back(line2d(nodeID, obstacles[x].start));
76  actions.push_back(line2d(nodeID, obstacles[x].end));
77  }
78  for (unsigned int x = 0; x < actions.size(); x++)
79  {
80  if (!Legal(actions[x].start, actions[x].end))
81  {
82  actions[x] = actions.back();
83  actions.pop_back();
84  x--;
85  }
86  }
87 }
88 
89 bool ConfigEnvironment::Legal(const recVec &a, const recVec &b) const
90 {
91  line2d l(a, b);
92  for (unsigned int x = 0; x < obstacles.size(); x++)
93  {
94  if (obstacles[x].crosses(l))
95  {
96 // printf("[(%f, %f) to (%f, %f)] CROSSES [(%f, %f) to (%f, %f)]\n",
97 // a.x, a.y, b.x, b.y,
98 // obstacles[x].start.x, obstacles[x].start.y,
99 // obstacles[x].end.x, obstacles[x].end.y);
100  return false;
101  }
102 // printf("[(%f, %f) to (%f, %f)] does not cross [(%f, %f) to (%f, %f)]\n",
103 // a.x, a.y, b.x, b.y,
104 // obstacles[x].start.x, obstacles[x].start.y,
105 // obstacles[x].end.x, obstacles[x].end.y);
106  }
107  return true;
108 }
109 
111 {
112  line2d d;
113  d.start = s1;
114  d.end = s2;
115  return d;
116 }
117 
119 {
120  s = dir.end;
121 }
122 
123 
125 {
126  line2d b = a;
127  a.start = b.end;
128  a.end = b.start;
129  return true;
130 }
131 
132 
133 double ConfigEnvironment::HCost(const recVec &node1, const recVec &node2) const
134 {
135  return sqrt((node1.x-node2.x)*(node1.x-node2.x) +
136  (node1.y-node2.y)*(node1.y-node2.y) +
137  (node1.z-node2.z)*(node1.z-node2.z));
138 }
139 
140 double ConfigEnvironment::GCost(const recVec &node1, const recVec &node2) const
141 {
142  return sqrt((node1.x-node2.x)*(node1.x-node2.x) +
143  (node1.y-node2.y)*(node1.y-node2.y) +
144  (node1.z-node2.z)*(node1.z-node2.z));
145 }
146 
147 double ConfigEnvironment::GCost(const recVec &node1, const line2d &act) const
148 {
149  return sqrt((node1.x-act.end.x)*(node1.x-act.end.x) +
150  (node1.y-act.end.y)*(node1.y-act.end.y) +
151  (node1.z-act.end.z)*(node1.z-act.end.z));
152 }
153 
154 bool ConfigEnvironment::GoalTest(const recVec &node, const recVec &theGoal) const
155 {
156  return (fequal(node.x, theGoal.x) && fequal(node.y, theGoal.y) && fequal(node.z, theGoal.z));
157 }
158 
160 {
161  int x, y;
162  bool nx = node.x<0, ny = node.y<0;
163  x = (int)((double)fabs(node.x)*10000.0);
164  y = (int)((double)fabs(node.y)*10000.0);
165  uint64_t val = 0;
166  val = x;
167  val = (val<<14)|y;
168  val = (val<<1)|(nx?1:0);
169  val = (val<<1)|(ny?1:0);
170  return val;
171 }
172 
174 {
175  return (GetStateHash(act.start)^GetStateHash(act.end));
176 }
177 
178 
180 {
181  glColor3f(0, 0, 1.0);
182  for (unsigned int x = 0; x < obstacles.size(); x++)
183  DrawLine(obstacles[x]);
184 }
185 
187 {
188  glBegin(GL_POINT);
189  glVertex3f(l.x, l.y, l.z);
190  glEnd();
191 }
192 
193 void ConfigEnvironment::OpenGLDraw(const recVec &, const line2d &) const
194 {
195 }
196 
197 //void ConfigEnvironment::OpenGLDraw(const recVec &, const line2d &, GLfloat r, GLfloat g, GLfloat b) const
198 //{
199 //}
200 //
201 //void ConfigEnvironment::OpenGLDraw(const recVec &l, GLfloat r, GLfloat g, GLfloat b) const
202 //{
203 //}
204 
205 
206 void ConfigEnvironment::GetNextState(const recVec &, line2d dir, recVec &news) const
207 {
208  news = dir.end;
209 }
210 
212 {
213  glLineWidth(3);
214  glBegin(GL_LINES);
215  glVertex3f(l.start.x, l.start.y, 0);
216  glVertex3f(l.end.x, l.end.y, 0);
217  glEnd();
218  glLineWidth(1);
219 }
ConfigEnvironment::DrawLine
void DrawLine(line2d l) const
Definition: ConfigEnvironment.cpp:211
line2d::start
recVec start
Definition: GLUtil.h:160
recVec
A generic vector (essentially the same as a point, but offers normalization)
Definition: GLUtil.h:78
recVec::z
GLdouble z
Definition: GLUtil.h:98
ConfigEnvironment::HCost
virtual double HCost(const recVec &) const
Heuristic value between node and the stored goal.
Definition: ConfigEnvironment.h:30
ConfigEnvironment::goal_stored
bool goal_stored
Definition: ConfigEnvironment.h:66
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
ConfigEnvironment::GetStateHash
uint64_t GetStateHash(const recVec &node) const
Definition: ConfigEnvironment.cpp:159
line2d::end
recVec end
Definition: GLUtil.h:161
ConfigEnvironment::obstacles
std::vector< line2d > obstacles
Definition: ConfigEnvironment.h:64
FPUtil.h
ConfigEnvironment::GetSuccessors
void GetSuccessors(const recVec &nodeID, std::vector< recVec > &neighbors) const
Definition: ConfigEnvironment.cpp:24
ConfigEnvironment::goal
recVec goal
Definition: ConfigEnvironment.h:62
line2d
‍**
Definition: GLUtil.h:155
ConfigEnvironment.h
ConfigEnvironment::GCost
virtual double GCost(const recVec &node1, const recVec &node2) const
Definition: ConfigEnvironment.cpp:140
ConfigEnvironment::~ConfigEnvironment
virtual ~ConfigEnvironment()
Definition: ConfigEnvironment.cpp:20
ConfigEnvironment::GoalTest
bool GoalTest(const recVec &node, const recVec &goal) const
Definition: ConfigEnvironment.cpp:154
ConfigEnvironment::ApplyAction
virtual void ApplyAction(recVec &s, line2d dir) const
Definition: ConfigEnvironment.cpp:118
ConfigEnvironment::GetActionHash
uint64_t GetActionHash(line2d act) const
Definition: ConfigEnvironment.cpp:173
ConfigEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: ConfigEnvironment.cpp:179
ConfigEnvironment::GetActions
void GetActions(const recVec &nodeID, std::vector< line2d > &actions) const
Definition: ConfigEnvironment.cpp:69
ConfigEnvironment::Legal
bool Legal(const recVec &a, const recVec &b) const
Definition: ConfigEnvironment.cpp:89
ConfigEnvironment::InvertAction
virtual bool InvertAction(line2d &a) const
Definition: ConfigEnvironment.cpp:124
recVec::y
GLdouble y
Definition: GLUtil.h:98
ConfigEnvironment::GetAction
line2d GetAction(const recVec &s1, const recVec &s2) const
Definition: ConfigEnvironment.cpp:110
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
recVec::x
GLdouble x
Definition: GLUtil.h:98
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
ConfigEnvironment::GetNextState
virtual void GetNextState(const recVec &currents, line2d dir, recVec &news) const
Definition: ConfigEnvironment.cpp:206
ConfigEnvironment::ConfigEnvironment
ConfigEnvironment()
Definition: ConfigEnvironment.cpp:15