ElementService.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import Point from "../Geometry/Point.js";
  2. import Line from "../Geometry/Line.js";
  3. import Road from "../Geometry/Road.js";
  4. import RoadEdge from "../Geometry/RoadEdge.js";
  5. import ElementEvents from "../enum/ElementEvents.js";
  6. import VectorCategory from "../enum/VectorCategory.js";
  7. import { listenLayer } from "../ListenLayer";
  8. import Constant from "../Constant";
  9. import { dataService } from "./DataService.js";
  10. import { mathUtil } from "../Util/MathUtil";
  11. import { coordinate } from "../Coordinate.js";
  12. export class ElementService {
  13. constructor() {
  14. this.point = null;
  15. this.newLine = null;
  16. this.newLineStart = null;
  17. this.newLineEnd = null;
  18. this.newRoad = null;
  19. this.checkLinesXStart = null;
  20. this.checkLinesXEnd = null;
  21. this.checkLinesYStart = null;
  22. this.checkLinesYEnd = null;
  23. this.checkLines = {
  24. X: null,
  25. Y: null,
  26. };
  27. this.vCheckLinesXStart = null;
  28. this.vCheckLinesXEnd = null;
  29. this.vCheckLinesYStart = null;
  30. this.vCheckLinesYEnd = null;
  31. this.vCheckLines = {
  32. X: null,
  33. Y: null,
  34. };
  35. this.init();
  36. }
  37. init() {
  38. this.point = new Point({ x: 0, y: 0 });
  39. this.point.name = ElementEvents.AddingPoint;
  40. this.newRoad = this.createTempRoad();
  41. this.newRoad.name = ElementEvents.NewRoad;
  42. this.newLineStart = new Point({ x: 0, y: 0 });
  43. this.newLineEnd = new Point({ x: 1, y: 1 });
  44. this.newLine = new Line(this.newLineStart, this.newLineEnd);
  45. this.newLine.setCategory(VectorCategory.Line.NormalLine);
  46. this.newLine.name = ElementEvents.NewLine;
  47. this.checkLinesXStart = new Point({ x: 0, y: 0 });
  48. this.checkLinesXEnd = new Point({ x: 1, y: 1 });
  49. this.checkLines.X = new Line(this.checkLinesXStart, this.checkLinesXEnd);
  50. this.newLine.setCategory(VectorCategory.Line.GuideLine);
  51. this.checkLines.X.name = ElementEvents.CheckLinesX;
  52. this.checkLinesYStart = new Point({ x: 0, y: 0 });
  53. this.checkLinesYEnd = new Point({ x: 1, y: 1 });
  54. this.checkLines.Y = new Line(this.checkLinesYStart, this.checkLinesYEnd);
  55. this.newLine.setCategory(VectorCategory.Line.GuideLine);
  56. this.checkLines.Y.name = ElementEvents.CheckLinesY;
  57. this.vCheckLinesXStart = new Point({ x: 0, y: 0 });
  58. this.vCheckLinesXEnd = new Point({ x: 1, y: 1 });
  59. this.vCheckLines.X = new Line(this.vCheckLinesXStart, this.vCheckLinesXEnd);
  60. this.newLine.setCategory(VectorCategory.Line.GuideLine);
  61. this.vCheckLines.X.name = ElementEvents.VCheckLinesX;
  62. this.vCheckLinesYStart = new Point({ x: 0, y: 0 });
  63. this.vCheckLinesYEnd = new Point({ x: 1, y: 1 });
  64. this.vCheckLines.Y = new Line(this.vCheckLinesYStart, this.vCheckLinesYEnd);
  65. this.newLine.setCategory(VectorCategory.Line.GuideLine);
  66. this.vCheckLines.Y.name = ElementEvents.VCheckLinesY;
  67. this.hideAll();
  68. }
  69. //临时的
  70. createTempRoad() {
  71. let p1 = new Point({ x: 0, y: 0 });
  72. let p2 = new Point({ x: 1, y: 1 });
  73. this.newRoad = new Road(p1.vectorId, p2.vectorId);
  74. this.newRoad.start = p1;
  75. this.newRoad.end = p2;
  76. let edgePoints;
  77. if (this.newRoad.way == Constant.oneWay) {
  78. edgePoints = mathUtil.RectangleVertex(p1, p2, newRoad.singleRoadWidth);
  79. } else {
  80. edgePoints = mathUtil.RectangleVertex(
  81. p1,
  82. p2,
  83. this.newRoad.leftWidth +
  84. this.newRoad.rightWidth +
  85. this.newRoad.midDivide.midDivideWidth
  86. );
  87. }
  88. let leftEdge = new RoadEdge(
  89. edgePoints.leftEdgeStart,
  90. edgePoints.leftEdgeEnd,
  91. null,
  92. this.newRoad.vectorId
  93. );
  94. let rightEdge = new RoadEdge(
  95. edgePoints.rightEdgeStart,
  96. edgePoints.rightEdgeEnd,
  97. null,
  98. this.newRoad.vectorId
  99. );
  100. this.newRoad.setLeftEdge(leftEdge.vectorId);
  101. this.newRoad.setRightEdge(rightEdge.vectorId);
  102. leftEdge.setEdgeParent(this.newRoad.vectorId);
  103. rightEdge.setEdgeParent(this.newRoad.vectorId);
  104. this.newRoad.leftEdge = leftEdge;
  105. this.newRoad.rightEdge = rightEdge;
  106. this.hideNewRoad();
  107. return this.newRoad;
  108. }
  109. setNewLineCategory(value) {
  110. this.newLine.setCategory(value);
  111. }
  112. showPoint() {
  113. this.point.display = true;
  114. }
  115. hidePoint() {
  116. this.point.display = false;
  117. }
  118. setPoint(position) {
  119. this.point.setPosition(position);
  120. }
  121. showNewRoad() {
  122. this.newRoad.display = true;
  123. }
  124. hideNewRoad() {
  125. this.newRoad.display = false;
  126. }
  127. setNewRoad(point1, point2) {
  128. this.newRoad.start.setPosition(point1);
  129. this.newRoad.end.setPosition(point2);
  130. //需要更新Edge坐标
  131. if (!mathUtil.equalPoint(point1, point2)) {
  132. let edgePoints = mathUtil.RectangleVertex(
  133. point1,
  134. point2,
  135. Constant.defaultRoadWidth
  136. );
  137. this.newRoad.leftEdge.setPositions(
  138. edgePoints.leftEdgeStart,
  139. edgePoints.leftEdgeEnd
  140. );
  141. this.newRoad.rightEdge.setPositions(
  142. edgePoints.rightEdgeStart,
  143. edgePoints.rightEdgeEnd
  144. );
  145. }
  146. }
  147. setNewLine(point1, point2) {
  148. this.newLineStart.setPositions(point1);
  149. this.newLineEnd.setPositions(point2);
  150. }
  151. showNewLine() {
  152. this.newLine.display = true;
  153. }
  154. hideNewLine() {
  155. this.newLine.display = false;
  156. }
  157. setNewRoadState(state) {
  158. this.newRoad.state = state;
  159. }
  160. showCheckLinesX() {
  161. this.checkLines.X.display = true;
  162. }
  163. hideCheckLinesX() {
  164. this.checkLines.X.display = false;
  165. }
  166. setCheckLinesX(point1, point2) {
  167. this.checkLinesXStart.setPositions(point1);
  168. this.checkLinesXEnd.setPositions(point2);
  169. }
  170. showCheckLinesY() {
  171. this.checkLines.Y.display = true;
  172. }
  173. hideCheckLinesY() {
  174. this.checkLines.Y.display = false;
  175. }
  176. setCheckLinesY(point1, point2) {
  177. this.checkLinesYStart.setPositions(point1);
  178. this.checkLinesYEnd.setPositions(point2);
  179. }
  180. showVCheckLinesX() {
  181. this.vCheckLines.X.display = true;
  182. }
  183. hideVCheckLinesX() {
  184. this.vCheckLines.X.display = false;
  185. }
  186. setVCheckLinesX(point1, point2) {
  187. this.vCheckLinesXStart.setPositions(point1);
  188. this.vCheckLinesXEnd.setPositions(point2);
  189. }
  190. showVCheckLinesY() {
  191. this.vCheckLines.Y.display = true;
  192. }
  193. hideVCheckLinesY() {
  194. this.vCheckLines.Y.display = false;
  195. }
  196. setVCheckLinesY(point1, point2) {
  197. this.vCheckLinesYStart.setPositions(point1);
  198. this.vCheckLinesYEnd.setPositions(point2);
  199. }
  200. hideAll() {
  201. this.hideCheckLinesX();
  202. this.hideCheckLinesY();
  203. this.hidePoint();
  204. this.hideNewRoad();
  205. this.hideNewLine();
  206. this.hideVCheckLinesX();
  207. this.hideVCheckLinesY();
  208. }
  209. execute(startPosition, position) {
  210. this.hideVCheckLinesX();
  211. this.hideVCheckLinesY();
  212. this.hideCheckLinesX();
  213. this.hideCheckLinesY();
  214. //垂直校验
  215. if (startPosition) {
  216. let start = coordinate.getXYFromScreen({
  217. x: 0,
  218. y: 0,
  219. });
  220. let end = coordinate.getXYFromScreen({
  221. x: coordinate.width,
  222. y: coordinate.height,
  223. });
  224. if (Math.abs(position.x - startPosition.x) < Constant.minAdsorbPix) {
  225. position.x = startPosition.x;
  226. start.x = position.x;
  227. end.x = position.x;
  228. this.setVCheckLinesX(start, end);
  229. this.showVCheckLinesX();
  230. }
  231. if (Math.abs(position.y - startPosition.y) < Constant.minAdsorbPix) {
  232. position.y = startPosition.y;
  233. start.y = position.y;
  234. end.y = position.y;
  235. this.setVCheckLinesY(start, end);
  236. this.showVCheckLinesY();
  237. }
  238. if (mathUtil.equalPoint(position, startPosition)) {
  239. this.hideVCheckLinesX();
  240. this.hideVCheckLinesY();
  241. }
  242. }
  243. if (!this.vCheckLines.Y.display && !this.vCheckLines.Y.display) {
  244. if (listenLayer.modifyPoint) {
  245. if (listenLayer.modifyPoint.linkedRoadPointIdX) {
  246. let linkedPointX = dataService.getRoadPoint(
  247. listenLayer.modifyPoint.linkedRoadPointIdX
  248. );
  249. this.setCheckLinesX(linkedPointX, position);
  250. this.showCheckLinesX();
  251. } else if (listenLayer.modifyPoint.linkedCurveRoadPointIdX) {
  252. let linkedPointX = dataService.getCurveRoadPoint(
  253. listenLayer.modifyPoint.linkedCurveRoadPointIdX
  254. );
  255. this.setCheckLinesX(linkedPointX, position);
  256. this.showCheckLinesX();
  257. } else if (listenLayer.modifyPoint.linkedPointIdX) {
  258. let linkedPointX = dataService.getPoint(
  259. listenLayer.modifyPoint.linkedPointIdX
  260. );
  261. this.setCheckLinesX(linkedPointX, position);
  262. this.showCheckLinesX();
  263. }
  264. if (listenLayer.modifyPoint.linkedRoadPointIdY) {
  265. let linkedPointY = dataService.getRoadPoint(
  266. listenLayer.modifyPoint.linkedRoadPointIdY
  267. );
  268. this.setCheckLinesY(linkedPointY, position);
  269. this.showCheckLinesY();
  270. } else if (listenLayer.modifyPoint.linkedCurveRoadPointIdY) {
  271. let linkedPointY = dataService.getCurveRoadPoint(
  272. listenLayer.modifyPoint.linkedCurveRoadPointIdY
  273. );
  274. this.setCheckLinesY(linkedPointY, position);
  275. this.showCheckLinesY();
  276. } else if (listenLayer.modifyPoint.linkedPointIdY) {
  277. let linkedPointY = dataService.getPoint(
  278. listenLayer.modifyPoint.linkedPointIdY
  279. );
  280. this.setCheckLinesY(linkedPointY, position);
  281. this.showCheckLinesY();
  282. }
  283. }
  284. }
  285. }
  286. // //pointId是角度的顶点
  287. // //exceptPointId表示position对应的pointId(如果有的话)
  288. // checkAngle(position, pointId, exceptPointId) {
  289. // //type:1表示90°,2表示180°
  290. // function ajust(position, point1, point2, type) {
  291. // let line = mathUtil.createLine1(point1, point2);
  292. // let join = null;
  293. // if (type == 1) {
  294. // let vLine = mathUtil.getVerticalLine(line, point1);
  295. // join = mathUtil.getJoinLinePoint(position, vLine);
  296. // } else if (type == 2) {
  297. // join = mathUtil.getJoinLinePoint(position, line);
  298. // }
  299. // return join;
  300. // }
  301. // let points = roadService.getNeighPoints(pointId, exceptPointId);
  302. // let point = dataService.getRoadPoint(pointId);
  303. // let newPosition = null;
  304. // for (let i = 0; i < points.length; ++i) {
  305. // let angle = mathUtil.Angle(point, position, points[i]);
  306. // if (Math.abs((angle - 90) < 5) {
  307. // newPosition = ajust(position, point, points[i], 1);
  308. // } else if (
  309. // Math.abs(angle < 5 ||
  310. // Math.abs(angle - 180) < 5
  311. // ) {
  312. // newPosition = ajust(position, point, points[i], 2);
  313. // }
  314. // if (newPosition != null) {
  315. // return newPosition;
  316. // }
  317. // }
  318. // return newPosition;
  319. // }
  320. }
  321. const elementService = new ElementService();
  322. window.elementService = elementService;
  323. export { elementService };