PolyVox  0.2.1
Open source voxel management library
AStarPathfinderImpl.h
Go to the documentation of this file.
1 /*******************************************************************************
2 Copyright (c) 2005-2009 David Williams
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 *******************************************************************************/
23 
24 #ifndef __PolyVox_AStarPathfinderImpl_H__
25 #define __PolyVox_AStarPathfinderImpl_H__
26 
27 #include "PolyVoxCore/Vector.h"
28 
29 #include <algorithm>
30 #include <limits> //For numeric_limits
31 #include <set>
32 #include <vector>
33 
34 namespace PolyVox
35 {
36  class OpenNodesContainer;
37  class ClosedNodesContainer;
38  class ThermiteGameLogic;
39 
42  {
49  };
50 
51  struct Node
52  {
53  Node(int x, int y, int z)
54  :gVal(std::numeric_limits<float>::quiet_NaN()) //Initilise with NaNs so that we will
55  ,hVal(std::numeric_limits<float>::quiet_NaN()) //know if we forget to set these properly.
56  ,parent(0)
57  {
58  position.setX(x);
59  position.setY(y);
60  position.setZ(z);
61  }
62 
63  bool operator==(const Node& rhs) const
64  {
65  return position == rhs.position;
66  }
67 
68  bool operator<(const Node& rhs) const
69  {
70  if (position.getX() < rhs.position.getX())
71  return true;
72  if (rhs.position.getX() < position.getX())
73  return false;
74 
75  if (position.getY() < rhs.position.getY())
76  return true;
77  if (rhs.position.getY() < position.getY())
78  return false;
79 
80  if (position.getZ() < rhs.position.getZ())
81  return true;
82  if (rhs.position.getZ() < position.getZ())
83  return false;
84 
85  return false;
86  }
87 
89  float gVal;
90  float hVal;
92 
93  float f(void) const
94  {
95  return gVal + hVal;
96  }
97  };
98 
99  typedef std::set<Node> AllNodesContainer;
100 
102  {
103  public:
104  bool operator() (const AllNodesContainer::iterator& lhs, const AllNodesContainer::iterator& rhs) const
105  {
106  return (&(*lhs)) < (&(*rhs));
107  }
108  };
109 
110  class NodeSort
111  {
112  public:
113  bool operator() (const AllNodesContainer::iterator& lhs, const AllNodesContainer::iterator& rhs) const
114  {
115  return lhs->f() > rhs->f();
116  }
117  };
118 
120  {
121  public:
122  typedef std::vector<AllNodesContainer::iterator>::iterator iterator;
123 
124  public:
125  void clear(void)
126  {
127  open.clear();
128  }
129 
130  bool empty(void) const
131  {
132  return open.empty();
133  }
134 
135  void insert(AllNodesContainer::iterator node)
136  {
137  open.push_back(node);
138  push_heap(open.begin(), open.end(), NodeSort());
139  }
140 
141  AllNodesContainer::iterator getFirst(void)
142  {
143  return open[0];
144  }
145 
146  void removeFirst(void)
147  {
148  pop_heap(open.begin(), open.end(), NodeSort());
149  open.pop_back();
150  }
151 
152  void remove(iterator iterToRemove)
153  {
154  open.erase(iterToRemove);
155  make_heap(open.begin(), open.end(), NodeSort());
156  }
157 
159  {
160  return open.begin();
161  }
162 
163  iterator end(void)
164  {
165  return open.end();
166  }
167 
168  iterator find(AllNodesContainer::iterator node)
169  {
170  std::vector<AllNodesContainer::iterator>::iterator openIter = std::find(open.begin(), open.end(), node);
171  return openIter;
172  }
173 
174  private:
175  std::vector<AllNodesContainer::iterator> open;
176  };
177 
179  {
180  public:
181  typedef std::set<AllNodesContainer::iterator, AllNodesContainerIteratorComparator>::iterator iterator;
182 
183  public:
184  void clear(void)
185  {
186  closed.clear();
187  }
188 
189  void insert(AllNodesContainer::iterator node)
190  {
191  closed.insert(node);
192  }
193 
194  void remove(iterator iterToRemove)
195  {
196  closed.erase(iterToRemove);
197  }
198 
200  {
201  return closed.begin();
202  }
203 
204  iterator end(void)
205  {
206  return closed.end();
207  }
208 
209  iterator find(AllNodesContainer::iterator node)
210  {
211  iterator iter = std::find(closed.begin(), closed.end(), node);
212  return iter;
213  }
214 
215  private:
216  std::set<AllNodesContainer::iterator, AllNodesContainerIteratorComparator> closed;
217  };
218 
219 
220  //bool operator<(const AllNodesContainer::iterator& lhs, const AllNodesContainer::iterator& rhs);
221 }
222 
223 #endif //__PolyVox_AStarPathfinderImpl_H__