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 var cartographic = Cesium.Cartographic.fromDegrees(-75.59777, 40.03883); var pointInFixed = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude); // Transform point to the ICRF axes var 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):這個方法接受一個原點引數、一個橢球體引數和一個可選的結果引數,回傳一個4x4的變換矩陣,將一個點或向量從東北上(ENU)區域坐標系變換到地球固定系,這個方法可以用于創建以某個位置為中心的區域參考系,例如,如果你想在地圖上添加一個以某個位置為中心的方向指示器,你可以這樣做1:
// 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 transform const 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):這個方法接受一個變換矩陣引數和一個可選的結果引數,回傳一個緊密包圍給定變換矩陣的包圍球,這個方法可以用于計算變換后的幾何體或模型的包圍球,例如,如果你想計算一個模型在地球上某個位置旋轉后的包圍球,你可以這樣做2:
// Load a model var model = scene.primitives.add(Cesium.Model.fromGltf({ url : 'model.gltf', })); // Get the model's bounding sphere var boundingSphere = model.boundingSphere; // Create a transform matrix that rotates the model by 45 degrees around the z-axis at a certain position on Earth var 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 sphere var transformedBoundingSphere = Cesium.BoundingSphere.fromTransformation(transform, boundingSphere);
- TransformEditor(options):這是一個工具類,用于編輯物件(如模型、物體、圖元等)的變換(位置、方向、縮放),它接受一個選項物件引數,該引數可以指定要編輯的物件、場景、容器元素等屬性,這個工具類可以用于互動式地調整物件在場景中的顯示效果,例如,如果你想在場景中添加一個模型,并使用TransformEditor來編輯它,你可以這樣做3:
// Create a scene const viewer = new Cesium.Viewer("cesiumContainer"); // Add a model to the scene const 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 edit const transformEditor = new Cesium.TransformEditor({ entity: modelEntity, });
- Model(modelOptions):這是一個表示三維模型(如glTF格式)的類,它接受一個模型選項物件引數,該引數可以指定模型的URL、位置、方向、縮放等屬性,它還有一些方法和屬性來控制模型的影片、著色器、材質等效果,這個類可以用于在場景中渲染復雜和精細的三維物體,例如,如果你想在場景中添加一個飛機模型,并讓它沿著一條路徑飛行,你可以這樣做?:
// Create a scene const viewer = new Cesium.Viewer("cesiumContainer"); // Create a path for the airplane to follow const start = Cesium.JulianDate.fromDate(new Date(2015, 2, 25)); const
- Transforms.computeFixedToIcrfMatrix(date, result):這個方法接受一個日期引數和一個可選的結果引數,回傳一個從地球固定坐標系到國際天文參考系(ICRF)坐標系的變換矩陣,這個方法可以用于將地球上的位置轉換為太陽系中的位置,例如,如果你想計算2020年1月1日午夜時刻,在緯度0度經度0度處的位置在太陽系中的坐標,你可以這樣做1:
// Create a date object var date = new Cesium.JulianDate.fromDate(new Date(2020, 0, 1)); // Get the position on Earth in Cartesian coordinates var positionOnEarth = Cesium.Cartesian3.fromDegrees(0.0, 0.0); // Get the transform matrix from Earth fixed frame to ICRF frame var transform = Cesium.Transforms.computeFixedToIcrfMatrix(date); // Apply the transform to get the position in ICRF frame var positionInSpace = Cesium.Matrix3.multiplyByVector(transform, positionOnEarth);
- Transforms.eastNorthUpToFixedFrame(origin, ellipsoid, result):這個方法接受一個原點引數、一個可選的橢球引數和一個可選的結果引數,回傳一個從東北上(ENU)區域坐標系到地球固定坐標系的變換矩陣,這個方法可以用于創建一個以給定點為原點、以東方為x軸、以北方為y軸、以垂直方向為z軸的區域參考系,例如,如果你想創建一個以紐約市為原點的區域參考系,并在其中添加一些圖形元素,你可以這樣做1:
// Create a scene const viewer = new Cesium.Viewer("cesiumContainer"); // Get the position of New York City in Cartesian coordinates var origin = Cesium.Cartesian3.fromDegrees(-74.01881302800248, 40.69114333714821); // Get the transform matrix from ENU frame to Earth fixed frame var transform = Cesium.Transforms.eastNorthUpToFixedFrame(origin); // Add a red sphere of radius 5 meters at the origin of the local frame viewer.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 frame viewer.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,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/545473.html
標籤:GIS
