123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- import { listenLayer } from "../ListenLayer";
- import Constant from "../Constant";
- import { mathUtil } from "../Util/MathUtil";
- import { dataService } from "../Service/DataService";
- import { pointService } from "../Service/PointService";
- import { elementService } from "../Service/ElementService";
- import { stateService } from "../Service/StateService";
- 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.minAdsorbPix
- ) {
- return false;
- }
- } else {
- if (
- mathUtil.getDistance(this.startInfo.position, endPt) <
- Constant.minAdsorbPix
- ) {
- 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.minAdsorbPix
- ) &&
- mathUtil.getDistance(startPoint, startJoin) < Constant.minAdsorbPix
- ) {
- 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.minAdsorbPix
- ) &&
- mathUtil.getDistance(endPoint, endJoin) < Constant.minAdsorbPix
- ) {
- 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.setNewRoadStartPosition(this.startInfo.position);
- }
- setNewRoadPoint(dir, position) {
- if (dir == "start") {
- if (listenLayer.modifyPoint) {
- this.setPointInfo(dir, listenLayer.modifyPoint);
- } else {
- this.setPointInfo(dir, position);
- }
- return true;
- } else if (dir == "end") {
- if (listenLayer.modifyPoint) {
- this.setPointInfo(dir, listenLayer.modifyPoint);
- } else {
- this.setPointInfo(dir, position);
- }
- return true;
- }
- return false;
- }
- clear() {
- this.startInfo = {};
- this.endInfo = {};
- this.canAdd = false;
- elementService.hideNewRoad();
- elementService.hideStartAddRoad();
- }
- }
- const addRoad = new AddRoad();
- export { addRoad };
|