HOG2
Directional2DEnvironment.h
Go to the documentation of this file.
1 /*
2  * Directional2DEnvironment.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 2/20/08.
6  * Copyright 2008 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 #ifndef DIRECTIONAL2DENVIRONMENT
11 #define DIRECTIONAL2DENVIRONMENT
12 
13 #include <stdint.h>
14 #include <iostream>
15 #include "Map.h"
16 #include "SearchEnvironment.h"
17 #include "UnitSimulation.h"
18 #include "ReservationProvider.h"
19 //#include "BitVector.h"
20 
21 #include <cassert>
22 
24  // base location {0, 0}
25  int8_t speed;
26  int8_t rotation;
27  std::vector<std::vector<float> > hTable;
28 };
29 
30 //struct heading {
31 //public:
32 // float x;
33 // float y;
34 // void normalize()
35 // {
36 // double length = sqrt(x * x + y * y);
37 // if (length != 0)
38 // { x /= length; y /= length; }
39 // else
40 // { x = 0; y = 0; }
41 // }
42 // void add(heading &h)
43 // { x += h.x; y += h.y; normalize(); }
44 // void turn(float angle) // in radians
45 // {
46 // float ang = atan(y/x);
47 // ang+=angle;
48 // x = cos(angle);
49 // y = sin(angle);
50 // }
51 //};
52 //
53 //struct xySpeedHeading {
54 //public:
55 // xySpeedHeading() {}
56 // xySpeedHeading(float _x, float _y) :x(_x), y(_y) {}
57 // uint8_t speed;
58 // heading h;
59 // float x;
60 // float y;
61 //};
62 //
63 //struct xySpeedHeading {
64 //public:
65 // xySpeedHeading() {}
66 // xySpeedHeading(float _x, float _y) :x(_x), y(_y) {}
67 // uint8_t speed;
68 // heading h;
69 // float x;
70 // float y;
71 //};
72 
73 
74 // action
76 public:
77  int8_t turn; // -3...3
78  int8_t speed; // +- some amount
79 };
80 
81 // state
83 public:
85  xySpeedHeading(float _x, float _y) :x(_x), y(_y), speed(0), rotation(0) { }
86  float x;
87  float y;
88  int8_t speed;
89  int8_t rotation;
90 };
91 
92 enum model {
95  kTank=2,
96  // the better tank is always moving speed 0, as it can speed up or slow down
97  // in one step anyway. So the regular tank has more states than necessary
99 };
100 
105 };
106 
107 class GoalTester {
108 public:
109  virtual ~GoalTester() {}
110  virtual bool goalTest(const xySpeedHeading &i1) const = 0;
111 };
112 
113 class Directional2DEnvironment : public SearchEnvironment<xySpeedHeading, deltaSpeedHeading>
114 {
115 public:
117  virtual ~Directional2DEnvironment();
118  void GetSuccessors(const xySpeedHeading &nodeID, std::vector<xySpeedHeading> &neighbors) const;
119  void GetActions(const xySpeedHeading &nodeID, std::vector<deltaSpeedHeading> &actions) const;
120  deltaSpeedHeading GetAction(const xySpeedHeading &s1, const xySpeedHeading &s2) const;
121  virtual void ApplyAction(xySpeedHeading &s, deltaSpeedHeading dir) const;
122  virtual void UndoAction(xySpeedHeading &s, deltaSpeedHeading dir) const;
124 
125  virtual bool InvertAction(deltaSpeedHeading &a) const;
126 
127  virtual double HCost(const xySpeedHeading &node1, const xySpeedHeading &node2) const;
128  virtual double HCost(const xySpeedHeading &) const { assert(false); return 0; }
129  virtual double GCost(const xySpeedHeading &node1, const xySpeedHeading &node2) const;
130  virtual double GCost(const xySpeedHeading &node1, const deltaSpeedHeading &act) const;
131 
132  int GetNumAngles();
133 
134  void SetGoalTest(GoalTester *t) {test = t;}
135  bool GoalTest(const xySpeedHeading &node, const xySpeedHeading &goal) const;
136  bool GoalTest(const xySpeedHeading &) const { assert(false); return false; }
137  uint64_t GetStateHash(const xySpeedHeading &node) const;
138  uint64_t GetActionHash(deltaSpeedHeading act) const;
139  virtual void OpenGLDraw() const;
140  virtual void OpenGLDraw(const xySpeedHeading &l) const;
141  virtual void OpenGLDraw(const xySpeedHeading& oldState, const xySpeedHeading &newState, float perc) const;
142  virtual void OpenGLDraw(const xySpeedHeading &, const deltaSpeedHeading &) const;
143  void GLDrawLine(const xySpeedHeading &a, const xySpeedHeading &b) const;
144 
145  Map* GetMap() { return map; }
146  void SetHeuristicType(heuristicType theType) { hType = theType; }
147  virtual void GetNextState(const xySpeedHeading &currents, deltaSpeedHeading dir, xySpeedHeading &news) const;
148 private:
149  bool Legal(const xySpeedHeading &node1, const deltaSpeedHeading &act) const;
150 
152 // void BuildAStarTable();
153  void BuildAngleTables();
154  float mySin(int dir) const;
155  float myCos(int dir) const;
156  float LookupStateHash(const xySpeedHeading &s, const dirHeuristicTable &t) const;
157  bool LookupStateHashIndex(const xySpeedHeading &s, int &index1, int &index2) const;
158  float LookupStateHeuristic(const xySpeedHeading &s1, const xySpeedHeading &s2) const;
159  void RotateCCW(xySpeedHeading &s, unsigned int rotation) const;
164 protected:
165  std::vector<dirHeuristicTable> heuristics;
166 // std::vector<std::vector<float> > hTable;
167 // std::vector<std::vector<std::vector<float> > > hTable;
168  std::vector<float> cosTable;
169  std::vector<float> sinTable;
171 };
172 
174 
175 static bool operator==(const xySpeedHeading &l1, const xySpeedHeading &l2)
176 {
177  if (l1.speed != l2.speed)
178  return false;
179  if (l1.rotation != l2.rotation)
180  return false;
181  if ((floorf(l1.x) != floorf(l2.x)) && (!fequal(l1.x, l2.x)))
182  return false;
183  if ((floorf(l1.y) != floorf(l2.y)) && (!fequal(l1.y, l2.y)))
184  return false;
185  return true;
186 }
187 
188 static bool operator==(const deltaSpeedHeading &l1, const deltaSpeedHeading &l2)
189 {
190  if (l1.speed != l2.speed)
191  return false;
192  if (l1.turn != l2.turn)
193  return false;
194  return true;
195 }
196 
197 static std::ostream& operator <<(std::ostream &out, const xySpeedHeading &loc)
198 {
199  out << "(" << loc.x << ", " << loc.y << ")";
200  out << "[" << (int)loc.speed << ":" << (int)loc.rotation << "]";
201  return out;
202 }
203 
204 static std::ostream& operator <<(std::ostream &out, const deltaSpeedHeading &loc)
205 {
206  out << "{" << ((int)loc.turn) << "|" << ((int)loc.speed) << "}";
207  return out;
208 }
209 
210 #endif
dirHeuristicTable::speed
int8_t speed
Definition: Directional2DEnvironment.h:25
loc::x
int x
Definition: MapGenerators.cpp:296
deltaSpeedHeading::turn
int8_t turn
Definition: Directional2DEnvironment.h:77
dirHeuristicTable
Definition: Directional2DEnvironment.h:23
UnitSimulation.h
Directional2DEnvironment::hType
heuristicType hType
Definition: Directional2DEnvironment.h:163
kHumanoid
@ kHumanoid
Definition: Directional2DEnvironment.h:93
xySpeedHeading::speed
int8_t speed
Definition: Directional2DEnvironment.h:88
Directional2DEnvironment::cosTable
std::vector< float > cosTable
Definition: Directional2DEnvironment.h:168
Directional2DEnvironment::GetStateHash
uint64_t GetStateHash(const xySpeedHeading &node) const
Definition: Directional2DEnvironment.cpp:620
loc::y
int y
Definition: MapGenerators.cpp:296
dirHeuristicTable::hTable
std::vector< std::vector< float > > hTable
Definition: Directional2DEnvironment.h:27
Directional2DEnvironment::GetNumAngles
int GetNumAngles()
Definition: Directional2DEnvironment.cpp:45
GoalTester::~GoalTester
virtual ~GoalTester()
Definition: Directional2DEnvironment.h:109
DirectionSimulation
UnitSimulation< xySpeedHeading, deltaSpeedHeading, Directional2DEnvironment > DirectionSimulation
Definition: Directional2DEnvironment.h:173
kVehicle
@ kVehicle
Definition: Directional2DEnvironment.h:94
Directional2DEnvironment::HCost
virtual double HCost(const xySpeedHeading &) const
Heuristic value between node and the stored goal.
Definition: Directional2DEnvironment.h:128
kBetterTank
@ kBetterTank
Definition: Directional2DEnvironment.h:98
Directional2DEnvironment::GetActionHash
uint64_t GetActionHash(deltaSpeedHeading act) const
Definition: Directional2DEnvironment.cpp:634
model
model
Definition: Directional2DEnvironment.h:92
Directional2DEnvironment::GetActions
void GetActions(const xySpeedHeading &nodeID, std::vector< deltaSpeedHeading > &actions) const
Definition: Directional2DEnvironment.cpp:71
Directional2DEnvironment::GetOccupancyInfo
virtual OccupancyInterface< xySpeedHeading, deltaSpeedHeading > * GetOccupancyInfo()
Definition: Directional2DEnvironment.h:123
Directional2DEnvironment::BuildAngleTables
void BuildAngleTables()
Definition: Directional2DEnvironment.cpp:1058
rotation
float rotation[3][3]
Definition: RC.cpp:21
Directional2DEnvironment::Legal
bool Legal(const xySpeedHeading &node1, const deltaSpeedHeading &act) const
Definition: Directional2DEnvironment.cpp:500
kTank
@ kTank
Definition: Directional2DEnvironment.h:95
Directional2DEnvironment::GoalTest
bool GoalTest(const xySpeedHeading &node, const xySpeedHeading &goal) const
Definition: Directional2DEnvironment.cpp:611
operator<<
static std::ostream & operator<<(std::ostream &out, const xySpeedHeading &loc)
Definition: Directional2DEnvironment.h:197
Directional2DEnvironment::InvertAction
virtual bool InvertAction(deltaSpeedHeading &a) const
Definition: Directional2DEnvironment.cpp:251
xySpeedHeading
Definition: Directional2DEnvironment.h:82
xySpeedHeading::x
float x
Definition: Directional2DEnvironment.h:86
Directional2DEnvironment::UndoAction
virtual void UndoAction(xySpeedHeading &s, deltaSpeedHeading dir) const
Definition: Directional2DEnvironment.cpp:380
Directional2DEnvironment::GoalTest
bool GoalTest(const xySpeedHeading &) const
Goal Test if the goal is stored.
Definition: Directional2DEnvironment.h:136
kOctileHeuristic
@ kOctileHeuristic
Definition: Directional2DEnvironment.h:102
Directional2DEnvironment::SetGoalTest
void SetGoalTest(GoalTester *t)
Definition: Directional2DEnvironment.h:134
kExtendedPerimeterHeuristic
@ kExtendedPerimeterHeuristic
Definition: Directional2DEnvironment.h:104
Directional2DEnvironment::map
Map * map
Definition: Directional2DEnvironment.h:170
Directional2DEnvironment
Definition: Directional2DEnvironment.h:113
loc
Definition: MapGenerators.cpp:296
Directional2DEnvironment::HCost
virtual double HCost(const xySpeedHeading &node1, const xySpeedHeading &node2) const
Heuristic value between two arbitrary nodes.
Definition: Directional2DEnvironment.cpp:486
Directional2DEnvironment::GCost
virtual double GCost(const xySpeedHeading &node1, const xySpeedHeading &node2) const
Definition: Directional2DEnvironment.cpp:566
xySpeedHeading::xySpeedHeading
xySpeedHeading()
Definition: Directional2DEnvironment.h:84
Directional2DEnvironment::heuristics
std::vector< dirHeuristicTable > heuristics
Definition: Directional2DEnvironment.h:165
Directional2DEnvironment::~Directional2DEnvironment
virtual ~Directional2DEnvironment()
Definition: Directional2DEnvironment.cpp:40
Directional2DEnvironment::GLDrawLine
void GLDrawLine(const xySpeedHeading &a, const xySpeedHeading &b) const
Definition: Directional2DEnvironment.cpp:745
xySpeedHeading::y
float y
Definition: Directional2DEnvironment.h:87
dirHeuristicTable::rotation
int8_t rotation
Definition: Directional2DEnvironment.h:26
Directional2DEnvironment::motionModel
model motionModel
Definition: Directional2DEnvironment.h:162
Directional2DEnvironment::OpenGLDraw
virtual void OpenGLDraw() const
Definition: Directional2DEnvironment.cpp:639
Directional2DEnvironment::Directional2DEnvironment
Directional2DEnvironment(Map *m, model envType=kVehicle, heuristicType heuristic=kExtendedPerimeterHeuristic)
Definition: Directional2DEnvironment.cpp:18
Directional2DEnvironment::GetNextState
virtual void GetNextState(const xySpeedHeading &currents, deltaSpeedHeading dir, xySpeedHeading &news) const
Definition: Directional2DEnvironment.cpp:761
Directional2DEnvironment::myCos
float myCos(int dir) const
Definition: Directional2DEnvironment.cpp:1077
deltaSpeedHeading::speed
int8_t speed
Definition: Directional2DEnvironment.h:78
heuristicType
heuristicType
Definition: Directional2DEnvironment.h:101
Directional2DEnvironment::RotateCCW
void RotateCCW(xySpeedHeading &s, unsigned int rotation) const
Definition: Directional2DEnvironment.cpp:263
Directional2DEnvironment::LookupStateHashIndex
bool LookupStateHashIndex(const xySpeedHeading &s, int &index1, int &index2) const
Definition: Directional2DEnvironment.cpp:890
Directional2DEnvironment::ApplyAction
virtual void ApplyAction(xySpeedHeading &s, deltaSpeedHeading dir) const
Definition: Directional2DEnvironment.cpp:273
GoalTester::goalTest
virtual bool goalTest(const xySpeedHeading &i1) const =0
GoalTester
Definition: Directional2DEnvironment.h:107
Directional2DEnvironment::mySin
float mySin(int dir) const
Definition: Directional2DEnvironment.cpp:1072
ReservationProvider.h
UnitSimulation
The basic simulation class for the world.
Definition: UnitSimulation.h:85
deltaSpeedHeading
Definition: Directional2DEnvironment.h:75
xySpeedHeading::rotation
int8_t rotation
Definition: Directional2DEnvironment.h:89
kPerimeterHeuristic
@ kPerimeterHeuristic
Definition: Directional2DEnvironment.h:103
Directional2DEnvironment::SetHeuristicType
void SetHeuristicType(heuristicType theType)
Definition: Directional2DEnvironment.h:146
xySpeedHeading::xySpeedHeading
xySpeedHeading(float _x, float _y)
Definition: Directional2DEnvironment.h:85
Directional2DEnvironment::BuildHTable
void BuildHTable(dirHeuristicTable &t)
Definition: Directional2DEnvironment.cpp:772
Map.h
Directional2DEnvironment::test
GoalTester * test
Definition: Directional2DEnvironment.h:161
Directional2DEnvironment::LookupStateHash
float LookupStateHash(const xySpeedHeading &s, const dirHeuristicTable &t) const
Definition: Directional2DEnvironment.cpp:908
fequal
bool fequal(double a, double b, double tolerance=TOLERANCE)
Definition: FPUtil.h:32
Directional2DEnvironment::checkLegal
bool checkLegal
Definition: Directional2DEnvironment.h:160
Directional2DEnvironment::sinTable
std::vector< float > sinTable
Definition: Directional2DEnvironment.h:169
SearchEnvironment
Definition: SearchEnvironment.h:30
Directional2DEnvironment::GetSuccessors
void GetSuccessors(const xySpeedHeading &nodeID, std::vector< xySpeedHeading > &neighbors) const
Definition: Directional2DEnvironment.cpp:58
Directional2DEnvironment::GetAction
deltaSpeedHeading GetAction(const xySpeedHeading &s1, const xySpeedHeading &s2) const
Definition: Directional2DEnvironment.cpp:231
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
SearchEnvironment.h
Directional2DEnvironment::GetMap
Map * GetMap()
Definition: Directional2DEnvironment.h:145
Directional2DEnvironment::LookupStateHeuristic
float LookupStateHeuristic(const xySpeedHeading &s1, const xySpeedHeading &s2) const
Definition: Directional2DEnvironment.cpp:927
OccupancyInterface
Definition: OccupancyInterface.h:36
operator==
static bool operator==(const xySpeedHeading &l1, const xySpeedHeading &l2)
Definition: Directional2DEnvironment.h:175