util.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <assert.h>
  11. #include <limits.h>
  12. #include <stdlib.h>
  13. #include <pathplan/pathutil.h>
  14. #include <util/alloc.h>
  15. void freePath(Ppolyline_t* p)
  16. {
  17. free(p->ps);
  18. free(p);
  19. }
  20. int Ppolybarriers(Ppoly_t ** polys, int npolys, Pedge_t ** barriers,
  21. int *n_barriers)
  22. {
  23. Ppoly_t pp;
  24. int i, n, b;
  25. n = 0;
  26. for (i = 0; i < npolys; i++) {
  27. assert(polys[i]->pn <= INT_MAX);
  28. n += (int)polys[i]->pn;
  29. }
  30. Pedge_t *bar = gv_calloc(n, sizeof(Pedge_t));
  31. b = 0;
  32. for (i = 0; i < npolys; i++) {
  33. pp = *polys[i];
  34. for (size_t j = 0; j < pp.pn; j++) {
  35. size_t k = j + 1;
  36. if (k >= pp.pn)
  37. k = 0;
  38. bar[b].a = pp.ps[j];
  39. bar[b].b = pp.ps[k];
  40. b++;
  41. }
  42. }
  43. assert(b == n);
  44. *barriers = bar;
  45. *n_barriers = n;
  46. return 1;
  47. }
  48. /* make_polyline:
  49. */
  50. void
  51. make_polyline(Ppolyline_t line, Ppolyline_t* sline)
  52. {
  53. static size_t isz = 0;
  54. static Ppoint_t* ispline = 0;
  55. const size_t npts = 4 + 3 * (line.pn - 2);
  56. if (npts > isz) {
  57. ispline = gv_recalloc(ispline, isz, npts, sizeof(Ppoint_t));
  58. isz = npts;
  59. }
  60. size_t j = 0;
  61. size_t i = 0;
  62. ispline[j+1] = ispline[j] = line.ps[i];
  63. j += 2;
  64. i++;
  65. for (; i + 1 < line.pn; i++) {
  66. ispline[j+2] = ispline[j+1] = ispline[j] = line.ps[i];
  67. j += 3;
  68. }
  69. ispline[j+1] = ispline[j] = line.ps[i];
  70. sline->pn = npts;
  71. sline->ps = ispline;
  72. }
  73. /**
  74. * @dir lib/pathplan
  75. * @brief finds and smooths shortest paths, API pathplan.h
  76. *
  77. * [man 3 pathplan](https://graphviz.org/pdf/pathplan.3.pdf)
  78. *
  79. */