import { mathUtil } from "../Util/MathUtil"; import { circleService } from "../Service/CircleService"; import { listenLayer } from "../ListenLayer"; import { roadPointService } from "../Service/RoadPointService"; import { roadService } from "../Service/RoadService"; import { edgeService } from "../Service/EdgeService"; import { dataService } from "../Service/DataService"; import { uiService } from "../Service/UIService"; import UIEvents from "../enum/UIEvents.js"; import Constant from "../Constant.js"; import Settings from "../Settings.js"; import RoadTemplate from "../enum/RoadTemplate"; import { curveRoadPointService } from "../Service/CurveRoadPointService"; import { curveRoadService } from "../Service/CurveRoadService"; import { curveEdgeService } from "../Service/CurveEdgeService"; import CurveRoad from "../Geometry/CurveRoad.js"; export default class AddCrossRoad { constructor() {} build(position) { const roadTemplate = uiService.getSelectRoadTemplate(); // 丁字路口 if (roadTemplate == RoadTemplate.TJunction) { this.buildThree(position); } // 十字路口 else if (roadTemplate == RoadTemplate.Crossroads) { this.buildFour(position); } // 五岔路口 else if (roadTemplate == RoadTemplate.FiveForks) { this.buildFive(position); } // 六岔路口 else if (roadTemplate == RoadTemplate.SixForkIntersection) { this.buildSix(position); } // s型弯路 else if (roadTemplate == RoadTemplate.SBend) { this.buildCurveRoad(position); } // 出口匝道 else if (roadTemplate == RoadTemplate.ExitRamp) { this.buildExitRamp(position); } // 国道(路肩) else if (roadTemplate == RoadTemplate.NationalHighwayShoulder) { this.buildNationalHighwayShoulder(position); } // 室内路段 else if (roadTemplate == RoadTemplate.IndoorSection) { this.buildIndoorSection(position); } // 弯道 else if (roadTemplate == RoadTemplate.Bend) { this.buildBend(position); } // 急转弯道 else if (roadTemplate == RoadTemplate.SharpCurve) { this.buildSharpCurve(position); } // 直角弯道 else if (roadTemplate == RoadTemplate.Corner) { this.buildCorner(position); } // 进口砸到 else if (roadTemplate == RoadTemplate.ImportSmashedRoad) { this.buildImportSmashedRoad(position); } // 高速收费站 else if (roadTemplate == RoadTemplate.HighSpeedTollBooth) { this.buildHighSpeedTollBooth(position); } // 高速港湾 else if (roadTemplate == RoadTemplate.HighSpeedHarbor) { this.buildHighSpeedHarbor(position); } // 高速路段 else if (roadTemplate == RoadTemplate.HighwaySection) { this.buildHighwaySection(position); } // 宽变窄路段 else if (roadTemplate == RoadTemplate.WideNarrowRoad) { this.buildWideNarrowRoad(position); } } //s型弯路 buildCurveRoad(position) { const roadLen = 500; const offsetDis = 100; let startPoint = { x: position.x, y: position.y + roadLen / 2, }; let endPoint = { x: position.x, y: position.y - roadLen / 2, }; startPoint = curveRoadPointService.create(startPoint); endPoint = curveRoadPointService.create(endPoint); let curveRoad = new CurveRoad(startPoint.vectorId, endPoint.vectorId); dataService.addCurveRoad(curveRoad); startPoint.setPointParent(curveRoad.vectorId); startPoint.setIndex(0); endPoint.setPointParent(curveRoad.vectorId); endPoint.setIndex(2); let edgePoints = mathUtil.RectangleVertex( startPoint, endPoint, curveRoad.leftWidth + curveRoad.rightWidth + curveRoad.midDivide.midDivideWidth ); let leftEdge = curveEdgeService.create( edgePoints.leftEdgeStart, edgePoints.leftEdgeEnd, null, curveRoad.vectorId ); let rightEdge = curveEdgeService.create( edgePoints.rightEdgeStart, edgePoints.rightEdgeEnd, null, curveRoad.vectorId ); curveRoad.setLeftEdge(leftEdge.vectorId); curveRoad.setRightEdge(rightEdge.vectorId); curveRoad.points.push(startPoint); curveRoad.points.push(endPoint); leftEdge.points.push(edgePoints.leftEdgeStart); leftEdge.points.push(edgePoints.leftEdgeEnd); rightEdge.points.push(edgePoints.rightEdgeStart); rightEdge.points.push(edgePoints.rightEdgeEnd); //得有点曲率 const midPoint = { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, }; let mid1 = { x: (startPoint.x + midPoint.x) / 2, y: (startPoint.y + midPoint.y) / 2, }; let mid2 = { x: (endPoint.x + midPoint.x) / 2, y: (endPoint.y + midPoint.y) / 2, }; curveRoadService.addCPoint(curveRoad, mid1, 0); curveRoadService.addCPoint(curveRoad, mid2, 1); mid1.x -= offsetDis; mid2.x += offsetDis; curveRoadService.updateForMovePoint(curveRoad.points[1].vectorId, mid1); curveRoadService.updateForMovePoint(curveRoad.points[2].vectorId, mid2); } //三岔口 buildThree(position) { const len = 300; let end = roadPointService.create(position); let start1 = roadPointService.create({ x: end.x, y: end.y + len, }); let start2 = roadPointService.create({ x: end.x + len, y: end.y, }); let start3 = roadPointService.create({ x: end.x, y: end.y - len, }); //需要设置公路的车道数,是否双向等等 uiService.isBelongRoad(UIEvents.TwoEdgeOneLanRoad); roadService.create(start1.vectorId, end.vectorId); roadService.create(start2.vectorId, end.vectorId); roadService.create(start3.vectorId, end.vectorId); edgeService.updateEdgeForMulRoad(end.vectorId); } //四岔口 buildFour(position) { const len = 300; let end = roadPointService.create(position); let start1 = roadPointService.create({ x: end.x, y: end.y + len, }); let start2 = roadPointService.create({ x: end.x, y: end.y - len, }); let start3 = roadPointService.create({ x: end.x + len, y: end.y, }); let start4 = roadPointService.create({ x: end.x - len, y: end.y, }); //需要设置公路的车道数,是否双向等等 uiService.isBelongRoad(UIEvents.TwoEdgeOneLanRoad); roadService.create(start1.vectorId, end.vectorId); roadService.create(start2.vectorId, end.vectorId); roadService.create(start3.vectorId, end.vectorId); roadService.create(start4.vectorId, end.vectorId); edgeService.updateEdgeForMulRoad(end.vectorId); } //五岔口 buildFive(position) { const len = 300; const points = mathUtil.createFivePointedStar(position, len); let end = roadPointService.create(position); let start1 = roadPointService.create(points[0]); let start2 = roadPointService.create(points[1]); let start3 = roadPointService.create(points[2]); let start4 = roadPointService.create(points[3]); let start5 = roadPointService.create(points[4]); roadService.create(start1.vectorId, end.vectorId); roadService.create(start2.vectorId, end.vectorId); roadService.create(start3.vectorId, end.vectorId); roadService.create(start4.vectorId, end.vectorId); roadService.create(start5.vectorId, end.vectorId); edgeService.updateEdgeForMulRoad(end.vectorId); } //六岔口 buildSix(position) { const len = 300; const points = mathUtil.createSixPoint(position, len); let end = roadPointService.create(position); let start1 = roadPointService.create(points[0]); let start2 = roadPointService.create(points[1]); let start3 = roadPointService.create(points[2]); let start4 = roadPointService.create(points[3]); let start5 = roadPointService.create(points[4]); let start6 = roadPointService.create(points[5]); roadService.create(start1.vectorId, end.vectorId); roadService.create(start2.vectorId, end.vectorId); roadService.create(start3.vectorId, end.vectorId); roadService.create(start4.vectorId, end.vectorId); roadService.create(start5.vectorId, end.vectorId); roadService.create(start6.vectorId, end.vectorId); edgeService.updateEdgeForMulRoad(end.vectorId); } buildExitRamp(position) { const roadLen = 600; let startPoint = { x: position.x, y: position.y + (roadLen * 3) / 5, }; let endPoint = { x: position.x, y: position.y - (roadLen * 2) / 5, }; startPoint = roadPointService.create(startPoint); endPoint = roadPointService.create(endPoint); let crossPoint = roadPointService.create(position); uiService.isBelongRoad(UIEvents.OneEdgeThreeLanRoad); roadService.create(startPoint.vectorId, crossPoint.vectorId); roadService.create(crossPoint.vectorId, endPoint.vectorId); let startPoint2 = { x: startPoint.x + roadLen / 4, y: startPoint.y, }; startPoint2 = roadPointService.create(startPoint2); uiService.isBelongRoad(UIEvents.OneEdgeOneLanRoad); roadService.create(startPoint2.vectorId, crossPoint.vectorId); edgeService.updateEdgeForMulRoad(crossPoint.vectorId); } buildNationalHighwayShoulder(position) { const roadLen = 600; let startPoint = { x: position.x, y: position.y + roadLen / 2, }; let endPoint = { x: position.x, y: position.y - roadLen / 2, }; startPoint = roadPointService.create(startPoint); endPoint = roadPointService.create(endPoint); uiService.isBelongRoad(UIEvents.TwoEdgeTwoLanRoad); roadService.create(startPoint.vectorId, endPoint.vectorId); } buildIndoorSection(position) { const roadLen = 600; let startPoint = { x: position.x, y: position.y + roadLen / 2, }; let endPoint = { x: position.x, y: position.y - roadLen / 2, }; startPoint = roadPointService.create(startPoint); endPoint = roadPointService.create(endPoint); uiService.isBelongRoad(UIEvents.TwoEdgeThreeLanRoad); roadService.create(startPoint.vectorId, endPoint.vectorId); } buildBend(position) { const roadLen = 500; const offsetDis = 150; let startPoint = { x: position.x, y: position.y + roadLen / 2, }; let endPoint = { x: position.x, y: position.y - roadLen / 2, }; startPoint = curveRoadPointService.create(startPoint); endPoint = curveRoadPointService.create(endPoint); uiService.setCurveRoadLeftDrivewayCount(2); uiService.setCurveRoadRightDrivewayCount(2); uiService.setLeftCurveRoadWidth(100); uiService.setRightCurveRoadWidth(100); const curveRoad = curveRoadService.create( startPoint.vectorId, endPoint.vectorId ); const midPoint = { x: (startPoint.x + endPoint.x) / 2 - offsetDis, y: (startPoint.y + endPoint.y) / 2, }; curveRoadService.updateForMovePoint(curveRoad.points[1].vectorId, midPoint); uiService.setCurveRoadLeftDrivewayCount(1); uiService.setCurveRoadRightDrivewayCount(1); uiService.setLeftCurveRoadWidth(50); uiService.setRightCurveRoadWidth(50); } buildSharpCurve(position) { const roadLen = 650; const height = 450; let startPoint = { x: position.x + roadLen, y: position.y + height / 2, }; let endPoint = { x: position.x + roadLen, y: position.y - height / 2, }; startPoint = curveRoadPointService.create(startPoint); endPoint = curveRoadPointService.create(endPoint); uiService.setCurveRoadLeftDrivewayCount(2); uiService.setCurveRoadRightDrivewayCount(2); uiService.setLeftCurveRoadWidth(100); uiService.setRightCurveRoadWidth(100); let curveRoad = new CurveRoad(startPoint.vectorId, endPoint.vectorId); dataService.addCurveRoad(curveRoad); startPoint.setPointParent(curveRoad.vectorId); startPoint.setIndex(0); endPoint.setPointParent(curveRoad.vectorId); endPoint.setIndex(2); let edgePoints = mathUtil.RectangleVertex( startPoint, endPoint, curveRoad.leftWidth + curveRoad.rightWidth + curveRoad.midDivide.midDivideWidth ); let leftEdge = curveEdgeService.create( edgePoints.leftEdgeStart, edgePoints.leftEdgeEnd, null, curveRoad.vectorId ); let rightEdge = curveEdgeService.create( edgePoints.rightEdgeStart, edgePoints.rightEdgeEnd, null, curveRoad.vectorId ); curveRoad.setLeftEdge(leftEdge.vectorId); curveRoad.setRightEdge(rightEdge.vectorId); curveRoad.points.push(startPoint); curveRoad.points.push(endPoint); leftEdge.points.push(edgePoints.leftEdgeStart); leftEdge.points.push(edgePoints.leftEdgeEnd); rightEdge.points.push(edgePoints.rightEdgeStart); rightEdge.points.push(edgePoints.rightEdgeEnd); let point1 = { x: position.x + 200, y: position.y + height / 2, }; let point2 = { x: position.x + 50, y: position.y + height / 2 - 50, }; let point3 = { x: position.x + 50, y: position.y - height / 2 + 50, }; let point4 = { x: position.x + 200, y: position.y - height / 2, }; curveRoadService.addCPoint(curveRoad, point1, 0); curveRoadService.addCPoint(curveRoad, point2, 1); curveRoadService.addCPoint(curveRoad, position, 2); curveRoadService.addCPoint(curveRoad, point3, 3); curveRoadService.addCPoint(curveRoad, point4, 4); curveRoadService.updateForMovePoint(curveRoad.points[1].vectorId, point1); curveRoadService.updateForMovePoint(curveRoad.points[2].vectorId, point2); curveRoadService.updateForMovePoint(curveRoad.points[3].vectorId, position); curveRoadService.updateForMovePoint(curveRoad.points[4].vectorId, point3); curveRoadService.updateForMovePoint(curveRoad.points[5].vectorId, point4); uiService.setCurveRoadLeftDrivewayCount(1); uiService.setCurveRoadRightDrivewayCount(1); uiService.setLeftCurveRoadWidth(50); uiService.setRightCurveRoadWidth(50); } buildCorner(position) { const len = 300; let end = roadPointService.create(position); let start1 = roadPointService.create({ x: end.x + len, y: end.y, }); let start2 = roadPointService.create({ x: end.x, y: end.y - len, }); //需要设置公路的车道数,是否双向等等 uiService.isBelongRoad(UIEvents.TwoEdgeTwoLanRoad); roadService.create(start1.vectorId, end.vectorId); roadService.create(start2.vectorId, end.vectorId); edgeService.updateEdgeForMulRoad(end.vectorId); } buildImportSmashedRoad(position) { const roadLen = 600; let startPoint = { x: position.x, y: position.y - (roadLen * 3) / 5, }; let endPoint = { x: position.x, y: position.y + (roadLen * 2) / 5, }; startPoint = roadPointService.create(startPoint); endPoint = roadPointService.create(endPoint); let crossPoint = roadPointService.create(position); uiService.isBelongRoad(UIEvents.OneEdgeThreeLanRoad); roadService.create(startPoint.vectorId, crossPoint.vectorId); roadService.create(crossPoint.vectorId, endPoint.vectorId); let startPoint2 = { x: startPoint.x + roadLen / 4, y: startPoint.y, }; startPoint2 = roadPointService.create(startPoint2); uiService.isBelongRoad(UIEvents.OneEdgeOneLanRoad); roadService.create(startPoint2.vectorId, crossPoint.vectorId); edgeService.updateEdgeForMulRoad(crossPoint.vectorId); } buildHighSpeedTollBooth(position) { const roadLen = 600; let startPoint = { x: position.x, y: position.y + roadLen / 2, }; let endPoint = { x: position.x, y: position.y - roadLen / 2, }; startPoint = roadPointService.create(startPoint); endPoint = roadPointService.create(endPoint); uiService.setWayType(Constant.oneWay); uiService.setSingleRoadDrivewayCount(6); uiService.setSingleRoadWidth(Settings.singleLaneWidth * 6); roadService.create(startPoint.vectorId, endPoint.vectorId); } buildHighwaySection(position) { const roadLen = 600; let startPoint = { x: position.x, y: position.y + roadLen / 2, }; let endPoint = { x: position.x, y: position.y - roadLen / 2, }; startPoint = roadPointService.create(startPoint); endPoint = roadPointService.create(endPoint); uiService.isBelongRoad(UIEvents.TwoEdgeTwoLanRoad); roadService.create(startPoint.vectorId, endPoint.vectorId); } //这个还没解决 buildHighSpeedHarbor(position) {} //这个还没解决 buildWideNarrowRoad(position) {} } const addCrossRoad = new AddCrossRoad(); export { addCrossRoad };