PolyVox  0.2.1
Open source voxel management library
AStarPathfinder.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_AStarPathfinder_H__
25 #define __PolyVox_AStarPathfinder_H__
26 
28 #include "Impl/TypeDef.h"
29 
30 #include "PolyVoxCore/Array.h"
31 
32 #include <list>
33 #include <stdexcept> //For runtime_error
34 
35 namespace PolyVox
36 {
37  const float sqrt_1 = 1.0f;
38  const float sqrt_2 = 1.4143f;
39  const float sqrt_3 = 1.7321f;
40 
44 
47  template<typename VolumeType>
48  bool aStarDefaultVoxelValidator(const VolumeType* volData, const Vector3DInt32& v3dPos);
49 
61  template<typename VolumeType>
63  {
64  public:
66  (
67  VolumeType* volData,
68  const Vector3DInt32& v3dStart,
69  const Vector3DInt32& v3dEnd,
70  std::list<Vector3DInt32>* listResult,
71  float fHBias = 1.0,
72  uint32_t uMaxNoOfNodes = 10000,
73  Connectivity requiredConnectivity = TwentySixConnected,
74  polyvox_function<bool (const VolumeType*, const Vector3DInt32&)> funcIsVoxelValidForPath = &aStarDefaultVoxelValidator,
75  polyvox_function<void (float)> funcProgressCallback = 0
76  )
77  :volume(volData)
78  ,start(v3dStart)
79  ,end(v3dEnd)
80  ,result(listResult)
81  ,connectivity(requiredConnectivity)
82  ,hBias(fHBias)
83  ,maxNumberOfNodes(uMaxNoOfNodes)
84  ,isVoxelValidForPath(funcIsVoxelValidForPath)
85  ,progressCallback(funcProgressCallback)
86  {
87  }
88 
90  VolumeType* volume;
91 
94 
97 
100  std::list<Vector3DInt32>* result;
101 
106 
115  float hBias;
116 
122 
129  polyvox_function<bool (const VolumeType*, const Vector3DInt32&)> isVoxelValidForPath;
130 
136  polyvox_function<void (float)> progressCallback;
137  };
138 
163  template<typename VolumeType>
165  {
166  public:
168 
169  void execute();
170 
171  private:
172  void processNeighbour(const Vector3DInt32& neighbourPos, float neighbourGVal);
173 
174  float SixConnectedCost(const Vector3DInt32& a, const Vector3DInt32& b);
175  float EighteenConnectedCost(const Vector3DInt32& a, const Vector3DInt32& b);
176  float TwentySixConnectedCost(const Vector3DInt32& a, const Vector3DInt32& b);
177  float computeH(const Vector3DInt32& a, const Vector3DInt32& b);
178  uint32_t hash(uint32_t a);
179 
180  //Node containers
181  AllNodesContainer allNodes;
182  OpenNodesContainer openNodes;
183  ClosedNodesContainer closedNodes;
184 
185  //The current node
186  AllNodesContainer::iterator current;
187 
188  float m_fProgress;
189 
191  };
192 }
193 
195 
196 #endif //__PolyVox_AStarPathfinder_H__