modeling/src/measurements/measureAggregateBoundingBox.js

  1. const flatten = require('../utils/flatten')
  2. const vec3min = require('../maths/vec3/min')
  3. const vec3max = require('../maths/vec3/max')
  4. const measureBoundingBox = require('./measureBoundingBox')
  5. /**
  6. * Measure the aggregated minimum and maximum bounds for the given geometries.
  7. * @param {...Object} geometries - the geometries to measure
  8. * @return {Array} the min and max bounds for the group of geometry, i.e. [[x,y,z],[X,Y,Z]]
  9. * @alias module:modeling/measurements.measureAggregateBoundingBox
  10. *
  11. * @example
  12. * let bounds = measureAggregateBoundingBox(sphere(),cube())
  13. */
  14. const measureAggregateBoundingBox = (...geometries) => {
  15. geometries = flatten(geometries)
  16. if (geometries.length === 0) throw new Error('measureAggregateBoundingBox: no geometries supplied')
  17. const bounds = measureBoundingBox(geometries)
  18. if (geometries.length === 1) {
  19. return bounds
  20. }
  21. const result = [[Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE], [-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE]]
  22. return bounds.reduce((result, item) => {
  23. result = [vec3min(result[0], result[0], item[0]), vec3max(result[1], result[1], item[1])]
  24. return result
  25. }, result)
  26. }
  27. module.exports = measureAggregateBoundingBox