// @ts-nocheck import { Axis } from "../../Common" import { Enviroment } from "../../Enviroment" import Utils from "../../Utils" import { MapElement } from "../BaseElement" import { ElementOperator, type PositeInfo, type Positeble, type Rotateble } from '../Common' import { SdkError } from "../../Error" export type ArcInfo = PositeInfo & { radius: number, angle: number, matrix?: Cesium.Matrix3, readonly reachList: Array, readonly unreachList: Array, } export type ArcDes = { width: number, color: Cesium.Color, editColor: Cesium.Color, // 编辑时颜色 closeCenter: boolean, // 连接中心点 showRing: boolean, // 显示外层圆环 ringColor: Cesium.Color, // 圆环颜色 ringEditColor: Cesium.Color, // 编辑时圆环颜色 distanceDisplayCondition?: Cesium.DistanceDisplayCondition;// 距离 - 显隐 } export class ArcEle extends MapElement implements Rotateble, Positeble { static create(id: string, position: Cesium.Cartesian3, radius: number, angle: number, des?: ArcDes): ArcEle { return new ArcEle(id, { position: position, radius: radius, angle: ArcEle.formatAngle(angle), reachList: [], unreachList: [] }, des); } protected createDes(): ArcDes { return ArcEle.defaultDes(); } static defaultDes(): ArcDes { return { width: 2, color: Cesium.Color.RED, editColor: Cesium.Color.YELLOW, closeCenter: true, showRing: true, ringColor: Cesium.Color.BLUE, ringEditColor: Cesium.Color.GREEN }; } init(): ArcEle { this.operator = new ArcOperator(this.id, this, this.info, this.des); this.calculatePositionList(); return this; } /** * 重置数据 * @param radius 半径 * */ resetRadius(radius: number): ArcEle { this.info.radius = radius; this.repositeGeo(); return this; } /** * 重置数据 * @param angle 角度 * @param hpr 旋转 * */ resetAngle(angle: number, hpr?: Cesium.HeadingPitchRoll): ArcEle { this.info.angle = ArcEle.formatAngle(angle); return this.rotate(hpr); } private static formatAngle(angle: number): number { if (angle > 360 || angle < 0) { throw SdkError.createDefault('DataError', '角度需在0-360范围内'); } if (angle == 360) { angle = 0; } else { angle = Math.round(angle); } return angle; } /** * 旋转 * @param angle 角度 * @param hpr 旋转 * */ rotate(hpr: Cesium.HeadingPitchRoll): ArcEle { if (hpr != undefined) { this.info.matrix = Cesium.Matrix3.fromHeadingPitchRoll(hpr, new Cesium.Matrix3()); } if (this.info.angle != undefined) { this.calculatePositionList(); } this.refreshGeo(); return this; } /** * 移动位置 * */ move(vector: Cesium.Cartesian3): ArcEle { this.info.position = Utils.Vector.move(this.info.position, vector); for (let i = 0, s = this.info.unreachList?.length; i < s; i++) { const position: Cesium.Cartesian3 = this.info.unreachList[i]; this.info.unreachList[i] = Utils.Vector.move(position, vector); } for (let i = 0, s = this.info.reachList?.length; i < s; i++) { const position: Cesium.Cartesian3 = this.info.reachList[i]; this.info.reachList[i] = Utils.Vector.move(position, vector); } this.repositeGeo(); return this; } reposite(position: Cesium.Cartesian3): ArcEle { const vector = Utils.Vector.create(this.info.position, position); return this.move(vector); } private calculatePositionList() { const axis = Axis.create(this.info.position); this.calculateArcPosition(axis); this.calculateRingPosition(axis); } private calculateArcPosition(axis: Axis) { this.info.reachList.splice(0); const num = (this.info.angle == 360 ? this.info.angle : this.info.angle! + 1); for (let i = 0; i < num; i++) { const position = this.calculatePosition(Utils.Oval.positions()[i]); this.info.reachList.push(axis.toWorld(position)); } if (this.info.angle == 360) { this.info.reachList.push(this.info.reachList[0]); } if (this.des.closeCenter) { this.info.reachList.push(this.info.position); } } private calculateRingPosition(axis: Axis) { this.info.unreachList.splice(0); for (let i = this.info.angle!; i < Utils.Oval.size; i++) { const position = this.calculatePosition(Utils.Oval.positions()[i]); this.info.unreachList.push(axis.toWorld(position)); } if (this.info.angle != 360) { this.info.unreachList.push(this.info.reachList[0]); } } private calculatePosition(position: Cesium.Cartesian3): Cesium.Cartesian3 { if (this.info.matrix != undefined) { position = Cesium.Matrix3.multiplyByVector(this.info.matrix, position, new Cesium.Cartesian3()); } return Utils.Vector.percent(position, this.info.radius!); } findReachPosition(): Cesium.Cartesian3 { let index = this.info.reachList.length - 1; if (this.des.closeCenter) { index -= 1; } return this.info.reachList[index]; } } class ArcOperator extends ElementOperator{ private arc?: Cesium.Polyline = undefined; private ring?: Cesium.Polyline = undefined; inflate(): void { if (this.ring == undefined) { this.ring = Enviroment.lines.add({ id: Utils.IdGroup.serialize(this.id, '_ring'), positions: this.info.unreachList, width: this.des.width, loop: false, material: this.getRingMaterial(), disableDepthTestDistance: Number.POSITIVE_INFINITY, // 禁用深度测试 distanceDisplayCondition: this.des.distanceDisplayCondition, show: this.stateble.isVisible() && this.des.showRing }); } if (this.arc == undefined) { this.arc = Enviroment.lines.add({ id: Utils.IdGroup.serialize(this.id, '_arc'), positions: this.info.reachList, width: this.des.width, loop: this.des.closeCenter, material: this.getArcMaterial(), disableDepthTestDistance: Number.POSITIVE_INFINITY, // 禁用深度测试 distanceDisplayCondition: this.des.distanceDisplayCondition, show: this.stateble.isVisible() }); } } remove(): void { if (this.ring != undefined) { Enviroment.lines.remove(this.ring); this.ring = undefined; } if (this.arc != undefined) { Enviroment.lines.remove(this.arc); this.arc = undefined; } } refresh(): void { if (this.ring != undefined) { this.ring.width = this.des.width; this.ring.material = this.getRingMaterial(); this.ring.distanceDisplayCondition = this.des.distanceDisplayCondition; this.ring.show = this.stateble.isVisible() && this.des.showRing; } if (this.arc != undefined) { this.arc.width = this.des.width; this.arc.loop = this.des.closeCenter; this.arc.material = this.getArcMaterial(); this.arc.distanceDisplayCondition = this.des.distanceDisplayCondition; this.arc.show = this.stateble.isVisible(); } } reposite(): void { if (this.ring != undefined) { this.ring.positions = this.info.unreachList; } if (this.arc != undefined) { this.arc.positions = this.info.reachList; } } private getArcMaterial(): Cesium.Material { return new Cesium.Material.fromType( Cesium.Material.ColorType, { color: (this.stateble.isEditing() ? this.des.editColor : this.des.color) } ); } private getRingMaterial(): Cesium.Material { return new Cesium.Material.fromType( Cesium.Material.ColorType, { color: (this.stateble.isEditing() ? this.des.ringEditColor : this.des.ringColor) } ); } }