clip.c 3.5 KB

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