import { dataService } from '../Service/DataService'; import { lineService } from '../Service/LineService'; import { pointService } from '../Service/PointService'; import VectorCategory from '../enum/VectorCategory'; import Point from '../Geometry/Point.js'; import { mathUtil } from '../Util/MathUtil'; import addLine from './AddLine'; import Settings from '../Settings'; import { stateService } from '../Service/StateService'; import LayerEvents from '../enum/LayerEvents'; import VectorType from '../enum/VectorType'; import Constant from '../Constant'; import { listenLayer } from '../ListenLayer'; export default class AddPoint { constructor() { this.testPointIds = []; //所有待测点 } buildPoint(position) { //只有一个基准点的时候,测量的时候自动选择基准点 if ((Settings.selectLocationMode == Constant.angleLocationMode || Settings.selectLocationMode == Constant.allLocationMode) && Settings.basePointIds.length == 1) { Settings.selectBasePointId = Settings.basePointIds[0]; } let newPoint; if (Settings.selectPointCategory == VectorCategory.Point.BasePoint) { newPoint = pointService.create(position); Settings.selectBasePointId = newPoint.vectorId; } else { if (Settings.selectBasePointId != null && Settings.selectLocationMode == Constant.angleLocationMode) { newPoint = pointService.create(position); this.setLocationByAngle(newPoint.vectorId); newPoint.setLocationMode(Constant.angleLocationMode); stateService.setEventName(LayerEvents.AddPoint); } else if (Settings.selectBasePointId != null && Settings.selectLocationMode == Constant.allLocationMode) { newPoint = pointService.create(position); this.setLocationByAll(newPoint.vectorId); newPoint.setLocationMode(Constant.allLocationMode); stateService.setEventName(LayerEvents.AddPoint); } else if (Settings.baseLineId != null && Settings.selectLocationMode == Constant.normalLocationMode) { newPoint = pointService.create(position); this.setLocationByNormal(newPoint.vectorId); newPoint.setLocationMode(Constant.normalLocationMode); stateService.setEventName(LayerEvents.AddPoint); } else if (Settings.selectBasePointId == null && (Settings.selectLocationMode == Constant.angleLocationMode || Settings.selectLocationMode == Constant.allLocationMode)) { return null; } if ( newPoint && (newPoint.getLocationMode() == Constant.allLocationMode || newPoint.getLocationMode() == Constant.angleLocationMode || newPoint.getLocationMode() == Constant.normalLocationMode) && newPoint.getCategory() == VectorCategory.Point.TestPoint ) { this.testPointIds.push(newPoint.vectorId); } } listenLayer.clear(); return newPoint; } isFocusBasePoint() { if (Settings.selectBasePointId) { let point = dataService.getPoint(Settings.selectBasePointId); if (point.getCategory() == VectorCategory.Point.BasePoint) { return point; } } return null; } //直角定位法 setLocationByAngle(testPointId) { let basePoint = this.isFocusBasePoint(); if (!basePoint) { return; } let testPoint = dataService.getPoint(testPointId); if (testPoint.getCategory() != VectorCategory.Point.TestPoint) { return; } testPoint.setLinkedBasePointId(basePoint.vectorId); let lineGeometry = dataService.getLine(Settings.baseLineId); let startPoint = dataService.getPoint(lineGeometry.startId); let endPoint = dataService.getPoint(lineGeometry.endId); let line = mathUtil.createLine1(startPoint, endPoint); let vLine = mathUtil.getVerticalLine(line, testPoint); let join = mathUtil.getJoinLinePoint(basePoint, vLine); join = pointService.create(join); join.setCategory(VectorCategory.Point.TestBasePoint); join.setLocationMode(Constant.angleLocationMode); join.setLinkedBasePointId(basePoint.vectorId); join.setLinkedTestPointId(testPointId); let guidePositionLine = lineService.createByPointId(testPointId, join.vectorId, VectorCategory.Line.GuidePositionLine); let positionLine = lineService.createByPointId(basePoint.vectorId, join.vectorId, VectorCategory.Line.PositionLine); guidePositionLine.setLocationMode(Constant.angleLocationMode); positionLine.setLocationMode(Constant.angleLocationMode); join.setCategory(VectorCategory.Point.TestBasePoint); } //综合定位法 setLocationByAll(testPointId) { let basePoint = this.isFocusBasePoint(); if (!basePoint) { return; } let testPoint = dataService.getPoint(testPointId); testPoint.setLinkedBasePointId(basePoint.vectorId); let lineGeometry = dataService.getLine(Settings.baseLineId); let startPoint = dataService.getPoint(lineGeometry.startId); let endPoint = dataService.getPoint(lineGeometry.endId); let line = mathUtil.createLine1(startPoint, endPoint); let join = mathUtil.getJoinLinePoint(testPoint, line); join = pointService.create(join); //经过待测点且与基准线垂直的线段,与基准线的交点 join.setCategory(VectorCategory.Point.TestBasePoint); join.setLocationMode(Constant.allLocationMode); join.setLinkedBasePointId(basePoint.vectorId); join.setLinkedTestPointId(testPointId); //待测点与基准线的垂直线 lineService.createByPointId(testPointId, join.vectorId, VectorCategory.Line.PositionLine); //暂时没有其他待测点 if (this.testPointIds.length == 0) { //待测点与基准线点的连线 lineService.createByPointId(basePoint.vectorId, testPointId, VectorCategory.Line.PositionLine); } else { //取上一个待测点 lineService.createByPointId(this.testPointIds[this.testPointIds.length - 1], testPointId, VectorCategory.Line.PositionLine); testPoint.setLinkedTestPointId(this.testPointIds[this.testPointIds.length - 1]); } } setLocationByNormal(testPointId) { let testPoint = dataService.getPoint(testPointId); if (testPoint.getCategory() != VectorCategory.Point.TestPoint) { return; } if (!Settings.baseLineId) { return; } let lineGeometry = dataService.getLine(Settings.baseLineId); let startPoint = dataService.getPoint(lineGeometry.startId); let endPoint = dataService.getPoint(lineGeometry.endId); let line = mathUtil.createLine1(startPoint, endPoint); let vLine = mathUtil.getVerticalLine(line, testPoint); let join = mathUtil.getIntersectionPoint(vLine, line); join = pointService.create(join); join.setCategory(VectorCategory.Point.TestBasePoint); join.setLocationMode(Constant.normalLocationMode); join.setLinkedTestPointId(testPointId); lineService.createByPointId(testPointId, join.vectorId, VectorCategory.Line.PositionLine); } deleteTestPoints() { for (let i = 0; i < this.testPointIds.length; ++i) { pointService.deletePoint(this.testPointIds[i]); } this.testPointIds = []; } resetTestPoints(){ this.testPointIds = []; } } const addPoint = new AddPoint(); export { addPoint };