-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTextureSplitter.cpp
160 lines (129 loc) · 4.45 KB
/
TextureSplitter.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
#include "TextureSplitter.h"
int Ray::TextureSplitter::Allocate(const int res[2], int pos[2]) {
const int i = Insert_Recursive(0, res);
if (i != -1) {
pos[0] = nodes_[i].pos[0];
pos[1] = nodes_[i].pos[1];
}
return i;
}
bool Ray::TextureSplitter::Free(const int pos[2]) {
const int i = Find_Recursive(0, pos);
return Free(i);
}
bool Ray::TextureSplitter::Free(const int i) {
if (i == -1 || nodes_[i].is_free) {
return false;
}
nodes_[i].is_free = true;
int par = nodes_[i].parent;
while (par != -1) {
int ch0 = nodes_[par].child[0], ch1 = nodes_[par].child[1];
if (!nodes_[ch0].has_children() && nodes_[ch0].is_free && !nodes_[ch1].has_children() && nodes_[ch1].is_free) {
SafeErase(ch0, &par, 1);
ch1 = nodes_[par].child[1];
SafeErase(ch1, &par, 1);
nodes_[par].child[0] = nodes_[par].child[1] = -1;
par = nodes_[par].parent;
} else {
par = -1;
}
}
return true;
}
int Ray::TextureSplitter::FindNode(const int pos[2], int size[2]) const {
const int i = Find_Recursive(0, pos);
if (i != -1) {
size[0] = nodes_[i].size[0];
size[1] = nodes_[i].size[1];
}
return i;
}
int Ray::TextureSplitter::Insert_Recursive(int i, const int res[2]) {
if (!nodes_[i].is_free || res[0] > nodes_[i].size[0] || res[1] > nodes_[i].size[1]) {
return -1;
}
int ch0 = nodes_[i].child[0], ch1 = nodes_[i].child[1];
if (ch0 != -1) {
const int new_node = Insert_Recursive(ch0, res);
if (new_node != -1) {
return new_node;
}
return Insert_Recursive(ch1, res);
} else {
if (res[0] == nodes_[i].size[0] && res[1] == nodes_[i].size[1]) {
nodes_[i].is_free = false;
return i;
}
nodes_[i].child[0] = ch0 = int(nodes_.size());
nodes_.emplace_back();
nodes_[i].child[1] = ch1 = int(nodes_.size());
nodes_.emplace_back();
node_t &n = nodes_[i];
const int dw = n.size[0] - res[0];
const int dh = n.size[1] - res[1];
if (dw > dh) {
nodes_[ch0].pos[0] = n.pos[0];
nodes_[ch0].pos[1] = n.pos[1];
nodes_[ch0].size[0] = res[0];
nodes_[ch0].size[1] = n.size[1];
nodes_[ch1].pos[0] = n.pos[0] + res[0];
nodes_[ch1].pos[1] = n.pos[1];
nodes_[ch1].size[0] = n.size[0] - res[0];
nodes_[ch1].size[1] = n.size[1];
} else {
nodes_[ch0].pos[0] = n.pos[0];
nodes_[ch0].pos[1] = n.pos[1];
nodes_[ch0].size[0] = n.size[0];
nodes_[ch0].size[1] = res[1];
nodes_[ch1].pos[0] = n.pos[0];
nodes_[ch1].pos[1] = n.pos[1] + res[1];
nodes_[ch1].size[0] = n.size[0];
nodes_[ch1].size[1] = n.size[1] - res[1];
}
nodes_[ch0].parent = nodes_[ch1].parent = i;
return Insert_Recursive(ch0, res);
}
}
int Ray::TextureSplitter::Find_Recursive(const int i, const int pos[2]) const {
if (nodes_[i].is_free || pos[0] < nodes_[i].pos[0] || pos[0] > (nodes_[i].pos[0] + nodes_[i].size[0]) ||
pos[1] < nodes_[i].pos[1] || pos[1] > (nodes_[i].pos[1] + nodes_[i].size[1])) {
return -1;
}
const int ch0 = nodes_[i].child[0], ch1 = nodes_[i].child[1];
if (ch0 != -1) {
const int ndx = Find_Recursive(ch0, pos);
if (ndx != -1) {
return ndx;
}
return Find_Recursive(ch1, pos);
} else {
if (pos[0] == nodes_[i].pos[0] && pos[1] == nodes_[i].pos[1]) {
return i;
} else {
return -1;
}
}
}
void Ray::TextureSplitter::SafeErase(const int i, int *indices, const int num) {
const int last = int(nodes_.size()) - 1;
if (last != i) {
const int ch0 = nodes_[last].child[0], ch1 = nodes_[last].child[1];
if (ch0 != -1 && nodes_[i].parent != last) {
nodes_[ch0].parent = nodes_[ch1].parent = i;
}
const int par = nodes_[last].parent;
if (nodes_[par].child[0] == last) {
nodes_[par].child[0] = i;
} else if (nodes_[par].child[1] == last) {
nodes_[par].child[1] = i;
}
nodes_[i] = nodes_[last];
}
nodes_.erase(nodes_.begin() + last);
for (int j = 0; j < num && indices; j++) {
if (indices[j] == last) {
indices[j] = i;
}
}
}