Trim 在 Cesium 中地形相交的插值折线

Trim interpolated polyline where terrain is intersecting in Cesium

我正在尝试创建一个弧线,该弧线会在与地形相交时停止。两个变量 arc1arc2 其中 arc1 没有地形交点, arc2与地形相交

arc1 工作正常,但 arc2 没有在与地形相交的地方停止。

var viewer = new Cesium.Viewer("cesiumContainer", {
  infoBox: false, //Disable InfoBox widget
  selectionIndicator: false, //Disable selection indicator
  shouldAnimate: true, // Enable animations
  terrainProvider: Cesium.createWorldTerrain(),
});

//Enable lighting based on the sun position
viewer.scene.globe.enableLighting = true;

//Enable depth testing so things behind the terrain disappear.
viewer.scene.globe.depthTestAgainstTerrain = true;

// Cartesian3: start, mid and end points for polyline
var controls = [{x:-1942111.383043348,y:-4780646.881480431,z:3737538.9114379627},
                {x:-1847333.0834675943,y:-5281519.814050422,z:3579120.5547780637},
                {x:-1611854.2607850158,y:-5380130.367796415,z:3146339.4631628506}];


// Applying interpolation
var spline = Cesium.HermiteSpline.createNaturalCubic({
        times: [0.0, 0.5, 1],
        points: controls,
    });

var ppos = []; //Interpolated polyline cordinates
for (var i = 0; i <= 50; i++) {
    var cartesian3 = spline.evaluate(i / 50);
    ppos.push(cartesian3);
}

/* Terrain and Polyline Intersection Detection */
var cartographic = [];
var cartographic_real = [];
ppos.forEach(p => {
    let dh = Cesium.Cartographic.fromCartesian(p);
    dh.height = 0;
    cartographic.push(dh);
    cartographic_real.push(Cesium.Cartographic.fromCartesian(p));
});

// collecting actual terrain heights
Cesium.sampleTerrainMostDetailed(viewer.terrainProvider, cartographic).then((raisedPositionsCartograhpic) => {
    var t = true;
    var rpos = []; // 
    cartographic_real.forEach((p, i) => {
        if(t) rpos.push(ppos[i]);
        // If terrain height is more than polyline cordinate height
        if (i > 0 && t && (raisedPositionsCartograhpic[i].height - p.height)) {
          t = false;
          
          console.log("Stopped at:", p.height);
          console.log(rpos);
          drawNewArc(rpos);

        }
    });
});

/* --- */

function drawNewArc(rpos)
{
  var arc1 = {};
  arc1.polyline = { // Actual line without terrain intersection
          positions: ppos,
          width: 3,
          material: Cesium.Color.RED.withAlpha(0.5),
      }
  
  var arc2 = {};
  arc2.polyline = { // This should stop where it intersects terrain
          positions: rpos,
          width: 3,
          material: Cesium.Color.GREEN.withAlpha(1.0),
      }
  
  var entity = viewer.entities.add(arc1);
  var entity2 = viewer.entities.add(arc2);
}

viewer.camera.flyTo({destination: Cesium.Cartesian3.fromDegrees(-112.081,36.097,10000)});

我希望绿色折线在遇到任何地形时停止。

example

我想我需要获取并匹配弧的所有点。不知道怎么得到绿弧的所有点数

这是沙堡中的完整代码link

修复代码中的各种错误后获得成功,例如:

多段线分割不正确:

for (var i = 0; i <= 50; i++) {

身高匹配条件不正确:

if (i > 0 && t && (raisedPositionsCartograhpic[i].height - p.height)) {

这是完整的工作代码link。它也可能有错误,必须有更好的解决方案才能实现 objective。如果有人有任何更好更快的解决方案,请在这里post。