10#include <tracking/trackFindingCDC/hough/trees/DynTree.h>
11#include <tracking/trackFindingCDC/hough/baseelements/WithWeightedItems.h>
12#include <tracking/trackFindingCDC/hough/baseelements/WithSharedMark.h>
27 namespace TrackFindingCDC {
30 template<
class T,
class ADomain,
class ADomainDivsion>
32 public DynTree< WithWeightedItems<ADomain, T>, ADomainDivsion> {
50 template<
class T,
class ADomain,
class ADomainDivsion>
63 using Node =
typename Super::Node;
72 for (
auto&& item : items) {
74 bool& markOfItem =
m_marks.back();
75 TrackingUtilities::Weight weight = DBL_MAX;
81 template <
class AItemInDomainMeasure>
82 std::vector<std::pair<ADomain, std::vector<T>>>
87 auto skipLowWeightNode = [minWeight](
const Node * node) {
88 return not(node->getWeight() >= minWeight);
94 template <
class AItemInDomainMeasure,
class ASkipNodePredicate>
95 std::vector<std::pair<ADomain, std::vector<T>>>
98 ASkipNodePredicate& skipNode)
100 std::vector<std::pair<ADomain, std::vector<T> > > found;
101 auto isLeaf = [&found, &skipNode, maxLevel](
Node * node) {
103 if (skipNode(node)) {
110 if (node->getLevel() >= maxLevel) {
111 const ADomain* domain = node;
112 found.emplace_back(*domain, std::vector<T>(node->begin(), node->end()));
124 fillWalk(weightItemInDomain, isLeaf);
135 template <
class AItemInDomainMeasure>
136 std::vector<std::pair<ADomain, std::vector<T>>>
139 const TrackingUtilities::Weight minWeight = NAN)
141 auto skipLowWeightNode = [minWeight](
const Node * node) {
142 return not(node->getWeight() >= minWeight);
154 template <
class AItemInDomainMeasure,
class ASkipNodePredicate>
155 std::vector<std::pair<ADomain, std::vector<T>>>
158 ASkipNodePredicate& skipNode)
160 std::vector<std::pair<ADomain, std::vector<T> > > found;
163 const ADomain* domain = node;
164 found.emplace_back(*domain, std::vector<T>(node->begin(), node->end()));
178 template <
class AItemInDomainMeasure,
class ASkipNodePredicate>
179 std::unique_ptr<std::pair<ADomain, std::vector<T>>>
182 ASkipNodePredicate& skipNode)
184 using Result = std::pair<ADomain, std::vector<T> >;
185 std::unique_ptr<Result> found =
nullptr;
188 const ADomain* domain = node;
189 found.reset(
new Result(*domain, std::vector<T>(node->begin(), node->end())));
202 template <
class AItemInDomainMeasure,
class ASkipNodePredicate>
205 ASkipNodePredicate& skipNode)
207 Node* heaviestNode =
nullptr;
208 TrackingUtilities::Weight heighestWeigth = NAN;
209 auto isLeaf = [&heaviestNode, &heighestWeigth, maxLevel, &skipNode](
Node * node) {
211 if (skipNode(node)) {
215 TrackingUtilities::Weight nodeWeight = node->getWeight();
217 if (not std::isnan(heighestWeigth) and not(nodeWeight > heighestWeigth)) {
224 if (node->getLevel() >= maxLevel) {
226 heighestWeigth = nodeWeight;
232 const bool isLeafMarksItems =
false;
233 fillWalk(weightItemInDomain, isLeaf, isLeafMarksItems);
242 template<
class AItemInDomainMeasure,
class AIsLeafPredicate>
243 void fillWalk(AItemInDomainMeasure& weightItemInDomain,
244 AIsLeafPredicate& isLeaf,
245 bool isLeafMarksItems =
true)
247 auto walker = [&weightItemInDomain, &isLeaf](
Node * node) {
258 typename Node::Children* children = node->getChildren();
260 node->createChildren();
261 children = node->getChildren();
262 if constexpr(std::is_invocable_v<AItemInDomainMeasure&, const T&, Node*>) {
267 const T& item(markableItem);
268 for (
Node& childNode : *children) {
269 const TrackingUtilities::Weight weight = weightItemInDomain(item, &childNode);
270 if (not std::isnan(weight)) {
271 childNode.insert(markableItem, weight);
276 for (
Node& childNode : *children) {
277 assert(childNode.getChildren() ==
nullptr);
278 assert(childNode.size() == 0);
281 [&childNode, &weightItemInDomain](
WithSharedMark<T>& markableItem) -> TrackingUtilities::Weight {
283 T & item(markableItem);
284 return weightItemInDomain(item, &childNode);
286 childNode.insert(*node, measure);
301 template<
class ATreeWalker>
305 auto unmarkedPriority = [](
Node * node) ->
float {
306 return node->getWeight();
308 this->
walk(walker, unmarkedPriority);
312 auto priority = [](
Node * node) ->
float {
315 return markableItem.isMarked();
317 node->eraseIf(isMarked);
318 return node->getWeight();
321 this->
walk(walker, priority);
DynTree(const Properties &properties, const SubPropertiesFactory &subPropertiesFactory=SubPropertiesFactory())
void walk(AWalker &walker)
Dynamic tree structure with weighted items in each node which are markable through out the tree.
void fell()
Fell to tree meaning deleting all child nodes from the tree. Keeps the top node.
void seed(const Ts &items)
Take the item set and insert them into the top node of the hough space.
Node * findHeaviestLeaf(AItemInDomainMeasure &weightItemInDomain, int maxLevel, ASkipNodePredicate &skipNode)
Go through all children until the maxLevel is reached and find the leaf with the highest weight.
std::vector< std::pair< ADomain, std::vector< T > > > findLeavesDisjoint(AItemInDomainMeasure &weightItemInDomain, int maxLevel, ASkipNodePredicate &skipNode)
Find all children node at maximum level and add them to the result list. Skip nodes if skipNode retur...
void raze()
Like fell but also releases all memory the tree has acquired during long executions.
static std::vector< std::pair< ADomain, std::vector< T > > > findHeaviestLeafRepeated(AItemInDomainMeasure &weightItemInDomain, int maxLevel, const TrackingUtilities::Weight minWeight=NAN)
Go through all children until maxLevel is reached and find the heaviest leaves.
std::deque< bool > m_marks
void fillWalk(AItemInDomainMeasure &weightItemInDomain, AIsLeafPredicate &isLeaf, bool isLeafMarksItems=true)
Walk through the children and fill them if necessary until isLeaf returns true.
std::unique_ptr< std::pair< ADomain, std::vector< T > > > findHeaviestLeafSingle(AItemInDomainMeasure &weightItemInDomain, int maxLevel, ASkipNodePredicate &skipNode)
Go through all children until the maxLevel is reached and find the leaf with the highest weight.
void walkHeighWeightFirst(ATreeWalker &walker, bool walkerMarksItems=true)
Walk the tree investigating the heaviest children with priority.
std::vector< std::pair< ADomain, std::vector< T > > > findHeavyLeavesDisjoint(AItemInDomainMeasure &weightItemInDomain, int maxLevel, double minWeight)
Find all children node at maximum level and add them to the result list. Skip nodes if their weight i...
std::vector< std::pair< ADomain, std::vector< T > > > findHeaviestLeafRepeated(AItemInDomainMeasure &weightItemInDomain, int maxLevel, ASkipNodePredicate &skipNode)
Go through all children until maxLevel is reached and find the heaviest leaves.
WeightedParititioningDynTree< WithSharedMark< AItemPtr >, HoughBox, BoxDivision > Super
typename Super::Node Node
DynTree< WithWeightedItems< ADomain, T >, ADomainDivsion > Super
Type of the base class.
WeightedParititioningDynTree(ADomain topDomain, ADomainDivsion domainDivsion)
Constructor attaching a vector of the weighted items to the top most node domain.
Mixin class to attach a mark that is shared among many instances.
A mixin class to attach a set of weighted items to a class.
Abstract base class for different kinds of events.