// @ts-nocheck import { Enviroment } from '../../Enviroment' import { Axis, type Disposable, type Activeble } from '../../Common' import Utils from '../../Utils' import { PointEle } from '../../map_element/element/Point' import { ArcEle } from '../../map_element/element/Arc' import { ActionManager } from '../../action/ActionManager' import type { Positeble } from '../../map_element/Common' type AxisHolder = { arc: ArcEle, point: PointEle } export type IndicatorHolder = { xAxis: AxisHolder, yAxis: AxisHolder, zAxis: AxisHolder } type RotateCallback = (hpr: Cesium.HeadingPitchRoll) => void; export class RotateIndicator implements Positeble, Activeble, Disposable { readonly id: string; readonly pointSize: number; readonly radius: number; private position: Cesium.Cartesian3; private axis: Axis | undefined = undefined; private angleX: number = 0; private angleY: number = 0; private angleZ: number = 0; readonly holder: IndicatorHolder; private callback: RotateCallback | undefined = undefined; constructor(id: string, position: Cesium.Cartesian3, hpr?: Cesium.HeadingPitchRoll, pointSize: number = 12, radius: number = 10) { this.id = id; this.pointSize = pointSize; this.radius = radius; this.position = position; this.angleX = 360 - (hpr == undefined ? 0 : hpr.pitch * Utils.DegreePerRadian); this.angleY = (hpr == undefined ? 0 : hpr.roll * Utils.DegreePerRadian); this.angleZ = (hpr == undefined ? 0 : hpr.heading * Utils.DegreePerRadian); this.holder = { xAxis: this.createAxisHolder('x', Cesium.Color.GREEN, this.angleX, new Cesium.HeadingPitchRoll(0, 0, Utils.Radian_90)), yAxis: this.createAxisHolder('y', Cesium.Color.BLUE, this.angleY, new Cesium.HeadingPitchRoll(0, Utils.Radian_90, 0)), zAxis: this.createAxisHolder('z', Cesium.Color.RED, this.angleZ, new Cesium.HeadingPitchRoll(Utils.Radian_90, 0, 0)) }; } private createAxisHolder(name: string, ringColor: Cesium.Color, angle: number, hpr: Cesium.HeadingPitchRoll): AxisHolder { const arc = ArcEle.create(this.id + '_arc_' + name, this.position, this.radius, angle).init() .stopEdit() .custom((des) => { des.color = Cesium.Color.YELLOW; des.ringColor = ringColor; des.closeCenter = false; des.showRing = true; }) .resetRadius(this.radius) .rotate(hpr); const point = PointEle.create(this.id + '_point_' + name, arc.findReachPosition()).init() .stopEdit() .custom(des => { des.pixelSize = this.pointSize; des.color = ringColor; }); return { arc: arc.inflate(), point: point.inflate() } } dispose(): void { this.disposeAxisHoder(this.holder.xAxis); this.disposeAxisHoder(this.holder.yAxis); this.disposeAxisHoder(this.holder.zAxis); } private disposeAxisHoder(holder: AxisHolder) { ActionManager.removeHelper(holder.point.id); holder.point.dispose(); holder.arc.dispose(); } onRotate(callback: RotateCallback): RotateIndicator { this.callback = callback; return this; } active(): RotateIndicator { this.activeX(); this.activeY(); this.activeZ(); return this; } activeX(): RotateIndicator { // 更新全局坐标系 if (this.axis == undefined) { this.axis = Axis.create(this.position); } // 相机坐标系 let cameraAxis: Axis | undefined = undefined; // 中心点 - 相机坐标系 let center_CA: Cesium.Cartesian3 | undefined = undefined; const axisHolder = this.holder.xAxis; const sv = this.axis.toLocal(axisHolder.arc.info.reachList[0]); ActionManager.newHelper(axisHolder.point.id) .drag.active() .drag.onMove((helper, info) => { // 鼠标点 - 相机坐标系 const p_CA = cameraAxis!.toLocal(info.position); // 鼠标在z轴环平面的点 - 相机坐标系 const ovalPercent = center_CA.y / p_CA.y; const ovalP_CA = Utils.Vector.percent(p_CA, ovalPercent); // 对应的z轴环上的点 - 相机坐标系 const wp = this.calculateRingPosition(cameraAxis!, center_CA, ovalP_CA); // 计算角度 const lv = this.axis!.toLocal(wp); let angle = Utils.Vector.angle_3D(sv, lv); if (lv.z > 0) { angle = 360 - angle; } axisHolder.arc.resetAngle(angle); axisHolder.point.reposite(wp); this.callback?.(this.getHPR()); }) .drag.onStart((helper, info) => { cameraAxis = Axis.create(Enviroment.viewer.camera.position) center_CA = cameraAxis.toLocal(this.position); }) .drag.onEnd((helper, info) => { cameraAxis = undefined; center_CA = undefined; }); return this; } activeY(): RotateIndicator { // 更新全局坐标系 if (this.axis == undefined) { this.axis = Axis.create(this.position); } // 相机坐标系 let cameraAxis: Axis | undefined = undefined; // 中心点 - 相机坐标系 let center_CA: Cesium.Cartesian3 | undefined = undefined; const axisHolder = this.holder.yAxis; const sv = this.axis.toLocal(axisHolder.arc.info.reachList[0]); ActionManager.newHelper(axisHolder.point.id) .drag.active() .drag.onMove((helper, info) => { // 鼠标点 - 相机坐标系 const p_CA = cameraAxis!.toLocal(info.position); // 鼠标在z轴环平面的点 - 相机坐标系 const ovalPercent = center_CA.x / p_CA.x; const ovalP_CA = Utils.Vector.percent(p_CA, ovalPercent); // 对应的z轴环上的点 - 相机坐标系 const wp = this.calculateRingPosition(cameraAxis!, center_CA, ovalP_CA); // 计算角度 const lv = this.axis!.toLocal(wp); let angle = Utils.Vector.angle_3D(sv, lv); if (lv.y > 0) { angle = 360 - angle; } axisHolder.arc.resetAngle(angle); axisHolder.point.reposite(wp); this.callback?.(this.getHPR()); }) .drag.onStart((helper, info) => { cameraAxis = Axis.create(Enviroment.viewer.camera.position) center_CA = cameraAxis.toLocal(this.position); }) .drag.onEnd((helper, info) => { cameraAxis = undefined; center_CA = undefined; }); return this; } activeZ(): RotateIndicator { // 更新全局坐标系 if (this.axis == undefined) { this.axis = Axis.create(this.position); } // 相机坐标系 let cameraAxis: Axis | undefined = undefined; // 中心点 - 相机坐标系 let center_CA: Cesium.Cartesian3 | undefined = undefined; const axisHolder = this.holder.zAxis; const sv = this.axis.toLocal(axisHolder.arc.info.reachList[0]); ActionManager.newHelper(axisHolder.point.id) .drag.active() .drag.onMove((helper, info) => { // 鼠标点 - 相机坐标系 const p_CA = cameraAxis!.toLocal(info.position); // 鼠标在z轴环平面的点 - 相机坐标系 const ovalPercent = center_CA.z / p_CA.z; const ovalP_CA = Utils.Vector.percent(p_CA, ovalPercent); // 对应的z轴环上的点 - 相机坐标系 const wp = this.calculateRingPosition(cameraAxis!, center_CA, ovalP_CA); // 计算角度 const lv = this.axis!.toLocal(wp); let angle = Utils.Vector.angle_3D(sv, lv); if (lv.x > 0) { angle = 360 - angle; } axisHolder.arc.resetAngle(angle); axisHolder.point.reposite(wp); this.callback?.(this.getHPR()); }) .drag.onStart((helper, info) => { cameraAxis = Axis.create(Enviroment.viewer.camera.position) center_CA = cameraAxis.toLocal(this.position); }) .drag.onEnd((helper, info) => { cameraAxis = undefined; center_CA = undefined; }); return this; } private calculateRingPosition(axis: Axis, center: Cesium.Cartesian3, position: Cesium.Cartesian3): Cesium.Cartesian3 { const distance = Cesium.Cartesian3.distance(center, position); const ringPercent = this.radius / distance; const vector_CA = Utils.Vector.create(center, position); const arc_Vector_CA = Utils.Vector.percent(vector_CA, ringPercent); const arcP_CA = Utils.Vector.move(center, arc_Vector_CA); return axis!.toWorld(arcP_CA); } deactive(): RotateIndicator { ActionManager.removeHelper(this.holder.xAxis.point.id); ActionManager.removeHelper(this.holder.yAxis.point.id); ActionManager.removeHelper(this.holder.zAxis.point.id); return this; } move(vector: Cesium.Cartesian3): RotateIndicator { this.position = Utils.Vector.move(this.position, vector); this.axis = undefined; this.moveAxisHolder(this.holder.xAxis, vector); this.moveAxisHolder(this.holder.yAxis, vector); this.moveAxisHolder(this.holder.zAxis, vector); return this; } private moveAxisHolder(holder: AxisHolder, vector: Cesium.Cartesian3) { holder.arc.move(vector); holder.point.move(vector); } reposite(position: Cesium.Cartesian3): RotateIndicator { const vector = Utils.Vector.create(this.position, position); return this.move(vector); } getHPR(): Cesium.HeadingPitchRoll { const heading = this.getZAngle() * Utils.RadianPerDegree; const pitch = (360 - this.getXAngle()) * Utils.RadianPerDegree; const roll = this.getYAngle() * Utils.RadianPerDegree; return new Cesium.HeadingPitchRoll(heading, pitch, roll); } getXAngle(): number { return this.holder.xAxis.arc.info.angle!; } getYAngle(): number { return this.holder.yAxis.arc.info.angle!; } getZAngle(): number { return this.holder.zAxis.arc.info.angle!; } rotete(x: number, y: number, z: number): RotateIndicator { this.rotateAxis(this.holder.xAxis, x); this.rotateAxis(this.holder.yAxis, y); this.rotateAxis(this.holder.zAxis, z); this.callback?.(this.getHPR()); return this; } private rotateAxis(axisHolder: AxisHolder, angle: number): RotateIndicator { axisHolder.arc.resetAngle(angle); axisHolder.point.reposite(axisHolder.arc.findReachPosition()); return this; } }