ControlPoint.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Constant from "../Constant.js";
  2. import { dataService } from "../Service/DataService.js";
  3. import { mathUtil } from "../Util/MathUtil.js";
  4. import VectorType from "../enum/VectorType.js";
  5. import Geometry from "./Geometry";
  6. // 二次贝塞尔曲线
  7. // var ctx=c.getContext("2d");
  8. // ctx.beginPath();
  9. // ctx.moveTo(200,20);
  10. // ctx.quadraticCurveTo(20,100,20,20);
  11. // ctx.stroke();
  12. export default class ControlPoint extends Geometry {
  13. constructor(position, vectorId) {
  14. super();
  15. //控制点坐标
  16. this.x = null;
  17. this.y = null;
  18. this.edgeInfo1 = {
  19. id: null,
  20. dir: null,
  21. };
  22. this.edgeInfo2 = {
  23. id: null,
  24. dir: null,
  25. };
  26. this.extremePoint = null; //极值
  27. this.curves = [];
  28. this.geoType = VectorType.ControlPoint;
  29. this.setId(vectorId);
  30. this.setPosition(position);
  31. }
  32. setPosition(position) {
  33. this.x = position.x;
  34. this.y = position.y;
  35. }
  36. setExtremePoint() {
  37. let points = [];
  38. let edge1 = dataService.getRoadEdge(this.edgeInfo1.id);
  39. let curve = {};
  40. if (this.edgeInfo1.dir == "start") {
  41. curve.start = {};
  42. mathUtil.clonePoint(curve.start, edge1.start);
  43. } else if (this.edgeInfo1.dir == "end") {
  44. curve.start = {};
  45. mathUtil.clonePoint(curve.start, edge1.end);
  46. }
  47. curve.controls = [];
  48. curve.controls.push({
  49. x: this.x,
  50. y: this.y,
  51. });
  52. let edge2 = dataService.getRoadEdge(this.edgeInfo2.id);
  53. if (this.edgeInfo2.dir == "start") {
  54. curve.end = {};
  55. mathUtil.clonePoint(curve.end, edge2.start);
  56. } else if (this.edgeInfo2.dir == "end") {
  57. curve.end = {};
  58. mathUtil.clonePoint(curve.end, edge2.end);
  59. }
  60. let joinInfo = mathUtil.getHitInfoForCurve(
  61. curve.controls[0],
  62. curve,
  63. Constant.defaultRoadWidth
  64. );
  65. this.extremePoint = {};
  66. mathUtil.clonePoint(this.extremePoint, joinInfo.position);
  67. this.curves = mathUtil.getCurvesByPoints([
  68. curve.start,
  69. this.extremePoint,
  70. curve.end,
  71. ]);
  72. }
  73. setCurves() {
  74. let points = [];
  75. let edge1 = dataService.getRoadEdge(this.edgeInfo1.id);
  76. if (this.edgeInfo1.dir == "start") {
  77. points[0] = edge1.start;
  78. } else if (this.edgeInfo1.dir == "end") {
  79. points[0] = edge1.end;
  80. }
  81. points[1] = {
  82. x: this.extremePoint.x,
  83. y: this.extremePoint.y,
  84. };
  85. let edge2 = dataService.getRoadEdge(this.edgeInfo2.id);
  86. if (this.edgeInfo2.dir == "start") {
  87. points[2] = edge2.start;
  88. } else if (this.edgeInfo2.dir == "end") {
  89. points[2] = edge2.end;
  90. }
  91. points[1] = this.extremePoint;
  92. this.curves = mathUtil.getCurvesByPoints(points);
  93. }
  94. setEdgeInfo(edgeId1, dir1, edgeId2, dir2) {
  95. this.edgeInfo1.id = edgeId1;
  96. this.edgeInfo1.dir = dir1;
  97. this.edgeInfo2.id = edgeId2;
  98. this.edgeInfo2.dir = dir2;
  99. }
  100. }