我试图通过一组内的 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