rlist.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "vnc.h"
  2. #include "vncs.h"
  3. static int tot;
  4. static void rprint(Rlist*);
  5. static void
  6. growrlist(Rlist *rlist, int n)
  7. {
  8. int old;
  9. if(rlist->nrect+n <= rlist->maxrect)
  10. return;
  11. old = rlist->maxrect;
  12. while(rlist->nrect+n > rlist->maxrect){
  13. if(rlist->maxrect == 0)
  14. rlist->maxrect = 16;
  15. else
  16. rlist->maxrect *= 2;
  17. }
  18. tot += rlist->maxrect - old;
  19. if(tot > 10000)
  20. sysfatal("too many rectangles");
  21. rlist->rect = realloc(rlist->rect, rlist->maxrect*sizeof(rlist->rect[0]));
  22. if(rlist->rect == nil)
  23. sysfatal("realloc failed in growrlist");
  24. }
  25. static void
  26. rappend(Rlist *rl, Rectangle r)
  27. {
  28. growrlist(rl, 1);
  29. rl->rect[rl->nrect++] = r;
  30. }
  31. /* remove rectangle i from the list */
  32. static int
  33. rtrim(Rlist *r, int i)
  34. {
  35. if(i < 0 || i >= r->nrect)
  36. return 0;
  37. if(i == r->nrect-1){
  38. r->nrect--;
  39. return 1;
  40. }
  41. r->rect[i] = r->rect[--r->nrect];
  42. return 1;
  43. }
  44. static int
  45. rectadjacent(Rectangle r, Rectangle s)
  46. {
  47. return r.min.x<=s.max.x && s.min.x<=r.max.x &&
  48. r.min.y<=s.max.y && s.min.y<=r.max.y;
  49. }
  50. /*
  51. * If s shares three edges with r, compute the
  52. * rectangle r - s and return 1.
  53. * Else return 0.
  54. */
  55. static int
  56. rectubr(Rectangle *r, Rectangle s)
  57. {
  58. if(r->min.y==s.min.y && r->max.y==s.max.y){
  59. if(r->min.x == s.min.x){
  60. r->min.x = s.max.x;
  61. assert(r->max.x > r->min.x);
  62. return 1;
  63. }
  64. if(r->max.x == s.max.x){
  65. r->max.x = s.min.x;
  66. assert(r->max.x > r->min.x);
  67. return 1;
  68. }
  69. }
  70. if(r->min.x==s.min.x && r->max.x==s.max.x){
  71. if(r->min.y == s.min.y){
  72. r->min.y = s.max.y;
  73. assert(r->max.y > r->min.y);
  74. return 1;
  75. }
  76. if(r->max.y == s.max.y){
  77. r->max.y = s.min.y;
  78. assert(r->max.y > r->min.y);
  79. return 1;
  80. }
  81. }
  82. return 0;
  83. }
  84. /*
  85. * If s is a corner of r, remove s from r, yielding
  86. * two rectangles r and rr. R holds the part with
  87. * smaller coordinates.
  88. */
  89. static int
  90. rectcornersubr(Rectangle *r, Rectangle s, Rectangle *rr)
  91. {
  92. # define UPRIGHT(r) Pt((r).max.x, (r).min.y)
  93. # define LOWLEFT(r) Pt((r).min.x, (r).max.y)
  94. *rr = *r;
  95. if(s.min.x == r->min.x){
  96. if(s.min.y == r->min.y){ // upper left
  97. *rr = Rpt(UPRIGHT(s), r->max);
  98. *r = Rpt(LOWLEFT(s), LOWLEFT(*rr));
  99. return 1;
  100. }
  101. if(s.max.y == r->max.y){ // lower left
  102. *rr = Rpt(Pt(s.max.x, r->min.y), r->max);
  103. *r = Rpt(r->min, UPRIGHT(s));
  104. return 1;
  105. }
  106. }
  107. if(s.max.x == r->max.x){
  108. if(s.max.y == r->max.y){ // lower right
  109. *rr = Rpt(Pt(s.min.x, r->min.y), UPRIGHT(s));
  110. *r = Rpt(r->min, LOWLEFT(s));
  111. return 1;
  112. }
  113. if(s.min.y == r->min.y){ // upper right
  114. *rr = Rpt(LOWLEFT(s), r->max);
  115. *r = Rpt(r->min, LOWLEFT(*rr));
  116. return 1;
  117. }
  118. }
  119. return 0;
  120. }
  121. /*
  122. * If s is a band cutting r into two pieces, set r to one piece
  123. * and rr to the other.
  124. */
  125. static int
  126. recttridesubr(Rectangle *nr, Rectangle s, Rectangle *rr)
  127. {
  128. *rr = *nr;
  129. if((nr->min.x == s.min.x && nr->max.x == s.max.x) &&
  130. (nr->min.y < s.min.y && s.max.y < nr->max.y)){
  131. nr->max.y = s.min.y;
  132. rr->min.y = s.max.y;
  133. return 1;
  134. }
  135. if((nr->min.y == s.min.y && nr->max.y == s.max.y) &&
  136. (nr->min.x < s.min.x && s.max.x < nr->max.x)){
  137. nr->max.x = s.min.x;
  138. rr->min.x = s.max.x;
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. void
  144. addtorlist(Rlist *rlist, Rectangle r)
  145. {
  146. int i, j;
  147. Rectangle ir, cr, rr;
  148. Rlist tmp;
  149. if(r.min.x >= r.max.x || r.min.y >= r.max.y)
  150. return;
  151. memset(&tmp, 0, sizeof tmp);
  152. rappend(&tmp, r);
  153. if(verbose > 5)
  154. fprint(2, "region union add %R:\n", r);
  155. combinerect(&rlist->bbox, r); // must do this first
  156. for(j = 0; j < tmp.nrect; j++){
  157. r = tmp.rect[j];
  158. for(i=0; i < rlist->nrect; i++){
  159. ir = rlist->rect[i];
  160. if(verbose > 5)
  161. fprint(2, "checking %R against %R\n", r, ir);
  162. if(!rectadjacent(ir, r))
  163. continue;
  164. /* r is covered by ir? */
  165. if(rectinrect(r, ir))
  166. break;
  167. /* r covers ir? */
  168. if(rectinrect(ir, r)){
  169. rtrim(rlist, i);
  170. i--;
  171. continue;
  172. }
  173. /* aligned and overlapping? */
  174. if((ir.min.y == r.min.y && ir.max.y == r.max.y) ||
  175. (ir.min.x == r.min.x && ir.max.x == r.max.x)){
  176. combinerect(&r, ir);
  177. rtrim(rlist, i);
  178. i--;
  179. continue;
  180. }
  181. /* not aligned */
  182. if(verbose > 5)
  183. fprint(2, "break up rect %R and %R\n", ir, r);
  184. /* 2->2 breakup */
  185. cr = ir;
  186. if (!rectclip(&cr, r)) /* share only one point */
  187. continue;
  188. if(rectubr(&r, cr))
  189. continue;
  190. if(rectubr(&rlist->rect[i], cr))
  191. continue;
  192. /* 2 -> 3 breakup */
  193. /* stride across */
  194. if(recttridesubr(&r, cr, &rr)){
  195. rappend(&tmp, rr);
  196. continue;
  197. }
  198. /* corner overlap */
  199. if(rectcornersubr(&r, cr, &rr)){
  200. rappend(&tmp, rr);
  201. continue;
  202. }
  203. abort();
  204. }
  205. if(i == rlist->nrect)
  206. rappend(rlist, r);
  207. }
  208. freerlist(&tmp);
  209. if(verbose > 5)
  210. rprint(rlist);
  211. }
  212. void
  213. freerlist(Rlist *r)
  214. {
  215. free(r->rect);
  216. tot -= r->maxrect;
  217. r->nrect = 0;
  218. r->maxrect = 0;
  219. r->rect = nil;
  220. }
  221. static void
  222. rprint(Rlist *r)
  223. {
  224. int i;
  225. fprint(2, "rlist %p:", r);
  226. for(i=0; i<r->nrect; i++)
  227. fprint(2, " %R", r->rect[i]);
  228. fprint(2, "\n");
  229. }
  230. #ifdef REGION_DEBUG
  231. int verbose = 10;
  232. void main(int argc, char * argv[])
  233. {
  234. Rectangle r1 = Rect(0, 0, 300, 200);
  235. Rectangle r2 = Rect(100, 100, 400, 300);
  236. Rectangle r3 = Rect(200, 100, 500, 300);
  237. Region reg;
  238. initdraw(0, 0, "vncviewer");
  239. region_init(&reg);
  240. region_union(&reg, r1, r1);
  241. region_union(&reg, r2, r2);
  242. region_union(&reg, r3, r3);
  243. }
  244. #endif