clip.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <math.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/display.h>
  5. #include <grass/glocale.h>
  6. #include "path.h"
  7. #include "clip.h"
  8. /******************************************************************************/
  9. static double dist_plane(double x, double y, const struct plane *p)
  10. {
  11. return x * p->x + y * p->y + p->k;
  12. }
  13. static double interpolate(double a, double b, double ka, double kb)
  14. {
  15. return (a * kb - b * ka) / (kb - ka);
  16. }
  17. static void clip_path_plane(struct path *dst, const struct path *src,
  18. const struct plane *p)
  19. {
  20. struct vertex *v0 = &src->vertices[src->count - 1];
  21. double x0 = v0->x;
  22. double y0 = v0->y;
  23. double d0 = dist_plane(x0, y0, p);
  24. int i;
  25. path_reset(dst);
  26. for (i = 0; i < src->count; i++) {
  27. struct vertex *v1 = &src->vertices[i];
  28. double x1 = v1->x;
  29. double y1 = v1->y;
  30. double d1 = dist_plane(x1, y1, p);
  31. int in0 = d0 <= 0;
  32. int in1 = d1 <= 0;
  33. if (in0 && !in1) {
  34. /* leaving */
  35. double x = interpolate(x0, x1, d0, d1);
  36. double y = interpolate(y0, y1, d0, d1);
  37. path_cont(dst, x, y);
  38. }
  39. if (!in0 && in1) {
  40. /* entering */
  41. double x = interpolate(x0, x1, d0, d1);
  42. double y = interpolate(y0, y1, d0, d1);
  43. path_move(dst, x, y);
  44. }
  45. if (in1)
  46. /* inside */
  47. path_cont(dst, x1, y1);
  48. x0 = x1;
  49. y0 = y1;
  50. d0 = d1;
  51. }
  52. }
  53. /******************************************************************************/
  54. static void cull_path_plane(struct path *dst, const struct path *src,
  55. const struct plane *p)
  56. {
  57. int last = -1;
  58. int prev = src->count - 1;
  59. struct vertex *v0 = &src->vertices[prev];
  60. double x0 = v0->x;
  61. double y0 = v0->y;
  62. double d0 = dist_plane(x0, y0, p);
  63. int i;
  64. path_reset(dst);
  65. for (i = 0; i < src->count; i++) {
  66. struct vertex *v1 = &src->vertices[i];
  67. double x1 = v1->x;
  68. double y1 = v1->y;
  69. double d1 = dist_plane(x1, y1, p);
  70. int in0 = d0 <= 0;
  71. int in1 = d1 <= 0;
  72. if (!in0 && in1 && last != prev) {
  73. /* entering */
  74. path_move(dst, x0, y0);
  75. last = prev;
  76. }
  77. if (in1 || in0) {
  78. /* inside or leaving */
  79. path_cont(dst, x1, y1);
  80. last = i;
  81. }
  82. x0 = x1;
  83. y0 = y1;
  84. d0 = d1;
  85. prev = i;
  86. }
  87. }
  88. /******************************************************************************/
  89. void D__set_clip_planes(struct clip *clip, const struct rectangle *rect)
  90. {
  91. clip->left.x = -1;
  92. clip->left.y = 0;
  93. clip->left.k = rect->left;
  94. clip->rite.x = 1;
  95. clip->rite.y = 0;
  96. clip->rite.k = -rect->rite;
  97. clip->bot.x = 0;
  98. clip->bot.y = -1;
  99. clip->bot.k = rect->bot;
  100. clip->top.x = 0;
  101. clip->top.y = 1;
  102. clip->top.k = -rect->top;
  103. }
  104. void D__cull_path(struct path *dst, const struct path *src, const struct clip *clip)
  105. {
  106. struct path tmp1, tmp2;
  107. path_init(&tmp1);
  108. path_init(&tmp2);
  109. cull_path_plane(&tmp1, src, &clip->left);
  110. cull_path_plane(&tmp2, &tmp1, &clip->rite);
  111. cull_path_plane(&tmp1, &tmp2, &clip->bot);
  112. cull_path_plane(dst, &tmp1, &clip->top);
  113. path_free(&tmp1);
  114. path_free(&tmp2);
  115. }
  116. void D__clip_path(struct path *dst, const struct path *src, const struct clip *clip)
  117. {
  118. struct path tmp1, tmp2;
  119. path_init(&tmp1);
  120. path_init(&tmp2);
  121. clip_path_plane(&tmp1, src, &clip->left);
  122. clip_path_plane(&tmp2, &tmp1, &clip->rite);
  123. clip_path_plane(&tmp1, &tmp2, &clip->bot);
  124. clip_path_plane(dst, &tmp1, &clip->top);
  125. path_free(&tmp1);
  126. path_free(&tmp2);
  127. }
  128. /******************************************************************************/