cesium是一个用于创建3D地球和空间场景的JavaScript库,它提供了一些用于坐标变换的类,统称为transform。transform类可以帮助我们在不同的参考系之间转换点或向量,例如从地球固定系到国际天文参考系,或者从WGS84坐标系到窗口坐标系。transform类还可以根据给定的位置和方向创建一个变换矩阵,例如从东北上到地球固定系,或者从局部坐标系到世界坐标系。

cesium中最常用的transform类有以下几个:

– Transforms.computeFixedToIcrfMatrix(date, result):计算一个旋转矩阵,将一个点或向量从地球固定系(ITRF)变换到国际天文参考系(GCRF/ICRF)惯性系。
– Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result):根据给定的原点和椭球体创建一个变换矩阵,将一个点或向量从东北上(ENU)局部坐标系变换到地球固定系。
– Transforms.localFrameToFixedFrameGenerator(firstAxis, secondAxis):返回一个函数,该函数根据给定的位置和方向创建一个变换矩阵,将一个点或向量从局部坐标系变换到地球固定系。
– Transforms.headingPitchRollToFixedFrame(origin, headingPitchRoll, ellipsoid, fixedFrameTransform, result):根据给定的原点、航偏俯角(HPR)和椭球体创建一个变换矩阵,将一个点或向量从HPR局部坐标系变换到地球固定系。

cesium还提供了一些用于在场景中转换位置的类:

– SceneTransforms.wgs84ToWindowCoordinates(scene, position, result):将WGS84坐标系中的位置转换为窗口坐标系中的位置。这通常用于将HTML元素放置在场景中某个对象的相同屏幕位置。
– SceneTransforms.wgs84ToDrawingBufferCoordinates(scene, position, result):将WGS84坐标系中的位置转换为绘图缓冲区坐标系中的位置。这通常用于在WebGL上下文中绘制与场景中某个对象对齐的图形。

除了上述类之外,cesium还有一些其他与模型、相机、投影等相关的transform类。可以在cesium文档中查看更多信息。

– Transforms.computeFixedToIcrfMatrix(date, result):这个方法接受一个日期参数和一个可选的结果参数,返回一个3×3的旋转矩阵,将一个点或向量从地球固定系(ITRF)变换到国际天文参考系(GCRF/ICRF)惯性系。这个方法可以用于将地球上的位置转换为太阳系中的位置。例如,如果你想知道现在地球上某个点在太阳系中的位置,你可以这样做¹:

// Get the position of a point on Earth in ITRF coordinatesvar cartographic = Cesium.Cartographic.fromDegrees(-75.59777, 40.03883);var pointInFixed = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude);// Transform point to the ICRF axesvar now = Cesium.JulianDate.now();var fixedToIcrf = Cesium.Transforms.computeFixedToIcrfMatrix(now);var pointInInertial = new Cesium.Cartesian3();Cesium.Matrix3.multiplyByVector(fixedToIcrf, pointInFixed, pointInInertial);

– Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result):这个方法接受一个原点参数、一个椭球体参数和一个可选的结果参数,返回一个4×4的变换矩阵,将一个点或向量从东北上(ENU)局部坐标系变换到地球固定系。这个方法可以用于创建以某个位置为中心的局部参考系。例如,如果你想在地图上添加一个以某个位置为中心的方向指示器,你可以这样做¹:

// Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame.const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);const transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);// Create a primitive that uses this transformconst scene = viewer.scene;const primitive = scene.primitives.add(new Cesium.Primitive({geometryInstances: new Cesium.GeometryInstance({geometry: new Cesium.SimplePolylineGeometry({positions: [new Cesium.Cartesian3(0.0, 0.0, 0.0), new Cesium.Cartesian3(10000000.0, 0.0, 0.0)],colors: [Cesium.Color.RED.withAlpha(1), Cesium.Color.RED.withAlpha(1)],followSurface: false,}),modelMatrix: transform,}),appearance: new Cesium.PolylineColorAppearance(),}));

– Transforms.localFrameToFixedFrameGenerator(firstAxis, secondAxis):这个方法接受两个轴参数(X、Y或Z),返回一个函数,该函数根据给定的位置和方向创建一个变换矩阵,将一个点或向量从局部坐标系变换到地球固定系。这个方法可以用于创建不同类型的局部参考系。例如,如果你想创建以北为第一轴、东为第二轴、上为第三轴(NEU)的局部参考系,你可以这样做:

// Create a local reference frame based on north-east-up axes at cartographic (11.34 degrees east longitude,// 46 degrees north latitude).const originCartographic = new Cesium.Cartographic(Cesium.Math.toRadians(11.34), Math.toRadians(46));const originCartesian = viewer.scene.globe.

– BoundingSphere.fromTransformation(transformation, result):这个方法接受一个变换矩阵参数和一个可选的结果参数,返回一个紧密包围给定变换矩阵的包围球。这个方法可以用于计算变换后的几何体或模型的包围球。例如,如果你想计算一个模型在地球上某个位置旋转后的包围球,你可以这样做²:

// Load a modelvar model = scene.primitives.add(Cesium.Model.fromGltf({url : 'model.gltf',}));// Get the model's bounding spherevar boundingSphere = model.boundingSphere;// Create a transform matrix that rotates the model by 45 degrees around the z-axis at a certain position on Earthvar position = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);var heading = Cesium.Math.toRadians(45.0);var pitch = 0;var roll = 0;var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);var transform = Cesium.Matrix4.fromTranslationQuaternionRotationScale(position, orientation, new Cesium.Cartesian3(1.0, 1.0, 1.0));// Compute the transformed bounding spherevar transformedBoundingSphere = Cesium.BoundingSphere.fromTransformation(transform, boundingSphere);

– TransformEditor(options):这是一个工具类,用于编辑对象(如模型、实体、图元等)的变换(位置、方向、缩放)。它接受一个选项对象参数,该参数可以指定要编辑的对象、场景、容器元素等属性。这个工具类可以用于交互式地调整对象在场景中的显示效果。例如,如果你想在场景中添加一个模型,并使用TransformEditor来编辑它,你可以这样做³:

// Create a sceneconst viewer = new Cesium.Viewer("cesiumContainer");// Add a model to the sceneconst modelEntity = viewer.entities.add({name: "model",position: Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),orientation: Cesium.Transforms.headingPitchRollQuaternion(Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(135), 0, 0)),model: {uri: "model.gltf",minimumPixelSize: 128,maximumScale: 20000,},});// Create a TransformEditor instance and pass in the entity to editconst transformEditor = new Cesium.TransformEditor({entity: modelEntity,});

– Model(modelOptions):这是一个表示三维模型(如glTF格式)的类,它接受一个模型选项对象参数,该参数可以指定模型的URL、位置、方向、缩放等属性。它还有一些方法和属性来控制模型的动画、着色器、材质等效果。这个类可以用于在场景中渲染复杂和精细的三维物体。例如,如果你想在场景中添加一个飞机模型,并让它沿着一条路径飞行,你可以这样做⁴:

// Create a sceneconst viewer = new Cesium.Viewer("cesiumContainer");// Create a path for the airplane to followconst start = Cesium.JulianDate.fromDate(new Date(2015, 2, 25));const

– Transforms.computeFixedToIcrfMatrix(date, result):这个方法接受一个日期参数和一个可选的结果参数,返回一个从地球固定坐标系到国际天文参考系(ICRF)坐标系的变换矩阵。这个方法可以用于将地球上的位置转换为太阳系中的位置。例如,如果你想计算2020年1月1日午夜时刻,在纬度0度经度0度处的位置在太阳系中的坐标,你可以这样做¹:

// Create a date objectvar date = new Cesium.JulianDate.fromDate(new Date(2020, 0, 1));// Get the position on Earth in Cartesian coordinatesvar positionOnEarth = Cesium.Cartesian3.fromDegrees(0.0, 0.0);// Get the transform matrix from Earth fixed frame to ICRF framevar transform = Cesium.Transforms.computeFixedToIcrfMatrix(date);// Apply the transform to get the position in ICRF framevar positionInSpace = Cesium.Matrix3.multiplyByVector(transform, positionOnEarth);

– Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result):这个方法接受一个原点参数、一个可选的椭球参数和一个可选的结果参数,返回一个从东北上(ENU)局部坐标系到地球固定坐标系的变换矩阵。这个方法可以用于创建一个以给定点为原点、以东方为x轴、以北方为y轴、以垂直方向为z轴的局部参考系。例如,如果你想创建一个以纽约市为原点的局部参考系,并在其中添加一些图形元素,你可以这样做¹:

// Create a sceneconst viewer = new Cesium.Viewer("cesiumContainer");// Get the position of New York City in Cartesian coordinatesvar origin = Cesium.Cartesian3.fromDegrees(-74.01881302800248, 40.69114333714821);// Get the transform matrix from ENU frame to Earth fixed framevar transform = Cesium.Transforms.eastNorthUpToFixedFrame(origin);// Add a red sphere of radius 5 meters at the origin of the local frameviewer.entities.add({name: "Red sphere",position: new Cesium.ConstantPositionProperty(origin),ellipsoid: {radii: new Cesium.Cartesian3(5.0, 5.0, 5.0),material: Cesium.Color.RED,},});// Add a blue box of dimensions 10 x 10 x 10 meters along the x-axis of the local frameviewer.entities.add({name: "Blue box",position: new Cesium.ConstantPositionProperty(Cesium.Matrix4.multiplyByPoint(transform, new Cesium.Cartesian3(10.0, 0.0, 0.0))),box: {dimensions: new Cesium.Cartesian3(10.0, 10.0,