Could anyone please recommend a grid sorting order for me?

Started by
5 comments, last by lucky6969b 7 years, 6 months ago

I've been trying all of these order combinations of x,y,z....

None of them seems to be working

I just sort them by pivot...

lowest y comes first, then x,z plane next

when trying x,y,z

x-35.294 y:0.066 z:-37.307

 x-26.294 y:0.066 z:-37.307

 x-22.694 y:0.066 z:-37.307

 x-31.694 y:0.066 z:-37.307

 x-33.494 y:0.066 z:-37.307

 x-20.894 y:0.066 z:-37.307

 x-29.894 y:0.066 z:-37.307

 x13.306 y:0.066 z:-35.507

 x7.906 y:0.066 z:-37.307

 x-28.094 y:0.066 z:-37.307

 x-19.094 y:0.066 z:-37.307

 x25.906 y:0.066 z:-35.507

 x-24.494 y:0.066 z:-37.307

 x-17.294 y:0.066 z:-37.307

 x-4.694 y:0.066 z:-35.507

 x-33.494 y:0.066 z:-35.507

 x22.306 y:13.266 z:-37.307

 x31.306 y:13.266 z:-37.307 <<< uninvited node

 x-15.494 y:0.066 z:-37.307

 x34.906 y:0.066 z:-37.307

 x-13.694 y:0.066 z:-37.307

 x38.506 y:0.066 z:-37.307

 x-11.894 y:0.066 z:-37.307

 x-24.494 y:0.066 z:-35.507

 x34.906 y:13.266 z:-37.307

any ideas?


bool operator<(const NodeExtent& other) const
{
     return std::tie(m_pivot.y, m_pivot.z, m_pivot.x)
            < std::tie(other.m_pivot.y, other.m_pivot.z, other.m_pivot.x);
}
Advertisement

How are you performing the check using your overloaded < operator? Are you using a for loop doing the check using node and node+1 then saving back into the same array or std::vector (or another container)?

I think that your operator would return you the correct bool value, however, how you are saving the results might be where you need to look.

something like this

std::shared_ptr<Octant> newOctant(new Octant(pivot, size));
newOctant->m_octree = this;
newOctant->status = Octant::FREE;
NodeExtent newExtent(newOctant->getPivot(), newOctant->getSize());
m_physical_nodes.insert(std::make_pair(newExtent, *newOctant));           
I can't find the connection between your first post and your second post.

If you are having issues with something in C++ (like comparing tuples with std::tie), it's best if you write a very small program that shows the unexpected behavior. You can then post it in its entirety here and we will all know what you are talking about. It is very likely that in the process of writing that small program you'll figure out the problem yourself.

Giving some help?

I want the other way round

But inverting the signs doesn't work


 x3 y:1 z:3
 x2 y:1 z:2
 x0 y:0 z:0
 x-1 y:-1 z:-1

#include <iostream>
#include <memory>
#include <vector>
#include <d3dx9.h>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <sstream>

class Indexable {

public:
    int Index;
};


class Octant : public Indexable
{
public:    
    enum Status
    {
        FREE = 0,
        OBSTRUCTED = 1
    };
    

    Octant(const D3DXVECTOR3& pivot, D3DXVECTOR3& size) { m_vPivot = pivot; m_vSize = size; }
    

    


    D3DXVECTOR3 getPivot() const { return m_vPivot; }
    
    D3DXVECTOR3 getSize() const { return m_vSize; }

    


private:
    D3DXVECTOR3 m_vPivot;
    D3DXVECTOR3 m_vSize;


public:    
    Status status;
    


};

class NodeExtent
{
public:
    NodeExtent() : NodeExtent(D3DXVECTOR3(-1, -1, -1), D3DXVECTOR3(-1, -1, -1))
    {

    }

    NodeExtent(D3DXVECTOR3 pivot, D3DXVECTOR3 size) {
        //m_pivot = D3DXVECTOR3(round_num(pivot.x, 0.001f), round_num(pivot.y, 0.001f), round_num(pivot.z, 0.001f));
        m_pivot = pivot;
        m_size = size;
        //m_boundingBox[0].x = m_pivot.x - (m_size.x / 2);
        //m_boundingBox[0].y = m_pivot.y - (m_size.y / 2);
        //m_boundingBox[0].z = m_pivot.z - (m_size.z / 2);

        //m_boundingBox[1].x = m_pivot.x + (m_size.x / 2);
        //m_boundingBox[1].y = m_pivot.y + (m_size.y / 2);
        //m_boundingBox[1].z = m_pivot.z + (m_size.z / 2);
    }

    bool operator==(const NodeExtent& other) const
    {

        //return m_boundingBox[0].x == other.m_boundingBox[0].x
        //    && m_boundingBox[0].y == other.m_boundingBox[0].y
        //    && m_boundingBox[0].z == other.m_boundingBox[0].z
        //    && m_boundingBox[1].x == other.m_boundingBox[1].x
        //    && m_boundingBox[1].y == other.m_boundingBox[1].y
        //    && m_boundingBox[1].z == other.m_boundingBox[1].z;        

        return m_pivot.x == other.m_pivot.x
            && m_pivot.y == other.m_pivot.y
            && m_pivot.z == other.m_pivot.z;

    }

    // for sorting on the fly
    bool operator<(const NodeExtent& other) const
    {
        //return std::tie(m_pivot.x, m_pivot.y, m_pivot.z)
        //    < std::tie(other.m_pivot.x, other.m_pivot.y, other.m_pivot.z);
        //return std::tie(m_boundingBox[0].z, m_boundingBox[0].y, m_boundingBox[0].x)
        //    < std::tie(m_boundingBox[1].z, m_boundingBox[1].y, m_boundingBox[1].x);

        //return (m_pivot.x < other.m_pivot.x &&
        //    m_pivot.y < other.m_pivot.y &&
        //    m_pivot.z < other.m_pivot.z);

        if (m_pivot.y < other.m_pivot.y)
        {
            return true;
        }
        else if (m_pivot.y > other.m_pivot.y)
        {
            return false;
        }
        else
        {
            if (m_pivot.x < other.m_pivot.x)
            {
                return true;
            }
            else if (m_pivot.x > other.m_pivot.x)
            {
                return false;
            }
            else
            {
                if (m_pivot.z < other.m_pivot.z)
                {
                    return true;
                }
                else if (m_pivot.z > other.m_pivot.z)
                {
                    return false;
                }
            }

        }

        return false;
    }

    
    D3DXVECTOR3 m_pivot;
    D3DXVECTOR3 m_size;
};

struct NodeExtentHasher
{

    std::size_t operator()(const NodeExtent& k) const
    {
        using boost::hash_value;
        using boost::hash_combine;

        std::size_t seed = 0;

        hash_combine(seed, hash_value(k.m_pivot.x));
        hash_combine(seed, hash_value(k.m_pivot.y));
        hash_combine(seed, hash_value(k.m_pivot.z));

        return seed;
    }



};



int main()
{

    D3DXVECTOR3 pivots[4]
    {
        D3DXVECTOR3(-1, -1, -1),
        D3DXVECTOR3(0, 0, 0),
        D3DXVECTOR3(2, 1, 2),
        D3DXVECTOR3(3, 1, 3)
    };

    D3DXVECTOR3 s[4]
    {
        D3DXVECTOR3(1, 1, 1),
        D3DXVECTOR3(1, 1, 1),
        D3DXVECTOR3(1, 1, 1),
        D3DXVECTOR3(1, 1, 1)
    };

    std::unordered_map<NodeExtent, Octant, NodeExtentHasher> m_physical_nodes;

    for (int i = 0; i < 4; i++)
    {
        std::shared_ptr<Octant> newOctant(new Octant(pivots[i], s[i]));        
        newOctant->status = Octant::FREE;
        NodeExtent newExtent(newOctant->getPivot(), newOctant->getSize());
        m_physical_nodes.insert(std::make_pair(newExtent, *newOctant));
    }


    for (const auto& n : m_physical_nodes)
    {
        std::stringstream oss;
        oss << " x" << n.first.m_pivot.x << " y:" << n.first.m_pivot.y << " z:" << n.first.m_pivot.z << std::endl;
        OutputDebugStringA(oss.str().c_str());
    }

 
}
So you are putting a bunch of objects in an unordered_map and you don't like that they come out in a bad order? Is that really what's going on?

Thanks Alvaro,

I know what I was doing now.

Just almost fell asleep yesterday....

Thanks my buddy

Jack

This topic is closed to new replies.

Advertisement