123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import ControlPoint from "../Geometry/ControlPoint.js";
- import { dataService } from "./DataService.js";
- import { mathUtil } from "../Util/MathUtil";
- import { edgeService } from "./EdgeService.js";
- import { roadService } from "./RoadService.js";
- export default class ControlPointService {
- constructor() {}
- //暂时没有添加到vectorData里
- create(position, vectorId) {
- let controlPoint = new ControlPoint(position, vectorId);
- return controlPoint;
- }
- update(realRosition, point1, point2, edge1, edge2, dir1, dir2) {
- let controlPoint = dataService.getControlPoint(
- edge1.vectorId,
- edge2.vectorId
- );
- const line1 = edgeService.getLine(edge1);
- const line2 = edgeService.getLine(edge2);
- let position = mathUtil.getPositionForExtendedLine(point1, point2);
- //更新edge的坐标
- let position1 = mathUtil.getJoinLinePoint(position, line1);
- let position2 = mathUtil.getJoinLinePoint(position, line2);
- //可能position1或者position2不在对应的edge上,这时候需要调换顺序
- if (!this.isPointOnEdgeRay(edge1, dir1, position)) {
- let _position = mathUtil.getPositionForExtendedLine(point2, point1);
- position1 = mathUtil.getJoinLinePoint(_position, line1);
- }
- if (!this.isPointOnEdgeRay(edge2, dir2, position)) {
- let _position = mathUtil.getPositionForExtendedLine(point2, point1);
- position2 = mathUtil.getJoinLinePoint(_position, line2);
- }
- if (controlPoint == null) {
- //新建控制点
- controlPoint = this.create(realRosition);
- //需要删除之前的控制点
- dataService.deleteControlPointForEdge(edge1.vectorId, dir1);
- dataService.deleteControlPointForEdge(edge2.vectorId, dir2);
- dataService.deleteControlPoint(edge1.vectorId, edge2.vectorId);
- //设置控制点的信息
- controlPoint.setEdgeInfo(edge1.vectorId, dir1, edge2.vectorId, dir2);
- //添加到数据集里
- dataService.addControlPoint(controlPoint);
- }
- //更新控制点坐标
- else {
- controlPoint.setPosition(realRosition);
- }
- edge1.setPosition(position1, dir1);
- edge2.setPosition(position2, dir2);
- controlPoint.setExtremePoint();
- }
- //newleft,end------------road.leftEdgeId,end
- replaceEdgeId(controlPoint, oldEdgeId, newEdgeId) {
- if (controlPoint == null) {
- return;
- }
- dataService.deleteControlPoint(
- controlPoint.edgeInfo1.id,
- controlPoint.edgeInfo2.id
- );
- if (controlPoint.edgeInfo1.id == oldEdgeId) {
- controlPoint.edgeInfo1.id = newEdgeId;
- } else if (controlPoint.edgeInfo2.id == oldEdgeId) {
- controlPoint.edgeInfo2.id = newEdgeId;
- }
- dataService.addControlPoint(controlPoint);
- }
- //角度小,road宽的时候,可能edge顺序会倒,但是road的顺序不会
- isPointOnEdgeRay(edge, dir, position) {
- let road = dataService.getRoad(edge.parent);
- let point1, point2;
- if (dir == "start") {
- point1 = dataService.getRoadPoint(road.startId);
- point2 = dataService.getRoadPoint(road.endId);
- } else if (dir == "end") {
- point1 = dataService.getRoadPoint(road.endId);
- point2 = dataService.getRoadPoint(road.startId);
- }
- position = mathUtil.getJoinLinePoint(
- position,
- roadService.getMidLine(road)
- );
- let v1 = {
- x: position.x - point1.x,
- y: position.y - point1.y,
- };
- let v2 = {
- x: point2.x - point1.x,
- y: point2.y - point1.y,
- };
- let v = {
- x: v1.x + v2.x,
- y: v1.y + v2.y,
- };
- let d = mathUtil.getDistance(v, { x: 0, y: 0 });
- let d1 = mathUtil.getDistance(position, point1);
- let d2 = mathUtil.getDistance(point2, point1);
- if (Math.abs(d - d1 - d2) < 0.2) {
- return true;
- } else {
- return false;
- }
- }
- updateForMovePoint(controlPointId, newPosition) {
- let controlPoint = dataService.getControlPoint2(controlPointId);
- mathUtil.clonePoint(controlPoint.extremePoint, newPosition);
- controlPoint.setCurves();
- }
- }
- const controlPointService = new ControlPointService();
- export { controlPointService };
|