ControlPointService.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import ControlPoint from "../Geometry/ControlPoint.js";
  2. import { dataService } from "./DataService.js";
  3. import { mathUtil } from "../Util/MathUtil";
  4. import { edgeService } from "./EdgeService.js";
  5. import { roadService } from "./RoadService.js";
  6. export default class ControlPointService {
  7. constructor() {}
  8. //暂时没有添加到vectorData里
  9. create(position, vectorId) {
  10. let controlPoint = new ControlPoint(position, vectorId);
  11. return controlPoint;
  12. }
  13. update(realRosition, point1, point2, edge1, edge2, dir1, dir2) {
  14. let controlPoint = dataService.getControlPoint(
  15. edge1.vectorId,
  16. edge2.vectorId
  17. );
  18. const line1 = edgeService.getLine(edge1);
  19. const line2 = edgeService.getLine(edge2);
  20. let position = mathUtil.getPositionForExtendedLine(point1, point2);
  21. //更新edge的坐标
  22. let position1 = mathUtil.getJoinLinePoint(position, line1);
  23. let position2 = mathUtil.getJoinLinePoint(position, line2);
  24. //可能position1或者position2不在对应的edge上,这时候需要调换顺序
  25. if (!this.isPointOnEdgeRay(edge1, dir1, position)) {
  26. let _position = mathUtil.getPositionForExtendedLine(point2, point1);
  27. position1 = mathUtil.getJoinLinePoint(_position, line1);
  28. }
  29. if (!this.isPointOnEdgeRay(edge2, dir2, position)) {
  30. let _position = mathUtil.getPositionForExtendedLine(point2, point1);
  31. position2 = mathUtil.getJoinLinePoint(_position, line2);
  32. }
  33. if (controlPoint == null) {
  34. //新建控制点
  35. controlPoint = this.create(realRosition);
  36. //需要删除之前的控制点
  37. dataService.deleteControlPointForEdge(edge1.vectorId, dir1);
  38. dataService.deleteControlPointForEdge(edge2.vectorId, dir2);
  39. dataService.deleteControlPoint(edge1.vectorId, edge2.vectorId);
  40. //设置控制点的信息
  41. controlPoint.setEdgeInfo(edge1.vectorId, dir1, edge2.vectorId, dir2);
  42. //添加到数据集里
  43. dataService.addControlPoint(controlPoint);
  44. }
  45. //更新控制点坐标
  46. else {
  47. controlPoint.setPosition(realRosition);
  48. }
  49. edge1.setPosition(position1, dir1);
  50. edge2.setPosition(position2, dir2);
  51. controlPoint.setExtremePoint();
  52. }
  53. //newleft,end------------road.leftEdgeId,end
  54. replaceEdgeId(controlPoint, oldEdgeId, newEdgeId) {
  55. if (controlPoint == null) {
  56. return;
  57. }
  58. dataService.deleteControlPoint(
  59. controlPoint.edgeInfo1.id,
  60. controlPoint.edgeInfo2.id
  61. );
  62. if (controlPoint.edgeInfo1.id == oldEdgeId) {
  63. controlPoint.edgeInfo1.id = newEdgeId;
  64. } else if (controlPoint.edgeInfo2.id == oldEdgeId) {
  65. controlPoint.edgeInfo2.id = newEdgeId;
  66. }
  67. dataService.addControlPoint(controlPoint);
  68. }
  69. //角度小,road宽的时候,可能edge顺序会倒,但是road的顺序不会
  70. isPointOnEdgeRay(edge, dir, position) {
  71. let road = dataService.getRoad(edge.parent);
  72. let point1, point2;
  73. if (dir == "start") {
  74. point1 = dataService.getRoadPoint(road.startId);
  75. point2 = dataService.getRoadPoint(road.endId);
  76. } else if (dir == "end") {
  77. point1 = dataService.getRoadPoint(road.endId);
  78. point2 = dataService.getRoadPoint(road.startId);
  79. }
  80. position = mathUtil.getJoinLinePoint(
  81. position,
  82. roadService.getMidLine(road)
  83. );
  84. let v1 = {
  85. x: position.x - point1.x,
  86. y: position.y - point1.y,
  87. };
  88. let v2 = {
  89. x: point2.x - point1.x,
  90. y: point2.y - point1.y,
  91. };
  92. let v = {
  93. x: v1.x + v2.x,
  94. y: v1.y + v2.y,
  95. };
  96. let d = mathUtil.getDistance(v, { x: 0, y: 0 });
  97. let d1 = mathUtil.getDistance(position, point1);
  98. let d2 = mathUtil.getDistance(point2, point1);
  99. if (Math.abs(d - d1 - d2) < 0.2) {
  100. return true;
  101. } else {
  102. return false;
  103. }
  104. }
  105. updateForMovePoint(controlPointId, newPosition) {
  106. let controlPoint = dataService.getControlPoint2(controlPointId);
  107. mathUtil.clonePoint(controlPoint.extremePoint, newPosition);
  108. controlPoint.setCurves();
  109. }
  110. }
  111. const controlPointService = new ControlPointService();
  112. export { controlPointService };