-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregion.c
191 lines (162 loc) · 4.74 KB
/
region.c
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
/**
*
* Copyright (c) 2010-2015 Voidware Ltd. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code as
* defined in and that are subject to the Voidware Public Source Licence version
* 1.0 (the 'Licence'). You may not use this file except in compliance with the
* Licence or with expressly written permission from Voidware. Please obtain a
* copy of the Licence at http://www.voidware.com/legal/vpsl1.txt and read it
* before using this file.
*
* The Original Code and all software distributed under the Licence are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
* OR IMPLIED, AND VOIDWARE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
* WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Please see the Licence for the specific language governing rights and
* limitations under the Licence.
*
*/
#include "region.h"
#include "2di.h"
/** Region2D *************************************************************/
void Region2DInit(Region2D* rg)
{
memset(rg, 0, sizeof(Region2D));
}
void Region2DEmpty(Region2D* rg)
{
Region2DInit(rg);
}
static int rectWasteCost(Rect2D* ra, Rect2D* rb)
{
/* calculate the area wasted if we combine two rectangles
* this is the area of, union(A,B) - A - B + intersection(A,B)
*/
Rect2D ur, ir;
int ua, ia, aa, ba;
aa = BOXAREA(*ra);
ba = BOXAREA(*rb);
BOXUNION(*ra, *rb, ur);
ua = BOXAREA(ur);
BOXINTERSECT(*ra, *rb, ir);
ia = BOXAREA(ir);
return ua - aa - ba + ia;
}
static void changeBoxAt(Region2D* rg, int i, Rect2D* box)
{
int j;
int c;
Rect2D b = *box;
BOXAT(*rg, i, i) = 1;
again:
rg->b_[i] = b;
/* recompute cost metrics */
for (j = 0; j < REGION_NUM_BOXES; ++j) {
if (j == i || !BOXAT(*rg, j, j)) continue;
c = rectWasteCost(&rg->b_[j], &rg->b_[i]);
if (!c) {
/* box `j' is deleted. */
BOXUNION(rg->b_[j], rg->b_[i], b);
BOXAT(*rg, j, j) = 0;
goto again;
}
if (j < i) {
BOXAT(*rg, j, i) = c;
}
else {
BOXAT(*rg, i, j) = c;
}
}
}
static int makeSpareBox(Region2D* rg)
{
int i, j;
int minc;
int mini, minj;
Rect2D ru;
/* first quickly scan for am existing spare box */
for (i = 0; i < REGION_NUM_BOXES; ++i) {
if (!BOXAT(*rg, i,i)) return i; // already spare
}
/* all boxes full, find the combination with the min waste
* metric and combine.
*/
minc = 0;
for (i = 0; i < REGION_NUM_BOXES; ++i) {
for (j = i+1; j < REGION_NUM_BOXES; ++j) {
int c = BOXAT(*rg, i, j);
if (!minc || c < minc) {
minc = c;
mini = i;
minj = j;
}
}
}
/* mini + minj -> mini */
BOXAT(*rg, minj,minj) = 0; // mark as empty.
BOXUNION(rg->b_[mini], rg->b_[minj], ru);
changeBoxAt(rg, mini, &ru);
return minj;
}
void Region2DAdd(Region2D* rg, Rect2D* r)
{
/* first look for a spare slot.
* this is indicated by examining the box difference for (i,i)
*/
int i;
if (BOXOK(*r)) { // reject any empty boxes.
i = makeSpareBox(rg);
changeBoxAt(rg, i, r);
}
}
int Region2DBoundingBox(Region2D* rg, Rect2D* r)
{
/* find the overall bounding box for this region.
* return 0 if no valid bounds.
*/
Rect2D b;
int begun = 0;
int i;
for (i = 0; i < REGION_NUM_BOXES; ++i) {
if (BOXAT(*rg, i, i)) {
if (!begun) {
/* start with the first valid box */
b = rg->b_[i];
begun = 1;
}
else {
/* otherwise expand to include this box */
const Rect2D* bi = &rg->b_[i];
EXTBOX(b, bi->tl.x, bi->tl.y);
EXTBOX(b, bi->br.x, bi->br.y);
}
}
}
if (begun) *r = b;
else {
/* fill out a null box */
r->tl.x = 0; r->tl.y = 0;
r->br.x = 0; r->br.y = 0;
}
return begun;
}
int Region2DValid(Region2D* rg)
{
/* return 0 iff `rg' is empty */
int i;
for (i = 0; i < REGION_NUM_BOXES; ++i) {
if (BOXAT(*rg, i, i)) {
return 1;
}
}
return 0;
}
/** Rect2D Helpers ********************************************************/
void RectUnion2D(Rect2D* ra, Rect2D* rb, Rect2D* rr)
{
BOXUNION(*ra, *rb, *rr);
}