File: QuadtreeAtlasAlloc.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (207 lines) | stat: -rw-r--r-- 4,591 bytes parent folder | download | duplicates (3)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <cstring> // memset
#include <algorithm>
#include <vector>

#include "QuadtreeAtlasAlloc.h"
#include "System/Exceptions.h"
#include "System/bitops.h"
#include "System/Log/ILog.h"

static int NODE_MIN_SIZE = 8;


struct QuadTreeNode {
	QuadTreeNode() {
		used = false;
		posx = posy = size = 0;
		memset(children, 0, 4 * sizeof(QuadTreeNode*));
	}
	~QuadTreeNode() {
		for (auto& child: children) {
			if (child != nullptr) {
				delete child;
				child = nullptr;
			}
		}
	}

	QuadTreeNode(QuadTreeNode* node, int i) {
		// i ... 0:=topleft, 1:=topright, 2:=btmleft, 3:=btmright
		used = false;
		size = node->size >> 1;
		posx = node->posx + ((i & 1)     ) * size;
		posy = node->posy + ((i & 2) >> 1) * size;
		memset(children, 0, 4 * sizeof(QuadTreeNode*));
	}

	QuadTreeNode* FindPosInQuadTree(int xsize, int ysize);

	int GetMinSize() {
		int minsize = size;
		for (const auto& child: children) {
			if (child != nullptr) {
				minsize = std::min(minsize, child->GetMinSize());
			}
		}
		return minsize;
	}

	short int posx;
	short int posy;
	int size;
	bool used;
	QuadTreeNode* children[4];
};


QuadTreeNode* QuadTreeNode::FindPosInQuadTree(int xsize, int ysize)
{
	if (used)
		return nullptr;

	if ((size < xsize) || (size < ysize))
		return nullptr;

	const bool xfit = ((size >> 1) < xsize);
	const bool yfit = ((size >> 1) < ysize);
	const bool minSizeReached = (size <= NODE_MIN_SIZE);

	if (xfit || yfit || minSizeReached) {
		if (!children[0]) {
			if (!minSizeReached) {
				if (!yfit) {
					children[0] = new QuadTreeNode(this, 0);
					children[1] = new QuadTreeNode(this, 1);
					children[0]->used = true;
					children[1]->used = true;
					return this;
				}
				if (!xfit) {
					children[0] = new QuadTreeNode(this, 0);
					children[2] = new QuadTreeNode(this, 2);
					children[0]->used = true;
					children[2]->used = true;
					return this;
				}
			}

			used = true;
			return this;
		} else {
			return nullptr; //FIXME dynamic x/y quadnode size?
		}
	}

	for (int i = 0; i < 4; ++i) {
		if (!children[i]) children[i] = new QuadTreeNode(this, i);
		QuadTreeNode* subnode = children[i]->FindPosInQuadTree(xsize, ysize);
		if (subnode) return subnode;
	}

	return nullptr;
}


CQuadtreeAtlasAlloc::CQuadtreeAtlasAlloc()
{
	root = nullptr;
}


CQuadtreeAtlasAlloc::~CQuadtreeAtlasAlloc()
{
	delete root;
}


QuadTreeNode* CQuadtreeAtlasAlloc::FindPosInQuadTree(int xsize, int ysize)
{
	QuadTreeNode* node = root->FindPosInQuadTree(xsize, ysize);
	while (!node) {
		if (root->size >= maxsize.x) {
			break;
		}

		if (!root->used && !root->children[0]) {
			root->size = root->size << 1;
			node = root->FindPosInQuadTree(xsize, ysize);
			continue;
		}
		QuadTreeNode* oldroot = root;
		root = new QuadTreeNode();
		root->posx = 0;
		root->posy = 0;
		root->size = oldroot->size << 1;
		root->children[0] = oldroot;
		node = root->FindPosInQuadTree(xsize, ysize);
	}
	return node;
}


int CQuadtreeAtlasAlloc::CompareTex(SAtlasEntry* tex1, SAtlasEntry* tex2)
{
	const size_t area1 = std::max(tex1->size.x, tex1->size.y);
	const size_t area2 = std::max(tex2->size.x, tex2->size.y);
	return (area1 > area2);
}


bool CQuadtreeAtlasAlloc::Allocate()
{
	if (root == nullptr) {
		root = new QuadTreeNode();
		root->posx = 0;
		root->posy = 0;
		root->size = 32;
	}

	bool failure = false;

	std::vector<SAtlasEntry*> sortedEntries;
	sortedEntries.reserve(entries.size());

	for (auto& entry: entries) {
		sortedEntries.push_back(&entry.second);
	}

	std::sort(sortedEntries.begin(), sortedEntries.end(), CQuadtreeAtlasAlloc::CompareTex);

	for (const auto& item: sortedEntries) {
		SAtlasEntry& entry = *item;
		QuadTreeNode* node = FindPosInQuadTree(entry.size.x, entry.size.y);

		if (node == nullptr) {
			for (auto jt = entries.begin(); jt != entries.end(); ++jt) {
				if (&entry == &(jt->second)) {
					LOG_L(L_ERROR, "CQuadtreeAtlasAlloc full: failed to add %s", jt->first.c_str());
					break;
				}
			}
			failure = true;
			continue;
		}

		entry.texCoords.x1 = node->posx;
		entry.texCoords.y1 = node->posy;
		entry.texCoords.x2 = node->posx + entry.size.x - 1; //FIXME stretch if image is smaller than node!
		entry.texCoords.y2 = node->posy + entry.size.y - 1;
	}

	atlasSize.x = root->size;
	atlasSize.y = root->size;
	if (!root->children[2] && !root->children[3]) {
		atlasSize.y = std::max(atlasSize.y >> 1, NODE_MIN_SIZE);
	}

	return !failure;
}


int CQuadtreeAtlasAlloc::GetMaxMipMaps()
{
	if (!root) return 0;
	return bits_ffs(root->GetMinSize());
}