HOG2
OldSearchEnvironment.cpp
Go to the documentation of this file.
1 /*
2  * SearchEnvironment.cpp
3  * hog
4  *
5  * Created by Nathan Sturtevant on 4/5/07.
6  * Copyright 2007 Nathan Sturtevant, University of Alberta. All rights reserved.
7  *
8  */
9 
10 #include "OldSearchEnvironment.h"
11 
12 namespace OldSearchCode {
13 
14  void MapSearchEnvironment::getNeighbors(uint32_t nodeID, std::vector<uint32_t> &neighbors)
15  {
16  int x1, y1;
17  bool up=false, down=false;
18  x1 = nodeID>>16; y1 = nodeID&0xFFFF;
19  if ((map->GetTerrainType(x1, y1+1) == kGround))
20  {
21  down = true;
22  neighbors.push_back((x1<<16)|(y1+1));
23  }
24  if ((map->GetTerrainType(x1, y1-1) == kGround))
25  {
26  up = true;
27  neighbors.push_back((x1<<16)|(y1-1));
28  }
29  if ((map->GetTerrainType(x1-1, y1) == kGround))
30  {
31  if ((up && (map->GetTerrainType(x1-1, y1-1) == kGround)))
32  neighbors.push_back(((x1-1)<<16)|(y1-1));
33  if ((down && (map->GetTerrainType(x1-1, y1+1) == kGround)))
34  neighbors.push_back(((x1-1)<<16)|(y1+1));
35  neighbors.push_back(((x1-1)<<16)| y1);
36  }
37  if ((map->GetTerrainType(x1+1, y1) == kGround))
38  {
39  if ((up && (map->GetTerrainType(x1+1, y1-1) == kGround)))
40  neighbors.push_back(((x1+1)<<16)|(y1-1));
41  if ((down && (map->GetTerrainType(x1+1, y1+1) == kGround)))
42  neighbors.push_back(((x1+1)<<16)|(y1+1));
43  neighbors.push_back(((x1+1)<<16)| y1);
44  }
45  }
46 
47  double MapSearchEnvironment::gcost(uint32_t node1, uint32_t node2)
48  {
49  return heuristic(node1, node2);
50  }
51 
52  double MapSearchEnvironment::heuristic(uint32_t node1, uint32_t node2)
53  {
54  int x1, x2, y1, y2;
55  x1 = node1>>16; y1 = node1&0xFFFF;
56  x2 = node2>>16; y2 = node2&0xFFFF;
57  double a = ((x1>x2)?(x1-x2):(x2-x1));
58  double b = ((y1>y2)?(y1-y2):(y2-y1));
59  return (a>b)?(b*ROOT_TWO+a-b):(a*ROOT_TWO+b-a);
60  //return ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
61  }
62 
66  void GraphSearchEnvironment::getNeighbors(uint32_t nodeID, std::vector<uint32_t> &neighbors)
67  {
68  node *n1 = g->GetNode(nodeID);
70  for (int next = n1->nodeNeighborNext(ni); next != -1; next = n1->nodeNeighborNext(ni))
71  {
72  neighbors.push_back(next);
73  }
74  }
75 
76  double GraphSearchEnvironment::heuristic(uint32_t node1, uint32_t node2)
77  {
78  edge *e = g->FindEdge(node1, node2);
79  if (e)
80  return e->GetWeight();
81  return 0;
82  }
83 
84  double GraphSearchEnvironment::gcost(uint32_t node1, uint32_t node2)
85  {
86  return heuristic(node1, node2);
87  }
88 
89 
93  void MapGraphSearchEnvironment::getNeighbors(uint32_t nodeID, std::vector<uint32_t> &neighbors)
94  {
95 // node *n1 = g->GetNode(nodeID);
96 // neighbor_iterator ni = n1->getNeighborIter();
97 // for (int next = n1->nodeNeighborNext(ni); next != -1; next = n1->nodeNeighborNext(ni))
98 // {
99 // neighbors.push_back(next);
100 // }
101  node *n = g->GetNode(nodeID);
103  for (edge *e = n->edgeIterNextOutgoing(ei); e; e = n->edgeIterNextOutgoing(ei))
104  {
105  neighbors.push_back(e->getTo());
106  }
107  }
108 
109  double MapGraphSearchEnvironment::heuristic(uint32_t state1, uint32_t state2)
110  {
111  int x1 = g->GetNode(state1)->GetLabelL(9);
112  int y1 = g->GetNode(state1)->GetLabelL(10);
113  int x2 = g->GetNode(state2)->GetLabelL(9);
114  int y2 = g->GetNode(state2)->GetLabelL(10);
115 // int x1 = g->GetNode(state1)->GetLabelL(GraphSearchConstants::kMapX);
116 // int y1 = g->GetNode(state1)->GetLabelL(GraphSearchConstants::kMapY);
117 // int x2 = g->GetNode(state2)->GetLabelL(GraphSearchConstants::kMapX);
118 // int y2 = g->GetNode(state2)->GetLabelL(GraphSearchConstants::kMapY);
119 
120  double a = ((x1>x2)?(x1-x2):(x2-x1));
121  double b = ((y1>y2)?(y1-y2):(y2-y1));
122  return (a>b)?(b*ROOT_TWO+a-b):(a*ROOT_TWO+b-a);
123  }
124 
125  double MapGraphSearchEnvironment::gcost(uint32_t node1, uint32_t node2)
126  {
127  return heuristic(node1, node2);
128  }
129 
130 }
edge::GetWeight
double GetWeight()
Definition: Graph.h:140
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
node::getOutgoingEdgeIter
edge_iterator getOutgoingEdgeIter() const
Definition: Graph.cpp:733
OldSearchCode::MapSearchEnvironment::map
Map * map
Definition: OldSearchEnvironment.h:37
OldSearchCode::MapGraphSearchEnvironment::g
Graph * g
Definition: OldSearchEnvironment.h:61
OldSearchCode
Definition: OldSearchEnvironment.cpp:12
edge_iterator
std::vector< edge * >::const_iterator edge_iterator
Definition: Graph.h:30
node::edgeIterNextOutgoing
edge * edgeIterNextOutgoing(edge_iterator &) const
Definition: Graph.cpp:738
OldSearchCode::MapGraphSearchEnvironment::gcost
double gcost(uint32_t node1, uint32_t node2)
Definition: OldSearchEnvironment.cpp:125
OldSearchCode::MapSearchEnvironment::gcost
double gcost(uint32_t node1, uint32_t node2)
Definition: OldSearchEnvironment.cpp:47
Graph::GetNode
node * GetNode(unsigned long num)
Definition: Graph.cpp:152
kGround
@ kGround
Definition: Map.h:55
OldSearchCode::GraphSearchEnvironment::gcost
double gcost(uint32_t node1, uint32_t node2)
Definition: OldSearchEnvironment.cpp:84
OldSearchEnvironment.h
OldSearchCode::GraphSearchEnvironment::getNeighbors
void getNeighbors(uint32_t nodeID, std::vector< uint32_t > &neighbors)
GraphSearchEnvironment.
Definition: OldSearchEnvironment.cpp:66
OldSearchCode::GraphSearchEnvironment::heuristic
double heuristic(uint32_t node1, uint32_t node2)
Definition: OldSearchEnvironment.cpp:76
OldSearchCode::MapGraphSearchEnvironment::getNeighbors
void getNeighbors(uint32_t nodeID, std::vector< uint32_t > &neighbors)
MapGraphSearchEnvironment.
Definition: OldSearchEnvironment.cpp:93
OldSearchCode::MapGraphSearchEnvironment::heuristic
double heuristic(uint32_t node1, uint32_t node2)
Definition: OldSearchEnvironment.cpp:109
ROOT_TWO
static const double ROOT_TWO
Definition: GLUtil.h:61
OldSearchCode::MapSearchEnvironment::getNeighbors
void getNeighbors(uint32_t nodeID, std::vector< uint32_t > &neighbors)
Definition: OldSearchEnvironment.cpp:14
Map::GetTerrainType
long GetTerrainType(long x, long y, tSplitSide split=kWholeTile) const
Get the terrain type of the (split) tile at x, y.
Definition: Map.cpp:1028
node::nodeNeighborNext
int nodeNeighborNext(neighbor_iterator &) const
Definition: Graph.cpp:807
OldSearchCode::MapSearchEnvironment::heuristic
double heuristic(uint32_t node1, uint32_t node2)
Definition: OldSearchEnvironment.cpp:52
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
node::getNeighborIter
neighbor_iterator getNeighborIter() const
Definition: Graph.cpp:802
OldSearchCode::GraphSearchEnvironment::g
Graph * g
Definition: OldSearchEnvironment.h:49
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129