region.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include "region.h"
  5. extern int verbose;
  6. #define OKRECT(r) ((r).min.x < (r).max.x && (r).min.y < (r).max.y)
  7. extern void region_print(Region *);
  8. static tot_rects = 0;
  9. static void region_alloc(RegData * pReg, int howmany)
  10. {
  11. int old = pReg->size;
  12. if( pReg->nrects + howmany <= pReg->size )
  13. return;
  14. do {
  15. if( pReg->size == 0 )
  16. pReg->size = 16;
  17. else
  18. pReg->size <<= 1;
  19. } while( pReg->nrects + howmany >= pReg->size );
  20. tot_rects += pReg->size - old;
  21. if( tot_rects > 10000 ) {
  22. fprint(2, "too many rectangles\n");
  23. while(1) sleep(10000);
  24. abort();
  25. }
  26. pReg->rects = realloc( pReg->rects, pReg->size*sizeof(Rectangle) );
  27. if( pReg->rects == nil )
  28. sysfatal("region_apprend: realloc failed %r\n");
  29. }
  30. static void region_append(Region * pReg, Rectangle r)
  31. {
  32. region_alloc(&pReg->RegData, 1);
  33. pReg->rects[ pReg->nrects++ ] = r;
  34. }
  35. void region_init(Region * pReg)
  36. {
  37. pReg->extents = Rect(0,0,0,0);
  38. pReg->size = pReg->nrects = 0;
  39. pReg->rects = nil;
  40. }
  41. /* trim the ith item */
  42. int region_trim(Region * pReg, int i)
  43. {
  44. int last = pReg->nrects-1;
  45. if( last < 0 || i < 0 || i > last )
  46. return 0;
  47. // assert( last >=0 && && i >= 0 && i <= last );
  48. if( i != last )
  49. pReg->rects[i] = pReg->rects[last];
  50. pReg->nrects--;
  51. return 1;
  52. }
  53. /* adjacency check, differ from rectXrect by one pixel */
  54. static int
  55. rectadjacent(Rectangle r, Rectangle s)
  56. {
  57. return r.min.x<=s.max.x && s.min.x<=r.max.x &&
  58. r.min.y<=s.max.y && s.min.y<=r.max.y;
  59. }
  60. /* s is inside of nr share one edge.
  61. * if Dz(s) == Dz(*nr), then
  62. * compute *nr = *nr - s
  63. */
  64. static int
  65. rectsubr(Rectangle *nr, Rectangle s)
  66. {
  67. if( nr->min.y == s.min.y && nr->max.y == s.max.y ) {
  68. if( nr->min.x == s.min.x ) {
  69. nr->min.x = s.max.x;
  70. assert(nr->max.x > nr->min.x);
  71. return 1;
  72. }
  73. if( nr->max.x == s.max.x ) {
  74. nr->max.x = s.min.x;
  75. assert(nr->max.x > nr->min.x);
  76. return 1;
  77. }
  78. }
  79. if( nr->min.x == s.min.x && nr->max.x == s.max.x ) {
  80. if( nr->min.y == s.min.y ) {
  81. nr->min.y = s.max.y;
  82. assert(nr->max.y > nr->min.y);
  83. return 1;
  84. }
  85. if( nr->max.y == s.max.y ) {
  86. nr->max.y = s.min.y;
  87. assert(nr->max.y > nr->min.y);
  88. return 1;
  89. }
  90. }
  91. return 0;
  92. }
  93. #define UPRIGHT(r) Pt((r).max.x, (r).min.y)
  94. #define LOWLEFT(r) Pt((r).min.x, (r).max.y)
  95. /* s is a corner of nr, Dz(s) < Dz(*nr) for z=x,y
  96. * cut it so that *nr = (*nr - s - *rr)
  97. * cut along Y, *nr holds the left part
  98. */
  99. static int
  100. rectcornersubr(Rectangle *nr, Rectangle s, Rectangle *rr)
  101. {
  102. *rr = *nr;
  103. if( s.min.x == nr->min.x ) {
  104. if( s.min.y == nr->min.y ) { // upper left
  105. *rr = Rpt(UPRIGHT(s), nr->max);
  106. *nr = Rpt(LOWLEFT(s), LOWLEFT(*rr));
  107. return 1;
  108. }
  109. if( s.max.y == nr->max.y ) { // lower left
  110. *rr = Rpt(Pt(s.max.x, nr->min.y), nr->max);
  111. *nr = Rpt(nr->min, UPRIGHT(s));
  112. return 1;
  113. }
  114. }
  115. if( s.max.x == nr->max.x ) {
  116. if( s.max.y == nr->max.y ) { // lower right
  117. *rr = Rpt(Pt(s.min.x, nr->min.y), UPRIGHT(s));
  118. *nr = Rpt(nr->min, LOWLEFT(s));
  119. return 1;
  120. }
  121. if( s.min.y == nr->min.y ) { // upper right
  122. *rr = Rpt(LOWLEFT(s), nr->max);
  123. *nr = Rpt(nr->min, LOWLEFT(*rr));
  124. return 1;
  125. }
  126. }
  127. return 0;
  128. }
  129. /* check if s is a band cutting nr in two pieces, Dz(s) == Dz(*nr) for *one* of x and y
  130. * if so, cut it so that *nr = (*nr - s - *rr)
  131. */
  132. static int
  133. rectstridesubr(Rectangle *nr, Rectangle s, Rectangle *rr)
  134. {
  135. *rr = *nr;
  136. if( (nr->min.x == s.min.x && nr->max.x == s.max.x) &&
  137. (nr->min.y < s.min.y && s.max.y < nr->max.y) ) {
  138. nr->max.y = s.min.y;
  139. rr->min.y = s.max.y;
  140. return 1;
  141. }
  142. if( (nr->min.y == s.min.y && nr->max.y == s.max.y) &&
  143. (nr->min.x < s.min.x && s.max.x < nr->max.x) ) {
  144. nr->max.x = s.min.x;
  145. rr->min.x = s.max.x;
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. /* allow partial overlaping */
  151. void region_union(Region * pReg, Rectangle r, Rectangle clipr)
  152. {
  153. int i, j;
  154. Rectangle ir, cr, rr;
  155. Region rreg;
  156. if( ! OKRECT(r) )
  157. return;
  158. if( !rectclip(&r, clipr) )
  159. return;
  160. if( verbose > 5 )
  161. fprint(2, "region union add %R:\n", r);
  162. region_init(&rreg);
  163. region_append(&rreg, r);
  164. combinerect(&pReg->extents, r); // must do this first
  165. for(j = 0; j < rreg.nrects; j++) {
  166. r = rreg.rects[j];
  167. for(i=0; i < pReg->nrects; i++) {
  168. ir = pReg->rects[i];
  169. if(verbose > 5)
  170. fprint(2, "checking %R against %R\n", r, ir);
  171. if( ! rectadjacent(ir, r) )
  172. continue;
  173. if( rectinrect(r, ir) ) // r is covered
  174. break;
  175. if( rectinrect(ir, r) ) { // r covers
  176. region_trim(pReg, i);
  177. i--; // scan the new item in i or breakout
  178. continue;
  179. }
  180. // X/Y aligned and overlaping
  181. if( (ir.min.y == r.min.y && ir.max.y == r.max.y ) ||
  182. (ir.min.x == r.min.x && ir.max.x == r.max.x ) ) {
  183. combinerect(&r, ir);
  184. region_trim(pReg, i);
  185. i--;
  186. continue;
  187. }
  188. // not aligned
  189. if( verbose > 5 ) fprint(2, "break up rect %R and %R\n", ir, r);
  190. // 2 -> 2 breakup
  191. cr = ir;
  192. if ( !rectclip(&cr, r) ) // share one point only
  193. continue;
  194. if( rectsubr(&r, cr) )
  195. continue;
  196. if( rectsubr(&pReg->rects[i], cr) )
  197. continue;
  198. // 2 -> 3 breakup
  199. // stride across
  200. if( rectstridesubr(&r, cr, &rr) ) {
  201. region_append(&rreg, rr);
  202. continue;
  203. }
  204. // corner overlap
  205. if( rectcornersubr(&r, cr, &rr) ) {
  206. region_append(&rreg, rr);
  207. continue;
  208. }
  209. sysfatal("should not be here\n");
  210. }
  211. if( i == pReg->nrects )
  212. region_append(pReg, r);
  213. }
  214. region_reset(&rreg);
  215. if(verbose > 5)
  216. region_print(pReg);
  217. }
  218. void region_reset(Region * pReg)
  219. {
  220. tot_rects -= pReg->size;
  221. if( pReg->size )
  222. free(pReg->rects);
  223. region_init(pReg);
  224. }
  225. /* make a copy, override dst */
  226. void region_copy(Region * dst, Region *src)
  227. {
  228. region_alloc(&dst->RegData, src->nrects - dst->nrects);
  229. memcpy(dst->rects, src->rects, src->nrects * sizeof(Rectangle));
  230. dst->extents = src->extents;
  231. dst->nrects = src->nrects;
  232. }
  233. void region_print(Region * pReg)
  234. {
  235. int i;
  236. fprint(2, "region %p: ", pReg);
  237. for(i = 0; i < pReg->nrects; i++) {
  238. fprint(2, "%R ", pReg->rects[i]);
  239. }
  240. fprint(2, "\n");
  241. }
  242. #ifdef REGION_DEBUG
  243. int verbose = 10;
  244. void main(int argc, char * argv[])
  245. {
  246. Rectangle r1 = Rect(0, 0, 300, 200);
  247. Rectangle r2 = Rect(100, 100, 400, 300);
  248. Rectangle r3 = Rect(200, 100, 500, 300);
  249. Region reg;
  250. initdraw(0, 0, "vncviewer");
  251. region_init(&reg);
  252. region_union(&reg, r1, r1);
  253. region_union(&reg, r2, r2);
  254. region_union(&reg, r3, r3);
  255. }
  256. #endif