Node.js 3.6 KB

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