Map/Hash a range of D3DXVECTOR3 into a grid space?

Started by
2 comments, last by BitMaster 8 years, 7 months ago

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;
}
Advertisement

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


What does not work? Does it not compile? Then what are the exact compile errors?
Does it compile but crash at runtime? What happens when you use the debugger to home in on the crash site? Where is it? What is the local state?
Does it compile and run but not do what you expect? What do you expect? What do you observe?
Here is the error.

Error	4	error C2664: 'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)' : cannot convert argument 1 from 'cPoint *' to 'const std::pair<const _Kty,_Ty> &'	C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0	600	1	TestKeyHasher
I think I didn't provide the keys for the pairs.
But I don't know how. Because what I want to do is to pick a certain D3DXVECTOR3 (any numbers)
and hash it to a bucket and it will return a cPoint pointer to me.
The insert member function of the map types requires std::pairs because an insert only makes sense if you supply both a key and its corresponding value. In your case you need to insert std::pair<D3DXVECTOR3, cPoint*>.

This topic is closed to new replies.

Advertisement