- A+
所属分类:Web前端
需求
设置飞机的一些坐标位置(经纬度高度),插值得到更多的坐标位置,然后飞机按照这些坐标集合形成的航线飞行,飞机的朝向、俯仰角以及飞机转弯时的翻转角根据坐标集合计算得出,而不需要手动设置heading、pitch、roll。
坐标插值
不知道为什么,可能是飞行速度变化太大,我用Cesium自带的插值,计算出的航线很奇怪
// 如下代码插值计算出的航线有问题 property.setInterpolationOptions({ interpolationDegree : 5, interpolationAlgorithm : Cesium.LagrangePolynomialApproximation });
自己写的插值计算,效果等同于Cesium自带的线性插值。
思路很简单,每次插值,就是取时间的中点,两个坐标的中点。
代码如下:
/** * 重新采样this.DronePositions */ DetectsDrones.prototype.sameple = function () { for (let i = 0; i < 3; i++) { this.samepleOnce(); } } /** * 重新采样this.DronePositions */ DetectsDrones.prototype.samepleOnce = function () { for (let i = 0; i < this.DronePositions.length - 1; i += 2) { let pos1 = this.DronePositions[i]; let pos2 = this.DronePositions[i + 1]; let time1 = dayjs(pos1.time, 'YYYY-MM-DD HH:mm:ss'); let time2 = dayjs(pos2.time, 'YYYY-MM-DD HH:mm:ss'); let time = time1.add(time2.diff(time1) / 2.0, 'millisecond'); let lng = (pos1.targetPosition.lng + pos2.targetPosition.lng) / 2.0; let lat = (pos1.targetPosition.lat + pos2.targetPosition.lat) / 2.0; let height = (pos1.targetPosition.height + pos2.targetPosition.height) / 2.0; let heading = (pos1.targetPosition.heading + pos2.targetPosition.heading) / 2.0; let pitch = (pos1.targetPosition.pitch + pos2.targetPosition.pitch) / 2.0; let roll = (pos1.targetPosition.roll + pos2.targetPosition.roll) / 2.0; let pos = { time: time.format('YYYY-MM-DD HH:mm:ss.SSS'), targetPosition: { lng: lng, lat: lat, height: height, heading: heading, pitch: pitch, roll: roll, } } this.DronePositions.splice(i + 1, 0, pos); } }
根据航线坐标集合计算heading、pitch、roll
从网上抄的计算heading和pitch的方法(参考博客:https://blog.csdn.net/u010447508/article/details/105562542?_refluxos=a10):
/** * 根据两个坐标点,获取Heading(朝向) * @param { Cesium.Cartesian3 } pointA * @param { Cesium.Cartesian3 } pointB * @returns */ function getHeading(pointA, pointB) { //建立以点A为原点,X轴为east,Y轴为north,Z轴朝上的坐标系 const transform = Cesium.Transforms.eastNorthUpToFixedFrame(pointA); //向量AB const positionvector = Cesium.Cartesian3.subtract(pointB, pointA, new Cesium.Cartesian3()); //因transform是将A为原点的eastNorthUp坐标系中的点转换到世界坐标系的矩阵 //AB为世界坐标中的向量 //因此将AB向量转换为A原点坐标系中的向量,需乘以transform的逆矩阵。 const vector = Cesium.Matrix4.multiplyByPointAsVector( Cesium.Matrix4.inverse(transform, new Cesium.Matrix4()), positionvector, new Cesium.Cartesian3() ); //归一化 const direction = Cesium.Cartesian3.normalize(vector, new Cesium.Cartesian3()); //heading let heading = Math.atan2(direction.y, direction.x) - Cesium.Math.PI_OVER_TWO; heading = Cesium.Math.TWO_PI - Cesium.Math.zeroToTwoPi(heading); return Cesium.Math.toDegrees(heading); } /** * 根据两个坐标点,获取Pitch(仰角) * @param { Cesium.Cartesian3 } pointA * @param { Cesium.Cartesian3 } pointB * @returns */ function getPitch(pointA, pointB) { let transfrom = Cesium.Transforms.eastNorthUpToFixedFrame(pointA); const vector = Cesium.Cartesian3.subtract(pointB, pointA, new Cesium.Cartesian3()); let direction = Cesium.Matrix4.multiplyByPointAsVector(Cesium.Matrix4.inverse(transfrom, transfrom), vector, vector); Cesium.Cartesian3.normalize(direction, direction); //因为direction已归一化,斜边长度等于1,所以余弦函数等于direction.z let pitch = Cesium.Math.PI_OVER_TWO - Cesium.Math.acosClamped(direction.z); return Cesium.Math.toDegrees(pitch); }
根据航线坐标集合计算heading、pitch、roll:
代码中this.DronePositions是无人机群的坐标集合,坐标放在targetPosition属性中
/** * 计算无人机群的heading */ DetectsDrones.prototype.calcHeading = function () { // 清空原有heading this.DronePositions.map(pos => { pos.targetPosition.heading = undefined; }); for (let i = 0; i < this.DronePositions.length - 1; i++) { let pos1 = this.DronePositions[i]; let pos2 = this.DronePositions[i + 1]; let heading = -90 + getHeading(Cesium.Cartesian3.fromDegrees(pos1.targetPosition.lng, pos1.targetPosition.lat), Cesium.Cartesian3.fromDegrees(pos2.targetPosition.lng, pos2.targetPosition.lat)); if (!pos1.targetPosition.heading) { pos1.targetPosition.heading = heading; } pos2.targetPosition.heading = heading; } } /** * 计算无人机群的pitch */ DetectsDrones.prototype.calcPitch = function () { // 清空原有pitch this.DronePositions.map(pos => { pos.targetPosition.pitch = undefined; }); for (let i = 0; i < this.DronePositions.length - 1; i++) { let pos1 = this.DronePositions[i]; let pos2 = this.DronePositions[i + 1]; let pitch = getPitch(Cesium.Cartesian3.fromDegrees(pos1.targetPosition.lng, pos1.targetPosition.lat, pos1.targetPosition.height), Cesium.Cartesian3.fromDegrees(pos2.targetPosition.lng, pos2.targetPosition.lat, pos2.targetPosition.height)); if (!pos1.targetPosition.pitch) { pos1.targetPosition.pitch = pitch; } pos2.targetPosition.pitch = pitch; } } /** * 计算无人机群的roll(不支持转弯大于90度) */ DetectsDrones.prototype.calcRoll = function () { // 清空原有roll this.DronePositions.map(pos => { pos.targetPosition.roll = undefined; }); for (let i = 1; i < this.DronePositions.length - 1; i++) { let pos1 = this.DronePositions[i]; let pos2 = this.DronePositions[i + 1]; let deltaHeading = pos2.targetPosition.heading - pos1.targetPosition.heading; pos2.targetPosition.roll = deltaHeading / 1.5; } }
效果
主要是飞机的朝向和转弯时的翻滚,俯仰角这里没体现。
遇到的问题
- 插值计算的问题,就是设置的坐标集合,是拆线,最好把它插值成平滑曲线,但是Cesium自带的插值,有时间参数,而我想仅仅通过经纬度集合来插值。
- 我写的计算roll的方法有问题,不支持转弯大于90度的情况,花了一些时间,没搞定。转弯小于90度,凑合用,测试了几组数据没问题,但仍不确定有没有BUG。严格来讲,根据这些参数,这个roll是算不出来的,但是,该算法要求根据飞机的转弯半径及方向,给出一个相对合理的roll值。
抛砖引玉,有没有高手给个提示,插值问题怎么解决?roll的正确的通用的计算方法?