summaryrefslogtreecommitdiff
path: root/sys/src/cmd/vnc/rlist.c
blob: dddad64fcbadbe87960f7a08531a36c28588092d (plain)
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
#include "vnc.h"
#include "vncs.h"

static int tot;
static void rprint(Rlist*);

static void
growrlist(Rlist *rlist, int n)
{
	int old;

	if(rlist->nrect+n <= rlist->maxrect)
		return;

	old = rlist->maxrect;
	while(rlist->nrect+n > rlist->maxrect){
		if(rlist->maxrect == 0)
			rlist->maxrect = 16;
		else
			rlist->maxrect *= 2;
	}

	tot += rlist->maxrect - old;
	if(tot > 10000)
		sysfatal("too many rectangles");

	rlist->rect = realloc(rlist->rect, rlist->maxrect*sizeof(rlist->rect[0]));
	if(rlist->rect == nil)
		sysfatal("realloc failed in growrlist");
}

static void
rappend(Rlist *rl, Rectangle r)
{
	growrlist(rl, 1);
	rl->rect[rl->nrect++] = r;
}

/* remove rectangle i from the list */
static int
rtrim(Rlist *r, int i)
{
	if(i < 0 || i >= r->nrect)
		return 0;
	if(i == r->nrect-1){
		r->nrect--;
		return 1;
	}
	r->rect[i] = r->rect[--r->nrect];
	return 1;
}

static int
rectadjacent(Rectangle r, Rectangle s)
{
	return r.min.x<=s.max.x && s.min.x<=r.max.x &&
	       r.min.y<=s.max.y && s.min.y<=r.max.y;
}

/*
 * If s shares three edges with r, compute the
 * rectangle r - s and return 1.
 * Else return 0.
 */
static int
rectubr(Rectangle *r, Rectangle s)
{
	if(r->min.y==s.min.y && r->max.y==s.max.y){
		if(r->min.x == s.min.x){
			r->min.x = s.max.x;
			assert(r->max.x > r->min.x);
			return 1;
		}
		if(r->max.x == s.max.x){
			r->max.x = s.min.x;
			assert(r->max.x > r->min.x);
			return 1;
		}
	}
	if(r->min.x==s.min.x && r->max.x==s.max.x){
		if(r->min.y == s.min.y){
			r->min.y = s.max.y;
			assert(r->max.y > r->min.y);
			return 1;
		}
		if(r->max.y == s.max.y){
			r->max.y = s.min.y;
			assert(r->max.y > r->min.y);
			return 1;
		}
	}
	return 0;
}

/*
 * If s is a corner of r, remove s from r, yielding
 * two rectangles r and rr.  R holds the part with
 * smaller coordinates.
 */
static int
rectcornersubr(Rectangle *r, Rectangle s, Rectangle *rr)
{
#	define UPRIGHT(r) Pt((r).max.x, (r).min.y)
#	define LOWLEFT(r) Pt((r).min.x, (r).max.y)

	*rr = *r;

	if(s.min.x == r->min.x){  
		if(s.min.y == r->min.y){  // upper left
			*rr = Rpt(UPRIGHT(s), r->max);
			*r = Rpt(LOWLEFT(s), LOWLEFT(*rr));
			return 1;
		}
		if(s.max.y == r->max.y){ // lower left
			*rr = Rpt(Pt(s.max.x, r->min.y), r->max);
			*r = Rpt(r->min, UPRIGHT(s));
			return 1;
		}
	}
	if(s.max.x == r->max.x){
		if(s.max.y == r->max.y){ // lower right
			*rr = Rpt(Pt(s.min.x, r->min.y), UPRIGHT(s));
			*r = Rpt(r->min, LOWLEFT(s));
			return 1;
		}
		if(s.min.y == r->min.y){ // upper right
			*rr = Rpt(LOWLEFT(s), r->max);
			*r = Rpt(r->min, LOWLEFT(*rr));
			return 1;
		}
	}
	return 0;
}

/*
 * If s is a band cutting r into two pieces, set r to one piece
 * and rr to the other.
 */
static int
recttridesubr(Rectangle *nr, Rectangle s, Rectangle *rr)
{
	*rr = *nr;
	if((nr->min.x == s.min.x && nr->max.x == s.max.x) &&
	    (nr->min.y < s.min.y && s.max.y < nr->max.y)){
		nr->max.y = s.min.y;
		rr->min.y = s.max.y;
		return 1;
	}

	if((nr->min.y == s.min.y && nr->max.y == s.max.y) &&
	    (nr->min.x < s.min.x && s.max.x < nr->max.x)){
		nr->max.x = s.min.x;
		rr->min.x = s.max.x;
		return 1;
	}
	return 0;
}

void
addtorlist(Rlist *rlist, Rectangle r)
{
	int i, j;
	Rectangle ir, cr, rr;
	Rlist tmp;

	if(r.min.x >= r.max.x || r.min.y >= r.max.y)
		return;

	memset(&tmp, 0, sizeof tmp);
	rappend(&tmp, r);
	
	if(verbose > 5)
		fprint(2, "region union add %R:\n", r);

	combinerect(&rlist->bbox, r); // must do this first
	for(j = 0; j < tmp.nrect; j++){
		r = tmp.rect[j];

		for(i=0; i < rlist->nrect; i++){
			ir = rlist->rect[i];

			if(verbose > 5)
				fprint(2, "checking %R against %R\n", r, ir);
			if(!rectadjacent(ir, r))
				continue;

			/* r is covered by ir? */
			if(rectinrect(r, ir))
				break;

			/* r covers ir? */
 			if(rectinrect(ir, r)){
				rtrim(rlist, i);
				i--;
				continue;
			}

			/* aligned and overlapping? */
			if((ir.min.y == r.min.y && ir.max.y == r.max.y) ||
		    	(ir.min.x == r.min.x && ir.max.x == r.max.x)){
				combinerect(&r, ir);
				rtrim(rlist, i);
				i--;
				continue;
			}

			/* not aligned */ 
			if(verbose > 5)
				fprint(2, "break up rect %R and %R\n", ir, r);
			/* 2->2 breakup */
			cr = ir;
			if (!rectclip(&cr, r))	/* share only one point */
				continue;

			if(rectubr(&r, cr))
				continue;

			if(rectubr(&rlist->rect[i], cr))
				continue;

			/* 2 -> 3 breakup */
			/* stride across */
			if(recttridesubr(&r, cr, &rr)){
				rappend(&tmp, rr);
				continue;
			}

			/* corner overlap */
			if(rectcornersubr(&r, cr, &rr)){
				rappend(&tmp, rr);
				continue;
			}
			abort();
		}
		if(i == rlist->nrect)
			rappend(rlist, r);
	}
	freerlist(&tmp);
	if(verbose > 5)
		rprint(rlist);
}

void
freerlist(Rlist *r)
{
	free(r->rect);
	tot -= r->maxrect;
	r->nrect = 0;
	r->maxrect = 0;
	r->rect = nil;
}

static void
rprint(Rlist *r)
{
	int i;

	fprint(2, "rlist %p:", r);
	for(i=0; i<r->nrect; i++)
		fprint(2, " %R", r->rect[i]);
	fprint(2, "\n");
}


#ifdef REGION_DEBUG

int verbose = 10;

void main(int argc, char * argv[])
{
	Rectangle r1 = Rect(0, 0, 300, 200);
	Rectangle r2 = Rect(100, 100, 400, 300);
	Rectangle r3 = Rect(200, 100, 500, 300);
	Region reg;

	initdraw(0, 0, "vncviewer");
	region_init(&reg);
	region_union(&reg, r1, r1);
	region_union(&reg, r2, r2);
	region_union(&reg, r3, r3);
}

#endif