line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*!
  2. \file vector/Vlib/line.c
  3. \brief Vector library - vector feature geometry
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  8. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  9. \author Some updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
  10. */
  11. #include <grass/config.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <grass/gis.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Creates and initializes a struct line_pnts (internal use only)
  19. Use Vect_new_line_struct() instead.
  20. \return pointer to allocated line_pnts structure
  21. \return NULL on error
  22. */
  23. struct line_pnts *Vect__new_line_struct(void);
  24. /*!
  25. \brief Creates and initializes a line_pnts structure
  26. This structure is used for reading and writing vector lines and
  27. polygons. The library routines handle all memory allocation. If 3
  28. lines in memory are needed at the same time, then simply 3 line_pnts
  29. structures have to be used.
  30. Calls G_fatal_error() on error.
  31. \return pointer to line_pnts
  32. */
  33. struct line_pnts *Vect_new_line_struct()
  34. {
  35. struct line_pnts *p;
  36. if (NULL == (p = Vect__new_line_struct()))
  37. G_fatal_error("Vect_new_line_struct(): %s", _("Out of memory"));
  38. return p;
  39. }
  40. struct line_pnts *Vect__new_line_struct()
  41. {
  42. struct line_pnts *p;
  43. p = (struct line_pnts *)malloc(sizeof(struct line_pnts));
  44. /* alloc_points MUST be initialized to zero */
  45. if (p)
  46. p->alloc_points = p->n_points = 0;
  47. if (p)
  48. p->x = p->y = p->z = NULL;
  49. return p;
  50. }
  51. /*!
  52. \brief Frees all memory associated with a line_pnts structure,
  53. including the structure itself
  54. \param p pointer to line_pnts structure
  55. */
  56. void Vect_destroy_line_struct(struct line_pnts *p)
  57. {
  58. if (p) { /* probably a moot test */
  59. if (p->alloc_points) {
  60. G_free((char *)p->x);
  61. G_free((char *)p->y);
  62. G_free((char *)p->z);
  63. }
  64. G_free((char *)p);
  65. }
  66. }
  67. /*!
  68. \brief Copy points from array to line_pnts structure
  69. \param Points pointer to line_ptns structure
  70. \param x,y,z array of coordinates
  71. \param number of points to be copied
  72. \return 0 on success
  73. \return -1 on out of memory
  74. */
  75. int Vect_copy_xyz_to_pnts(struct line_pnts *Points, const double *x, const double *y,
  76. const double *z, int n)
  77. {
  78. int i;
  79. if (0 > dig_alloc_points(Points, n))
  80. return -1;
  81. for (i = 0; i < n; i++) {
  82. Points->x[i] = x[i];
  83. Points->y[i] = y[i];
  84. if (z != NULL)
  85. Points->z[i] = z[i];
  86. else
  87. Points->z[i] = 0;
  88. Points->n_points = n;
  89. }
  90. return 0;
  91. }
  92. /*!
  93. \brief Reset line
  94. Make sure line structure is clean to be re-used, i.e. it has no
  95. points associated with it Points must have previously been created
  96. with Vect_new_line_struct().
  97. \param Points pointer to line_pnts structure to be reset
  98. */
  99. void Vect_reset_line(struct line_pnts *Points)
  100. {
  101. Points->n_points = 0;
  102. }
  103. /*!
  104. \brief Appends one point to the end of a line.
  105. If you are re-using a line struct, be sure to clear out old data
  106. first by calling Vect_reset_line().
  107. \param Points pointer to line_pnts structure
  108. \param x,y,z point coordinates to be added
  109. \return number of points
  110. \return -1 on error (out of memory)
  111. */
  112. int Vect_append_point(struct line_pnts *Points, double x, double y, double z)
  113. {
  114. register int n;
  115. if (0 > dig_alloc_points(Points, Points->n_points + 1))
  116. return (-1);
  117. n = Points->n_points;
  118. Points->x[n] = x;
  119. Points->y[n] = y;
  120. Points->z[n] = z;
  121. return ++(Points->n_points);
  122. }
  123. /*!
  124. \brief Insert new point at index position and move all old points
  125. at that position and above up
  126. \param Points pointer to line_pnts structure
  127. \param index (from 0 to Points->n_points-1)
  128. \param x,y,z point coordinates
  129. \return number of points
  130. \return -1 on error (alocation)
  131. */
  132. int Vect_line_insert_point(struct line_pnts *Points, int index, double x,
  133. double y, double z)
  134. {
  135. int n;
  136. if (index < 0 || index > Points->n_points - 1)
  137. G_fatal_error("Vect_line_insert_point(): %s",
  138. _("Index out of range in"));
  139. if (0 > dig_alloc_points(Points, Points->n_points + 1))
  140. return -1;
  141. /* move up */
  142. for (n = Points->n_points; n > index; n--) {
  143. Points->x[n] = Points->x[n - 1];
  144. Points->y[n] = Points->y[n - 1];
  145. Points->z[n] = Points->z[n - 1];
  146. }
  147. Points->x[index] = x;
  148. Points->y[index] = y;
  149. Points->z[index] = z;
  150. return ++(Points->n_points);
  151. }
  152. /*!
  153. \brief Delete point at given index and move all points above down
  154. \param Points pointer to line_pnts structure
  155. \param index point (from 0 to Points->n_points-1)
  156. \return number of points
  157. */
  158. int Vect_line_delete_point(struct line_pnts *Points, int index)
  159. {
  160. int n;
  161. if (index < 0 || index > Points->n_points - 1)
  162. G_fatal_error("Vect_line_insert_point(): %s",
  163. _("Index out of range in"));
  164. if (Points->n_points == 0)
  165. return 0;
  166. /* move down */
  167. for (n = index; n < Points->n_points - 1; n++) {
  168. Points->x[n] = Points->x[n + 1];
  169. Points->y[n] = Points->y[n + 1];
  170. Points->z[n] = Points->z[n + 1];
  171. }
  172. return --(Points->n_points);
  173. }
  174. /*!
  175. \brief Get line point of given index
  176. Calls G_fatal_error() when index is not range in.
  177. \param Points pointer to line_pnts structure
  178. \param index point index (from 0 to Points->n_points-1)
  179. \param x pointer to x coordinate or NULL
  180. \param y pointer to y coordinate or NULL
  181. \param z pointer to z coordinate or NULL
  182. \return number of points
  183. */
  184. int Vect_line_get_point(const struct line_pnts *Points, int index,
  185. double *x, double *y, double *z)
  186. {
  187. if (index < 0 || index > Points->n_points - 1)
  188. G_fatal_error("Vect_line_get_point(): %s",
  189. _("Index out of range in"));
  190. if (x)
  191. *x = Points->x[index];
  192. if (y)
  193. *y = Points->y[index];
  194. if (z)
  195. *z = Points->z[index];
  196. return Points->n_points;
  197. }
  198. /*!
  199. \brief Get number of line points
  200. \param Points pointer to line_pnts structure
  201. \return number of points
  202. */
  203. int Vect_get_num_line_points(const struct line_pnts *Points)
  204. {
  205. return Points->n_points;
  206. }
  207. /*!
  208. \brief Remove duplicate points, i.e. zero length segments
  209. \param Points pointer to line_pnts structure
  210. \return number of points
  211. */
  212. int Vect_line_prune(struct line_pnts *Points)
  213. {
  214. int i, j;
  215. if (Points->n_points > 0) {
  216. j = 1;
  217. for (i = 1; i < Points->n_points; i++) {
  218. if (Points->x[i] != Points->x[j - 1] ||
  219. Points->y[i] != Points->y[j - 1]
  220. || Points->z[i] != Points->z[j - 1]) {
  221. Points->x[j] = Points->x[i];
  222. Points->y[j] = Points->y[i];
  223. Points->z[j] = Points->z[i];
  224. j++;
  225. }
  226. }
  227. Points->n_points = j;
  228. }
  229. return (Points->n_points);
  230. }
  231. /*!
  232. \brief Remove points in threshold
  233. \param Points pointer to line_pnts structure
  234. \param threshold threshold value
  235. \return number of points in result
  236. */
  237. int Vect_line_prune_thresh(struct line_pnts *Points, double threshold)
  238. {
  239. int ret;
  240. ret = dig_prune(Points, threshold);
  241. if (ret < Points->n_points)
  242. Points->n_points = ret;
  243. return (Points->n_points);
  244. }
  245. /*!
  246. \brief Appends points to the end of a line.
  247. Note, this will append to whatever is in line_pnts structure. If you
  248. are re-using a line struct, be sure to clear out old data first by
  249. calling Vect_reset_line().
  250. \param Points pointer to line_pnts structure
  251. \param APoints points to be included
  252. \param direction direction (GV_FORWARD, GV_BACKWARD)
  253. \return new number of points
  254. \return -1 on out of memory
  255. */
  256. int Vect_append_points(struct line_pnts *Points, const struct line_pnts *APoints,
  257. int direction)
  258. {
  259. int i, n, on, an;
  260. on = Points->n_points;
  261. an = APoints->n_points;
  262. n = on + an;
  263. /* Should be OK, dig_alloc_points calls realloc */
  264. if (0 > dig_alloc_points(Points, n))
  265. return (-1);
  266. if (direction == GV_FORWARD) {
  267. for (i = 0; i < an; i++) {
  268. Points->x[on + i] = APoints->x[i];
  269. Points->y[on + i] = APoints->y[i];
  270. Points->z[on + i] = APoints->z[i];
  271. }
  272. }
  273. else {
  274. for (i = 0; i < an; i++) {
  275. Points->x[on + i] = APoints->x[an - i - 1];
  276. Points->y[on + i] = APoints->y[an - i - 1];
  277. Points->z[on + i] = APoints->z[an - i - 1];
  278. }
  279. }
  280. Points->n_points = n;
  281. return n;
  282. }
  283. /*!
  284. \brief Copy points from line structure to array
  285. x/y/z arrays MUST be at least as large as Points->n_points
  286. Also note that n is a pointer to int.
  287. \param Points pointer to line_pnts structure
  288. \param x,y,z coordinates arrays
  289. \param n number of points
  290. \return number of points copied
  291. */
  292. int Vect_copy_pnts_to_xyz(const struct line_pnts *Points, double *x, double *y,
  293. double *z, int *n)
  294. {
  295. int i;
  296. for (i = 0; i < *n; i++) {
  297. x[i] = Points->x[i];
  298. y[i] = Points->y[i];
  299. if (z != NULL)
  300. z[i] = Points->z[i];
  301. *n = Points->n_points;
  302. }
  303. return (Points->n_points);
  304. }
  305. /*!
  306. \brief Find point on line in the specified distance.
  307. From the begining, measured along line.
  308. If the distance is greater than line length or negative, error is returned.
  309. \param Points pointer to line_pnts structure
  310. \param distance distance value
  311. \param x,y,z pointers to point coordinates or NULL
  312. \param angle pointer to angle of line in that point (radians, counter clockwise from x axis) or NULL
  313. \param slope pointer to slope angle in radians (positive up)
  314. \return number of segment the point is on (first is 1),
  315. \return 0 error when point is outside the line
  316. */
  317. int Vect_point_on_line(const struct line_pnts *Points, double distance,
  318. double *x, double *y, double *z, double *angle,
  319. double *slope)
  320. {
  321. int j, np, seg = 0;
  322. double dist = 0, length;
  323. double xp = 0, yp = 0, zp = 0, dx = 0, dy = 0, dz = 0, dxy =
  324. 0, dxyz, k, rest;
  325. G_debug(3, "Vect_point_on_line(): distance = %f", distance);
  326. if ((distance < 0) || (Points->n_points < 2))
  327. return 0;
  328. /* Check if first or last */
  329. length = Vect_line_length(Points);
  330. G_debug(3, " length = %f", length);
  331. if (distance < 0 || distance > length) {
  332. G_debug(3, " -> outside line");
  333. return 0;
  334. }
  335. np = Points->n_points;
  336. if (distance == 0) {
  337. G_debug(3, " -> first point");
  338. xp = Points->x[0];
  339. yp = Points->y[0];
  340. zp = Points->z[0];
  341. dx = Points->x[1] - Points->x[0];
  342. dy = Points->y[1] - Points->y[0];
  343. dz = Points->z[1] - Points->z[0];
  344. dxy = hypot(dx, dy);
  345. seg = 1;
  346. }
  347. else if (distance == length) {
  348. G_debug(3, " -> last point");
  349. xp = Points->x[np - 1];
  350. yp = Points->y[np - 1];
  351. zp = Points->z[np - 1];
  352. dx = Points->x[np - 1] - Points->x[np - 2];
  353. dy = Points->y[np - 1] - Points->y[np - 2];
  354. dz = Points->z[np - 1] - Points->z[np - 2];
  355. dxy = hypot(dx, dy);
  356. seg = np - 1;
  357. }
  358. else {
  359. for (j = 0; j < Points->n_points - 1; j++) {
  360. /* dxyz = G_distance(Points->x[j], Points->y[j],
  361. Points->x[j+1], Points->y[j+1]); */
  362. dx = Points->x[j + 1] - Points->x[j];
  363. dy = Points->y[j + 1] - Points->y[j];
  364. dz = Points->z[j + 1] - Points->z[j];
  365. dxy = hypot(dx, dy);
  366. dxyz = hypot(dxy, dz);
  367. dist += dxyz;
  368. if (dist >= distance) { /* point is on the current line part */
  369. rest = distance - dist + dxyz; /* from first point of segment to point */
  370. k = rest / dxyz;
  371. xp = Points->x[j] + k * dx;
  372. yp = Points->y[j] + k * dy;
  373. zp = Points->z[j] + k * dz;
  374. seg = j + 1;
  375. break;
  376. }
  377. }
  378. }
  379. if (x != NULL)
  380. *x = xp;
  381. if (y != NULL)
  382. *y = yp;
  383. if (z != NULL)
  384. *z = zp;
  385. /* calculate angle */
  386. if (angle != NULL)
  387. *angle = atan2(dy, dx);
  388. /* calculate slope */
  389. if (slope != NULL)
  390. *slope = atan2(dz, dxy);
  391. return seg;
  392. }
  393. /*!
  394. \brief Create line segment.
  395. Creates segment of InPoints from start to end measured along the
  396. line and write it to OutPoints.
  397. If the distance is greater than line length or negative, error is
  398. returned.
  399. \param InPoints input line
  400. \param start segment number
  401. \param end segment number
  402. \param OutPoints output line
  403. \return 1 success
  404. \return 0 error when start > length or end < 0 or start < 0 or end > length
  405. */
  406. int Vect_line_segment(const struct line_pnts *InPoints, double start, double end,
  407. struct line_pnts *OutPoints)
  408. {
  409. int i, seg1, seg2;
  410. double length, tmp;
  411. double x1, y1, z1, x2, y2, z2;
  412. G_debug(3, "Vect_line_segment(): start = %f, end = %f, n_points = %d",
  413. start, end, InPoints->n_points);
  414. Vect_reset_line(OutPoints);
  415. if (start > end) {
  416. tmp = start;
  417. start = end;
  418. end = tmp;
  419. }
  420. /* Check start/end */
  421. if (end < 0)
  422. return 0;
  423. length = Vect_line_length(InPoints);
  424. if (start > length)
  425. return 0;
  426. /* Find coordinates and segments of start/end */
  427. seg1 = Vect_point_on_line(InPoints, start, &x1, &y1, &z1, NULL, NULL);
  428. seg2 = Vect_point_on_line(InPoints, end, &x2, &y2, &z2, NULL, NULL);
  429. G_debug(3, " -> seg1 = %d seg2 = %d", seg1, seg2);
  430. if (seg1 == 0 || seg2 == 0) {
  431. G_warning(_("Segment outside line, no segment created"));
  432. return 0;
  433. }
  434. Vect_append_point(OutPoints, x1, y1, z1);
  435. for (i = seg1; i < seg2; i++) {
  436. Vect_append_point(OutPoints, InPoints->x[i], InPoints->y[i],
  437. InPoints->z[i]);
  438. };
  439. Vect_append_point(OutPoints, x2, y2, z2);
  440. return 1;
  441. }
  442. /*!
  443. \brief Calculate line length, 3D-length in case of 3D vector line
  444. For Lat-Long use Vect_line_geodesic_length() instead.
  445. \param Points pointer to line_pnts structure geometry
  446. \return line length
  447. */
  448. double Vect_line_length(const struct line_pnts *Points)
  449. {
  450. int j;
  451. double dx, dy, dz, len = 0;
  452. if (Points->n_points < 2)
  453. return 0;
  454. for (j = 0; j < Points->n_points - 1; j++) {
  455. dx = Points->x[j + 1] - Points->x[j];
  456. dy = Points->y[j + 1] - Points->y[j];
  457. dz = Points->z[j + 1] - Points->z[j];
  458. len += hypot(hypot(dx, dy), dz);
  459. }
  460. return len;
  461. }
  462. /*!
  463. \brief Calculate line length.
  464. If projection is LL, the length is measured along the geodesic.
  465. \param Points pointer to line_pnts structure geometry
  466. \return line length
  467. */
  468. double Vect_line_geodesic_length(const struct line_pnts *Points)
  469. {
  470. int j, dc;
  471. double dx, dy, dz, dxy, len = 0;
  472. dc = G_begin_distance_calculations();
  473. if (Points->n_points < 2)
  474. return 0;
  475. for (j = 0; j < Points->n_points - 1; j++) {
  476. if (dc == 2)
  477. dxy =
  478. G_geodesic_distance(Points->x[j], Points->y[j],
  479. Points->x[j + 1], Points->y[j + 1]);
  480. else {
  481. dx = Points->x[j + 1] - Points->x[j];
  482. dy = Points->y[j + 1] - Points->y[j];
  483. dxy = hypot(dx, dy);
  484. }
  485. dz = Points->z[j + 1] - Points->z[j];
  486. len += hypot(dxy, dz);
  487. }
  488. return len;
  489. }
  490. /*!
  491. \brief Calculate line distance.
  492. Sets (if not null):
  493. - px, py - point on line,
  494. - dist - distance to line,
  495. - spdist - distance to point on line from segment beginning,
  496. - sldist - distance to point on line form line beginning along line
  497. \param points pointer to line_pnts structure
  498. \param ux,uy,uz point coordinates
  499. \param with_z flag if to use z coordinate (3D calculation)
  500. \param[out] px,py,pz point on line
  501. \param[out] dist distance to line,
  502. \param[out] spdist distance of point from segment beginning
  503. \param[out] lpdist distance of point from line
  504. \return nearest segment (first is 1)
  505. */
  506. int Vect_line_distance(const struct line_pnts *points,
  507. double ux, double uy, double uz,
  508. int with_z,
  509. double *px, double *py, double *pz,
  510. double *dist, double *spdist, double *lpdist)
  511. {
  512. int i;
  513. double distance;
  514. double new_dist;
  515. double tpx, tpy, tpz, tdist, tspdist, tlpdist = 0;
  516. double dx, dy, dz;
  517. int n_points;
  518. int segment;
  519. n_points = points->n_points;
  520. if (n_points == 1) {
  521. distance =
  522. dig_distance2_point_to_line(ux, uy, uz, points->x[0],
  523. points->y[0], points->z[0],
  524. points->x[0], points->y[0],
  525. points->z[0], with_z, NULL, NULL,
  526. NULL, NULL, NULL);
  527. tpx = points->x[0];
  528. tpy = points->y[0];
  529. tpz = points->z[0];
  530. tdist = sqrt(distance);
  531. tspdist = 0;
  532. tlpdist = 0;
  533. segment = 0;
  534. }
  535. else {
  536. distance =
  537. dig_distance2_point_to_line(ux, uy, uz, points->x[0],
  538. points->y[0], points->z[0],
  539. points->x[1], points->y[1],
  540. points->z[1], with_z, NULL, NULL,
  541. NULL, NULL, NULL);
  542. segment = 1;
  543. for (i = 1; i < n_points - 1; i++) {
  544. new_dist = dig_distance2_point_to_line(ux, uy, uz,
  545. points->x[i], points->y[i],
  546. points->z[i],
  547. points->x[i + 1],
  548. points->y[i + 1],
  549. points->z[i + 1], with_z,
  550. NULL, NULL, NULL, NULL,
  551. NULL);
  552. if (new_dist < distance) {
  553. distance = new_dist;
  554. segment = i + 1;
  555. }
  556. }
  557. /* we have segment and now we can recalculate other values (speed) */
  558. new_dist = dig_distance2_point_to_line(ux, uy, uz,
  559. points->x[segment - 1],
  560. points->y[segment - 1],
  561. points->z[segment - 1],
  562. points->x[segment],
  563. points->y[segment],
  564. points->z[segment], with_z,
  565. &tpx, &tpy, &tpz, &tspdist,
  566. NULL);
  567. /* calculate distance from beginning of line */
  568. if (lpdist) {
  569. tlpdist = 0;
  570. for (i = 0; i < segment - 1; i++) {
  571. dx = points->x[i + 1] - points->x[i];
  572. dy = points->y[i + 1] - points->y[i];
  573. if (with_z)
  574. dz = points->z[i + 1] - points->z[i];
  575. else
  576. dz = 0;
  577. tlpdist += hypot(hypot(dx, dy), dz);
  578. }
  579. tlpdist += tspdist;
  580. }
  581. tdist = sqrt(distance);
  582. }
  583. if (px)
  584. *px = tpx;
  585. if (py)
  586. *py = tpy;
  587. if (pz && with_z)
  588. *pz = tpz;
  589. if (dist)
  590. *dist = tdist;
  591. if (spdist)
  592. *spdist = tspdist;
  593. if (lpdist)
  594. *lpdist = tlpdist;
  595. return (segment);
  596. }
  597. /*!
  598. \brief Calculate distance of 2 points
  599. Simply uses Pythagoras.
  600. \param x1,y1,z1 first point
  601. \param x2,y2,z2 second point
  602. \param with_z use z coordinate
  603. \return distance
  604. */
  605. double Vect_points_distance(double x1, double y1, double z1, /* point 1 */
  606. double x2, double y2, double z2, /* point 2 */
  607. int with_z)
  608. {
  609. double dx, dy, dz;
  610. dx = x2 - x1;
  611. dy = y2 - y1;
  612. dz = z2 - z1;
  613. if (with_z)
  614. return hypot(hypot(dx, dy), dz);
  615. else
  616. return hypot(dx, dy);
  617. }
  618. /*!
  619. \brief Get bounding box of line
  620. \param Points pointer to line_pnts structure
  621. \param[out] Box bounding box
  622. */
  623. void Vect_line_box(const struct line_pnts *Points, struct bound_box *Box)
  624. {
  625. dig_line_box(Points, Box);
  626. }
  627. /*!
  628. \brief Reverse the order of vertices
  629. \param Points pointer to line_pnts structure to be changed
  630. */
  631. void Vect_line_reverse(struct line_pnts *Points)
  632. {
  633. int i, j, np;
  634. double x, y, z;
  635. np = (int)Points->n_points / 2;
  636. for (i = 0; i < np; i++) {
  637. j = Points->n_points - i - 1;
  638. x = Points->x[i];
  639. y = Points->y[i];
  640. z = Points->z[i];
  641. Points->x[i] = Points->x[j];
  642. Points->y[i] = Points->y[j];
  643. Points->z[i] = Points->z[j];
  644. Points->x[j] = x;
  645. Points->y[j] = y;
  646. Points->z[j] = z;
  647. }
  648. }
  649. /*!
  650. \brief Fetches FIRST category number for given vector line and field
  651. \param Map pointer to Map_info structure
  652. \param line line id
  653. \param field layer number
  654. \return -1 no category
  655. \return category number (>=0)
  656. */
  657. int Vect_get_line_cat(const struct Map_info *Map, int line, int field)
  658. {
  659. static struct line_cats *cats = NULL;
  660. int cat, ltype;
  661. if (cats == NULL)
  662. cats = Vect_new_cats_struct();
  663. ltype = Vect_read_line(Map, NULL, cats, line);
  664. Vect_cat_get(cats, field, &cat);
  665. G_debug(3, "Vect_get_line_cat: display line %d, ltype %d, cat %d", line,
  666. ltype, cat);
  667. return cat;
  668. }