modeling/src/operations/hulls/hullChain.js

  1. const flatten = require('../../utils/flatten')
  2. const union = require('../booleans/union')
  3. const hull = require('./hull')
  4. /**
  5. * Create a chain of hulled geometries from the given geometries.
  6. * Essentially hull A+B, B+C, C+D, etc., then union the results.
  7. * The given geometries should be of the same type, either geom2 or geom3 or path2.
  8. *
  9. * @param {...Objects} geometries - list of geometries from which to create a hull
  10. * @returns {geom2|geom3} new geometry
  11. * @alias module:modeling/hulls.hullChain
  12. *
  13. * @example
  14. * let newshape = hullChain(rectangle({center: [-5,-5]}), circle({center: [0,0]}), rectangle({center: [5,5]}))
  15. *
  16. * @example
  17. * +-------+ +-------+ +-------+ +------+
  18. * | | | | | \ / |
  19. * | A | | C | | | |
  20. * | | | | | |
  21. * +-------+ +-------+ + +
  22. * = \ /
  23. * +-------+ \ /
  24. * | | \ /
  25. * | B | \ /
  26. * | | \ /
  27. * +-------+ +-------+
  28. */
  29. const hullChain = (...geometries) => {
  30. geometries = flatten(geometries)
  31. if (geometries.length < 2) throw new Error('wrong number of arguments')
  32. const hulls = []
  33. for (let i = 1; i < geometries.length; i++) {
  34. hulls.push(hull(geometries[i - 1], geometries[i]))
  35. }
  36. return union(hulls)
  37. }
  38. module.exports = hullChain