import { dataService } from "./DataService"; import { curvePointService } from "./CurvePointService"; import { curveEdgeService } from "./CurveEdgeService"; import { mathUtil } from "../Util/MathUtil.js"; import CurveRoad from "../Geometry/CurveRoad.js"; import VectorType from "../enum/VectorType"; import Constant from "../Constant"; import RoadService from "./RoadService"; export default class CurveRoadService extends RoadService { constructor() { super(); } create(startId, endId, vectorId) { let curveRoad = new CurveRoad(startId, endId, vectorId); dataService.addCurveRoad(curveRoad); let startPoint = dataService.getCurvePoint(startId); startPoint.setPointParent(curveRoad.vectorId); startPoint.setIndex(0); let endPoint = dataService.getCurvePoint(endId); endPoint.setPointParent(curveRoad.vectorId); endPoint.setIndex(2); let edgePoints = mathUtil.RectangleVertex( startPoint, endPoint, curveRoad.width ); let leftEdge = curveEdgeService.create( edgePoints.leftEdgeStart, edgePoints.leftEdgeEnd, null, vectorId ); let rightEdge = curveEdgeService.create( edgePoints.rightEdgeStart, edgePoints.rightEdgeEnd, null, vectorId ); curveRoad.setLeftEdge(leftEdge.vectorId); curveRoad.setRightEdge(rightEdge.vectorId); if (!vectorId) { leftEdge.setEdgeParent(curveRoad.vectorId); rightEdge.setEdgeParent(curveRoad.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); this.setLanes(curveRoad); this.addCPoint( curveRoad, { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, }, 0 ); return curveRoad; } //不能加首尾,只能加中间 addCPoint(curveRoad, position, startIndex) { let point = curvePointService.create(position); curveRoad.points.splice(startIndex + 1, 0, point); point.setPointParent(curveRoad.vectorId, startIndex); point.setIndex(startIndex + 1); for (let i = startIndex + 2; i < curveRoad.points.length; ++i) { curveRoad.points[i].setIndex(i); } let leftCount = curveRoad.leftDrivewayCount; let rightCount = curveRoad.rightDrivewayCount; let leftJoin = null; let rightJoin = null; const leftCurveEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); const rightCurveEdge = dataService.getCurveEdge(curveRoad.rightEdgeId); let line1 = mathUtil.createLine1( curveRoad.points[startIndex], curveRoad.points[startIndex + 1] ); let line2 = mathUtil.createLine1( curveRoad.points[startIndex + 1], curveRoad.points[startIndex + 2] ); const leftLine = mathUtil.createLine1( leftCurveEdge.points[startIndex], leftCurveEdge.points[startIndex + 1] ); let leftLine1 = mathUtil.createLine3( line1, leftCurveEdge.points[startIndex] ); let leftLine2 = mathUtil.createLine3( line2, leftCurveEdge.points[startIndex + 1] ); const rightLine = mathUtil.createLine1( rightCurveEdge.points[startIndex], rightCurveEdge.points[startIndex + 1] ); let rightLine1 = mathUtil.createLine3( line1, rightCurveEdge.points[startIndex] ); let rightLine2 = mathUtil.createLine3( line2, rightCurveEdge.points[startIndex + 1] ); if ( mathUtil.Angle( curveRoad.points[startIndex + 1], curveRoad.points[startIndex], curveRoad.points[startIndex + 2] ) > Constant.maxAngle ) { leftJoin = mathUtil.getJoinLinePoint(position, leftLine); leftCurveEdge.points.splice(startIndex + 1, 0, leftJoin); curveEdgeService.setCurves(leftCurveEdge); rightJoin = mathUtil.getJoinLinePoint(position, rightLine); rightCurveEdge.points.splice(startIndex + 1, 0, rightJoin); curveEdgeService.setCurves(rightCurveEdge); for (let i = 0; i < leftCount - 1; ++i) { const leftLaneLine = mathUtil.createLine1( curveRoad.leftLanes[i][startIndex], curveRoad.leftLanes[i][startIndex + 1] ); leftJoin = mathUtil.getJoinLinePoint(position, leftLaneLine); curveRoad.leftLanes[i].splice(startIndex + 1, 0, leftJoin); curveRoad.leftLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.leftLanes[i] ); } for (let i = 0; i < rightCount - 1; ++i) { const rightLaneLine = mathUtil.createLine1( curveRoad.rightLanes[i][startIndex], curveRoad.rightLanes[i][startIndex + 1] ); rightJoin = mathUtil.getJoinLinePoint(position, rightLaneLine); curveRoad.rightLanes[i].splice(startIndex + 1, 0, rightJoin); curveRoad.rightLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.rightLanes[i] ); } } else { leftJoin = mathUtil.getIntersectionPoint(leftLine1, leftLine2); leftCurveEdge.points.splice(startIndex + 1, 0, leftJoin); curveEdgeService.setCurves(leftCurveEdge); for (let i = 0; i < leftCount - 1; ++i) { leftLine1 = mathUtil.createLine3( line1, curveRoad.leftLanes[i][startIndex] ); leftLine2 = mathUtil.createLine3( line2, curveRoad.leftLanes[i][startIndex + 1] ); leftJoin = mathUtil.getIntersectionPoint(leftLine1, leftLine2); curveRoad.leftLanes[i].splice(startIndex + 1, 0, leftJoin); curveRoad.leftLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.leftLanes[i] ); } rightJoin = mathUtil.getIntersectionPoint(rightLine1, rightLine2); rightCurveEdge.points.splice(startIndex + 1, 0, rightJoin); curveEdgeService.setCurves(rightCurveEdge); for (let i = 0; i < rightCount - 1; ++i) { rightLine1 = mathUtil.createLine3( line1, curveRoad.rightLanes[i][startIndex] ); rightLine2 = mathUtil.createLine3( line2, curveRoad.rightLanes[i][startIndex + 1] ); rightJoin = mathUtil.getIntersectionPoint(rightLine1, rightLine2); curveRoad.rightLanes[i].splice(startIndex + 1, 0, rightJoin); curveRoad.rightLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.rightLanes[i] ); } } this.setCurves(curveRoad); } subCPoint(curveRoad, index) { dataService.deleteCurvePoint(curveRoad.points[index].vectorId); curveRoad.points.splice(index, 1); for (let i = index; i < curveRoad.points.length; ++i) { curveRoad.points[i].setIndex(i); } this.setCurves(curveRoad); const leftCurveEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); leftCurveEdge.points.splice(index, 1); curveEdgeService.setCurves(leftCurveEdge); const rightCurveEdge = dataService.getCurveEdge(curveRoad.rightEdgeId); rightCurveEdge.points.splice(index, 1); curveEdgeService.setCurves(rightCurveEdge); this.removeCPointToLanes(curveRoad, index); } removeCPointToLanes(curveRoad, index) { for (let i = 0; i < curveRoad.leftLanes.length; ++i) { curveRoad.leftLanes[i].splice(index, 1); curveRoad.leftLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.leftLanes[i] ); } for (let i = 0; i < curveRoad.rightLanes.length; ++i) { curveRoad.rightLanes[i].splice(index, 1); curveRoad.rightLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.rightLanes[i] ); } } setLanesPoints(curveRoadId) { let curveRoad = dataService.getCurveRoad(curveRoadId); let startPoint = dataService.getPoint(curveRoad.startId); let endPoint = dataService.getPoint(curveRoad.endId); let midPoint = curvePointService.create({ x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, }); curveRoad.points = []; curveRoad.points.push(startPoint); curveRoad.points.push(midPoint); curveRoad.points.push(endPoint); } //车道 //points的第一个元素是start,最后一个是end setLanes(curveRoad, dir) { let points = curveRoad.points; const leftEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); const rightEdge = dataService.getCurveEdge(curveRoad.rightEdgeId); const leftEdgePoints = leftEdge.points; const rightEdgePoints = rightEdge.points; const leftCount = curveRoad.leftDrivewayCount; const rightCount = curveRoad.rightDrivewayCount; let leftLanes = []; let leftLanesCurves = []; let rightLanes = []; let rightLanesCurves = []; if (dir == "left" || !dir) { for (let i = 0; i < leftCount - 1; ++i) { for (let j = 0; j < points.length; ++j) { if (!leftLanes[i]) { leftLanes[i] = []; } const leftdx = (leftEdgePoints[j].x - points[j].x) / leftCount; const leftdy = (leftEdgePoints[j].y - points[j].y) / leftCount; leftLanes[i][j] = {}; leftLanes[i][j].x = points[j].x + leftdx * (i + 1); leftLanes[i][j].y = points[j].y + leftdy * (i + 1); } leftLanesCurves[i] = mathUtil.getCurvesByPoints(leftLanes[i]); } curveRoad.leftLanes = leftLanes; curveRoad.leftLanesCurves = leftLanesCurves; } if (dir == "right" || !dir) { for (let i = 0; i < rightCount - 1; ++i) { for (let j = 0; j < points.length; ++j) { if (!rightLanes[i]) { rightLanes[i] = []; } const rightdx = (rightEdgePoints[j].x - points[j].x) / rightCount; const rightdy = (rightEdgePoints[j].y - points[j].y) / rightCount; rightLanes[i][j] = {}; rightLanes[i][j].x = points[j].x + rightdx * (i + 1); rightLanes[i][j].y = points[j].y + rightdy * (i + 1); } rightLanesCurves[i] = mathUtil.getCurvesByPoints(rightLanes[i]); } curveRoad.rightLanes = rightLanes; curveRoad.rightLanesCurves = rightLanesCurves; } } //删除或者减少车道 updateForAddSubtractLanesCount(curveRoad, newCount, dir) { let curveEdge, oldCount, lanes; if (newCount < 0) { newCount = 0; } if (dir == "left") { curveEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); oldCount = curveRoad.leftDrivewayCount; curveRoad.leftDrivewayCount = newCount; lanes = curveRoad.leftLanes; } else if (dir == "right") { curveEdge = dataService.getCurveEdge(curveRoad.rightEdgeId); oldCount = curveRoad.rightDrivewayCount; curveRoad.rightDrivewayCount = newCount; lanes = curveRoad.rightLanes; } if (newCount == oldCount) { return; } else if (newCount == 0) { this.mulToSinglelane(curveRoad); return; } else if (oldCount == 0) { this.singleToMullane(curveRoad); return; } for (let i = 0; i < curveRoad.points.length; ++i) { const dx = (curveEdge.points[i].x - curveRoad.points[i].x) / oldCount; curveEdge.points[i].x = curveRoad.points[i].x + dx * newCount; const dy = (curveEdge.points[i].y - curveRoad.points[i].y) / oldCount; curveEdge.points[i].y = curveRoad.points[i].y + dy * newCount; } mathUtil.clonePoint(curveEdge.start, curveEdge.points[0]); mathUtil.clonePoint( curveEdge.end, curveEdge.points[curveEdge.points.length - 1] ); curveEdgeService.setCurves(curveEdge); this.setLanes(curveRoad, dir); const line = mathUtil.createLine1(curveRoad.points[0], curveRoad.points[1]); const leftCurveEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); const rightCurveEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); const leftWidth = mathUtil.getDisForPoinLine(leftCurveEdge.start, line); const rightWidth = mathUtil.getDisForPoinLine(rightCurveEdge.start, line); curveRoad.setWidth(leftWidth + rightWidth); } //单车道转多车道,默认是转换成左右两边各一个 //不改变路的宽度 singleToMullane(curveRoad) { curveRoad.leftDrivewayCount = 1; curveRoad.rightDrivewayCount = 1; this.setLanes(curveRoad); } //多车道转单车道 mulToSinglelane(curveRoad) { if (curveRoad.leftDrivewayCount > 0) { this.updateForAddSubtractLanesCount(curveRoad, 1, "left"); } if (curveRoad.rightDrivewayCount > 0) { this.updateForAddSubtractLanesCount(curveRoad, 1, "right"); } curveRoad.leftDrivewayCount = 0; curveRoad.rightDrivewayCount = 0; curveRoad.leftLanesCurves = []; curveRoad.rightLanesCurves = []; curveRoad.leftLanes = []; curveRoad.rightLanes = []; } updateForMovePoint(pointId, position) { let curvePoint = dataService.getCurvePoint(pointId); let curveRoadId = curvePoint.getParent(); let curveRoad = dataService.getCurveRoad(curveRoadId); let index = curvePoint.getIndex(); let dx = position.x - curvePoint.x; let dy = position.y - curvePoint.y; curvePoint.setPosition(position); let line = null; let join = null; let len = curveRoad.points.length; const leftCurveEdge = dataService.getCurveEdge(curveRoad.leftEdgeId); leftCurveEdge.points[index].x += dx; leftCurveEdge.points[index].y += dy; const rightCurveEdge = dataService.getCurveEdge(curveRoad.rightEdgeId); rightCurveEdge.points[index].x += dx; rightCurveEdge.points[index].y += dy; if (index == 0 || index == 1) { line = mathUtil.createLine1(curveRoad.points[0], curveRoad.points[1]); if ( line != null && !line.hasOwnProperty("x") && !line.hasOwnProperty("y") ) { let line1 = mathUtil.createLine3(line, leftCurveEdge.points[index]); join = mathUtil.getJoinLinePoint(curveRoad.points[0], line1); leftCurveEdge.points[0].x = join.x; leftCurveEdge.points[0].y = join.y; leftCurveEdge.start.x = join.x; leftCurveEdge.start.y = join.y; let line2 = mathUtil.createLine3(line, rightCurveEdge.points[index]); join = mathUtil.getJoinLinePoint(curveRoad.points[0], line2); rightCurveEdge.points[0].x = join.x; rightCurveEdge.points[0].y = join.y; rightCurveEdge.start.x = join.x; rightCurveEdge.start.y = join.y; } } if ( index == curveRoad.points.length - 1 || index == curveRoad.points.length - 2 ) { line = mathUtil.createLine1( curveRoad.points[len - 2], curveRoad.points[len - 1] ); if ( line != null && !line.hasOwnProperty("x") && !line.hasOwnProperty("y") ) { let line1 = mathUtil.createLine3(line, leftCurveEdge.points[index]); join = mathUtil.getJoinLinePoint(curveRoad.points[len - 1], line1); leftCurveEdge.points[len - 1].x = join.x; leftCurveEdge.points[len - 1].y = join.y; leftCurveEdge.end.x = join.x; leftCurveEdge.end.y = join.y; let line2 = mathUtil.createLine3(line, rightCurveEdge.points[index]); join = mathUtil.getJoinLinePoint(curveRoad.points[len - 1], line2); rightCurveEdge.points[len - 1].x = join.x; rightCurveEdge.points[len - 1].y = join.y; rightCurveEdge.end.x = join.x; rightCurveEdge.end.y = join.y; } } for (let i = 0; i < curveRoad.leftLanes.length; ++i) { curveRoad.leftLanes[i][index].x += dx; curveRoad.leftLanes[i][index].y += dy; //需要修改端点 if ( index == 0 || index == 1 || index == curveRoad.points.length - 1 || index == curveRoad.points.length - 2 ) { if (index == 0 || index == 1) { line = mathUtil.createLine1(curveRoad.points[0], curveRoad.points[1]); if ( line != null && !line.hasOwnProperty("x") && !line.hasOwnProperty("y") ) { line = mathUtil.createLine3(line, curveRoad.leftLanes[i][index]); join = mathUtil.getJoinLinePoint(curveRoad.points[0], line); curveRoad.leftLanes[i][0].x = join.x; curveRoad.leftLanes[i][0].y = join.y; } } if ( index == curveRoad.points.length - 1 || index == curveRoad.points.length - 2 ) { line = mathUtil.createLine1( curveRoad.points[len - 2], curveRoad.points[len - 1] ); if ( line != null && !line.hasOwnProperty("x") && !line.hasOwnProperty("y") ) { line = mathUtil.createLine3(line, curveRoad.leftLanes[i][index]); join = mathUtil.getJoinLinePoint(curveRoad.points[len - 1], line); curveRoad.leftLanes[i][len - 1].x = join.x; curveRoad.leftLanes[i][len - 1].y = join.y; } } } curveRoad.leftLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.leftLanes[i] ); } for (let i = 0; i < curveRoad.rightLanes.length; ++i) { curveRoad.rightLanes[i][index].x += dx; curveRoad.rightLanes[i][index].y += dy; //需要修改端点 if ( index == 0 || index == 1 || index == curveRoad.points.length - 1 || index == curveRoad.points.length - 2 ) { if (index == 0 || index == 1) { line = mathUtil.createLine1(curveRoad.points[0], curveRoad.points[1]); if ( line != null && !line.hasOwnProperty("x") && !line.hasOwnProperty("y") ) { line = mathUtil.createLine3(line, curveRoad.rightLanes[i][index]); join = mathUtil.getJoinLinePoint(curveRoad.points[0], line); curveRoad.rightLanes[i][0].x = join.x; curveRoad.rightLanes[i][0].y = join.y; } } if ( index == curveRoad.points.length - 1 || index == curveRoad.points.length - 2 ) { line = mathUtil.createLine1( curveRoad.points[len - 2], curveRoad.points[len - 1] ); if ( line != null && !line.hasOwnProperty("x") && !line.hasOwnProperty("y") ) { line = mathUtil.createLine3(line, curveRoad.rightLanes[i][index]); join = mathUtil.getJoinLinePoint(curveRoad.points[len - 1], line); curveRoad.rightLanes[i][len - 1].x = join.x; curveRoad.rightLanes[i][len - 1].y = join.y; } } } curveRoad.rightLanesCurves[i] = mathUtil.getCurvesByPoints( curveRoad.rightLanes[i] ); } this.setCurves(curveRoad); curveEdgeService.setCurves(leftCurveEdge); curveEdgeService.setCurves(rightCurveEdge); } setCurves(curveRoad) { curveRoad.curves = mathUtil.getCurvesByPoints(curveRoad.points); } } const curveRoadService = new CurveRoadService(); export { curveRoadService };