PointService.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Point from "../Geometry/Point.js";
  2. import Line from "../Geometry/Line.js";
  3. import { dataService } from "./DataService.js";
  4. import VectorCategory from "../enum/VectorCategory.js";
  5. import { mathUtil } from "../Util/MathUtil.js";
  6. import { lineService } from "./LineService.js";
  7. import Settings from "../Settings.js";
  8. export default class PointService {
  9. constructor() {}
  10. create(position, vectorId) {
  11. let point = new Point(position, vectorId);
  12. dataService.addPoint(point);
  13. this.updateBasePointIds()
  14. return point;
  15. }
  16. //将pointId1合并到pointId2
  17. mergePoint(pointId1, pointId2) {
  18. if (pointId1 == pointId2) {
  19. return;
  20. } else if (pointId1 != null && pointId2 != null) {
  21. let point1 = dataService.getPoint(pointId1);
  22. let parent1 = point1.getParent();
  23. let point2 = dataService.getPoint(pointId2);
  24. if (mathUtil.equalPoint(point1, point2)) {
  25. let lineId = lineService.getLine(pointId1, pointId2);
  26. if (lineId) {
  27. dataService.deleteLine(lineId);
  28. return;
  29. }
  30. }
  31. for (let key in parent1) {
  32. let line = dataService.getLine(key);
  33. let dir = line.getDir(pointId1);
  34. if (dir == "start") {
  35. line.startId = pointId2;
  36. } else if (dir == "end") {
  37. line.endId = pointId2;
  38. }
  39. point2.setPointParent(key, dir);
  40. }
  41. dataService.deletePoint(pointId1);
  42. }
  43. }
  44. deletePoint(pointId) {
  45. let point = dataService.getPoint(pointId);
  46. if (point) {
  47. const category = point.getCategory();
  48. if (category == VectorCategory.Point.NormalPoint) {
  49. dataService.deletePoint(pointId); //暂时简单粗暴
  50. } else if (category == VectorCategory.Point.BasePoint) {
  51. this.deleteBasePoint(pointId);
  52. } else if (
  53. category == VectorCategory.Point.TestPoint ||
  54. category == VectorCategory.Point.TestBasePoint
  55. ) {
  56. this.deleteTestPoint(pointId);
  57. }
  58. }
  59. }
  60. updateBasePointIds() {
  61. let points = dataService.vectorData.points;
  62. Settings.basePointIds = [];
  63. for (let key in points) {
  64. if (points[key].category == VectorCategory.Point.BasePoint) {
  65. Settings.basePointIds.push(points[key].vectorId);
  66. }
  67. }
  68. }
  69. deleteBasePoint(basePointId) {
  70. let points = dataService.getPoints();
  71. let needDeletePointIds = [];
  72. for (let key in points) {
  73. let point = dataService.getPoint(key);
  74. if (point.vectorId == basePointId) {
  75. needDeletePointIds.push(basePointId);
  76. } else if (point.linkedBasePointId == basePointId) {
  77. needDeletePointIds.push(key);
  78. }
  79. }
  80. let lines = dataService.getLines();
  81. for (let key in lines) {
  82. let line = dataService.getLine(key);
  83. if (
  84. needDeletePointIds.indexOf(line.startId) > -1 ||
  85. needDeletePointIds.indexOf(line.endId) > -1
  86. ) {
  87. dataService.deleteLine(key);
  88. }
  89. }
  90. dataService.deletePoint(basePointId);
  91. if (Settings.selectBasePointId == basePointId) {
  92. Settings.selectBasePointId = null;
  93. }
  94. this.updateBasePointIds()
  95. }
  96. deleteTestPoint(testPointId) {
  97. let points = dataService.getPoints();
  98. let needDeletePointIds = [];
  99. for (let key in points) {
  100. let point = dataService.getPoint(key);
  101. if(point.getCategory() == VectorCategory.Point.BasePoint){
  102. continue
  103. }
  104. if (point.vectorId == testPointId) {
  105. needDeletePointIds.push(testPointId);
  106. } else if (point.linkedTestPointId == testPointId) {
  107. needDeletePointIds.push(key);
  108. }
  109. }
  110. let lines = dataService.getLines();
  111. for (let key in lines) {
  112. let line = dataService.getLine(key);
  113. let startPoint = dataService.getPoint(line.startId);
  114. let endPoint = dataService.getPoint(line.endId);
  115. if (
  116. needDeletePointIds.indexOf(line.startId) > -1 ||
  117. needDeletePointIds.indexOf(line.endId) > -1
  118. ) {
  119. dataService.deleteLine(key);
  120. if (needDeletePointIds.indexOf(line.startId) > -1&& endPoint.getCategory()!= VectorCategory.Point.BasePoint) {
  121. this.deleteTestPoint(line.endId);
  122. }
  123. if (needDeletePointIds.indexOf(line.endId) > -1 && startPoint.getCategory()!= VectorCategory.Point.BasePoint) {
  124. this.deleteTestPoint(line.startId);
  125. }
  126. }
  127. }
  128. dataService.deletePoint(testPointId);
  129. }
  130. }
  131. const pointService = new PointService();
  132. export { pointService };