AddCrossRoad.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. import { mathUtil } from "../Util/MathUtil";
  2. import { circleService } from "../Service/CircleService";
  3. import { listenLayer } from "../ListenLayer";
  4. import { roadPointService } from "../Service/RoadPointService";
  5. import { roadService } from "../Service/RoadService";
  6. import { edgeService } from "../Service/EdgeService";
  7. import { dataService } from "../Service/DataService";
  8. import { uiService } from "../Service/UIService";
  9. import UIEvents from "../enum/UIEvents.js";
  10. import Constant from "../Constant.js";
  11. import Settings from "../Settings.js";
  12. import RoadTemplate from "../enum/RoadTemplate";
  13. import { curveRoadPointService } from "../Service/CurveRoadPointService";
  14. import { curveRoadService } from "../Service/CurveRoadService";
  15. import { curveEdgeService } from "../Service/CurveEdgeService";
  16. import CurveRoad from "../Geometry/CurveRoad.js";
  17. export default class AddCrossRoad {
  18. constructor() {}
  19. build(position) {
  20. const roadTemplate = uiService.getSelectRoadTemplate();
  21. // 丁字路口
  22. if (roadTemplate == RoadTemplate.TJunction) {
  23. this.buildThree(position);
  24. }
  25. // 十字路口
  26. else if (roadTemplate == RoadTemplate.Crossroads) {
  27. this.buildFour(position);
  28. }
  29. // 五岔路口
  30. else if (roadTemplate == RoadTemplate.FiveForks) {
  31. this.buildFive(position);
  32. }
  33. // 六岔路口
  34. else if (roadTemplate == RoadTemplate.SixForkIntersection) {
  35. this.buildSix(position);
  36. }
  37. // s型弯路
  38. else if (roadTemplate == RoadTemplate.SBend) {
  39. this.buildCurveRoad(position);
  40. }
  41. // 出口匝道
  42. else if (roadTemplate == RoadTemplate.ExitRamp) {
  43. this.buildExitRamp(position);
  44. }
  45. // 国道(路肩)
  46. else if (roadTemplate == RoadTemplate.NationalHighwayShoulder) {
  47. this.buildNationalHighwayShoulder(position);
  48. }
  49. // 室内路段
  50. else if (roadTemplate == RoadTemplate.IndoorSection) {
  51. this.buildIndoorSection(position);
  52. }
  53. // 弯道
  54. else if (roadTemplate == RoadTemplate.Bend) {
  55. this.buildBend(position);
  56. }
  57. // 急转弯道
  58. else if (roadTemplate == RoadTemplate.SharpCurve) {
  59. this.buildSharpCurve(position);
  60. }
  61. // 直角弯道
  62. else if (roadTemplate == RoadTemplate.Corner) {
  63. this.buildCorner(position);
  64. }
  65. // 进口砸到
  66. else if (roadTemplate == RoadTemplate.ImportSmashedRoad) {
  67. this.buildImportSmashedRoad(position);
  68. }
  69. // 高速收费站
  70. else if (roadTemplate == RoadTemplate.HighSpeedTollBooth) {
  71. this.buildHighSpeedTollBooth(position);
  72. }
  73. // 高速港湾
  74. else if (roadTemplate == RoadTemplate.HighSpeedHarbor) {
  75. this.buildHighSpeedHarbor(position);
  76. }
  77. // 高速路段
  78. else if (roadTemplate == RoadTemplate.HighwaySection) {
  79. this.buildHighwaySection(position);
  80. }
  81. // 宽变窄路段
  82. else if (roadTemplate == RoadTemplate.WideNarrowRoad) {
  83. this.buildWideNarrowRoad(position);
  84. }
  85. }
  86. //s型弯路
  87. buildCurveRoad(position) {
  88. const roadLen = 500;
  89. const offsetDis = 100;
  90. let startPoint = {
  91. x: position.x,
  92. y: position.y + roadLen / 2,
  93. };
  94. let endPoint = {
  95. x: position.x,
  96. y: position.y - roadLen / 2,
  97. };
  98. startPoint = curveRoadPointService.create(startPoint);
  99. endPoint = curveRoadPointService.create(endPoint);
  100. let curveRoad = new CurveRoad(startPoint.vectorId, endPoint.vectorId);
  101. dataService.addCurveRoad(curveRoad);
  102. startPoint.setPointParent(curveRoad.vectorId);
  103. startPoint.setIndex(0);
  104. endPoint.setPointParent(curveRoad.vectorId);
  105. endPoint.setIndex(2);
  106. let edgePoints = mathUtil.RectangleVertex(
  107. startPoint,
  108. endPoint,
  109. curveRoad.leftWidth +
  110. curveRoad.rightWidth +
  111. curveRoad.midDivide.midDivideWidth
  112. );
  113. let leftEdge = curveEdgeService.create(
  114. edgePoints.leftEdgeStart,
  115. edgePoints.leftEdgeEnd,
  116. null,
  117. curveRoad.vectorId
  118. );
  119. let rightEdge = curveEdgeService.create(
  120. edgePoints.rightEdgeStart,
  121. edgePoints.rightEdgeEnd,
  122. null,
  123. curveRoad.vectorId
  124. );
  125. curveRoad.setLeftEdge(leftEdge.vectorId);
  126. curveRoad.setRightEdge(rightEdge.vectorId);
  127. curveRoad.points.push(startPoint);
  128. curveRoad.points.push(endPoint);
  129. leftEdge.points.push(edgePoints.leftEdgeStart);
  130. leftEdge.points.push(edgePoints.leftEdgeEnd);
  131. rightEdge.points.push(edgePoints.rightEdgeStart);
  132. rightEdge.points.push(edgePoints.rightEdgeEnd);
  133. //得有点曲率
  134. const midPoint = {
  135. x: (startPoint.x + endPoint.x) / 2,
  136. y: (startPoint.y + endPoint.y) / 2,
  137. };
  138. let mid1 = {
  139. x: (startPoint.x + midPoint.x) / 2,
  140. y: (startPoint.y + midPoint.y) / 2,
  141. };
  142. let mid2 = {
  143. x: (endPoint.x + midPoint.x) / 2,
  144. y: (endPoint.y + midPoint.y) / 2,
  145. };
  146. curveRoadService.addCPoint(curveRoad, mid1, 0);
  147. curveRoadService.addCPoint(curveRoad, mid2, 1);
  148. mid1.x -= offsetDis;
  149. mid2.x += offsetDis;
  150. curveRoadService.updateForMovePoint(curveRoad.points[1].vectorId, mid1);
  151. curveRoadService.updateForMovePoint(curveRoad.points[2].vectorId, mid2);
  152. }
  153. //三岔口
  154. buildThree(position) {
  155. const len = 300;
  156. let end = roadPointService.create(position);
  157. let start1 = roadPointService.create({
  158. x: end.x,
  159. y: end.y + len,
  160. });
  161. let start2 = roadPointService.create({
  162. x: end.x + len,
  163. y: end.y,
  164. });
  165. let start3 = roadPointService.create({
  166. x: end.x,
  167. y: end.y - len,
  168. });
  169. //需要设置公路的车道数,是否双向等等
  170. uiService.isBelongRoad(UIEvents.TwoEdgeOneLanRoad);
  171. roadService.create(start1.vectorId, end.vectorId);
  172. roadService.create(start2.vectorId, end.vectorId);
  173. roadService.create(start3.vectorId, end.vectorId);
  174. edgeService.updateEdgeForMulRoad(end.vectorId);
  175. }
  176. //四岔口
  177. buildFour(position) {
  178. const len = 300;
  179. let end = roadPointService.create(position);
  180. let start1 = roadPointService.create({
  181. x: end.x,
  182. y: end.y + len,
  183. });
  184. let start2 = roadPointService.create({
  185. x: end.x,
  186. y: end.y - len,
  187. });
  188. let start3 = roadPointService.create({
  189. x: end.x + len,
  190. y: end.y,
  191. });
  192. let start4 = roadPointService.create({
  193. x: end.x - len,
  194. y: end.y,
  195. });
  196. //需要设置公路的车道数,是否双向等等
  197. uiService.isBelongRoad(UIEvents.TwoEdgeOneLanRoad);
  198. roadService.create(start1.vectorId, end.vectorId);
  199. roadService.create(start2.vectorId, end.vectorId);
  200. roadService.create(start3.vectorId, end.vectorId);
  201. roadService.create(start4.vectorId, end.vectorId);
  202. edgeService.updateEdgeForMulRoad(end.vectorId);
  203. }
  204. //五岔口
  205. buildFive(position) {
  206. const len = 300;
  207. const points = mathUtil.createFivePointedStar(position, len);
  208. let end = roadPointService.create(position);
  209. let start1 = roadPointService.create(points[0]);
  210. let start2 = roadPointService.create(points[1]);
  211. let start3 = roadPointService.create(points[2]);
  212. let start4 = roadPointService.create(points[3]);
  213. let start5 = roadPointService.create(points[4]);
  214. roadService.create(start1.vectorId, end.vectorId);
  215. roadService.create(start2.vectorId, end.vectorId);
  216. roadService.create(start3.vectorId, end.vectorId);
  217. roadService.create(start4.vectorId, end.vectorId);
  218. roadService.create(start5.vectorId, end.vectorId);
  219. edgeService.updateEdgeForMulRoad(end.vectorId);
  220. }
  221. //六岔口
  222. buildSix(position) {
  223. const len = 300;
  224. const points = mathUtil.createSixPoint(position, len);
  225. let end = roadPointService.create(position);
  226. let start1 = roadPointService.create(points[0]);
  227. let start2 = roadPointService.create(points[1]);
  228. let start3 = roadPointService.create(points[2]);
  229. let start4 = roadPointService.create(points[3]);
  230. let start5 = roadPointService.create(points[4]);
  231. let start6 = roadPointService.create(points[5]);
  232. roadService.create(start1.vectorId, end.vectorId);
  233. roadService.create(start2.vectorId, end.vectorId);
  234. roadService.create(start3.vectorId, end.vectorId);
  235. roadService.create(start4.vectorId, end.vectorId);
  236. roadService.create(start5.vectorId, end.vectorId);
  237. roadService.create(start6.vectorId, end.vectorId);
  238. edgeService.updateEdgeForMulRoad(end.vectorId);
  239. }
  240. buildExitRamp(position) {
  241. const roadLen = 600;
  242. let startPoint = {
  243. x: position.x,
  244. y: position.y + (roadLen * 3) / 5,
  245. };
  246. let endPoint = {
  247. x: position.x,
  248. y: position.y - (roadLen * 2) / 5,
  249. };
  250. startPoint = roadPointService.create(startPoint);
  251. endPoint = roadPointService.create(endPoint);
  252. let crossPoint = roadPointService.create(position);
  253. uiService.isBelongRoad(UIEvents.OneEdgeThreeLanRoad);
  254. roadService.create(startPoint.vectorId, crossPoint.vectorId);
  255. roadService.create(crossPoint.vectorId, endPoint.vectorId);
  256. let startPoint2 = {
  257. x: startPoint.x + roadLen / 4,
  258. y: startPoint.y,
  259. };
  260. startPoint2 = roadPointService.create(startPoint2);
  261. uiService.isBelongRoad(UIEvents.OneEdgeOneLanRoad);
  262. roadService.create(startPoint2.vectorId, crossPoint.vectorId);
  263. edgeService.updateEdgeForMulRoad(crossPoint.vectorId);
  264. }
  265. buildNationalHighwayShoulder(position) {
  266. const roadLen = 600;
  267. let startPoint = {
  268. x: position.x,
  269. y: position.y + roadLen / 2,
  270. };
  271. let endPoint = {
  272. x: position.x,
  273. y: position.y - roadLen / 2,
  274. };
  275. startPoint = roadPointService.create(startPoint);
  276. endPoint = roadPointService.create(endPoint);
  277. uiService.isBelongRoad(UIEvents.TwoEdgeTwoLanRoad);
  278. roadService.create(startPoint.vectorId, endPoint.vectorId);
  279. }
  280. buildIndoorSection(position) {
  281. const roadLen = 600;
  282. let startPoint = {
  283. x: position.x,
  284. y: position.y + roadLen / 2,
  285. };
  286. let endPoint = {
  287. x: position.x,
  288. y: position.y - roadLen / 2,
  289. };
  290. startPoint = roadPointService.create(startPoint);
  291. endPoint = roadPointService.create(endPoint);
  292. uiService.isBelongRoad(UIEvents.TwoEdgeThreeLanRoad);
  293. roadService.create(startPoint.vectorId, endPoint.vectorId);
  294. }
  295. buildBend(position) {
  296. const roadLen = 500;
  297. const offsetDis = 150;
  298. let startPoint = {
  299. x: position.x,
  300. y: position.y + roadLen / 2,
  301. };
  302. let endPoint = {
  303. x: position.x,
  304. y: position.y - roadLen / 2,
  305. };
  306. startPoint = curveRoadPointService.create(startPoint);
  307. endPoint = curveRoadPointService.create(endPoint);
  308. uiService.setCurveRoadLeftDrivewayCount(2);
  309. uiService.setCurveRoadRightDrivewayCount(2);
  310. uiService.setLeftCurveRoadWidth(100);
  311. uiService.setRightCurveRoadWidth(100);
  312. const curveRoad = curveRoadService.create(
  313. startPoint.vectorId,
  314. endPoint.vectorId
  315. );
  316. const midPoint = {
  317. x: (startPoint.x + endPoint.x) / 2 - offsetDis,
  318. y: (startPoint.y + endPoint.y) / 2,
  319. };
  320. curveRoadService.updateForMovePoint(curveRoad.points[1].vectorId, midPoint);
  321. uiService.setCurveRoadLeftDrivewayCount(1);
  322. uiService.setCurveRoadRightDrivewayCount(1);
  323. uiService.setLeftCurveRoadWidth(50);
  324. uiService.setRightCurveRoadWidth(50);
  325. }
  326. buildSharpCurve(position) {
  327. const roadLen = 650;
  328. const height = 450;
  329. let startPoint = {
  330. x: position.x + roadLen,
  331. y: position.y + height / 2,
  332. };
  333. let endPoint = {
  334. x: position.x + roadLen,
  335. y: position.y - height / 2,
  336. };
  337. startPoint = curveRoadPointService.create(startPoint);
  338. endPoint = curveRoadPointService.create(endPoint);
  339. uiService.setCurveRoadLeftDrivewayCount(2);
  340. uiService.setCurveRoadRightDrivewayCount(2);
  341. uiService.setLeftCurveRoadWidth(100);
  342. uiService.setRightCurveRoadWidth(100);
  343. let curveRoad = new CurveRoad(startPoint.vectorId, endPoint.vectorId);
  344. dataService.addCurveRoad(curveRoad);
  345. startPoint.setPointParent(curveRoad.vectorId);
  346. startPoint.setIndex(0);
  347. endPoint.setPointParent(curveRoad.vectorId);
  348. endPoint.setIndex(2);
  349. let edgePoints = mathUtil.RectangleVertex(
  350. startPoint,
  351. endPoint,
  352. curveRoad.leftWidth +
  353. curveRoad.rightWidth +
  354. curveRoad.midDivide.midDivideWidth
  355. );
  356. let leftEdge = curveEdgeService.create(
  357. edgePoints.leftEdgeStart,
  358. edgePoints.leftEdgeEnd,
  359. null,
  360. curveRoad.vectorId
  361. );
  362. let rightEdge = curveEdgeService.create(
  363. edgePoints.rightEdgeStart,
  364. edgePoints.rightEdgeEnd,
  365. null,
  366. curveRoad.vectorId
  367. );
  368. curveRoad.setLeftEdge(leftEdge.vectorId);
  369. curveRoad.setRightEdge(rightEdge.vectorId);
  370. curveRoad.points.push(startPoint);
  371. curveRoad.points.push(endPoint);
  372. leftEdge.points.push(edgePoints.leftEdgeStart);
  373. leftEdge.points.push(edgePoints.leftEdgeEnd);
  374. rightEdge.points.push(edgePoints.rightEdgeStart);
  375. rightEdge.points.push(edgePoints.rightEdgeEnd);
  376. let point1 = {
  377. x: position.x + 200,
  378. y: position.y + height / 2,
  379. };
  380. let point2 = {
  381. x: position.x + 50,
  382. y: position.y + height / 2 - 50,
  383. };
  384. let point3 = {
  385. x: position.x + 50,
  386. y: position.y - height / 2 + 50,
  387. };
  388. let point4 = {
  389. x: position.x + 200,
  390. y: position.y - height / 2,
  391. };
  392. curveRoadService.addCPoint(curveRoad, point1, 0);
  393. curveRoadService.addCPoint(curveRoad, point2, 1);
  394. curveRoadService.addCPoint(curveRoad, position, 2);
  395. curveRoadService.addCPoint(curveRoad, point3, 3);
  396. curveRoadService.addCPoint(curveRoad, point4, 4);
  397. curveRoadService.updateForMovePoint(curveRoad.points[1].vectorId, point1);
  398. curveRoadService.updateForMovePoint(curveRoad.points[2].vectorId, point2);
  399. curveRoadService.updateForMovePoint(curveRoad.points[3].vectorId, position);
  400. curveRoadService.updateForMovePoint(curveRoad.points[4].vectorId, point3);
  401. curveRoadService.updateForMovePoint(curveRoad.points[5].vectorId, point4);
  402. uiService.setCurveRoadLeftDrivewayCount(1);
  403. uiService.setCurveRoadRightDrivewayCount(1);
  404. uiService.setLeftCurveRoadWidth(50);
  405. uiService.setRightCurveRoadWidth(50);
  406. }
  407. buildCorner(position) {
  408. const len = 300;
  409. let end = roadPointService.create(position);
  410. let start1 = roadPointService.create({
  411. x: end.x + len,
  412. y: end.y,
  413. });
  414. let start2 = roadPointService.create({
  415. x: end.x,
  416. y: end.y - len,
  417. });
  418. //需要设置公路的车道数,是否双向等等
  419. uiService.isBelongRoad(UIEvents.TwoEdgeTwoLanRoad);
  420. roadService.create(start1.vectorId, end.vectorId);
  421. roadService.create(start2.vectorId, end.vectorId);
  422. edgeService.updateEdgeForMulRoad(end.vectorId);
  423. }
  424. buildImportSmashedRoad(position) {
  425. const roadLen = 600;
  426. let startPoint = {
  427. x: position.x,
  428. y: position.y - (roadLen * 3) / 5,
  429. };
  430. let endPoint = {
  431. x: position.x,
  432. y: position.y + (roadLen * 2) / 5,
  433. };
  434. startPoint = roadPointService.create(startPoint);
  435. endPoint = roadPointService.create(endPoint);
  436. let crossPoint = roadPointService.create(position);
  437. uiService.isBelongRoad(UIEvents.OneEdgeThreeLanRoad);
  438. roadService.create(startPoint.vectorId, crossPoint.vectorId);
  439. roadService.create(crossPoint.vectorId, endPoint.vectorId);
  440. let startPoint2 = {
  441. x: startPoint.x + roadLen / 4,
  442. y: startPoint.y,
  443. };
  444. startPoint2 = roadPointService.create(startPoint2);
  445. uiService.isBelongRoad(UIEvents.OneEdgeOneLanRoad);
  446. roadService.create(startPoint2.vectorId, crossPoint.vectorId);
  447. edgeService.updateEdgeForMulRoad(crossPoint.vectorId);
  448. }
  449. buildHighSpeedTollBooth(position) {
  450. const roadLen = 600;
  451. let startPoint = {
  452. x: position.x,
  453. y: position.y + roadLen / 2,
  454. };
  455. let endPoint = {
  456. x: position.x,
  457. y: position.y - roadLen / 2,
  458. };
  459. startPoint = roadPointService.create(startPoint);
  460. endPoint = roadPointService.create(endPoint);
  461. uiService.setWayType(Constant.oneWay);
  462. uiService.setSingleRoadDrivewayCount(6);
  463. uiService.setSingleRoadWidth(Settings.singleLaneWidth * 6);
  464. roadService.create(startPoint.vectorId, endPoint.vectorId);
  465. }
  466. buildHighwaySection(position) {
  467. const roadLen = 600;
  468. let startPoint = {
  469. x: position.x,
  470. y: position.y + roadLen / 2,
  471. };
  472. let endPoint = {
  473. x: position.x,
  474. y: position.y - roadLen / 2,
  475. };
  476. startPoint = roadPointService.create(startPoint);
  477. endPoint = roadPointService.create(endPoint);
  478. uiService.isBelongRoad(UIEvents.TwoEdgeTwoLanRoad);
  479. roadService.create(startPoint.vectorId, endPoint.vectorId);
  480. }
  481. //这个还没解决
  482. buildHighSpeedHarbor(position) {}
  483. //这个还没解决
  484. buildWideNarrowRoad(position) {}
  485. }
  486. const addCrossRoad = new AddCrossRoad();
  487. export { addCrossRoad };