diff --git a/library/PolyVoxCore/include/AStarPathfinder.h b/library/PolyVoxCore/include/AStarPathfinder.h index 10969bdb..a057fb0b 100644 --- a/library/PolyVoxCore/include/AStarPathfinder.h +++ b/library/PolyVoxCore/include/AStarPathfinder.h @@ -32,8 +32,6 @@ freely, subject to the following restrictions: #include "PolyVoxImpl/TypeDef.h" -#include - namespace PolyVox { const float sqrt_1 = 1.0f; @@ -60,8 +58,8 @@ namespace PolyVox float fHBias = 1.0, uint32_t uMaxNoOfNodes = 10000, Connectivity connectivity = TwentySixConnected, - std::function*, const Vector3DInt16&)> funcIsVoxelValidForPath = &aStarDefaultVoxelValidator, - std::function funcProgressCallback = 0 + polyvox_function*, const Vector3DInt16&)> funcIsVoxelValidForPath = &aStarDefaultVoxelValidator, + polyvox_function funcProgressCallback = 0 ) :volume(volData) ,start(v3dStart) @@ -94,10 +92,10 @@ namespace PolyVox uint32_t maxNumberOfNodes; //Used to determine whether a given voxel is valid. - std::function*, const Vector3DInt16&)> isVoxelValidForPath; + polyvox_function*, const Vector3DInt16&)> isVoxelValidForPath; //Progress callback - std::function progressCallback; + polyvox_function progressCallback; }; template diff --git a/library/PolyVoxCore/include/AStarPathfinder.inl b/library/PolyVoxCore/include/AStarPathfinder.inl index 7732db3e..402e1349 100644 --- a/library/PolyVoxCore/include/AStarPathfinder.inl +++ b/library/PolyVoxCore/include/AStarPathfinder.inl @@ -264,7 +264,10 @@ namespace PolyVox array[0] = abs(a.getX() - b.getX()); array[1] = abs(a.getY() - b.getY()); array[2] = abs(a.getZ() - b.getZ()); - + + //Maybe this is better implemented directly + //using three compares and two swaps... but not + //until the profiler says so. std::sort(&array[0], &array[3]); uint16_t cornerSteps = array[0]; @@ -303,20 +306,22 @@ namespace PolyVox //Apply the bias to the computed h value; hVal *= m_params.hBias; - std::hash uint32Hash; - + //Having computed hVal, we now apply some random bias to break ties. + //This needs to be deterministic on the input position. This random + //bias means it is much les likely that two paths are exactly the same + //length, and so far fewer nodes must be expanded to find the shortest path. + //See http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html#S12 + polyvox_hash uint32Hash; uint32_t hashValX = uint32Hash(a.getX()); uint32_t hashValY = uint32Hash(a.getY()); uint32_t hashValZ = uint32Hash(a.getZ()); - uint32_t hashVal = hashValX ^ hashValY ^ hashValZ; - + //Stop hashVal going over 65535, and divide by 1000000 to make sure it is small. hashVal &= 0x0000FFFF; - float fHash = hashVal / 1000000.0f; + //Apply the hash and return hVal += fHash; - return hVal; } } \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxImpl/TypeDef.h b/library/PolyVoxCore/include/PolyVoxImpl/TypeDef.h index cf7786b3..642ca4da 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/TypeDef.h +++ b/library/PolyVoxCore/include/PolyVoxImpl/TypeDef.h @@ -45,6 +45,13 @@ freely, subject to the following restrictions: #include #define polyvox_shared_ptr boost::shared_ptr + #include + #define polyvox_function boost::function + + #include + #define polyvox_hash boost::hash + + //As long as we're requiring boost, we'll use it to compensate //for the missing cstdint header too. #include @@ -57,8 +64,11 @@ freely, subject to the following restrictions: #else //We have a decent compiler - use real C++0x features #include + #include #include #define polyvox_shared_ptr std::shared_ptr + #define polyvox_function std::function + #define polyvox_hash std::hash #endif #endif