# 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;
``````

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?

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

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

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

I go back to the subject. Does anyone have an idea to improve the computation ?

``````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;
``````

Hi there,

Bit hard to grasp what you exactly want, for example the distance between the camera and “terrain” is a bit vague (is the terrain a model? terrainModel? Mesh model?).

If it’s any terrain (ie. whatever is in view), I’d create a line between the camera position (easily found through viewer.scene.camera) and some point in front of the camera. It’s this latter that’s a bit hard to understand. Do you mean the center of the window / camera view? How far from camera do you need it? To the end of the horizon? To whatever tilt / pan of the camera?

I would simply create a line between these two points, and sample along it using various methods of precission you need (for example sampleHighest between me and, say, 100 meters off, then sample normal till 400 meters, and then do every X points normal after that, etc. There are many ways of slicing and dicing this, but it all depends on what you’re trying to achieve. To say “you want to sample every point between you and the terrain” is, well, what you’re doing, and that’s a ton of points, and that’s going to take a long time. Do you need full resolution all the time on everything? What are the parameters of that profile? What’s it used for?

And so on. You’re currently doing the full resolution version where you do everything in high resolution. Are there parameters to tweak?

Cheers,

Alex