import { listenLayer } from "../ListenLayer"; import Constant from "../Constant"; // import { mathUtil } from "../MathUtil"; import { dataService } from "../Service/DataService"; import { pointService } from "../Service/PointService"; import { elementService } from "../Service/ElementService"; // import { wallService } from "../Service/WallService"; import { stateService } from "../Service/StateService"; import { symbolService } from "../Service/SymbolService"; import VectorType from "../enum/VectorType"; import { roadService } from "../Service/RoadService"; export default class AddRoad { constructor() { this.startInfo = {}; this.endInfo = {}; this.canAdd = false; } setPointInfo(dir, pointInfo) { if (dir == "start") { this.startInfo = { position: { x: pointInfo.x, y: pointInfo.y, }, linkedPointId: pointInfo.linkedPointId, linkedRoadId: pointInfo.linkedRoadId, }; } else if (dir == "end") { this.endInfo = { position: { x: pointInfo.x, y: pointInfo.y, }, linkedPointId: pointInfo.linkedPointId, linkedRoadId: pointInfo.linkedRoadId, }; } } buildRoad() { console.log("添加路段!"); let joinInfos = this.getJoinsForRoads(); let splitPointIds = this.splitAllJoins(joinInfos); this.creatNewRoads(splitPointIds); this.canAdd = false; this.updateStart( this.endInfo.position, splitPointIds[splitPointIds.length - 1] ); listenLayer.clear(); } //开始新建公路,可能是多个 creatNewRoads(splitPointIds) { for (let i = 0; i < splitPointIds.length - 1; ++i) { let pointId1 = splitPointIds[i]; let pointId2 = splitPointIds[i + 1]; let roadId = roadService.getRoadId(pointId1, pointId2); if (!roadId) { let road = roadService.create(pointId1, pointId2); } } } canAddRoadForEnd(endPt) { if (listenLayer.modifyPoint) { if ( mathUtil.getDistance(this.startInfo.position, listenLayer.modifyPoint) < Constant.minAdsorb ) { return false; } } else { if ( mathUtil.getDistance(this.startInfo.position, endPt) < Constant.minAdsorb ) { return false; } } return true; } getJoinsForRoads() { let result = []; const roads = dataService.getRoads(); let hasComputerPointIds = []; let hasExitPointIds = []; if (this.startInfo.linkedPointId) { hasComputerPointIds.push(this.startInfo.linkedPointId); hasExitPointIds.push(this.startInfo.linkedPointId); result.push({ join: { x: this.startInfo.position.x, y: this.startInfo.position.y }, pointId: this.startInfo.linkedPointId, }); } else if (this.startInfo.linkedRoadId) { result.push({ join: { x: this.startInfo.position.x, y: this.startInfo.position.y }, roadId: this.startInfo.linkedRoadId, }); } else { result.push({ join: { x: this.startInfo.position.x, y: this.startInfo.position.y }, }); } if (this.endInfo.linkedPointId) { hasComputerPointIds.push(this.endInfo.linkedPointId); hasExitPointIds.push(this.endInfo.linkedPointId); result.push({ join: { x: this.endInfo.position.x, y: this.endInfo.position.y }, pointId: this.endInfo.linkedPointId, }); } else if (this.endInfo.linkedRoadId) { result.push({ join: { x: this.endInfo.position.x, y: this.endInfo.position.y }, roadId: this.endInfo.linkedRoadId, }); } else { result.push({ join: { x: this.endInfo.position.x, y: this.endInfo.position.y }, }); } const line = mathUtil.createLine1( this.startInfo.position, this.endInfo.position ); for (let key in roads) { if (this.startInfo.linkedRoadId && this.startInfo.linkedRoadId == key) { continue; } else if ( this.endInfo.linkedRoadId && this.endInfo.linkedRoadId == key ) { continue; } let road = roads[key]; if ( hasExitPointIds.indexOf(road.startId) > -1 || hasExitPointIds.indexOf(road.endId) > -1 ) { continue; } let startPoint = dataService.getPoint(road.startId); let endPoint = dataService.getPoint(road.endId); let flag = false; if (hasComputerPointIds.indexOf(road.startId) < 0) { hasComputerPointIds.push(road.startId); const startJoin = mathUtil.getJoinLinePoint(startPoint, line); if ( mathUtil.PointInSegment( startJoin, this.startInfo.position, this.endInfo.position, Constant.minAdsorb ) && mathUtil.getDistance(startPoint, startJoin) < Constant.minAdsorb ) { result.push({ join: { x: startJoin.x, y: startJoin.y }, pointId: road.startId, }); flag = true; hasExitPointIds.push(road.startId); } } if (hasComputerPointIds.indexOf(road.endId) < 0) { hasComputerPointIds.push(road.endId); const endJoin = mathUtil.getJoinLinePoint(endPoint, line); if ( mathUtil.PointInSegment( endJoin, this.startInfo.position, this.endInfo.position, Constant.minAdsorb ) && mathUtil.getDistance(endPoint, endJoin) < Constant.minAdsorb ) { result.push({ join: { x: endJoin.x, y: endJoin.y }, pointId: road.endId, }); flag = true; hasExitPointIds.push(road.endId); } } if (!flag) { //不与墙key的端点相交 if ( hasExitPointIds.indexOf(road.startId) < 0 && hasExitPointIds.indexOf(road.endId) < 0 ) { let join = mathUtil.getIntersectionPoint3( startPoint, endPoint, this.startInfo.position, this.endInfo.position ); if (join) { result.push({ join: { x: join.x, y: join.y }, roadId: key }); } } } } return result; } splitAllJoins(joinInfos) { // 对joinInfos要进行排序 joinInfos = joinInfos.sort(sortNumber.bind(this)); function sortNumber(a, b) { return ( mathUtil.getDistance(this.startInfo.position, a.join) - mathUtil.getDistance(this.startInfo.position, b.join) ); } const splitPointIds = []; for (let i = 0; i < joinInfos.length; ++i) { const info = joinInfos[i]; const join = info.join; const roadId = info.roadId; const pointId = info.pointId; if (pointId) { splitPointIds.push(pointId); } else if (roadId) { // 拆分roadId const splitPoint = pointService.create(join); const splitPointId = splitPoint.vectorId; //可能joinInfos的两个点都在roadId上 let newRoadId = null; if ( joinInfos[i + 1] && joinInfos[i].roadId == joinInfos[i + 1].roadId ) { let road = dataService.getRoad(roadId); let startPoint = dataService.getPoint(road.startId); if ( mathUtil.getDistance(startPoint, joinInfos[i].join) < mathUtil.getDistance(startPoint, joinInfos[i + 1].join) ) { newRoadId = roadService.splitRoad(roadId, splitPointId, "end"); } else { newRoadId = roadService.splitRoad(roadId, splitPointId, "start"); } } else { newRoadId = roadService.splitRoad(roadId, splitPointId, "start"); } if (newRoadId == null) { dataService.deletePoint(splitPointId); continue; } splitPointIds.push(splitPointId); } //首尾没有吸附的情况 else { const splitPoint = pointService.create(join); splitPointIds.push(splitPoint.vectorId); } } return splitPointIds; } updateStart(position, linkedPointId) { mathUtil.clonePoint(this.startInfo.position, position); this.startInfo.linkedPointId = linkedPointId; //elementService.setNewWallStartPosition(this.startInfo.position); } clear() { this.startInfo = {}; this.endInfo = {}; this.canAdd = false; //elementService.hideNewWall(); //elementService.hideStartAddWall(); } } const addRoad = new AddRoad(); export { addRoad };