Cesium Transform(二十)

打印 上一主题 下一主题

主题 506|帖子 506|积分 1518

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):这个方法接受一个日期参数和一个可选的结果参数,返回一个3x3的旋转矩阵,将一个点或向量从地球固定系(ITRF)变换到国际天文参考系(GCRF/ICRF)惯性系。这个方法可以用于将地球上的位置转换为太阳系中的位置。例如,如果你想知道现在地球上某个点在太阳系中的位置,你可以这样做¹:
  1. // Get the position of a point on Earth in ITRF coordinates
  2. var cartographic = Cesium.Cartographic.fromDegrees(-75.59777, 40.03883);
  3. var pointInFixed = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude);
  4. // Transform point to the ICRF axes
  5. var now = Cesium.JulianDate.now();
  6. var fixedToIcrf = Cesium.Transforms.computeFixedToIcrfMatrix(now);
  7. var pointInInertial = new Cesium.Cartesian3();
  8. Cesium.Matrix3.multiplyByVector(fixedToIcrf, pointInFixed, pointInInertial);
复制代码
 
- Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result):这个方法接受一个原点参数、一个椭球体参数和一个可选的结果参数,返回一个4x4的变换矩阵,将一个点或向量从东北上(ENU)局部坐标系变换到地球固定系。这个方法可以用于创建以某个位置为中心的局部参考系。例如,如果你想在地图上添加一个以某个位置为中心的方向指示器,你可以这样做¹:
  1. // Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame.
  2. const center = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  3. const transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);
  4. // Create a primitive that uses this transform
  5. const scene = viewer.scene;
  6. const primitive = scene.primitives.add(new Cesium.Primitive({
  7. geometryInstances: new Cesium.GeometryInstance({
  8. geometry: new Cesium.SimplePolylineGeometry({
  9. positions: [new Cesium.Cartesian3(0.0, 0.0, 0.0), new Cesium.Cartesian3(10000000.0, 0.0, 0.0)],
  10. colors: [Cesium.Color.RED.withAlpha(1), Cesium.Color.RED.withAlpha(1)],
  11. followSurface: false,
  12. }),
  13. modelMatrix: transform,
  14. }),
  15. appearance: new Cesium.PolylineColorAppearance(),
  16. }));
复制代码
 
- Transforms.localFrameToFixedFrameGenerator(firstAxis, secondAxis):这个方法接受两个轴参数(X、Y或Z),返回一个函数,该函数根据给定的位置和方向创建一个变换矩阵,将一个点或向量从局部坐标系变换到地球固定系。这个方法可以用于创建不同类型的局部参考系。例如,如果你想创建以北为第一轴、东为第二轴、上为第三轴(NEU)的局部参考系,你可以这样做:
  1. // Create a local reference frame based on north-east-up axes at cartographic (11.34 degrees east longitude,
  2. // 46 degrees north latitude).
  3. const originCartographic = new Cesium.Cartographic(Cesium.Math.toRadians(11.34), Math.toRadians(46));
  4. const originCartesian = viewer.scene.globe.
复制代码
 
- BoundingSphere.fromTransformation(transformation, result):这个方法接受一个变换矩阵参数和一个可选的结果参数,返回一个紧密包围给定变换矩阵的包围球。这个方法可以用于计算变换后的几何体或模型的包围球。例如,如果你想计算一个模型在地球上某个位置旋转后的包围球,你可以这样做²:
  1. // Load a model
  2. var model = scene.primitives.add(Cesium.Model.fromGltf({
  3. url : 'model.gltf',
  4. }));
  5. // Get the model's bounding sphere
  6. var boundingSphere = model.boundingSphere;
  7. // Create a transform matrix that rotates the model by 45 degrees around the z-axis at a certain position on Earth
  8. var position = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
  9. var heading = Cesium.Math.toRadians(45.0);
  10. var pitch = 0;
  11. var roll = 0;
  12. var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  13. var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
  14. var transform = Cesium.Matrix4.fromTranslationQuaternionRotationScale(position, orientation, new Cesium.Cartesian3(1.0, 1.0, 1.0));
  15. // Compute the transformed bounding sphere
  16. var transformedBoundingSphere = Cesium.BoundingSphere.fromTransformation(transform, boundingSphere);
复制代码
 
- TransformEditor(options):这是一个工具类,用于编辑对象(如模型、实体、图元等)的变换(位置、方向、缩放)。它接受一个选项对象参数,该参数可以指定要编辑的对象、场景、容器元素等属性。这个工具类可以用于交互式地调整对象在场景中的显示效果。例如,如果你想在场景中添加一个模型,并使用TransformEditor来编辑它,你可以这样做³:
  1. // Create a scene
  2. const viewer = new Cesium.Viewer("cesiumContainer");
  3. // Add a model to the scene
  4. const modelEntity = viewer.entities.add({
  5. name: "model",
  6. position: Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),
  7. orientation: Cesium.Transforms.headingPitchRollQuaternion(
  8. Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706),
  9. new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(135), 0, 0)
  10. ),
  11. model: {
  12. uri: "model.gltf",
  13. minimumPixelSize: 128,
  14. maximumScale: 20000,
  15. },
  16. });
  17. // Create a TransformEditor instance and pass in the entity to edit
  18. const transformEditor = new Cesium.TransformEditor({
  19. entity: modelEntity,
  20. });
复制代码
 
- Model(modelOptions):这是一个表示三维模型(如glTF格式)的类,它接受一个模型选项对象参数,该参数可以指定模型的URL、位置、方向、缩放等属性。它还有一些方法和属性来控制模型的动画、着色器、材质等效果。这个类可以用于在场景中渲染复杂和精细的三维物体。例如,如果你想在场景中添加一个飞机模型,并让它沿着一条路径飞行,你可以这样做⁴:
  1. // Create a scene
  2. const viewer = new Cesium.Viewer("cesiumContainer");
  3. // Create a path for the airplane to follow
  4. const start = Cesium.JulianDate.fromDate(new Date(2015, 2, 25));
  5. const
复制代码
 
- Transforms.computeFixedToIcrfMatrix(date, result):这个方法接受一个日期参数和一个可选的结果参数,返回一个从地球固定坐标系到国际天文参考系(ICRF)坐标系的变换矩阵。这个方法可以用于将地球上的位置转换为太阳系中的位置。例如,如果你想计算2020年1月1日午夜时刻,在纬度0度经度0度处的位置在太阳系中的坐标,你可以这样做¹:
  1. // Create a date object
  2. var date = new Cesium.JulianDate.fromDate(new Date(2020, 0, 1));
  3. // Get the position on Earth in Cartesian coordinates
  4. var positionOnEarth = Cesium.Cartesian3.fromDegrees(0.0, 0.0);
  5. // Get the transform matrix from Earth fixed frame to ICRF frame
  6. var transform = Cesium.Transforms.computeFixedToIcrfMatrix(date);
  7. // Apply the transform to get the position in ICRF frame
  8. var positionInSpace = Cesium.Matrix3.multiplyByVector(transform, positionOnEarth);
复制代码
 
- Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result):这个方法接受一个原点参数、一个可选的椭球参数和一个可选的结果参数,返回一个从东北上(ENU)局部坐标系到地球固定坐标系的变换矩阵。这个方法可以用于创建一个以给定点为原点、以东方为x轴、以北方为y轴、以垂直方向为z轴的局部参考系。例如,如果你想创建一个以纽约市为原点的局部参考系,并在其中添加一些图形元素,你可以这样做¹:
  1. // Create a scene
  2. const viewer = new Cesium.Viewer("cesiumContainer");
  3. // Get the position of New York City in Cartesian coordinates
  4. var origin = Cesium.Cartesian3.fromDegrees(-74.01881302800248, 40.69114333714821);
  5. // Get the transform matrix from ENU frame to Earth fixed frame
  6. var transform = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
  7. // Add a red sphere of radius 5 meters at the origin of the local frame
  8. viewer.entities.add({
  9. name: "Red sphere",
  10. position: new Cesium.ConstantPositionProperty(origin),
  11. ellipsoid: {
  12. radii: new Cesium.Cartesian3(5.0, 5.0, 5.0),
  13. material: Cesium.Color.RED,
  14. },
  15. });
  16. // Add a blue box of dimensions 10 x 10 x 10 meters along the x-axis of the local frame
  17. viewer.entities.add({
  18. name: "Blue box",
  19. position: new Cesium.ConstantPositionProperty(
  20. Cesium.Matrix4.multiplyByPoint(transform, new Cesium.Cartesian3(10.0, 0.0, 0.0))
  21. ),
  22. box: {
  23. dimensions: new Cesium.Cartesian3(10.0, 10.0,
复制代码
 



 
 



 
 
 
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

万万哇

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表