File: RectangleOptimizer.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (325 lines) | stat: -rwxr-xr-x 8,507 bytes parent folder | download
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "RectangleOptimizer.h"
#include "System/Log/ILog.h"

#include <cassert>


unsigned CRectangleOptimizer::statsTotalSize = 0;
unsigned CRectangleOptimizer::statsOptSize   = 0;


CRectangleOptimizer::CRectangleOptimizer()
	: maxAreaPerRect(500 * 500) // FIXME auto adjust this in HeightMapUpdate!
	, needsUpdate(false)
{
}


CRectangleOptimizer::~CRectangleOptimizer()
{
	float reduction = 0.f;
	if (statsTotalSize > 0) {
		reduction = 100.f - ((100.f * statsOptSize) / statsTotalSize);
	}
	LOG("Statistics for RectangleOptimizer: %.0f%%", reduction);
}


unsigned CRectangleOptimizer::GetTotalArea() const
{
	unsigned ret = 0;
	for (CRectangleOptimizer::const_iterator it = rectangles.begin(); it != rectangles.end(); ++it) {
		const int w = it->GetWidth();
		const int h = it->GetHeight();
		ret += (w * h);
	}
	return ret;
}


void CRectangleOptimizer::Optimize()
{
	if (!needsUpdate) {
		return;
	}

	CRectangleOptimizer::iterator it;
	CRectangleOptimizer::iterator jt;

	//TODO this is not fully correct, when there was still rectangles
	//     left from the last update we shouldn't count them twice!
	statsTotalSize += GetTotalArea();

	//! Fix Overlap
	for (it = rectangles.begin(); it != rectangles.end(); ++it) {
		for (jt = it, ++jt; jt != rectangles.end(); ) {
			int del = HandleOverlapping(&(*it), &(*jt));
			if (del < 0) {
				it = rectangles.erase(it);
				jt = it; ++jt;
			} else if (del > 0) {
				//jt = rectangles.erase(jt);
				std::swap(*it, *jt);
				it = rectangles.erase(it);
				jt = it; ++jt;
			} else {
				++jt;
			}
		}
	}

	statsOptSize += GetTotalArea();

	//! Merge when possible
	rectangles.sort();
	for (it = rectangles.begin(); it != rectangles.end(); ++it) {
		for (jt = it, ++jt; jt != rectangles.end(); ) {
			bool del = HandleMerge(*it, *jt);
			if (del) {
				it = rectangles.erase(it);
				jt = it; ++jt;
			} else {
				++jt;
			}
		}
	}

	//! Split too large
	for (it = rectangles.begin(); it != rectangles.end(); ++it) {
		SRectangle& rect1 = *it;
		int width  = rect1.GetWidth();
		int height = rect1.GetHeight();

		while ((width * height) > maxAreaPerRect) {
			SRectangle rect2 = rect1;
			if (width > maxAreaPerRect) {
				rect1.x2 = (rect1.x1 + rect1.x2) / 2;
				rect2.x1 = (rect2.x1 + rect2.x2) / 2;
			} else {
				rect1.z2 = (rect1.z1 + rect1.z2) / 2;
				rect2.z1 = (rect2.z1 + rect2.z2) / 2;
			}
			rectangles.push_back(rect2);

			width  = rect1.GetWidth();
			height = rect1.GetHeight();
		}
	}
}


inline std::bitset<4> CRectangleOptimizer::GetEdgesInRect(const SRectangle& rect1, const SRectangle& rect2)
{
	std::bitset<4> bits;
	bits[0] = (rect2.x1 >= rect1.x1) && (rect2.x1 <= rect1.x2);
	bits[1] = (rect2.x2 >= rect1.x1) && (rect2.x2 <= rect1.x2);
	bits[2] = (rect2.z1 >= rect1.z1) && (rect2.z1 <= rect1.z2);
	bits[3] = (rect2.z2 >= rect1.z1) && (rect2.z2 <= rect1.z2);
	return bits;
}


inline std::bitset<4> CRectangleOptimizer::GetSharedEdges(const SRectangle& rect1, const SRectangle& rect2)
{
	std::bitset<4> bits;
	bits[0] = (rect2.x1 == rect1.x1) || (rect2.x1 == rect1.x2);
	bits[1] = (rect2.x2 == rect1.x1) || (rect2.x2 == rect1.x2);
	bits[2] = (rect2.z1 == rect1.z1) || (rect2.z1 == rect1.z2);
	bits[3] = (rect2.z2 == rect1.z1) || (rect2.z2 == rect1.z2);
	return bits;
}


inline bool CRectangleOptimizer::DoOverlap(const SRectangle& rect1, const SRectangle& rect2)
{
	SRectangle boundRect(rect1);
	if (rect2.x1 < rect1.x1) boundRect.x1 = rect2.x1;
	if (rect2.x2 > rect1.x2) boundRect.x2 = rect2.x2;
	if (rect2.z1 < rect1.z1) boundRect.z1 = rect2.z1;
	if (rect2.z2 > rect1.z2) boundRect.z2 = rect2.z2;

	const bool overlapX = (boundRect.GetWidth() < (rect1.GetWidth() + rect2.GetWidth()));
	const bool overlapZ = (boundRect.GetHeight() < (rect1.GetHeight() + rect2.GetHeight()));

	const bool overlap = (overlapX && overlapZ);
	return overlap;
}


inline bool CRectangleOptimizer::AreMergable(const SRectangle& rect1, const SRectangle& rect2)
{
	SRectangle boundRect(rect1);
	if (rect2.x1 < rect1.x1) boundRect.x1 = rect2.x1;
	if (rect2.x2 > rect1.x2) boundRect.x2 = rect2.x2;
	if (rect2.z1 < rect1.z1) boundRect.z1 = rect2.z1;
	if (rect2.z2 > rect1.z2) boundRect.z2 = rect2.z2;

	const bool touchX = (boundRect.GetWidth() <= (rect1.GetWidth() + rect2.GetWidth()));
	const bool touchZ = (boundRect.GetHeight() <= (rect1.GetHeight() + rect2.GetHeight()));

	const bool mergableX = ((rect1.GetHeight() == rect2.GetHeight()) && (boundRect.GetHeight() == rect2.GetHeight()) && touchX);
	const bool mergableZ = ((rect1.GetWidth() == rect2.GetWidth()) && (boundRect.GetWidth() == rect2.GetWidth()) && touchZ);
	return mergableX || mergableZ;
}


bool CRectangleOptimizer::HandleMerge(SRectangle& rect1, SRectangle& rect2)
{
	if (!AreMergable(rect1, rect2)) {
		//  ____
		// |    |_____
		// |    |     |  etc.
		// |____|_____|
		//
		return false;
	}

	//! make rect1 point to the `larger`/`outer` rectangle
	std::bitset<4> edgesInRect12 = GetEdgesInRect(rect1, rect2);
	std::bitset<4> edgesInRect21 = GetEdgesInRect(rect2, rect1);
	if (edgesInRect12.count() < edgesInRect21.count()) {
		std::swap(edgesInRect12, edgesInRect21);
		std::swap(rect1, rect2); //FIXME only swap if we `return true`!!!
	}

	//! check if edges are really shared
	//! (if they are shared we can merge those two rects into one)
	std::bitset<4> sharedEdges = GetSharedEdges(rect1, rect2);

	if (edgesInRect12.count() == 3) {
		if (sharedEdges.count() == 3 || (
			(sharedEdges.count() == 2) && (
				(sharedEdges[0] && sharedEdges[1]) || (sharedEdges[2] && sharedEdges[3])
			)
		)) {
			//  __________
			// |  | |     |
			// |  | |     |
			// |__|_|_____|
			//

			//FIXME check if no overlapping (only touching), if so check maxAreaPerRect!!!

			//! merge
			rect2.x1 = std::min(rect1.x1, rect2.x1);
			rect2.x2 = std::max(rect1.x2, rect2.x2);
			rect2.z1 = std::min(rect1.z1, rect2.z1);
			rect2.z2 = std::max(rect1.z2, rect2.z2);
			return true; //! erase rect1
		}
	}

	return false;
}


int CRectangleOptimizer::HandleOverlapping(SRectangle* rect1, SRectangle* rect2)
{
	if (!DoOverlap(*rect1, *rect2)) {
		//  ______
		// |      |  ___
		// |      | |   |
		// |      | |___|
		// |______|
		//

		return 0;
	}

	//! make rect1 point to the `larger`/`outer` rectangle
	bool swapped = false;
	std::bitset<4> edgesInRect12 = GetEdgesInRect(*rect1, *rect2);
	std::bitset<4> edgesInRect21 = GetEdgesInRect(*rect2, *rect1);
	if (edgesInRect12.count() < edgesInRect21.count()) {
		std::swap(edgesInRect12, edgesInRect21);
		std::swap(rect1, rect2); //! swap the POINTERS (not the content!)
		swapped = true;
	}


	if (edgesInRect12.count() == 4) {
		//  ________
		// |  ____  |
		// | |    | |
		// | |____| |
		// |________|

		//! 2 is fully in 1
		return (swapped) ? -1 : 1;
	} else if (edgesInRect12.count() == 3) {
		//  ______
		// |     _|___
		// |    | |   |
		// |    |_|___|
		// |______|
		//

		//! make one rect smaller
		if (!edgesInRect12[0]) {
			rect2->x2 = rect1->x1;
		} else if (!edgesInRect12[1]) {
			rect2->x1 = rect1->x2;
		} else if (!edgesInRect12[2]) {
			rect2->z2 = rect1->z1;
		} else {
			rect2->z1 = rect1->z2;
		}
	} else if ((edgesInRect12[0] || edgesInRect12[1]) && (edgesInRect12[2] || edgesInRect12[3]) ) {
		//assert(edgesInRect12.count() == 2);

		//  ______
		// |      |
		// |  ____|___
		// |_|____|   |
		//   |________|

		//! make one smaller and create a new one
		SRectangle rect3 = *rect2;

		if (edgesInRect12[2]) {
			rect2->z1 = rect1->z2;
			rect3.z2  = rect1->z2;
		} else { //(edgesInRect12[3])
			rect2->z2 = rect1->z1;
			rect3.z1  = rect1->z1;
		}

		if (edgesInRect12[0]) {
			rect3.x1 = rect1->x2;
		} else { //(edgesInRect12[1])
			rect3.x2 = rect1->x1;
		}

		//! create a new one
		rectangles.push_back(rect3);
	} else if ((edgesInRect12.count() == 2) && (edgesInRect12[0] != edgesInRect21[0])) {
		//assert(edgesInRect12.count() == 2);

		//    ______
		//  _|______|_
		// | |      | |
		// |_|______|_|
		//   |______|
		//

		//! make one smaller and create a new one
		SRectangle rect3 = *rect2;

		if (edgesInRect12[0]) {
			assert(edgesInRect12[0] && edgesInRect12[1] && !edgesInRect12[2] && !edgesInRect12[3]);
			rect2->z2 = rect1->z1;
			rect3.z1  = rect1->z2;
		} else {
			assert(!edgesInRect12[0] && !edgesInRect12[1] && edgesInRect12[2] && edgesInRect12[3]);
			rect2->x2 = rect1->x1;
			rect3.x1  = rect1->x2;
		}

		//! create a new one
		rectangles.push_back(rect3);
	}

	return 0;
}