HOG2
CanonicalGrid.h
Go to the documentation of this file.
1 //
2 // CanonicalGrid.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 7/30/15.
6 // Copyright (c) 2015 University of Denver. All rights reserved.
7 //
8 
9 #ifndef __hog2_glut__CanonicalGrid__
10 #define __hog2_glut__CanonicalGrid__
11 
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <iostream>
16 #include <string.h>
17 #include "Map.h"
18 #include "SearchEnvironment.h"
19 #include "UnitSimulation.h"
20 #include <cassert>
21 #include "Graphics.h"
22 
23 namespace CanonicalGrid {
24 
25  enum tDirection {
26  kN=0x8, kS=0x4, kE=0x2, kW=0x1, kNW=kN|kW, kNE=kN|kE,
28  };
29 
30  struct xyLoc {
31  public:
32  xyLoc() { x = -1; y = -1; parent = kAll; }
33  xyLoc(uint16_t _x, uint16_t _y, tDirection _parent = kAll) :x(_x), y(_y), parent(_parent) {}
34  uint16_t x;
35  uint16_t y;
37  };
38 
39  static std::ostream& operator <<(std::ostream & out, const xyLoc &loc)
40  {
41  out << "(" << loc.x << ", " << loc.y << " : " << loc.parent << ")";
42  return out;
43  }
44 
45  static bool operator==(const xyLoc &l1, const xyLoc &l2) {
46  return (l1.x == l2.x) && (l1.y == l2.y);
47  }
48 
49  static bool operator!=(const xyLoc &l1, const xyLoc &l2) {
50  return (l1.x != l2.x) || (l1.y != l2.y);
51  }
52 
53  class CanonicalGrid : public SearchEnvironment<xyLoc, tDirection>
54  {
55  public:
56  CanonicalGrid(Map *m);
57  void GetSuccessors(const xyLoc &nodeID, std::vector<xyLoc> &neighbors) const;
58  void GetActions(const xyLoc &nodeID, std::vector<tDirection> &actions) const;
59  tDirection GetAction(const xyLoc &s1, const xyLoc &s2) const;
60  void ApplyAction(xyLoc &s, tDirection dir) const;
61 
62  bool InvertAction(tDirection &a) const;
63 
64  double HCost(const xyLoc &) const {
65  fprintf(stderr, "ERROR: Single State HCost not implemented for CanonicalGrid\n");
66  exit(1); return -1.0;}
67  double HCost(const xyLoc &node1, const xyLoc &node2) const;
68  double GCost(const xyLoc &node1, const xyLoc &node2) const;
69  double GCost(const xyLoc &node1, const tDirection &act) const;
70  bool GoalTest(const xyLoc &node, const xyLoc &goal) const;
71 
72  bool GoalTest(const xyLoc &) const {
73  fprintf(stderr, "ERROR: Single State Goal Test not implemented for CanonicalGrid\n");
74  exit(1); return false;}
75 
76  uint64_t GetMaxHash() const;
77  uint64_t GetStateHash(const xyLoc &node) const;
78  uint64_t GetActionHash(tDirection act) const;
79  void OpenGLDraw() const;
80  void OpenGLDraw(const xyLoc &l) const;
81  void OpenGLDraw(const xyLoc &l1, const xyLoc &l2, float v) const;
82  void OpenGLDraw(const xyLoc &, const tDirection &) const;
83  void GLLabelState(const xyLoc &, const char *) const;
84  void GLLabelState(const xyLoc &s, const char *str, double scale) const;
85  void GLDrawLine(const xyLoc &x, const xyLoc &y) const;
86  Map* GetMap() const { return map; }
87 
88  std::string SVGHeader();
89  std::string SVGDraw();
90  std::string SVGDraw(const xyLoc &);
91  std::string SVGLabelState(const xyLoc &, const char *, double scale) const;
92  std::string SVGDrawLine(const xyLoc &x, const xyLoc &y, int width=1) const;
93  std::string SVGFrameRect(int left, int top, int right, int bottom, int width = 1);
94 
95  virtual void Draw(Graphics::Display &disp) const;
96  virtual void Draw(Graphics::Display &disp, const xyLoc &l) const;
97  virtual void DrawAlternate(Graphics::Display &disp, const xyLoc &l) const;
98  virtual void Draw(Graphics::Display &disp, const xyLoc &l1, const xyLoc &l2, float v) const;
99  virtual void DrawStateLabel(Graphics::Display &disp, const xyLoc &l1, const char *txt) const;
100  virtual void DrawStateLabel(Graphics::Display &disp, const xyLoc &l1, const xyLoc &l2, float v, const char *txt) const;
101  virtual void DrawLine(Graphics::Display &disp, const xyLoc &x, const xyLoc &y, double width = 1.0) const;
102  virtual void DrawArrow(Graphics::Display &disp, const xyLoc &x, const xyLoc &y, double width = 1.0) const;
103 
104  void DrawBasicOrdering(::Graphics::Display &disp, const xyLoc l) const; // Only draws canonical ordering
105  void DrawOrdering(::Graphics::Display &disp, const xyLoc l) const; // Only draws canonical ordering
106  void GetFirstJumpPoints(const xyLoc &nodeID, std::vector<xyLoc> &neighbors) const;
107 
108  void GetNextState(const xyLoc &currents, tDirection dir, xyLoc &news) const;
109 
110  void StoreGoal(xyLoc &) {} // stores the locations for the given goal state
111  void ClearGoal() {}
112  bool IsGoalStored() const {return false;}
113  void SetDiagonalCost(double val) { DIAGONAL_COST = val; }
114  double GetDiagonalCost() { return DIAGONAL_COST; }
115  bool FourConnected() { return fourConnected; }
116  bool EightConnected() { return !fourConnected; }
117  void SetFourConnected() { fourConnected = true; }
118  void SetEightConnected() { fourConnected = false; }
119  protected:
120  void GetBasicSuccessors(const xyLoc &nodeID, std::vector<xyLoc> &neighbors) const;
124  std::vector<bool> grid;
125  };
126 
128 }
129 
130 
131 
132 #endif /* defined(__hog2_glut__CanonicalGrid__) */
loc::x
int x
Definition: MapGenerators.cpp:296
UnitSimulation.h
CanonicalGrid::CanonicalGrid::GoalTest
bool GoalTest(const xyLoc &) const
Definition: CanonicalGrid.h:72
CanonicalGrid::kW
@ kW
Definition: CanonicalGrid.h:26
CanonicalGrid::kSE
@ kSE
Definition: CanonicalGrid.h:27
CanonicalGrid::CanonicalGrid::SetDiagonalCost
void SetDiagonalCost(double val)
Definition: CanonicalGrid.h:113
loc::y
int y
Definition: MapGenerators.cpp:296
CanonicalGrid::kN
@ kN
Definition: CanonicalGrid.h:26
CanonicalGrid::kE
@ kE
Definition: CanonicalGrid.h:26
CanonicalGrid::xyLoc::xyLoc
xyLoc(uint16_t _x, uint16_t _y, tDirection _parent=kAll)
Definition: CanonicalGrid.h:33
CanonicalGrid::kSW
@ kSW
Definition: CanonicalGrid.h:27
CanonicalGrid::operator<<
static std::ostream & operator<<(std::ostream &out, const xyLoc &loc)
Definition: CanonicalGrid.h:39
CanonicalGrid::CanonicalGrid::IsGoalStored
bool IsGoalStored() const
Definition: CanonicalGrid.h:112
CanonicalGrid::CanonicalGrid::FourConnected
bool FourConnected()
Definition: CanonicalGrid.h:115
width
int width
Definition: SFML_HOG.cpp:54
CanonicalGrid::tDirection
tDirection
Definition: CanonicalGrid.h:25
CanonicalGrid::xyLoc::xyLoc
xyLoc()
Definition: CanonicalGrid.h:32
CanonicalGrid::operator!=
static bool operator!=(const xyLoc &l1, const xyLoc &l2)
Definition: CanonicalGrid.h:49
CanonicalGrid::CanonicalGrid::SetEightConnected
void SetEightConnected()
Definition: CanonicalGrid.h:118
CanonicalGrid::operator==
static bool operator==(const xyLoc &l1, const xyLoc &l2)
Definition: CanonicalGrid.h:45
CanonicalGrid::UnitMapSimulation
UnitSimulation< xyLoc, tDirection, CanonicalGrid > UnitMapSimulation
Definition: CanonicalGrid.h:127
CanonicalGrid::kNW
@ kNW
Definition: CanonicalGrid.h:26
loc
Definition: MapGenerators.cpp:296
CanonicalGrid::CanonicalGrid::SetFourConnected
void SetFourConnected()
Definition: CanonicalGrid.h:117
Graphics::Display
Definition: Graphics.h:146
CanonicalGrid::CanonicalGrid::fourConnected
bool fourConnected
Definition: CanonicalGrid.h:123
CanonicalGrid
Definition: CanonicalGrid.cpp:14
CanonicalGrid::CanonicalGrid::GetMap
Map * GetMap() const
Definition: CanonicalGrid.h:86
CanonicalGrid::CanonicalGrid::GetDiagonalCost
double GetDiagonalCost()
Definition: CanonicalGrid.h:114
CanonicalGrid::kS
@ kS
Definition: CanonicalGrid.h:26
Graphics.h
CanonicalGrid::xyLoc
Definition: CanonicalGrid.h:30
CanonicalGrid::kStay
@ kStay
Definition: CanonicalGrid.h:27
CanonicalGrid::CanonicalGrid::grid
std::vector< bool > grid
Definition: CanonicalGrid.h:124
SVGDrawLine
std::string SVGDrawLine(float x1, float y1, float x2, float y2, float width, rgbColor c)
Definition: SVGUtil.cpp:192
UnitSimulation
The basic simulation class for the world.
Definition: UnitSimulation.h:85
CanonicalGrid::kNE
@ kNE
Definition: CanonicalGrid.h:26
CanonicalGrid::CanonicalGrid::DIAGONAL_COST
double DIAGONAL_COST
Definition: CanonicalGrid.h:122
CanonicalGrid::xyLoc::y
uint16_t y
Definition: CanonicalGrid.h:35
CanonicalGrid::CanonicalGrid::ClearGoal
void ClearGoal()
Clears the goal from memory.
Definition: CanonicalGrid.h:111
CanonicalGrid::CanonicalGrid::EightConnected
bool EightConnected()
Definition: CanonicalGrid.h:116
Map.h
CanonicalGrid::CanonicalGrid::StoreGoal
void StoreGoal(xyLoc &)
Definition: CanonicalGrid.h:110
CanonicalGrid::xyLoc::parent
tDirection parent
Definition: CanonicalGrid.h:36
CanonicalGrid::CanonicalGrid::HCost
double HCost(const xyLoc &) const
Definition: CanonicalGrid.h:64
CanonicalGrid::CanonicalGrid::map
Map * map
Definition: CanonicalGrid.h:121
SearchEnvironment
Definition: SearchEnvironment.h:30
CanonicalGrid::xyLoc::x
uint16_t x
Definition: CanonicalGrid.h:34
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
SVGFrameRect
std::string SVGFrameRect(float x, float y, float width, float height, float border, rgbColor c)
Definition: SVGUtil.cpp:99
SearchEnvironment.h
CanonicalGrid::kAll
@ kAll
Definition: CanonicalGrid.h:27