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"; 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 || category == VectorCategory.Point.TestPoint ) { dataService.deletePoint(key); } } Settings.baseLineId = 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); return newLine; } 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; } /******************************************************************************曲线**************************************************************************************/ 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, "start"); endPoint.setPointParent(curveLine.vectorId, "end"); let midPoint = curvePointService.create({ x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2, }); 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; } createCurveLineForPoints(points, vectorId) { let curvePoints = []; for (let i = 0; i < points.length; ++i) { let curvePoint = curvePointService.create(points[i]); curvePoints.push(curvePoint); } let curveLine = new CurveLine( curvePoints[0].vectorId, curvePoints[curvePoints.length - 1].vectorId, vectorId ); curvePoints[0].setPointParent(curveLine.vectorId, "start"); curvePoints[curvePoints.length - 1].setPointParent( curveLine.vectorId, "end" ); curveLine.points = JSON.parse(JSON.stringify(curvePoints)); curveLine.curves = mathUtil.getCurvesByPoints(curveLine.points); dataService.addCurveLine(curveLine); return curveLine; } } const lineService = new LineService(); export { lineService };