cesium-plot-js/src/arrow/attack-arrow.ts

153 lines
5.6 KiB
TypeScript
Raw Normal View History

import Base from '../base';
2023-08-16 08:17:33 +00:00
import * as Utils from '../utils';
2023-08-22 12:07:59 +00:00
// @ts-ignore
2024-02-22 13:54:59 +00:00
import { Cartesian3 } from 'cesium';
import { PolygonStyle } from '../interface';
2023-08-16 08:17:33 +00:00
export default class AttackArrow extends Base {
2023-08-16 08:17:33 +00:00
points: Cartesian3[] = [];
headHeightFactor: number;
headWidthFactor: number;
neckHeightFactor: number;
neckWidthFactor: number;
headTailFactor: number;
2024-03-23 11:08:47 +00:00
minPointsForShape: number;
2023-08-16 08:17:33 +00:00
2023-08-23 06:51:36 +00:00
constructor(cesium: any, viewer: any, style?: PolygonStyle) {
super(cesium, viewer, style);
2023-08-16 08:17:33 +00:00
this.cesium = cesium;
this.headHeightFactor = 0.18;
this.headWidthFactor = 0.3;
this.neckHeightFactor = 0.85;
this.neckWidthFactor = 0.15;
this.headTailFactor = 0.8;
2024-03-23 11:08:47 +00:00
this.minPointsForShape = 3;
2023-08-16 08:17:33 +00:00
this.setState('drawing');
this.onDoubleClick();
}
2023-08-23 06:51:36 +00:00
getType(): 'polygon' | 'line' {
return 'polygon';
}
2023-08-16 08:17:33 +00:00
/**
* Add points only on click events
*/
addPoint(cartesian: Cartesian3) {
this.points.push(cartesian);
if (this.points.length < 2) {
this.onMouseMove();
} else if (this.points.length === 2) {
this.setGeometryPoints(this.points);
2023-08-21 05:47:28 +00:00
this.drawPolygon();
2023-08-16 08:17:33 +00:00
}
}
/**
* Draw a shape based on mouse movement points during the initial drawing.
*/
updateMovingPoint(cartesian: Cartesian3) {
const tempPoints = [...this.points, cartesian];
this.setGeometryPoints(tempPoints);
if (tempPoints.length === 2) {
2023-08-23 03:49:11 +00:00
this.addFirstLineOfTheArrow();
2023-08-16 08:17:33 +00:00
} else {
2024-03-23 11:08:47 +00:00
const geometryPoints = this.createGraphic(tempPoints);
2023-08-16 08:17:33 +00:00
this.setGeometryPoints(geometryPoints);
2023-08-21 05:47:28 +00:00
this.drawPolygon();
2023-08-16 08:17:33 +00:00
}
}
/**
* Generate geometric shapes based on key points.
*/
2024-03-23 11:08:47 +00:00
createGraphic(positions: Cartesian3[]): Cartesian3[] {
2023-08-16 08:17:33 +00:00
const lnglatPoints = positions.map((pnt) => {
return this.cartesianToLnglat(pnt);
});
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 = this.getArrowHeadPoints(bonePnts, tailLeft, tailRight);
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);
const points = leftPnts.concat(headPnts, rightPnts.reverse());
const temp = [].concat(...points);
const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(temp);
return cartesianPoints;
}
getPoints() {
return this.points;
}
getArrowHeadPoints(points, tailLeft, tailRight) {
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, neckLeft, neckRight, tailWidthFactor) {
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] = [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);
}
2023-08-17 11:35:20 +00:00
/**
* In edit mode, drag key points to update corresponding key point data.
*/
updateDraggingPoint(cartesian: Cartesian3, index: number) {
this.points[index] = cartesian;
2024-03-23 11:08:47 +00:00
const geometryPoints = this.createGraphic(this.points);
2023-08-17 11:35:20 +00:00
this.setGeometryPoints(geometryPoints);
2023-08-21 05:47:28 +00:00
this.drawPolygon();
2023-08-17 11:35:20 +00:00
}
2023-08-16 08:17:33 +00:00
}