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

101 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-08-21 10:02:00 +00:00
import * as Utils from '../utils';
import Base from '../base';
2023-08-22 12:07:59 +00:00
// @ts-ignore
2023-08-21 10:02:00 +00:00
import { Cartesian3 } from '@examples/cesium';
2023-08-23 06:51:36 +00:00
import { LineStyle } from '../interface';
2023-08-21 10:02:00 +00:00
export default class CurvedArrow extends Base {
2023-08-21 10:02:00 +00:00
points: Cartesian3[] = [];
arrowLengthScale: number = 5;
maxArrowLength: number = 3000000;
t: number;
2023-08-23 06:51:36 +00:00
constructor(cesium: any, viewer: any, style?: LineStyle) {
super(cesium, viewer, style);
2023-08-21 10:02:00 +00:00
this.cesium = cesium;
this.t = 0.3;
this.setState('drawing');
this.onDoubleClick();
}
2023-08-23 06:51:36 +00:00
getType(): 'polygon' | 'line' {
return 'line';
}
2023-08-21 10:02:00 +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);
this.drawLine();
}
}
/**
* Draw a shape based on mouse movement points during the initial drawing.
*/
updateMovingPoint(cartesian: Cartesian3) {
const tempPoints = [...this.points, cartesian];
2023-08-23 03:49:11 +00:00
let geometryPoints = [];
2023-08-21 10:02:00 +00:00
if (tempPoints.length === 2) {
2023-08-23 03:49:11 +00:00
geometryPoints = this.createStraightArrow(tempPoints);
2023-08-21 10:02:00 +00:00
} else {
2023-08-23 03:49:11 +00:00
geometryPoints = this.createLine(tempPoints);
2023-08-21 10:02:00 +00:00
}
2023-08-23 03:49:11 +00:00
this.setGeometryPoints(geometryPoints);
2023-08-23 06:51:36 +00:00
this.drawLine();
2023-08-23 03:49:11 +00:00
}
createStraightArrow(positions: Cartesian3[]) {
const [pnt1, pnt2] = positions.map(this.cartesianToLnglat);
2023-08-23 09:53:39 +00:00
const distance = Utils.MathDistance(pnt1, pnt2);
let len = distance / this.arrowLengthScale;
2023-08-23 03:49:11 +00:00
len = len > this.maxArrowLength ? this.maxArrowLength : len;
const leftPnt = Utils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len, false);
const rightPnt = Utils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len, true);
const points = [...pnt1, ...pnt2, ...leftPnt, ...pnt2, ...rightPnt];
const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(points);
return cartesianPoints;
2023-08-21 10:02:00 +00:00
}
/**
* In edit mode, drag key points to update corresponding key point data.
*/
updateDraggingPoint(cartesian: Cartesian3, index: number) {
this.points[index] = cartesian;
const geometryPoints = this.createLine(this.points);
this.setGeometryPoints(geometryPoints);
this.drawLine();
}
/**
* Generate geometric shapes based on key points.
*/
createLine(positions: Cartesian3[]) {
const lnglatPoints = positions.map((pnt) => {
return this.cartesianToLnglat(pnt);
});
const curvePoints = Utils.getCurvePoints(this.t, lnglatPoints);
const pnt1 = lnglatPoints[lnglatPoints.length - 2];
const pnt2 = lnglatPoints[lnglatPoints.length - 1];
2023-08-23 09:53:39 +00:00
const distance = Utils.wholeDistance(lnglatPoints);
let len = distance / this.arrowLengthScale;
2023-08-21 10:02:00 +00:00
len = len > this.maxArrowLength ? this.maxArrowLength : len;
const leftPnt = Utils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len, false);
const rightPnt = Utils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len, true);
const temp = [].concat(...curvePoints);
const points = [...temp, ...leftPnt, ...pnt2, ...rightPnt];
const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(points);
return cartesianPoints;
}
getPoints() {
return this.points;
}
}