overlap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * overlap.c - Test for overlaps
  3. *
  4. * Written 2009, 2010 by Werner Almesberger
  5. * Copyright 2009, 2010 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <stdlib.h>
  13. #include "coord.h"
  14. #include "obj.h"
  15. #include "inst.h"
  16. #include "overlap.h"
  17. /*
  18. * @@@ result may be too optimistic if "b" is a rounded pad
  19. */
  20. int inside(const struct inst *a, const struct inst *b)
  21. {
  22. struct coord min_a, max_a;
  23. struct coord min_b, max_b;
  24. min_a = a->base;
  25. switch (a->obj->type) {
  26. case ot_pad:
  27. max_a = a->u.pad.other;
  28. break;
  29. case ot_hole:
  30. max_a = a->u.hole.other;
  31. break;
  32. default:
  33. abort();
  34. }
  35. sort_coord(&min_a, &max_a);
  36. min_b = b->base;
  37. switch (b->obj->type) {
  38. case ot_pad:
  39. max_b = b->u.pad.other;
  40. break;
  41. case ot_hole:
  42. max_b = b->u.hole.other;
  43. break;
  44. default:
  45. abort();
  46. }
  47. sort_coord(&min_b, &max_b);
  48. return min_a.x >= min_b.x && max_a.x <= max_b.x &&
  49. min_a.y >= min_b.y && max_a.y <= max_b.y;
  50. }
  51. /* ----- Overlap test for primitives --------------------------------------- */
  52. struct shape {
  53. int circle;
  54. struct coord center;
  55. unit_type r;
  56. struct coord min, max;
  57. };
  58. static int circle_circle_overlap(struct coord c1, unit_type r1,
  59. struct coord c2, unit_type r2, enum allow_overlap allow)
  60. {
  61. if (allow == ao_touch)
  62. return dist_point(c1, c2) < r1+r2;
  63. return dist_point(c1, c2) <= r1+r2;
  64. }
  65. static int circle_rect_overlap(struct coord c, unit_type r,
  66. struct coord min, struct coord max, enum allow_overlap allow)
  67. {
  68. if (allow == ao_touch) {
  69. if (c.x <= min.x-r || c.x >= max.x+r)
  70. return 0;
  71. if (c.y <= min.y-r || c.y >= max.y+r)
  72. return 0;
  73. }
  74. if (c.x < min.x-r || c.x > max.x+r)
  75. return 0;
  76. if (c.y < min.y-r || c.y > max.y+r)
  77. return 0;
  78. return 1;
  79. }
  80. static int rect_rect_overlap(struct coord min1, struct coord max1,
  81. struct coord min2, struct coord max2, enum allow_overlap allow)
  82. {
  83. if (allow == ao_touch) {
  84. if (max1.x <= min2.x || max2.x <= min1.x)
  85. return 0;
  86. if (max1.y <= min2.y || max2.y <= min1.y)
  87. return 0;
  88. }
  89. if (max1.x < min2.x || max2.x < min1.x)
  90. return 0;
  91. if (max1.y < min2.y || max2.y < min1.y)
  92. return 0;
  93. return 1;
  94. }
  95. static int shapes_overlap(const struct shape *a, const struct shape *b,
  96. enum allow_overlap allow)
  97. {
  98. if (a->circle && !b->circle)
  99. return shapes_overlap(b, a, allow);
  100. if (a->circle) /* b must be circle, too */
  101. return circle_circle_overlap(a->center, a->r, b->center, b->r,
  102. allow);
  103. if (b->circle) /* a must be rect */
  104. return circle_rect_overlap(b->center, b->r, a->min, a->max,
  105. allow);
  106. return rect_rect_overlap(a->min, a->max, b->min, b->max, allow);
  107. }
  108. /* ----- Recursive overlap tester ------------------------------------------ */
  109. static int test_overlap(const struct inst *a, const struct inst *b,
  110. const struct shape *other, enum allow_overlap allow);
  111. static int do_circle(const struct inst *next, const struct shape *other,
  112. unit_type x, unit_type y, unit_type r, enum allow_overlap allow)
  113. {
  114. struct shape shape = {
  115. .circle = 1,
  116. .center = {
  117. .x = x,
  118. .y = y,
  119. },
  120. .r = r,
  121. };
  122. if (next)
  123. return test_overlap(next, NULL, &shape, allow);
  124. return shapes_overlap(other, &shape, allow);
  125. }
  126. static int do_rect(const struct inst *next, const struct shape *other,
  127. unit_type x, unit_type y, unit_type w, unit_type h,
  128. enum allow_overlap allow)
  129. {
  130. struct shape shape = {
  131. .circle = 0,
  132. .min = {
  133. .x = x,
  134. .y = y,
  135. },
  136. .max = {
  137. .x = x+w,
  138. .y = y+h,
  139. },
  140. };
  141. if (next)
  142. return test_overlap(next, NULL, &shape, allow);
  143. return shapes_overlap(other, &shape, allow);
  144. }
  145. static int test_overlap(const struct inst *a, const struct inst *b,
  146. const struct shape *other, enum allow_overlap allow)
  147. {
  148. struct coord min, max;
  149. unit_type h, w, r;
  150. int rounded;
  151. min = a->base;
  152. switch (a->obj->type) {
  153. case ot_pad:
  154. max = a->u.pad.other;
  155. rounded = a->obj->u.pad.rounded;
  156. break;
  157. case ot_hole:
  158. max = a->u.hole.other;
  159. rounded = 1;
  160. break;
  161. default:
  162. abort();
  163. }
  164. sort_coord(&min, &max);
  165. h = max.y-min.y;
  166. w = max.x-min.x;
  167. if (!rounded)
  168. return do_rect(b, other, min.x, min.y, w, h, allow);
  169. if (h > w) {
  170. r = w/2;
  171. return do_circle(b, other, min.x+r, max.y-r, r, allow) ||
  172. do_rect(b, other, min.x, min.y+r, w, h-2*r, allow) ||
  173. do_circle(b, other, min.x+r, min.y+r, r, allow);
  174. } else {
  175. r = h/2;
  176. return do_circle(b, other, min.x+r, min.y+r, r, allow) ||
  177. do_rect(b, other, min.x+r, min.y, w-2*r, h, allow) ||
  178. do_circle(b, other, max.x-r, min.y+r, r, allow);
  179. }
  180. }
  181. int overlap(const struct inst *a, const struct inst *b,
  182. enum allow_overlap allow)
  183. {
  184. if (allow == ao_any)
  185. return 0;
  186. return test_overlap(a, b, NULL, allow);
  187. }