HOG2
MapSectorAbstraction.cpp
Go to the documentation of this file.
1 /*
2  * $Id: MapSectorAbstraction.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 8/8/05.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "MapSectorAbstraction.h"
13 #include "Graph.h"
14 
15 using namespace GraphAbstractionConstants;
16 
17 
18 
19 MapSectorAbstraction::MapSectorAbstraction(Map *_m, int _sectorSize, int _sectorMultiplier)
20 :MapAbstraction(_m), sectorSize(_sectorSize), sectorMultiplier(_sectorMultiplier)
21 {
22  assert(_sectorSize>1);
23  assert(_sectorMultiplier>1);
25 }
26 
28 :MapAbstraction(_m), sectorSize(_sectorSize), sectorMultiplier(_sectorSize)
29 {
30  assert(_sectorSize>1);
32 }
33 
35 {
36 }
37 
39 {
40  while (from != to)
41  {
42  if ((!from) || (!to) ||
43  (abstractions[from->GetLabelL(kAbstractionLevel)]->GetNumEdges() == 0))
44  return false;
45 
46  from = abstractions[from->GetLabelL(kAbstractionLevel)+1]->
47  GetNode(from->GetLabelL(kParent));
48  to = abstractions[to->GetLabelL(kAbstractionLevel)+1]->
49  GetNode(to->GetLabelL(kParent));
50  }
51  if ((from == 0) || (to == 0))
52  return false;
53  return true;
54 }
55 
56 // utility functions
59 {
60 }
61 
62 // hierarchical modifications
65 {
66  assert(false);
67 }
68 
71 {
72  assert(false);
73 }
74 
77 {
78  // add it to the
79  assert(false);
80 }
81 
83 void MapSectorAbstraction::AddEdge(edge *, unsigned int)
84 {
85  // if each end is already connected who cares -- doesn't mess anything up -- just add it throughout
86  assert(false);
87 }
88 
92 {
93 }
94 
96 {
97  //inefficient for the moment
98  abstractions.push_back(GetMapGraph(GetMap()));
99  while (abstractions.back()->GetNumEdges() > 0)
100  {
101  Graph *g = new Graph();
102  addNodes(g);
103  addEdges(g);
104  abstractions.push_back(g);
105  }
106 }
107 
109 {
110  node_iterator ni = abstractions.back()->getNodeIter();
111  for (node *next = abstractions.back()->nodeIterNext(ni); next;
112  next = abstractions.back()->nodeIterNext(ni))
113  {
114  // if it isn't abstracted, do a bfs according to the quadrant and abstract these nodes together
115  if (next->GetLabelL(kParent) == -1)
116  {
117  node *parent;
118  g->AddNode(parent = new node("??"));
119  parent->SetLabelL(kAbstractionLevel, next->GetLabelL(kAbstractionLevel)+1); // level in abstraction tree
120  parent->SetLabelL(kNumAbstractedNodes, 0); // number of abstracted nodes
121  parent->SetLabelL(kParent, -1); // parent of this node in abstraction hierarchy
123  parent->SetLabelL(kNodeBlocked, 0);
124  abstractionBFS(next, parent, getQuadrant(next));
125  }
126  }
127 }
128 
130 {
131  Graph *g = abstractions.back();
132  edge_iterator ei = g->getEdgeIter();
133  for (edge *e = g->edgeIterNext(ei); e; e = g->edgeIterNext(ei))
134  {
135 
136  int from = g->GetNode(e->getFrom())->GetLabelL(kParent);
137  int to = g->GetNode(e->getTo())->GetLabelL(kParent);
138  edge *f=0;
139 
140  if ((from != to) && (!(f = aGraph->FindEdge(to, from))))
141  {
142  double weight = h(aGraph->GetNode(from), aGraph->GetNode(to));
143  f = new edge(from, to, weight);
144  f->SetLabelL(kEdgeCapacity, 1);
145  aGraph->AddEdge(f);
146  }
147  else if (f) f->SetLabelL(kEdgeCapacity, f->GetLabelL(kEdgeCapacity)+1);
148  }
149 }
150 
151 void MapSectorAbstraction::abstractionBFS(node *which, node *parent, int quadrant) // depth in edges...should we try literal distance?
152 {
153  if ((which == 0) || (which->GetLabelL(kParent) != -1) || (getQuadrant(which) != quadrant))
154  return;
155  buildNodeIntoParent(which, parent);
156 
157  neighbor_iterator ni = which->getNeighborIter();
158  for (long tmp = which->nodeNeighborNext(ni); tmp != -1; tmp = which->nodeNeighborNext(ni))
159  {
160  abstractionBFS(abstractions.back()->GetNode(tmp), parent, quadrant);
161  }
162 }
163 
165 {
166  assert(GetAbstractionLevel(n)+1 == GetAbstractionLevel(parent));
167  n->SetLabelL(kParent, parent->GetNum());
168  parent->SetLabelL(kFirstData+parent->GetLabelL(kNumAbstractedNodes), n->GetNum());
171 }
172 
174 { // int sectorSize;
175  // 1. get any child
176  node *child = which;
177  while (child->GetLabelL(kAbstractionLevel) != 0)
178  child = GetNthChild(child, 0);
179 
180  int xloc = child->GetLabelL(kFirstData); // x loc in map
181  int yloc = child->GetLabelL(kFirstData+1); // y loc in map
182 
183  int level = which->GetLabelL(kAbstractionLevel);
184 // int absSectorSize = (int)pow((double)sectorSize, (double)level+1);
185  int absSectorSize = (int)sectorSize*pow((double)sectorMultiplier, (double)level);
186 
187  int sectorNum = (GetMap()->GetMapWidth()/absSectorSize)*(yloc/absSectorSize)+(xloc/absSectorSize);
188 
189  return sectorNum;
190 }
191 
192 //
193 // 0 1 2 3 ...
194 // w w+1 w+2 w+3 ...
195 
196 // rowSector = (lowerSector%width)/sectorSize
197 // colSector = (lowerSector/width)/sectorSize
198 
199 // or
200 
201 // size at level = sectorSize^level
202 // low-level-size-x%sizeatlevel
203 // low-level-size-y%sizeatlevel
Graph::AddNode
int AddNode(node *)
Definition: Graph.cpp:136
GraphAbstractionConstants
Definition: GraphAbstraction.h:22
node::SetLabelL
void SetLabelL(unsigned int index, long val) const
Definition: Graph.cpp:702
MapSectorAbstraction::Pathable
virtual bool Pathable(node *from, node *to)
Definition: MapSectorAbstraction.cpp:38
Graph.h
node::SetLabelF
void SetLabelF(unsigned int index, double val) const
Definition: Graph.cpp:687
Graph::FindEdge
edge * FindEdge(unsigned int from, unsigned int to)
Finds an edge between nodes with ids from and to, no matter which direction.
Definition: Graph.cpp:230
neighbor_iterator
unsigned int neighbor_iterator
Definition: Graph.h:34
MapSectorAbstraction::AddEdge
virtual void AddEdge(edge *e, unsigned int absLevel)
add edge to abstraction
Definition: MapSectorAbstraction.cpp:83
MapSectorAbstraction::MapSectorAbstraction
MapSectorAbstraction(Map *, int, int)
Creat a SectorAbstraction of the map.
Definition: MapSectorAbstraction.cpp:19
MapSectorAbstraction::getQuadrant
int getQuadrant(node *which)
Definition: MapSectorAbstraction.cpp:173
MapSectorAbstraction::sectorSize
int sectorSize
Definition: MapSectorAbstraction.h:52
MapSectorAbstraction::~MapSectorAbstraction
~MapSectorAbstraction()
Definition: MapSectorAbstraction.cpp:34
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
edge_iterator
std::vector< edge * >::const_iterator edge_iterator
Definition: Graph.h:30
GraphAbstractionConstants::kEdgeCapacity
@ kEdgeCapacity
Definition: GraphAbstraction.h:53
MapSectorAbstraction::AddNode
virtual void AddNode(node *n)
add node to abstraction
Definition: MapSectorAbstraction.cpp:76
Graph
A generic Graph class.
Definition: Graph.h:66
MapSectorAbstraction::buildNodeIntoParent
void buildNodeIntoParent(node *n, node *parent)
Definition: MapSectorAbstraction.cpp:164
Graph::GetNode
node * GetNode(unsigned long num)
Definition: Graph.cpp:152
Graph::edgeIterNext
edge * edgeIterNext(edge_iterator &) const
Definition: Graph.cpp:320
MapSectorAbstraction::RemoveNode
virtual void RemoveNode(node *n)
remove node from abstraction
Definition: MapSectorAbstraction.cpp:64
node_iterator
std::vector< node * >::const_iterator node_iterator
Definition: Graph.h:33
MapSectorAbstraction::RemoveEdge
virtual void RemoveEdge(edge *e, unsigned int absLevel)
remove edge from abstraction
Definition: MapSectorAbstraction.cpp:70
MapSectorAbstraction::buildAbstraction
void buildAbstraction()
Definition: MapSectorAbstraction.cpp:95
GraphAbstractionConstants::kNumAbstractedNodes
@ kNumAbstractedNodes
Definition: GraphAbstraction.h:26
edge::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:138
GetMapGraph
Graph * GetMapGraph(Map *m)
GetMapGraph(map)
Definition: MapAbstraction.cpp:312
MapSectorAbstraction.h
Graph::getEdgeIter
edge_iterator getEdgeIter() const
Definition: Graph.cpp:315
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
edge::SetLabelL
void SetLabelL(unsigned int index, long val)
Definition: Graph.cpp:598
MapSectorAbstraction::addNodes
void addNodes(Graph *g)
Definition: MapSectorAbstraction.cpp:108
MapSectorAbstraction::addEdges
void addEdges(Graph *g)
Definition: MapSectorAbstraction.cpp:129
GraphAbstractionConstants::kAbstractionLevel
@ kAbstractionLevel
Definition: GraphAbstraction.h:25
MapSectorAbstraction::VerifyHierarchy
virtual void VerifyHierarchy()
verify that the hierarchy is consistent
Definition: MapSectorAbstraction.cpp:58
node::nodeNeighborNext
int nodeNeighborNext(neighbor_iterator &) const
Definition: Graph.cpp:807
MapSectorAbstraction::RepairAbstraction
virtual void RepairAbstraction()
This must be called after any of the above add/remove operations.
Definition: MapSectorAbstraction.cpp:91
GraphAbstractionConstants::kXCoordinate
@ kXCoordinate
Definition: GraphAbstraction.h:30
MapSectorAbstraction::abstractionBFS
void abstractionBFS(node *which, node *parent, int quadrant)
Definition: MapSectorAbstraction.cpp:151
GraphAbstractionConstants::kNodeBlocked
@ kNodeBlocked
Definition: GraphAbstraction.h:33
node::GetNum
unsigned int GetNum() const
Definition: Graph.h:176
GraphAbstractionConstants::kParent
@ kParent
Definition: GraphAbstraction.h:27
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
MapSectorAbstraction::sectorMultiplier
int sectorMultiplier
Definition: MapSectorAbstraction.h:52
node::getNeighborIter
neighbor_iterator getNeighborIter() const
Definition: Graph.cpp:802
GraphAbstractionConstants::kUnknownPosition
const double kUnknownPosition
Definition: GraphAbstraction.h:56
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129