HOG2
NaryTree.cpp
Go to the documentation of this file.
1 /*
2  * NaryTree.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 10/20/10.
6  * Copyright 2010 University of Denver. All rights reserved.
7  *
8  */
9 
10 #include "NaryTree.h"
11 #include <iostream>
12 #include <cinttypes>
13 
14 NaryTree::NaryTree(int branchingFactor, int depth) :b(branchingFactor), d(depth)
15 {
16  scaleWidth = 1.0;
17  uint64_t tot = 1;
18  uint64_t sum = 1;
19  for (int x = 0; x <= d; x++)
20  {
21  nodesAtDepth.push_back(tot);
22  totalNodesAtDepth.push_back(sum);
23  tot*=b;
24  sum += tot;
25  }
26 // for (size_t x = 0; x < nodesAtDepth.size(); x++)
27 // {
28 // printf("%d %" PRId64 " %" PRId64 "\n", x, nodesAtDepth[x], totalNodesAtDepth[x]);
29 // }
30 }
31 
32 void NaryTree::GetSuccessors(const NaryState &nodeID, std::vector<NaryState> &neighbors) const
33 {
34 // std::cout << nodeID << " has depth " << GetDepth(nodeID) << std::endl;
35  neighbors.resize(0);
36  if (GetDepth(nodeID) >= d)
37  return;
38  for (int x = 0; x < b; x++)
39  neighbors.push_back(nodeID*b+x+1);
40 }
41 
42 void NaryTree::GetActions(const NaryState &nodeID, std::vector<NaryAction> &actions) const
43 {
44  assert(false); // code not tested -
45  actions.resize(0);
46  if (GetDepth(nodeID) >= d)
47  return;
48  actions.resize(b);
49  for (unsigned x = 0; x < actions.size(); x++)
50  actions[x] = x+1;
51 }
52 
54 {
55  assert(false);
56  return 0;
57 }
58 
60 {
61  // code not tested - should probably not be adding 1
62  assert(false);
63  if (a > 0)
64  s = s*b+a+1;
65  else {
66  s = (s-1)/b;
67  }
68 }
69 
71 {
72  if (a > 0)
73  s2 = s1*b+a;
74  else {
75  s2 = (s1-1)/b;
76  }
77 
78 }
79 
81 { a = -a; return true; }
82 
84 double NaryTree::HCost(const NaryState &node1, const NaryState &node2) const
85 { if (node1 == node2) return 0; return 1; }
86 
87 double NaryTree::GCost(const NaryState &, const NaryState &) const
88 { return 1; }
89 double NaryTree::GCost(const NaryState &, const NaryAction &) const
90 { return 1; }
91 bool NaryTree::GoalTest(const NaryState &node, const NaryState &goal) const
92 { return node == goal; }
93 
94 
95 uint64_t NaryTree::GetStateHash(const NaryState &node) const
96 { return node; }
97 
99 { return act+b; }
100 
101 float sqdist(float x1, float y1, float x2, float y2)
102 {
103  return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
104 }
105 
107 {
108  float bestDist = 100;
109  float x1, y1;
110  uint64_t best = 0;
111  for (uint64_t t = 0; t < totalNodesAtDepth.back(); t++)
112  {
113  GetLocation(t, x1, y1);
114  if (sqdist(x, y, x1, y1) < bestDist)
115  {
116  bestDist = sqdist(x, y, x1, y1);
117  best = t;
118  }
119  }
120  return best;
121 }
122 
123 
124 void NaryTree::GetLocation(const NaryState &s, float &x, float &y) const
125 {
126  int depth = GetDepth(s);
127  x = -1.0+(2.0*GetOffset(s))/nodesAtDepth[depth]+1.0/nodesAtDepth[depth];
128  x *= scaleWidth;
129  y = 2.0*float(depth)/float(d)-1;
130  x *= 0.95;
131  y *= 0.95;
132  //printf("%" PRId64 " depth %d offset %" PRId64 "\n", s, depth, GetOffset(s));
133 }
134 
135 int NaryTree::GetDepth(const NaryState s) const
136 {
137  if (s == 0)
138  return 0;
139  if (s <= b)
140  return 1;
141  return 1+GetDepth((int)(s-1)/b);
142 }
143 
144 uint64_t NaryTree::GetOffset(const NaryState s) const
145 {
146  if (s == 0)
147  return 0;
148  for (int x = 0; x < totalNodesAtDepth.size(); x++)
149  {
150  if (s >= totalNodesAtDepth[x]-1 && s < totalNodesAtDepth[x+1])
151  return s - totalNodesAtDepth[x];
152  }
153  return 0;
154 }
155 
157 {
158  if (s == 0)
159  return s;
160  return (s-1)/b;
161 }
162 
164 {
165  std::vector<NaryState> succ;
166  float x1, y1, x2, y2;
167  glBegin(GL_LINES);
168  glColor3f(1.0, 1.0, 1.0);
169  for (uint64_t t = 0; t < totalNodesAtDepth.back(); t++)
170  {
171  GetLocation(t, x1, y1);
172  GetSuccessors(t, succ);
173  for (uint64_t s : succ)
174  {
175  GetLocation(s, x2, y2);
176  glVertex3f(x1, y1, 0);
177  glVertex3f(x2, y2, 0);
178  }
179  }
180  glEnd();
181 }
182 
183 void NaryTree::OpenGLDraw(const NaryState &s) const
184 {
185  float x1, y1;
186  GLfloat r, g, b, t;
187  GetColor(r, g, b, t);
188  glColor4f(r, g, b, t);
189  GetLocation(s, x1, y1);
190  double r1 = 2.0/nodesAtDepth[GetDepth(s)];
191  double r2 = 0.1/d;
192  DrawSphere(x1, y1, 0, std::min(r1, r2));
193 }
194 
195 void NaryTree::GLDrawLine(const NaryState &s1, const NaryState &s2) const
196 {
197  float x1, y1, x2, y2;
198  GLfloat r, g, b, t;
199  GetColor(r, g, b, t);
200  glLineWidth(6.0);
201  glBegin(GL_LINES);
202  glColor4f(r, g, b, t);
203  GetLocation(s1, x1, y1);
204  GetLocation(s2, x2, y2);
205  glVertex3f(x1, y1, 0);
206  glVertex3f(x2, y2, 0);
207  glEnd();
208  glLineWidth(1.0);
209 }
210 
211 void NaryTree::Draw(Graphics::Display &display) const
212 {
213  rgbColor color = GetColor();
214  std::vector<NaryState> succ;
215  float x1, y1, x2, y2;
216  float r1, r2;
217  // loop through the total number of nodes in the whole tree
218  for (uint64_t t = 0; t < totalNodesAtDepth.back(); t++)
219  {
220  GetLocation(t, x1, y1);
221  GetSuccessors(t, succ);
222  int depth = GetDepth(t);
223  r1 = GetDepthRadius(depth);
224  for (uint64_t s : succ)
225  {
226  GetLocation(s, x2, y2);
227  r2 = GetDepthRadius(depth+1);
228 
229  float theta = atan2(x1-x2, y1-y2);
230  float stheta = -sin(theta);
231  float ctheta = cos(theta);
232 // display.DrawLine({x1, y1}, {x2, y2}, 2.0-1.8*(((y1+y2)/2.0+1.0)/2.0), color);
233 // float r1 = 1.0/nodesAtDepth[GetDepth(s)];
234 // float r2 = 0.1/d;
235 // float r = std::min(r1, r2);
236 // display.DrawLine({x1, y1}, {x2, y2}, r, color);
237 
238  display.FillTriangle({x1-r1*ctheta, y1-r1*stheta}, {x2-r2*ctheta, y2-r2*stheta}, {x1+r1*ctheta, y1+r1*stheta}, color);
239  display.FillTriangle({x1+r1*ctheta, y1+r1*stheta}, {x2-r2*ctheta, y2-r2*stheta}, {x2+r2*ctheta, y2+r2*stheta}, color);
240  }
241  display.FillCircle({x1-r1, y1-r1, x1+r1, y1+r1}, Colors::gray);
242  }
243 }
244 
245 float NaryTree::GetDepthRadius(int depth) const
246 {
247  float r1 = 0.33333/nodesAtDepth[depth];
248  float r2 = 0.008;
249  float r = std::min(r1, r2);
250  return r;
251 }
252 
253 void NaryTree::Draw(Graphics::Display &display, const NaryState &s) const
254 {
255  float x1, y1;
256  GetLocation(s, x1, y1);
257 // float r1 = 2.0/nodesAtDepth[GetDepth(s)];
258 // float r2 = 0.1/d;
259  float r = std::max(0.01f, 3.0f*GetDepthRadius(GetDepth(s)));//std::min(r1, r2));
260  display.FillCircle({x1-r, y1-r, x1+r, y1+r}, color);
261 }
262 
263 void NaryTree::DrawLine(Graphics::Display &display, const NaryState &f, const NaryState &t, float width) const
264 {
265  float x1, y1, x2, y2;
266  GetLocation(f, x1, y1);
267  int depth = GetDepth(f);
268  float r1 = GetDepthRadius(depth)*width;
269 
270  GetLocation(t, x2, y2);
271  float r2 = GetDepthRadius(depth+1)*width;
272 
273  float theta = atan2(x1-x2, y1-y2);
274  float stheta = -sin(theta);
275  float ctheta = cos(theta);
276 
277  display.FillCircle({x1, y1}, r1, color);
278  display.FillCircle({x2, y2}, r2, color);
279  display.FillTriangle({x1-r1*ctheta, y1-r1*stheta}, {x2-r2*ctheta, y2-r2*stheta}, {x1+r1*ctheta, y1+r1*stheta}, color);
280  display.FillTriangle({x1+r1*ctheta, y1+r1*stheta}, {x2-r2*ctheta, y2-r2*stheta}, {x2+r2*ctheta, y2+r2*stheta}, color);
281 }
282 
283 
284 void NaryTree::OpenGLDraw(const NaryState&, const NaryState&, float) const { }
285 void NaryTree::OpenGLDraw(const NaryState&, const NaryAction&) const { }
NaryTree::OpenGLDraw
virtual void OpenGLDraw() const
Definition: NaryTree.cpp:163
NaryTree::d
int d
Definition: NaryTree.h:78
NaryTree::HCost
virtual double HCost(const NaryState &node1, const NaryState &node2) const
Heuristic value between two arbitrary nodes.
Definition: NaryTree.cpp:84
NaryTree::GetOffset
uint64_t GetOffset(const NaryState s) const
Definition: NaryTree.cpp:144
rgbColor
A color; r/g/b are between 0...1.
Definition: Colors.h:17
NaryTree::DrawLine
virtual void DrawLine(Graphics::Display &display, const NaryState &, const NaryState &, float width) const
Definition: NaryTree.cpp:263
NaryTree::GetClosestNode
NaryState GetClosestNode(float x, float y)
Definition: NaryTree.cpp:106
min
double min(double a, double b)
Definition: FPUtil.h:35
NaryTree::GetLocation
void GetLocation(const NaryState &s, float &x, float &y) const
Definition: NaryTree.cpp:124
NaryTree::Draw
virtual void Draw(Graphics::Display &display) const
Definition: NaryTree.cpp:211
Graphics::Display::FillTriangle
void FillTriangle(point p1, point p2, point p3, rgbColor c)
Definition: Graphics.cpp:146
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
NaryTree::totalNodesAtDepth
std::vector< uint64_t > totalNodesAtDepth
Definition: NaryTree.h:80
DrawSphere
void DrawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
Definition: GLUtil.cpp:433
width
int width
Definition: SFML_HOG.cpp:54
NaryTree.h
NaryTree::GetDepth
int GetDepth(const NaryState s) const
Definition: NaryTree.cpp:135
NaryTree::GetActions
virtual void GetActions(const NaryState &nodeID, std::vector< NaryAction > &actions) const
Definition: NaryTree.cpp:42
NaryTree::ApplyAction
virtual void ApplyAction(NaryState &s, NaryAction a) const
Definition: NaryTree.cpp:59
NaryTree::b
int b
Definition: NaryTree.h:78
sqdist
float sqdist(float x1, float y1, float x2, float y2)
Definition: NaryTree.cpp:101
SearchEnvironment< NaryState, NaryAction >::color
rgbColor color
Definition: SearchEnvironment.h:114
NaryTree::nodesAtDepth
std::vector< uint64_t > nodesAtDepth
Definition: NaryTree.h:79
Graphics::Display
Definition: Graphics.h:146
NaryTree::GCost
virtual double GCost(const NaryState &node1, const NaryState &node2) const
Definition: NaryTree.cpp:87
NaryTree::GetDepthRadius
float GetDepthRadius(int depth) const
Definition: NaryTree.cpp:245
NaryTree::GetActionHash
virtual uint64_t GetActionHash(NaryAction act) const
Definition: NaryTree.cpp:98
NaryTree::GetStateHash
virtual uint64_t GetStateHash(const NaryState &node) const
Definition: NaryTree.cpp:95
SearchEnvironment< NaryState, NaryAction >::GetColor
virtual rgbColor GetColor() const
Definition: SearchEnvironment.h:105
NaryTree::GoalTest
virtual bool GoalTest(const NaryState &node, const NaryState &goal) const
Definition: NaryTree.cpp:91
NaryState
uint64_t NaryState
Definition: NaryTree.h:18
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
NaryTree::InvertAction
virtual bool InvertAction(NaryAction &a) const
Definition: NaryTree.cpp:80
NaryAction
int NaryAction
Definition: NaryTree.h:19
NaryTree::GetNextState
virtual void GetNextState(const NaryState &, NaryAction, NaryState &) const
Definition: NaryTree.cpp:70
Graphics::Display::FillCircle
void FillCircle(rect r, rgbColor c)
Definition: Graphics.cpp:128
NaryTree::scaleWidth
double scaleWidth
Definition: NaryTree.h:81
Colors::gray
const rgbColor gray
Definition: Colors.h:121
NaryTree::GLDrawLine
void GLDrawLine(const NaryState &x, const NaryState &y) const
Definition: NaryTree.cpp:195
NaryTree::NaryTree
NaryTree(int branchingFactor, int depth)
Definition: NaryTree.cpp:14
NaryTree::GetSuccessors
virtual void GetSuccessors(const NaryState &nodeID, std::vector< NaryState > &neighbors) const
Definition: NaryTree.cpp:32
NaryTree::GetParent
NaryState GetParent(NaryState s) const
Definition: NaryTree.cpp:156
NaryTree::GetAction
virtual NaryAction GetAction(const NaryState &s1, const NaryState &s2) const
Definition: NaryTree.cpp:53
node
Nodes to be stored within a Graph.
Definition: Graph.h:170