From 91abff669483fe3523b4b789a6516e36bbd5b46c Mon Sep 17 00:00:00 2001 From: zouyaoji <370681295@qq.com> Date: Fri, 11 Mar 2022 12:58:58 +0800 Subject: [PATCH] feat(utils): updated utils --- packages/utils/cesium-helpers.ts | 35 +++++++++++++++++++++++++++++--- packages/utils/util.ts | 11 ++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/packages/utils/cesium-helpers.ts b/packages/utils/cesium-helpers.ts index 8d909a403..fe55d4123 100644 --- a/packages/utils/cesium-helpers.ts +++ b/packages/utils/cesium-helpers.ts @@ -41,7 +41,9 @@ import type { CesiumMaterialProperty, CesiumMaterial, VcBoundingRectangle, - CesiumBoundingRectangle + CesiumBoundingRectangle, + VcHeadingPitchRoll, + VcHeadingPitchRange } from './types' import { hasOwn, isFunction, isArray, isString, isPlainObject, isEmptyObj, getObjClassName, isUndefined } from './util' @@ -949,7 +951,7 @@ export function makeJulianDate(val: string | Date | Cesium.JulianDate): Cesium.J if (val instanceof JulianDate) { return val } else if (isString(val)) { - return Cesium.JulianDate.fromIso8601(val) + return Cesium.JulianDate.fromDate(new Date(val)) } else if (val instanceof Date) { return Cesium.JulianDate.fromDate(val) } @@ -957,6 +959,33 @@ export function makeJulianDate(val: string | Date | Cesium.JulianDate): Cesium.J return Cesium.JulianDate.now() } +export function makeHeadingPitchRoll(val: VcHeadingPitchRoll): Cesium.HeadingPitchRoll { + const { HeadingPitchRoll, Math: CesiumMath } = Cesium + if (val instanceof Cesium.HeadingPitchRoll) { + return val + } else if (Array.isArray(val)) { + // 认为是经纬度数组 + return new HeadingPitchRoll(CesiumMath.toRadians(val[0]) || 0, CesiumMath.toRadians(val[1]) || 0, CesiumMath.toRadians(val[2]) || 0) + } else if (isPlainObject(val)) { + return new HeadingPitchRoll(val.heading || 0, val.pitch || 0, val.roll || 0) + } + + return new HeadingPitchRoll() +} + +export function makeHeadingPitchRang(val: VcHeadingPitchRange): Cesium.HeadingPitchRange { + const { HeadingPitchRange, Math: CesiumMath } = Cesium + if (val instanceof Cesium.HeadingPitchRange) { + return val + } else if (Array.isArray(val)) { + return new HeadingPitchRange(CesiumMath.toRadians(val[0]) || 0, CesiumMath.toRadians(val[1]) || 0, val[2] || 0) + } else if (isPlainObject(val)) { + return new HeadingPitchRange(val.heading || 0, val.pitch || 0, val.range || 0) + } + + return new HeadingPitchRange() +} + export function getPolylineSegmentHeading(start: Cesium.Cartesian3, end: Cesium.Cartesian3) { const { Cartesian3, Matrix4, Transforms, Math: CesiumMath } = Cesium const cartesian3Scratch = new Cartesian3() @@ -997,8 +1026,8 @@ export function getFirstIntersection( return undefined } -// 粗略计算 export function heightToLevel(altitude: number) { + // 粗略计算 const A = 40487.57 const B = 0.00007096758 const C = 91610.74 diff --git a/packages/utils/util.ts b/packages/utils/util.ts index e54494d84..3594fa859 100644 --- a/packages/utils/util.ts +++ b/packages/utils/util.ts @@ -138,9 +138,9 @@ export function getDefaultOptionByProps(props, ignores: Array = []) { return defaultOptions } -const addCustomProperty = (obj, options) => { +const addCustomProperty = (obj, options, ignores: Array = []) => { for (const prop in options) { - if (!obj[prop]) { + if (!obj[prop] && ignores.indexOf(prop) === -1) { obj[prop] = options[prop] } } @@ -155,6 +155,13 @@ export const merge = >(a: T, b: T) => { return obj } +export function isArrayLike(obj) { + if (Array.isArray(obj)) return true + if (typeof obj !== 'object' || !obj) return false + const length = obj.length + return typeof length === 'number' && length >= 0 +} + // reexport from lodash & vue shared export { hasOwn,