This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBvh.cpp
160 lines (145 loc) · 4.32 KB
/
Bvh.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Bvh.cpp
// ImageProcessing
//
// Created by Chris Marcellino on 1/24/11.
// Copyright 2011 Chris Marcellino. All rights reserved.
//
#import "opencv2/opencv.hpp"
#import "CvRectUtilities.hpp"
#import "Bvh.hpp"
BvhNode& BvhNode::operator=(const BvhNode& node)
{
if (this != &node) {
rect = node.rect;
BvhNode *l = NULL;
BvhNode *r = NULL;
if (node.left) {
try {
l = new BvhNode(*node.left);
r = new BvhNode(*node.right);
} catch (...) {
delete l;
delete r;
throw;
}
}
delete left;
left = l;
delete right;
right = r;
}
return *this;
}
void BvhNode::insert(const CvRect& newRect, bool skipContainedRects)
{
if (skipContainedRects && !left && rectContainsRect(rect, newRect)) {
return;
}
CvRect newBoundingBox = rectUnion(rect, newRect);
if (!left) {
assert(!right);
left = new BvhNode(rect);
right = new BvhNode(newRect);
rect = newBoundingBox;
} else {
int perimeter = rectPerimeter(newBoundingBox);
CvRect ifLeftRect = rectUnion(left->rect, newRect);
int ifLeftDifference = rectPerimeter(ifLeftRect) - rectPerimeter(left->rect);
CvRect ifRightRect = rectUnion(right->rect, newRect);
int ifRightDifference = rectPerimeter(ifRightRect) - rectPerimeter(right->rect);
rect = newBoundingBox;
if (ifLeftDifference < ifRightDifference && ifLeftDifference < perimeter / 8) {
assert(left); // silence spurious clang warning 1/8/2015
left->insert(newRect, skipContainedRects);
} else if (ifRightDifference < perimeter / 8) {
right->insert(newRect, skipContainedRects);
} else if (ifLeftDifference < ifRightDifference) {
BvhNode *temp = left;
left = new BvhNode(ifLeftRect);
left->left = temp;
left->right = new BvhNode(newRect);
} else {
BvhNode *temp = right;
right = new BvhNode(ifRightRect);
right->left = temp;
right->right = new BvhNode(newRect);
}
}
}
bool BvhNode::memberContains(int x, int y)
{
if (!rectContainsPoint(rect, x, y)) {
return false;
}
return !left || left->memberContains(x, y) || right->memberContains(x, y);
}
bool BvhNode::allMembersContaining(int x, int y, std::vector<CvRect>& members, bool remove)
{
if (!rectContainsPoint(rect, x, y)) {
return false;
}
if (!left) {
members.push_back(rect);
return remove;
}
bool removeLeft = left->allMembersContaining(x, y, members, remove);
bool removeRight = right->allMembersContaining(x, y, members, remove);
if (removeLeft && removeRight) {
return true;
} else if (removeLeft) {
removeChild(left);
} else if (removeRight) {
removeChild(right);
}
return false;
}
bool BvhNode::allMembersIntersecting(const CvRect& aRect, std::vector<CvRect>& members, bool remove)
{
if (!rectIntersectsRect(rect, aRect)) {
return false;
}
if (!left) {
members.push_back(rect);
return remove;
}
bool removeLeft = left->allMembersIntersecting(aRect, members, remove);
bool removeRight = right->allMembersIntersecting(aRect, members, remove);
if (removeLeft && removeRight) {
return true;
} else if (removeLeft) {
removeChild(left);
} else if (removeRight) {
removeChild(right);
}
return false;
}
bool BvhNode::getAnyRect(CvRect& rect, bool remove)
{
BvhNode *node = this;
BvhNode *prev = NULL;
while (node->left) {
prev = node;
node = node->left;
}
rect = node->rect;
if (remove && prev) {
prev->removeChild(node);
return false;
}
return remove;
}
void BvhNode::removeChild(BvhNode *leaf)
{
assert(leaf == this->left || leaf == this->right);
BvhNode *remaining = (leaf == this->left) ? right : left;
rect = remaining->rect;
BvhNode* l = remaining->left;
BvhNode* r = remaining->right;
remaining->left = NULL; // avoid double deallocation
remaining->right = NULL;
delete left;
delete right;
left = l;
right = r;
}