Node.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var util_1 = require("./util");
  4. /**
  5. * Node
  6. */
  7. var Node = /** @class */ (function () {
  8. function Node(coordinates) {
  9. this.id = Node.buildId(coordinates);
  10. this.coordinates = coordinates; //< {Number[]}
  11. this.innerEdges = []; //< {Edge[]}
  12. // We wil store to (out) edges in an CCW order as geos::planargraph::DirectedEdgeStar does
  13. this.outerEdges = []; //< {Edge[]}
  14. this.outerEdgesSorted = false; //< {Boolean} flag that stores if the outer Edges had been sorted
  15. }
  16. Node.buildId = function (coordinates) {
  17. return coordinates.join(",");
  18. };
  19. Node.prototype.removeInnerEdge = function (edge) {
  20. this.innerEdges = this.innerEdges.filter(function (e) { return e.from.id !== edge.from.id; });
  21. };
  22. Node.prototype.removeOuterEdge = function (edge) {
  23. this.outerEdges = this.outerEdges.filter(function (e) { return e.to.id !== edge.to.id; });
  24. };
  25. /**
  26. * Outer edges are stored CCW order.
  27. *
  28. * @memberof Node
  29. * @param {Edge} edge - Edge to add as an outerEdge.
  30. */
  31. Node.prototype.addOuterEdge = function (edge) {
  32. this.outerEdges.push(edge);
  33. this.outerEdgesSorted = false;
  34. };
  35. /**
  36. * Sorts outer edges in CCW way.
  37. *
  38. * @memberof Node
  39. * @private
  40. */
  41. Node.prototype.sortOuterEdges = function () {
  42. var _this = this;
  43. if (!this.outerEdgesSorted) {
  44. //this.outerEdges.sort((a, b) => a.compareTo(b));
  45. // Using this comparator in order to be deterministic
  46. this.outerEdges.sort(function (a, b) {
  47. var aNode = a.to, bNode = b.to;
  48. if (aNode.coordinates[0] - _this.coordinates[0] >= 0 &&
  49. bNode.coordinates[0] - _this.coordinates[0] < 0)
  50. return 1;
  51. if (aNode.coordinates[0] - _this.coordinates[0] < 0 &&
  52. bNode.coordinates[0] - _this.coordinates[0] >= 0)
  53. return -1;
  54. if (aNode.coordinates[0] - _this.coordinates[0] === 0 &&
  55. bNode.coordinates[0] - _this.coordinates[0] === 0) {
  56. if (aNode.coordinates[1] - _this.coordinates[1] >= 0 ||
  57. bNode.coordinates[1] - _this.coordinates[1] >= 0)
  58. return aNode.coordinates[1] - bNode.coordinates[1];
  59. return bNode.coordinates[1] - aNode.coordinates[1];
  60. }
  61. var det = util_1.orientationIndex(_this.coordinates, aNode.coordinates, bNode.coordinates);
  62. if (det < 0)
  63. return 1;
  64. if (det > 0)
  65. return -1;
  66. var d1 = Math.pow(aNode.coordinates[0] - _this.coordinates[0], 2) +
  67. Math.pow(aNode.coordinates[1] - _this.coordinates[1], 2), d2 = Math.pow(bNode.coordinates[0] - _this.coordinates[0], 2) +
  68. Math.pow(bNode.coordinates[1] - _this.coordinates[1], 2);
  69. return d1 - d2;
  70. });
  71. this.outerEdgesSorted = true;
  72. }
  73. };
  74. /**
  75. * Retrieves outer edges.
  76. *
  77. * They are sorted if they aren't in the CCW order.
  78. *
  79. * @memberof Node
  80. * @returns {Edge[]} - List of outer edges sorted in a CCW order.
  81. */
  82. Node.prototype.getOuterEdges = function () {
  83. this.sortOuterEdges();
  84. return this.outerEdges;
  85. };
  86. Node.prototype.getOuterEdge = function (i) {
  87. this.sortOuterEdges();
  88. return this.outerEdges[i];
  89. };
  90. Node.prototype.addInnerEdge = function (edge) {
  91. this.innerEdges.push(edge);
  92. };
  93. return Node;
  94. }());
  95. exports.default = Node;