import * as Utils from '../utils'; import { Cartesian3 } from 'cesium'; import Basic from '../bastic'; import { Category, CesiumTypes, LngLat, PolygonStyle, Viewer } from '../type'; export class ThreeArrow extends Basic { points: Cartesian3[] = []; headHeightFactor: number; headWidthFactor: number; neckHeightFactor: number; neckWidthFactor: number; headTailFactor: number; minPointsForShape: number; constructor(cesium: CesiumTypes, viewer: Viewer, style?: PolygonStyle) { super(cesium, viewer, style); this.cesium = cesium; this.headHeightFactor = 0.18; this.headWidthFactor = 0.3; this.neckHeightFactor = 0.85; this.neckWidthFactor = 0.15; this.headTailFactor = 0.8; this.minPointsForShape = 3; this.setState('drawing'); this.onDoubleClick(); } getCategory(): Category { return 'polygon' } addPoint(cartesian: Cartesian3) { this.points.push(cartesian); if (this.points.length < 2) { this.onMouseMove(); } else if (this.points.length === 2) { this.setGeometryPoints(this.points); this.drawPolygon(); } else if (this.points.length == 10) { const geometryPoints = this.createGraphic(this.points); this.setGeometryPoints(geometryPoints); this.drawPolygon() this.finishDrawing() } } updateMovingPoint(cartesian: Cartesian3) { const tempPoints = [...this.points, cartesian]; this.setGeometryPoints(tempPoints); if (tempPoints.length === 2) { this.addTempLine(); } else { this.removeTempLine(); const geometryPoints = this.createGraphic(tempPoints); this.setGeometryPoints(geometryPoints); this.drawPolygon(); } } createGraphic(positions: Cartesian3[]): Cartesian3[] { const lnglatPoints = positions.map((pnt) => { return this.cartesianToLnglat(pnt); }); let points: any[] = [] const total = lnglatPoints.length; let [curvePnts1, curvePnts2]: any = [null, null, null] if (total < 5) { const { leftPnts, rightPnts, headPnts } = this.getPnts(lnglatPoints); points = points.concat(leftPnts, headPnts, rightPnts.reverse()) } else if (total === 5) { const { rightPnts, headPnts, neckLeft } = this.getPnts(lnglatPoints.slice(0, 4)); const leftPnts = Utils.getBezierPoints([lnglatPoints[4], lnglatPoints[2], neckLeft]) points = points.concat(leftPnts, headPnts, rightPnts.reverse()) } else if (total > 5 && total < 8) { const { rightPnts, headPnts: head1, neckLeft } = this.getPnts(lnglatPoints.slice(0, 4)); const { leftPnts, headPnts: head2, neckRight } = this.getPnts([lnglatPoints[1], ...lnglatPoints.slice(4)]); curvePnts1 = Utils.getBezierPoints([neckRight, lnglatPoints[4], neckLeft]) points = points.concat(leftPnts, head2, curvePnts1, head1, rightPnts.reverse()) } else if (total == 8) { const { rightPnts, headPnts: head1, neckLeft: neckLeft1 } = this.getPnts(lnglatPoints.slice(0, 4)); const { headPnts: head2, neckRight: neckRight1, neckLeft: neckLeft2 } = this.getPnts([lnglatPoints[1], ...lnglatPoints.slice(4, 7)]); const Mid1 = Utils.Mid(lnglatPoints[1], lnglatPoints[4]); curvePnts1 = Utils.getBezierPoints([neckRight1, Mid1, neckLeft1]) const leftPnts = Utils.getBezierPoints([lnglatPoints[7], lnglatPoints[5], neckLeft2]) points = points.concat(leftPnts, head2, curvePnts1, head1, rightPnts.reverse()) } else { const { rightPnts, headPnts: head1, neckLeft: neckLeft1 } = this.getPnts(lnglatPoints.slice(0, 4)); const { headPnts: head2, neckLeft: neckLeft2, neckRight: neckRight1 } = this.getPnts([lnglatPoints[1], ...lnglatPoints.slice(4, 7)]); const Mid1 = Utils.Mid(lnglatPoints[1], lnglatPoints[4]); curvePnts1 = Utils.getBezierPoints([neckRight1, Mid1, neckLeft1]) const { headPnts: head3, leftPnts, neckRight: neckRight2 } = this.getPnts([lnglatPoints[4], ...lnglatPoints.slice(7)]) const Mid2 = Utils.Mid(lnglatPoints[4], lnglatPoints[7]); curvePnts2 = Utils.getBezierPoints([neckRight2, Mid2, neckLeft2]) points = points.concat(leftPnts, head3, curvePnts2, head2, curvePnts1, head1, rightPnts.reverse()) } const temp = [].concat(...points); const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(temp); return cartesianPoints; } getPnts(lnglatPoints: LngLat[]): { leftPnts: LngLat[], headPnts: LngLat[], rightPnts: LngLat[], neckLeft: LngLat, neckRight: LngLat } { let [tailLeft, tailRight] = [lnglatPoints[0], lnglatPoints[1]]; if (Utils.isClockWise(lnglatPoints[0], lnglatPoints[1], lnglatPoints[2])) { tailLeft = lnglatPoints[1]; tailRight = lnglatPoints[0]; } const midTail = Utils.Mid(tailLeft, tailRight); const bonePnts = [midTail].concat(lnglatPoints.slice(2)); const headPnts: LngLat[] = this.getArrowHeadPoints(bonePnts, tailLeft, tailRight) as LngLat[]; const [neckLeft, neckRight] = [headPnts![0], headPnts![4]]; const tailWidthFactor = Utils.MathDistance(tailLeft, tailRight) / Utils.getBaseLength(bonePnts); const bodyPnts = this.getArrowBodyPoints(bonePnts, neckLeft, neckRight, tailWidthFactor); const count = bodyPnts.length; let leftPnts = [tailLeft].concat(bodyPnts.slice(0, count / 2)); leftPnts.push(neckLeft); let rightPnts = [tailRight].concat(bodyPnts.slice(count / 2, count)); rightPnts.push(neckRight); leftPnts = Utils.getQBSplinePoints(leftPnts); rightPnts = Utils.getQBSplinePoints(rightPnts); return { leftPnts, headPnts, rightPnts, neckLeft, neckRight } } getPoints() { return this.points; } getArrowHeadPoints(points: LngLat[], tailLeft: LngLat, tailRight: LngLat) { try { let len = Utils.getBaseLength(points); let headHeight = len * this.headHeightFactor; const headPnt = points[points.length - 1]; len = Utils.MathDistance(headPnt, points[points.length - 2]); const tailWidth = Utils.MathDistance(tailLeft, tailRight); if (headHeight > tailWidth * this.headTailFactor) { headHeight = tailWidth * this.headTailFactor; } const headWidth = headHeight * this.headWidthFactor; const neckWidth = headHeight * this.neckWidthFactor; headHeight = headHeight > len ? len : headHeight; const neckHeight = headHeight * this.neckHeightFactor; const headEndPnt = Utils.getThirdPoint(points[points.length - 2], headPnt, 0, headHeight, true); const neckEndPnt = Utils.getThirdPoint(points[points.length - 2], headPnt, 0, neckHeight, true); const headLeft = Utils.getThirdPoint(headPnt, headEndPnt, Math.PI / 2, headWidth, false); const headRight = Utils.getThirdPoint(headPnt, headEndPnt, Math.PI / 2, headWidth, true); const neckLeft = Utils.getThirdPoint(headPnt, neckEndPnt, Math.PI / 2, neckWidth, false); const neckRight = Utils.getThirdPoint(headPnt, neckEndPnt, Math.PI / 2, neckWidth, true); return [neckLeft, headLeft, headPnt, headRight, neckRight]; } catch (e) { console.log(e); } } getArrowBodyPoints(points: LngLat[], neckLeft: LngLat, neckRight: LngLat, tailWidthFactor: number) { const allLen = Utils.wholeDistance(points); const len = Utils.getBaseLength(points); const tailWidth = len * tailWidthFactor; const neckWidth = Utils.MathDistance(neckLeft, neckRight); const widthDif = (tailWidth - neckWidth) / 2; let [tempLen, leftBodyPnts, rightBodyPnts]: any = [0, [], []]; for (let i = 1; i < points.length - 1; i++) { const angle = Utils.getAngleOfThreePoints(points[i - 1], points[i], points[i + 1]) / 2; tempLen += Utils.MathDistance(points[i - 1], points[i]); const w = (tailWidth / 2 - (tempLen / allLen) * widthDif) / Math.sin(angle); const left = Utils.getThirdPoint(points[i - 1], points[i], Math.PI - angle, w, true); const right = Utils.getThirdPoint(points[i - 1], points[i], angle, w, false); leftBodyPnts.push(left); rightBodyPnts.push(right); } return leftBodyPnts.concat(rightBodyPnts); } updateDraggingPoint(cartesian: Cartesian3, index: number) { this.points[index] = cartesian; const geometryPoints = this.createGraphic(this.points); this.setGeometryPoints(geometryPoints); this.drawPolygon(); } }