123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- 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 };
|