forked from larrylindsey/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentTreePruner.cpp
More file actions
89 lines (65 loc) · 2.51 KB
/
Copy pathComponentTreePruner.cpp
File metadata and controls
89 lines (65 loc) · 2.51 KB
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
80
81
82
83
84
85
86
87
88
89
#include <util/foreach.h>
#include <util/Logger.h>
#include "ComponentTreePruner.h"
static logger::LogChannel componenttreeprunerlog("componenttreeprunerlog", "[ComponentTreePruner] ");
ComponentTreePruner::ComponentTreePruner() {
registerInput(_componentTree, "component tree");
registerInput(_maxHeight, "max height");
registerOutput(_pruned, "component tree");
}
void
ComponentTreePruner::updateOutputs() {
if (!_pruned)
_pruned = new ComponentTree();
prune();
}
void
ComponentTreePruner::prune() {
// the new root will be a clone of the old root
_root = boost::make_shared<ComponentTree::Node>(_componentTree->getRoot()->getComponent());
// copy and prune on-the-fly, starting with the root node
int rootLevel;
boost::shared_ptr<ComponentTree::Node> pruned = prune(_componentTree->getRoot(), rootLevel);
// the whole tree did not exceed the threshold
if (pruned)
_root = pruned;
_pruned->setRoot(_root);
}
boost::shared_ptr<ComponentTree::Node>
ComponentTreePruner::prune(
boost::shared_ptr<ComponentTree::Node> node,
int& level) {
// copies of all children of node that do not exceed the threshold
std::vector<boost::shared_ptr<ComponentTree::Node> > validChildren;
// prune the trees under every child and add them to the clone
int maxChildLevel = -1;
foreach (boost::shared_ptr<ComponentTree::Node> child, node->getChildren()) {
// copy the child nodes and get their level
int childLevel;
boost::shared_ptr<ComponentTree::Node> childClone = prune(child, childLevel);
if (childLevel > maxChildLevel)
maxChildLevel = childLevel;
// collect the child clones temporarily (if there was no child clone
// returned, child was already exceeding the threshold)
if (childClone)
validChildren.push_back(childClone);
}
// update our level (zero if we have no children)
level = maxChildLevel + 1;
// we are exceeding the threshold
if (level > *_maxHeight) {
// connect our children (that are not exceeding the threshold) directly
// to the root node
foreach (boost::shared_ptr<ComponentTree::Node> child, validChildren)
_root->addChild(child);
// return nothing to indicate we are done in this branch
return boost::shared_ptr<ComponentTree::Node>();
}
// we are good, create a copy...
boost::shared_ptr<ComponentTree::Node> nodeClone = boost::make_shared<ComponentTree::Node>(node->getComponent());
// ...connect our children to it...
foreach (boost::shared_ptr<ComponentTree::Node> child, validChildren)
nodeClone->addChild(child);
// ...and return it
return nodeClone;
}