box.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Radim Blazek
  7. *
  8. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  9. *
  10. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdlib.h>
  18. #include <grass/vector.h>
  19. /*
  20. * dig_line_box ()
  21. * set box to points extent
  22. */
  23. int dig_line_box(const struct line_pnts *Points, struct bound_box * Box)
  24. {
  25. int i;
  26. if (Points->n_points <= 0) {
  27. G_zero(Box, sizeof(struct bound_box));
  28. return 0;
  29. }
  30. Box->E = Points->x[0];
  31. Box->W = Points->x[0];
  32. Box->N = Points->y[0];
  33. Box->S = Points->y[0];
  34. Box->T = Points->z[0];
  35. Box->B = Points->z[0];
  36. for (i = 1; i < Points->n_points; i++) {
  37. if (Points->x[i] > Box->E)
  38. Box->E = Points->x[i];
  39. else if (Points->x[i] < Box->W)
  40. Box->W = Points->x[i];
  41. if (Points->y[i] > Box->N)
  42. Box->N = Points->y[i];
  43. else if (Points->y[i] < Box->S)
  44. Box->S = Points->y[i];
  45. if (Points->z[i] > Box->T)
  46. Box->T = Points->z[i];
  47. else if (Points->z[i] < Box->B)
  48. Box->B = Points->z[i];
  49. }
  50. return 1;
  51. }
  52. /*
  53. * dig_box_copy ()
  54. * Copy B to A.
  55. */
  56. int dig_box_copy(struct bound_box * A, struct bound_box * B)
  57. {
  58. A->N = B->N;
  59. A->S = B->S;
  60. A->E = B->E;
  61. A->W = B->W;
  62. A->T = B->T;
  63. A->B = B->B;
  64. return 1;
  65. }
  66. /*
  67. * dig_box_extend ()
  68. * Extend A by B.
  69. */
  70. int dig_box_extend(struct bound_box * A, struct bound_box * B)
  71. {
  72. if (B->N > A->N)
  73. A->N = B->N;
  74. if (B->S < A->S)
  75. A->S = B->S;
  76. if (B->E > A->E)
  77. A->E = B->E;
  78. if (B->W < A->W)
  79. A->W = B->W;
  80. if (B->T > A->T)
  81. A->T = B->T;
  82. if (B->B < A->B)
  83. A->B = B->B;
  84. return 1;
  85. }