Compute the distance between terrain and camera position

Hello cesium community,

First of all, I would like to thank you for the development of CesiumJS which is a very interesting tool and which I use for personal projects.

And that’s why I need your help.

After positioning the camera (long, lat, height and pitch, roll), I want to compute the distance for each point of the image between the terrain and the position of the camera.

I am using the following code which works fine but is excessively heavy on computational time. Do you have any ideas / advice to speed up the calculation?

viewer.render();
var width = document.getElementById('cesiumContainer').scrollWidth;
var height = document.getElementById('cesiumContainer').scrollHeight;
var rows = new Array(height);
for (var y = 0; y < height; ++y) {
    var row = new Array(width).fill(-1);
    for (var x = 0; x < width; ++x) {
        var ray = viewer.camera.getPickRay(new Cesium.Cartesian2(x, y));
        var intersection = globe.pick(ray, viewer.scene);
			
        if (typeof intersection !== "undefined") {
            row[x] = Cesium.Cartesian3.distance(viewer.camera.position, intersection);
        }
    }
    rows[y] = row;
}
return rows;

Thanks in advance.
Best regards,
Emmanuel.

Hi Emmanuel,

Thank you for using CesiumJS!

I would like to understand your use case a bit more. What are you trying to achieve with these intersection points?

Hi and thank you for your answer !

I need to compute the distance for each point of the image between the terrain and the position of the camera. So,

1- From a camera point of view, i save a png image

2- From this point of view, i compute the distance between the terrain and the camera position for each pixel of the view using the code from the previous message. But this computation is sooo long.
I save this data in a csv file.

Outside my cesium apps, i use the csv file to:

1- generate an heatmap picture of the distance
Figure_1

2- I use the csv file and the heatmap picture in an ML algorithm.

To sum up, i need to have the distance between the terrain and thecamera for each point of the image and save these data in a csv file. As this computation is heavy, i ask the community if they are an idea to speed up this computation !

I hope I have been clear enough :slight_smile:

(An example of the csv data d.zip (1.7 MB) )