0

我试图通过一组内的 x 坐标对一组 OpenSG 点进行排序,但它似乎不起作用。

我的比较器:

struct less_than_x {
    inline bool operator() (const OSG::Pnt3f& p1, const OSG::Pnt3f& p2) {
        return (p1.x() < p2.x());
    }
};

我的代码:

std::set<OSG::Pnt3f, less_than_x> positions3D; 

OSG::GeoPnt3fPropertyRefPtr pos = OSG::GeoPnt3fProperty::create();

pos->addValue(OSG::Pnt3f(1,2,3));
pos->addValue(OSG::Pnt3f(5,2,1));
pos->addValue(OSG::Pnt3f(4,4,2));
pos->addValue(OSG::Pnt3f(6,4,0));
pos->addValue(OSG::Pnt3f(3,5,1));

// Remove the Z Axis from all of the points
for(OSG::UInt32 i = 0; i < pos->size(); i++)    {        
    OSG::Pnt3f p;              
    pos->getValue(p,i);
    OSG::Pnt3f p2 = OSG::Pnt3f(p.x(), p.y(), 0);
    positions3D.insert(p2);
}

for (std::set<OSG::Pnt3f>::iterator it = positions3D.begin(); it != positions3D.end(); it++) {
    std::cout << *it << std::endl;
}

打印出这组点时,它们仍然不按顺序排列。

任何帮助将不胜感激。

编辑:

vershov 的答案似乎对我有用,但是当将其更改为 double 并使用这组点时,它会失败。

positions3D.insert(Pnt3f(-1, 0, 0));
positions3D.insert(Pnt3f(-0.850651, -0.309017, 0));
positions3D.insert(Pnt3f(-0.850651, 0, 0));
positions3D.insert(Pnt3f(-0.525731, 0, 0));
positions3D.insert(Pnt3f(-0.525731, 0.809017, 0));
positions3D.insert(Pnt3f(-0.525731, 0.5, 0));
positions3D.insert(Pnt3f(-0.447214, 0.525731, 0));
positions3D.insert(Pnt3f(-0.447214, 0.850651, 0));
positions3D.insert(Pnt3f(-0.447214, 0, 0));
positions3D.insert(Pnt3f(-1.05104e-007, 0.309017, 0));
positions3D.insert(Pnt3f(-7.00695e-008, 0.809017, 0));
positions3D.insert(Pnt3f(0, 1, 0));
positions3D.insert(Pnt3f(7.00695e-008, 0.809017, 0));
positions3D.insert(Pnt3f(1.05104e-007, -0.309017, 0));
positions3D.insert(Pnt3f(0.447214, 0, 0));
positions3D.insert(Pnt3f(0.447214, 0.850651, 0));
positions3D.insert(Pnt3f(0.447214, 0.525731, 0));
positions3D.insert(Pnt3f(0.525731, 0.5, 0));
positions3D.insert(Pnt3f(0.525731, 0.809017, 0));
positions3D.insert(Pnt3f(0.525731, 0, 0));
positions3D.insert(Pnt3f(0.850651, 0, 0));
positions3D.insert(Pnt3f(0.850651, 0.5, 0));
positions3D.insert(Pnt3f(1, 0, 0));

这些按以下顺序排序:

-1, 0, 0
-0.850651, -0.309017, 0
-0.850651, 0, 0
-0.525731, 0, 0
-0.525731, 0.809017, 0
-0.525731, 0.5, 0
-0.447214, 0.525731, 0
-0.447214, 0.850651, 0
-0.447214, 0, 0
-1.05104e-007, 0.309017, 0
-7.00695e-008, 0.809017, 0
0, 1, 0
7.00695e-008, 0.809017, 0
1.05104e-007, -0.309017, 0
0.447214, 0, 0
0.447214, 0.850651, 0
0.447214, 0.525731, 0
0.525731, 0.5, 0
0.525731, 0.809017, 0
0.525731, 0, 0
0.850651, 0, 0
0.850651, 0.5, 0
1, 0, 0    
4

1 回答 1

1

请检查以下代码:

#include <set>
#include <iostream>

struct Pnt3f {
        Pnt3f(int x, int y, int z) : m_x(x), m_y(y), m_z(z) {}
        int x() const { return m_x; }
        int y() const { return m_y; }
        int z() const { return m_z; }
private:
        int m_x;
        int m_y;
        int m_z;
};

struct less_than_x {
    inline bool operator() (const Pnt3f& p1, const Pnt3f& p2) {
        return (p1.x() < p2.x());
    }
};

int main() {

        std::set<Pnt3f, less_than_x> positions3D;

        positions3D.insert(Pnt3f(1,2,0));
        positions3D.insert(Pnt3f(5,2,0));
        positions3D.insert(Pnt3f(4,4,0));
        positions3D.insert(Pnt3f(6,4,0));
        positions3D.insert(Pnt3f(3,5,0));

        for (std::set<Pnt3f>::iterator it = positions3D.begin(); it != positions3D.end(); ++it) {
                std::cout << "(" << it->x() << ", " << it->y() << ", " << it->z() << ")" << std::endl;
        }
        return 0;
}

输出如下:

./a.out 
(1, 2, 0)
(3, 5, 0)
(4, 4, 0)
(5, 2, 0)
(6, 4, 0)

因此,比较器按预期工作并进行排序。您需要在行前打印出值以了解出了什么问题。

positions3D.insert(p2);
于 2014-01-03T13:22:51.440 回答