import Point from "../Geometry/Point.js"; import Line from "../Geometry/Line.js"; import CurvePoint from "../Geometry/CurvePoint.js"; import CurveLine from "../Geometry/CurveLine.js"; import { dataService } from "./DataService.js"; import { curvePointService } from "./CurvePointService.js"; import VectorCategory from "../enum/VectorCategory.js"; import { mathUtil } from "../Util/MathUtil.js"; import { uiService } from "./UIService.js"; import { addLine } from "../Controls/AddLine.js"; import Constant from "../Constant.js"; export default class LineService { constructor() {} create(startPoint, endPoint, category, vectorId) { if (!startPoint || !endPoint || mathUtil.equalPoint(startPoint, endPoint)) { return null; } let start = new Point(startPoint); let end = new Point(endPoint); let line = new Line(start.vectorId, end.vectorId, vectorId); if (category) { line.setCategory(category); } start.setPointParent(line.vectorId, "start"); end.setPointParent(line.vectorId, "end"); dataService.addPoint(start); dataService.addPoint(end); dataService.addLine(line); return line; } createByPointId(startId, endId, category, vectorId) { let line = new Line(startId, endId, vectorId); if (category) { line.setCategory(category); } let start = dataService.getPoint(startId); let end = dataService.getPoint(endId); start.setPointParent(line.vectorId, "start"); end.setPointParent(line.vectorId, "end"); dataService.addLine(line); return line; } deleteLine(lineId) { let line = dataService.getLine(lineId); //如果是基准线 if (line.getCategory() == VectorCategory.Line.BaseLine) { let points = dataService.getPoints(); for (let key in points) { let point = dataService.getPoint(key); let category = point.getCategory(); if ( category == VectorCategory.Point.BasePoint || category == VectorCategory.Point.TestBasePoint ) { dataService.deletePoint(key); } } uiService.setBaseLineId(null); } dataService.deleteLine(); } copy(vectorId) { let line = dataService.getLine(vectorId); let startPoint = dataService.getPoint(line.startId); let endPoint = dataService.getPoint(line.endId); startPoint = uiService.getNewPositionForPop(startPoint); endPoint = uiService.getNewPositionForPop(endPoint); let newLine = this.create(startPoint, endPoint, line.category); newLine.setColor(line.color); if (line.weight) { newLine.setWeight(line.weight); } if (line.style) { newLine.setStyle(line.style); } return newLine.vectorId; } getLine(pointId1, pointId2) { let lines = dataService.getLines(); for (let key in lines) { const line = dataService.getLine(key); if ( (line.startId == pointId1 && line.endId == pointId2) || (line.endId == pointId1 && line.startId == pointId2) ) { return key; } } return null; } //是否已经设置了直角测量法 hasLocationModeByAngle() { let lines = dataService.getLines(); for (let key in lines) { let line = dataService.getLine(key); if (line.getLocationMode() == Constant.angleLocationMode) { return true; } } return false; } /******************************************************************************曲线**************************************************************************************/ createCurveLine(startPosition, endPosition, vectorId) { if ( !startPosition || !endPosition || mathUtil.equalPoint(startPosition, endPosition) ) { return null; } let startPoint = curvePointService.create(startPosition); let endPoint = curvePointService.create(endPosition); let curveLine = new CurveLine( startPoint.vectorId, endPoint.vectorId, vectorId ); startPoint.setPointParent(curveLine.vectorId); startPoint.setIndex(0); endPoint.setPointParent(curveLine.vectorId); endPoint.setIndex(2); let midPoint = curvePointService.create({ x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, }); midPoint.setPointParent(curveLine.vectorId); midPoint.setIndex(1); curveLine.points = []; curveLine.points.push(startPoint); curveLine.points.push(midPoint); curveLine.points.push(endPoint); curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points); dataService.addCurveLine(curveLine); return curveLine; } createCurveLineByPointIds(curvePoints, vectorId) { let curveLine = new CurveLine( curvePoints[0].vectorId, curvePoints[curvePoints.length - 1].vectorId, vectorId ); curveLine.points = []; for (let i = 0; i < curvePoints.length; ++i) { curveLine.points[i] = dataService.getCurvePoint(curvePoints[i].vectorId); curveLine.points[i].setIndex(i); curveLine.points[i].setPointParent(curveLine.vectorId); } curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points); dataService.addCurveLine(curveLine); return curveLine; } createCurveLineByPoints(points, vectorId) { let curvePoints = []; for (let i = 0; i < points.length; ++i) { let curvePoint = curvePointService.create(points[i]); curvePoint.setIndex(i); curvePoints.push(curvePoint); } let curveLine = new CurveLine( curvePoints[0].vectorId, curvePoints[curvePoints.length - 1].vectorId, vectorId ); curveLine.points = curvePoints; for (let i = 0; i < curvePoints.length; ++i) { curvePoints[i].setIndex(i); curvePoints[i].setPointParent(curveLine.vectorId); } curveLine.curves = mathUtil.getCurvesByPoints(curvePoints); dataService.addCurveLine(curveLine); return curveLine; } addCPoint(position, index, vectorId) { let curveLine = dataService.getCurveLine(vectorId); let newPoint = curvePointService.create(position); newPoint.setIndex(index); newPoint.setPointParent(vectorId); curveLine.points.splice(index, 0, newPoint); for (let i = index + 1; i < curveLine.points.length; ++i) { curveLine.points[i].setIndex(i); } curveLine.startId = curveLine.points[0].vectorId; curveLine.endId = curveLine.points[curveLine.points.length - 1].vectorId; curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points); } copyCurveLine(vectorId) { let curveLine = dataService.getCurveLine(vectorId); let startPoint = dataService.getCurvePoint(curveLine.startId); let endPoint = dataService.getCurvePoint(curveLine.endId); startPoint = uiService.getNewPositionForPop(startPoint); endPoint = uiService.getNewPositionForPop(endPoint); let newCurveLine = this.createCurveLine(startPoint, endPoint); newCurveLine.setColor(curveLine.color); if (curveLine.weight) { newCurveLine.setWeight(curveLine.weight); } if (curveLine.style) { newCurveLine.setStyle(curveLine.style); } mathUtil.clonePoint( newCurveLine.points[1], uiService.getNewPositionForPop(curveLine.points[1]) ); newCurveLine.points[1].setIndex(curveLine.points[1].getIndex()); for (let i = 2; i < curveLine.points.length - 1; ++i) { let newPoint = uiService.getNewPositionForPop(curveLine.points[i]); newPoint = curvePointService.create(newPoint); newPoint.setPointParent(vectorId); newPoint.setIndex(curveLine.points[i].getIndex()); newCurveLine.points.splice(i, 0, newPoint); } newCurveLine.points[newCurveLine.points.length - 1].setIndex( curveLine.points[curveLine.points.length - 1].getIndex() ); newCurveLine.curves = mathUtil.getCurvesByPoints(newCurveLine.points); return newCurveLine.vectorId; } deleteCrossPointForCurveLine(curvePointId, curveLineId) { let curveLine = dataService.getCurveLine(curveLineId); for (let i = 0; i < curveLine.points.length; ++i) { if (curveLine.points[i].vectorId == curvePointId) { if (i == 0) { curveLine.points.shift(); curveLine.startId = curveLine.points[0].vectorId; for (let j = 0; j < curveLine.points.length; ++j) { let _curvePoint = dataService.getCurvePoint( curveLine.points[j].vectorId ); const index = _curvePoint.getIndex(); _curvePoint.setIndex(index - 1); } } else if (i == curveLine.points.length - 1) { curveLine.points.pop(); curveLine.endId = curveLine.points[curveLine.points.length - 1].vectorId; } else { curveLine.points.splice(i, 1); for (let j = i; j < curveLine.points.length; ++j) { let _curvePoint = dataService.getCurvePoint( curveLine.points[j].vectorId ); const index = _curvePoint.getIndex(); _curvePoint.setIndex(index - 1); } } break; } } dataService.deleteCurvePoint(curvePointId); if (curveLine.points.length == 2) { let startPoint = dataService.getCurvePoint(curveLine.startId); let endPoint = dataService.getCurvePoint(curveLine.endId); let line = this.create(startPoint, endPoint); const weight = curveLine.getWeight(); line.setWeight(weight); const style = curveLine.getStyle(); line.setStyle(style); this.deleteCurveLine(curveLineId); } else { curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points); } } deleteCurveLine(curveLineId) { let curveLine = dataService.getCurveLine(curveLineId); for (let i = 0; i < curveLine.points.length; ++i) { dataService.deleteCurvePoint(curveLine.points[i].vectorId); } dataService.deleteCurveLine(curveLineId); } } const lineService = new LineService(); export { lineService };