line.c 19 KB

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