/
BestSelector.h
79 lines (67 loc) · 1.86 KB
/
BestSelector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <list>
#include "iselectiontest.h"
namespace selection
{
/**
* Selector implementation picking the selectables by their intersection quality.
* Only selectables with valid intersections that are better or equal than the
* currently best value are considered and added to a list of "best" selectables.
* Selectables with closer (=better) intersection values cause all previously added
* selectables to be discarded.
* Selectables which have roughly equal intersection values as the currently best one
* will get added to the end of the internal list.
*/
class BestSelector :
public Selector
{
private:
SelectionIntersection _curIntersection;
ISelectable* _selectable;
SelectionIntersection _bestIntersection;
std::list<ISelectable*> _bestSelectables;
public:
BestSelector() :
_selectable(nullptr)
{}
void pushSelectable(ISelectable& selectable) override
{
_curIntersection = SelectionIntersection();
_selectable = &selectable;
}
void popSelectable() override
{
if (_curIntersection.equalEpsilon(_bestIntersection, 0.25f, 0.001f))
{
_bestSelectables.push_back(_selectable);
_bestIntersection = _curIntersection;
}
else if (_curIntersection < _bestIntersection)
{
_bestSelectables.clear();
_bestSelectables.push_back(_selectable);
_bestIntersection = _curIntersection;
}
_curIntersection = SelectionIntersection();
}
void addIntersection(const SelectionIntersection& intersection) override
{
_curIntersection.assignIfCloser(intersection);
}
const std::list<ISelectable*>& getBestSelectables() const
{
return _bestSelectables;
}
bool empty() const override
{
return _bestSelectables.empty();
}
void foreachSelectable(const std::function<void(ISelectable*)>& functor) override
{
for (auto selectable : _bestSelectables)
{
functor(selectable);
}
}
};
}