vo_write.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <float.h>
  4. #include <grass/gis.h>
  5. #include <grass/vector.h>
  6. #include <grass/display.h>
  7. #include "sw_defs.h"
  8. #include "defs.h"
  9. static int write_skeleton(struct line_pnts *Points)
  10. {
  11. int i, area1, area2;
  12. static struct line_pnts *APoints = NULL;
  13. static struct line_cats *Cats = NULL;
  14. if (!APoints) {
  15. APoints = Vect_new_line_struct();
  16. Cats = Vect_new_cats_struct();
  17. }
  18. if ((area1 = Vect_find_area(&In, Points->x[0], Points->y[0])) == 0)
  19. return 0;
  20. if ((area2 = Vect_find_area(&In, Points->x[1], Points->y[1])) == 0)
  21. return 0;
  22. if (area1 != area2)
  23. return 0;
  24. if (!Vect_get_area_centroid(&In, area1))
  25. return 0;
  26. Vect_get_area_points(&In, area1, APoints);
  27. if (Vect_line_check_intersection(Points, APoints, 0))
  28. return 0;
  29. for (i = 0; i < Vect_get_area_num_isles(&In, area1); i++) {
  30. Vect_get_isle_points(&In, Vect_get_area_isle(&In, area1, i), APoints);
  31. if (Vect_line_check_intersection(Points, APoints, 0))
  32. return 0;
  33. }
  34. Vect_get_area_cats(&In, area1, Cats);
  35. Vect_write_line(&Out, GV_LINE, Points, Cats);
  36. return 1;
  37. }
  38. int vo_write(void)
  39. {
  40. struct Halfedge *lbnd;
  41. for (lbnd = ELright(ELleftend); lbnd != ELrightend; lbnd = ELright(lbnd)) {
  42. write_ep(lbnd->ELedge);
  43. }
  44. /* TODO: free memory */
  45. return 1;
  46. }
  47. int write_ep(struct Edge *e)
  48. {
  49. static struct line_pnts *Points = NULL;
  50. static struct line_cats *Cats = NULL;
  51. double x1, y1, x2, y2;
  52. if (!Points) {
  53. Points = Vect_new_line_struct();
  54. Cats = Vect_new_cats_struct();
  55. }
  56. if (in_area && e->reg[le]->sitenbr == e->reg[re]->sitenbr)
  57. return 0;
  58. if (e->ep[le] != NULL && e->ep[re] != NULL) { /* both end defined */
  59. x1 = e->ep[le]->coord.x;
  60. y1 = e->ep[le]->coord.y;
  61. x2 = e->ep[re]->coord.x;
  62. y2 = e->ep[re]->coord.y;
  63. if (!Vect_point_in_box(x1, y1, 0.0, &Box) ||
  64. !Vect_point_in_box(x2, y2, 0.0, &Box)) {
  65. Vect_box_clip(&x1, &y1, &x2, &y2, &Box);
  66. }
  67. /* Don't write zero length */
  68. if (x1 == x2 && y1 == y2)
  69. return 0;
  70. Vect_reset_line(Points);
  71. Vect_append_point(Points, x1, y1, 0.0);
  72. Vect_append_point(Points, x2, y2, 0.0);
  73. if (skeleton)
  74. write_skeleton(Points);
  75. else
  76. Vect_write_line(&Out, Type, Points, Cats);
  77. }
  78. else {
  79. int knownPointAtLeft = -1;
  80. if (e->ep[le] != NULL) {
  81. x1 = e->ep[le]->coord.x;
  82. y1 = e->ep[le]->coord.y;
  83. knownPointAtLeft = 1;
  84. }
  85. else if (e->ep[re] != NULL) {
  86. x1 = e->ep[re]->coord.x;
  87. y1 = e->ep[re]->coord.y;
  88. knownPointAtLeft = 0;
  89. }
  90. if (knownPointAtLeft == -1) {
  91. x2 = (e->reg[le]->coord.x + e->reg[re]->coord.x) / 2.0;
  92. y2 = (e->reg[le]->coord.y + e->reg[re]->coord.y) / 2.0;
  93. knownPointAtLeft = 0;
  94. if (!extend_line(Box.S, Box.N, Box.W, Box.E,
  95. e->a, e->b, e->c, x2, y2, &x1, &y1,
  96. knownPointAtLeft)) {
  97. G_warning("Undefined edge, unable to extend line");
  98. return 0;
  99. }
  100. knownPointAtLeft = 1;
  101. }
  102. if (extend_line(Box.S, Box.N, Box.W, Box.E,
  103. e->a, e->b, e->c, x1, y1, &x2, &y2,
  104. knownPointAtLeft)) {
  105. /* Don't write zero length */
  106. if (x1 == x2 && y1 == y2)
  107. return 0;
  108. Vect_reset_line(Points);
  109. Vect_append_point(Points, x1, y1, 0.0);
  110. Vect_append_point(Points, x2, y2, 0.0);
  111. if (skeleton)
  112. write_skeleton(Points);
  113. else
  114. Vect_write_line(&Out, Type, Points, Cats);
  115. }
  116. }
  117. return 0;
  118. }