forked from larrylindsey/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentTree.cpp
More file actions
142 lines (94 loc) · 2.92 KB
/
Copy pathComponentTree.cpp
File metadata and controls
142 lines (94 loc) · 2.92 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "ComponentTree.h"
ComponentTree::Node::Node(boost::shared_ptr<ConnectedComponent> component) :
_component(component) {}
void
ComponentTree::Node::setParent(boost::shared_ptr<ComponentTree::Node> parent) {
_parent = parent;
}
boost::shared_ptr<ComponentTree::Node>
ComponentTree::Node::getParent() {
boost::shared_ptr<ComponentTree::Node> parent = _parent.lock();
return parent;
}
void
ComponentTree::Node::addChild(boost::shared_ptr<ComponentTree::Node> componentNode) {
_children.push_back(componentNode);
}
std::vector<boost::shared_ptr<ComponentTree::Node> >
ComponentTree::Node::getChildren() {
return _children;
}
boost::shared_ptr<ConnectedComponent>
ComponentTree::Node::getComponent() {
return _component;
}
ComponentTree::ComponentTree() :
_boundingBox(0, 0, 0, 0) {}
void
ComponentTree::clear() {
_root.reset();
_boundingBox = util::rect<double>(0, 0, 0, 0);
}
void
ComponentTree::setRoot(boost::shared_ptr<ComponentTree::Node> root) {
_root = root;
updateBoundingBox();
}
boost::shared_ptr<ComponentTree::Node>
ComponentTree::getRoot() {
return _root;
}
unsigned int
ComponentTree::size() const {
return count(_root);
}
unsigned int
ComponentTree::count(boost::shared_ptr<ComponentTree::Node> node) const {
int numNodes = 0;
foreach (boost::shared_ptr<ComponentTree::Node> child, node->getChildren())
numNodes += count(child);
numNodes++;
return numNodes;
}
const util::rect<double>&
ComponentTree::getBoundingBox() const {
return _boundingBox;
}
/**
* Creates a copy of the component tree, but not a copy of the involved
* connected components.
*
* @return A copy of the component tree.
*/
ComponentTree
ComponentTree::clone() {
boost::shared_ptr<ComponentTree::Node> root = clone(_root);
ComponentTree tree;
tree.setRoot(_root);
return tree;
}
boost::shared_ptr<ComponentTree::Node>
ComponentTree::clone(boost::shared_ptr<ComponentTree::Node> node) {
boost::shared_ptr<ComponentTree::Node> nodeClone = boost::make_shared<ComponentTree::Node>(node->getComponent());
foreach (boost::shared_ptr<ComponentTree::Node> child, node->getChildren()) {
boost::shared_ptr<ComponentTree::Node> childClone = clone(child);
nodeClone->addChild(childClone);
}
return nodeClone;
}
void
ComponentTree::updateBoundingBox() {
_boundingBox = updateBoundingBox(_root);
}
util::rect<double>
ComponentTree::updateBoundingBox(boost::shared_ptr<ComponentTree::Node> node) {
util::rect<double> boundingBox = node->getComponent()->getBoundingBox();
foreach (boost::shared_ptr<ComponentTree::Node> child, node->getChildren()) {
util::rect<double> childBoundingBox = updateBoundingBox(child);
boundingBox.minX = std::min(boundingBox.minX, childBoundingBox.minX);
boundingBox.maxX = std::max(boundingBox.maxX, childBoundingBox.maxX);
boundingBox.minY = std::min(boundingBox.minY, childBoundingBox.minY);
boundingBox.maxY = std::max(boundingBox.maxY, childBoundingBox.maxY);
}
return boundingBox;
}