ControlPointService.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. export default class ControlPointService {
  6. constructor() {}
  7. //暂时没有添加到vectorData里
  8. create(position, vectorId) {
  9. let controlPoint = new ControlPoint(position, vectorId);
  10. return controlPoint;
  11. }
  12. update(realRosition, point1, point2, edge1, edge2, dir1, dir2) {
  13. let controlPoint = dataService.getControlPoint(
  14. edge1.vectorId,
  15. edge2.vectorId
  16. );
  17. const line1 = edgeService.getLine(edge1);
  18. const line2 = edgeService.getLine(edge2);
  19. let position = mathUtil.getPositionForExtendedLine(point1, point2);
  20. //更新edge的坐标
  21. let position1 = mathUtil.getJoinLinePoint(position, line1);
  22. let position2 = mathUtil.getJoinLinePoint(position, line2);
  23. //可能position1或者position2不在对应的edge上,这时候需要调换顺序
  24. if (
  25. !mathUtil.isContainForSegment(position1, edge1.start, edge1.end) ||
  26. !mathUtil.isContainForSegment(position2, edge2.start, edge2.end)
  27. ) {
  28. position = mathUtil.getPositionForExtendedLine(point2, point1);
  29. position1 = mathUtil.getJoinLinePoint(position, line1);
  30. position2 = mathUtil.getJoinLinePoint(position, line2);
  31. }
  32. // const realRosition = mathUtil.getPositionForExtendedLine(point2, point1);
  33. // const realRosition = {
  34. // x: (point1.x + point2.x) / 2,
  35. // y: (point1.y + point2.y) / 2,
  36. // };
  37. if (controlPoint == null) {
  38. //新建控制点
  39. controlPoint = this.create(realRosition);
  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. }
  52. }
  53. const controlPointService = new ControlPointService();
  54. export { controlPointService };