I am puzzled where the unordered_map should be sparsed and a range of D3DXVECTOR3 should map
into this space.
But this statement just won't work
// this thing doesn't work
tileMap.insert(ptVec.begin(), ptVec.end());
#include "stdafx.h"
#include <d3dx9.h>
#include "NavmeshSingleton.h"
#include "navmesher.h"
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <iostream>
using namespace std;
struct cPoint {
public:
cPoint() { }
cPoint(const cPoint& other) {
this->id = other.id;
this->x = other.x;
this->z = other.z;
this->vec3 = other.vec3;
}
cPoint(const QuadNode& other) {
this->id = other.Index;
this->x = other.x;
this->z = other.z;
}
int id;
int x;
int z;
D3DXVECTOR3 vec3;
};
struct D3DXVec3KeyHasher {
public:
std::size_t operator()(const D3DXVECTOR3 &vec3) const
{
std::size_t seed = 0;
//boost::hash_combine(seed, x.from);
//boost::hash_combine(seed, x.to);
float fXInc = NavmeshSingleton::GetNavMesher()->GetXInc();
float fZInc = NavmeshSingleton::GetNavMesher()->GetZInc();
const D3DXVECTOR3& bmin = NavmeshSingleton::GetNavMesher()->GetMin();
const D3DXVECTOR3& bmax = NavmeshSingleton::GetNavMesher()->GetMax();
// -30 - (-30) / 10 = 0
// -29 - (-30) / 10 = 0.1xx =>floor => 0
int newX = std::floor((vec3.x - bmin.x) / fXInc);
int newZ = std::floor((bmin.z - vec3.z) / fZInc);
boost::hash_combine(seed, newX);
boost::hash_combine(seed, newZ);
return seed;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::unordered_map<D3DXVECTOR3, cPoint*, D3DXVec3KeyHasher> tileMap;
std::vector<QuadNode*> walkables = NavmeshSingleton::GetNavMesher()->GetWalkables();
std::vector<cPoint*> ptVec;
for (auto& w : walkables) {
cPoint* pt = new cPoint(*w);
ptVec.push_back(pt);
}
// this thing doesn't work
tileMap.insert(ptVec.begin(), ptVec.end());
for (auto& t : tileMap) {
cout << "t x:" << t.second->x << " z:" << t.second->z << endl;
getchar();
}
// Test hashing
D3DXVECTOR3 testVec3;
testVec3.x = 0;
testVec3.z = 0;
cPoint* pt = tileMap[testVec3];
cout << "pt x:" << pt->x << " z:" << pt->z << endl;
return 0;
}