Я пытаюсь сортировать набор точек OpenSG по координате x внутри набора, но он, похоже, не работает.Компаратор C++ не работает
Мой компаратор:
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;
}
При печати из множества точек, они все еще не в порядке.
Любая помощь была бы принята с благодарностью.
Edit:
Vershov ответ, кажется, работает somwhat для меня, но при изменении ее в два раза и с помощью этого набора точек он терпит неудачу.
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
Вы пробовали распечатать элементы, прежде чем вставлять их в комплект? – juanchopanza
[OT]: Обратите внимание, что с вами компаратор, если у вас есть 2 записи с одним и тем же 'x', второй будет отброшен, так как' std :: set' рассмотрит '{x, 2, 3}' и '{x , 42, 42}. – Jarod42
Последний порядок отсортирован по x. Что мне не хватает? – Macke