sw_voronoi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include "sw_defs.h"
  4. /* implicit parameters: nsites, sqrt_nsites, xmin, xmax, ymin, ymax,
  5. deltax, deltay (can all be estimates).
  6. Performance suffers if they are wrong; better to make nsites,
  7. deltax, and deltay too big than too small. (?) */
  8. int voronoi(struct Site *(*nextsite) (void))
  9. {
  10. struct Site *newsite, *bot, *top, *temp, *p;
  11. struct Site *v;
  12. struct Point newintstar;
  13. int pm;
  14. struct Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
  15. struct Edge *e;
  16. int counter = 0;
  17. PQinitialize();
  18. bottomsite = (*nextsite) ();
  19. ELinitialize();
  20. newsite = (*nextsite) ();
  21. while (1) {
  22. if (!PQempty())
  23. newintstar = PQ_min();
  24. if (newsite != (struct Site *)NULL &&
  25. (PQempty() || newsite->coord.y < newintstar.y ||
  26. (newsite->coord.y == newintstar.y &&
  27. newsite->coord.x < newintstar.x))) { /* new site is smallest */
  28. G_percent(counter++, nsites, 2);
  29. lbnd = ELleftbnd(&(newsite->coord));
  30. rbnd = ELright(lbnd);
  31. bot = rightreg(lbnd);
  32. e = bisect(bot, newsite);
  33. bisector = HEcreate(e, le);
  34. ELinsert(lbnd, bisector);
  35. if ((p = intersect(lbnd, bisector)) != (struct Site *)NULL) {
  36. PQdelete(lbnd);
  37. PQinsert(lbnd, p, dist(p, newsite));
  38. }
  39. lbnd = bisector;
  40. bisector = HEcreate(e, re);
  41. ELinsert(lbnd, bisector);
  42. if ((p = intersect(bisector, rbnd)) != (struct Site *)NULL) {
  43. PQinsert(bisector, p, dist(p, newsite));
  44. }
  45. /* get next site, but ensure that it doesn't have the same
  46. coordinates as the previous. If so, step over to the following
  47. site. Andrea Aime 4/7/2001 */
  48. do
  49. temp = (*nextsite) ();
  50. while (temp != (struct Site *)NULL &&
  51. temp->coord.x == newsite->coord.x &&
  52. temp->coord.y == newsite->coord.y);
  53. newsite = temp;
  54. }
  55. else if (!PQempty()) {
  56. /* intersection is smallest */
  57. lbnd = PQextractmin();
  58. llbnd = ELleft(lbnd);
  59. rbnd = ELright(lbnd);
  60. rrbnd = ELright(rbnd);
  61. bot = leftreg(lbnd);
  62. top = rightreg(rbnd);
  63. v = lbnd->vertex;
  64. makevertex(v);
  65. endpoint(lbnd->ELedge, lbnd->ELpm, v);
  66. endpoint(rbnd->ELedge, rbnd->ELpm, v);
  67. ELdelete(lbnd);
  68. PQdelete(rbnd);
  69. ELdelete(rbnd);
  70. pm = le;
  71. if (bot->coord.y > top->coord.y) {
  72. temp = bot;
  73. bot = top;
  74. top = temp;
  75. pm = re;
  76. }
  77. e = bisect(bot, top);
  78. bisector = HEcreate(e, pm);
  79. ELinsert(llbnd, bisector);
  80. endpoint(e, re - pm, v);
  81. deref(v);
  82. if ((p = intersect(llbnd, bisector)) != (struct Site *)NULL) {
  83. PQdelete(llbnd);
  84. PQinsert(llbnd, p, dist(p, bot));
  85. }
  86. if ((p = intersect(bisector, rrbnd)) != (struct Site *)NULL) {
  87. PQinsert(bisector, p, dist(p, bot));
  88. }
  89. }
  90. else
  91. break;
  92. }
  93. G_percent(1, 1, 1);
  94. return 0;
  95. }