Get the bounding box of an array of geometries


I find myself looking for the bounding box that encloses a number of geometries.

Unfortunately, with api.scene.getBoundingBox( [path]) I can only retrieve the bounding box for one geometry. Surely, I could write a helper function that gets the bounding box for each geometry and then builds a “master bounding box” over all these bounding boxes. However, I wonder if there is really no simpler/native way to get the bounding box over an array of geometries?

Thank you!

There is no simpler way at the moment I’m afraid but I added this feature request on our roadmap. I will get back to you when we have an update on this.

1 Like

Just in case anyone stumbles across this, this is the helper function that I wrote for getting the boundingBox for an array of geometries. Of course, I cannot guarantee that it is reliable, but it has worked for me so far.

*@author DAUD
*@geometriesScenePaths expects an array of scenePaths for all geoemtries that should be encapsulated in the bounding box
*@return the bounding box encapsulating all geometries in geometriesScenePaths
function getBoundingBoxForGeos(geometriesScenePaths){
  let min = undefined;
  let max = undefined;
  geometriesScenePaths.forEach( (scenePath) => {
    let geoBB = api.scene.getBoundingBox(scenePath).data;
    if(min==undefined){//FIRST LOOP
      min = JSON.parse(JSON.stringify(geoBB.min)); //shallow copy;
      max = JSON.parse(JSON.stringify(geoBB.max));
      min.x = Math.min(geoBB.min.x, min.x);
      min.y = Math.min(geoBB.min.y, min.y);
      min.z = Math.min(geoBB.min.z, min.z);
      max.x = Math.max(geoBB.max.x, max.x);
      max.y = Math.max(geoBB.max.y, max.y);
      max.z = Math.max(geoBB.max.z, max.z);
  return {min:min, max: max};