Edge.js 2.6 KB

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