Edge.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var helpers_1 = require("@turf/helpers");
  4. var util_1 = require("./util");
  5. /**
  6. * This class is inspired by GEOS's geos::operation::polygonize::PolygonizeDirectedEdge
  7. */
  8. var Edge = /** @class */ (function () {
  9. /**
  10. * @param {Node} from - start node of the Edge
  11. * @param {Node} to - end node of the edge
  12. */
  13. function Edge(from, to) {
  14. this.from = from; //< start
  15. this.to = to; //< End
  16. this.next = undefined; //< The edge to be computed after
  17. this.label = undefined; //< Used in order to detect Cut Edges (Bridges)
  18. this.symetric = undefined; //< The symetric edge of this
  19. this.ring = undefined; //< EdgeRing in which the Edge is
  20. this.from.addOuterEdge(this);
  21. this.to.addInnerEdge(this);
  22. }
  23. /**
  24. * Creates or get the symetric Edge.
  25. *
  26. * @returns {Edge} - Symetric Edge.
  27. */
  28. Edge.prototype.getSymetric = function () {
  29. if (!this.symetric) {
  30. this.symetric = new Edge(this.to, this.from);
  31. this.symetric.symetric = this;
  32. }
  33. return this.symetric;
  34. };
  35. /**
  36. * Removes edge from from and to nodes.
  37. */
  38. Edge.prototype.deleteEdge = function () {
  39. this.from.removeOuterEdge(this);
  40. this.to.removeInnerEdge(this);
  41. };
  42. /**
  43. * Compares Edge equallity.
  44. *
  45. * An edge is equal to another, if the from and to nodes are the same.
  46. *
  47. * @param {Edge} edge - Another Edge
  48. * @returns {boolean} - True if Edges are equal, False otherwise
  49. */
  50. Edge.prototype.isEqual = function (edge) {
  51. return this.from.id === edge.from.id && this.to.id === edge.to.id;
  52. };
  53. Edge.prototype.toString = function () {
  54. return "Edge { " + this.from.id + " -> " + this.to.id + " }";
  55. };
  56. /**
  57. * Returns a LineString representation of the Edge
  58. *
  59. * @returns {Feature<LineString>} - LineString representation of the Edge
  60. */
  61. Edge.prototype.toLineString = function () {
  62. return helpers_1.lineString([this.from.coordinates, this.to.coordinates]);
  63. };
  64. /**
  65. * Comparator of two edges.
  66. *
  67. * Implementation of geos::planargraph::DirectedEdge::compareTo.
  68. *
  69. * @param {Edge} edge - Another edge to compare with this one
  70. * @returns {number} -1 if this Edge has a greater angle with the positive x-axis than b,
  71. * 0 if the Edges are colinear,
  72. * 1 otherwise
  73. */
  74. Edge.prototype.compareTo = function (edge) {
  75. return util_1.orientationIndex(edge.from.coordinates, edge.to.coordinates, this.to.coordinates);
  76. };
  77. return Edge;
  78. }());
  79. exports.default = Edge;