modeling/src/operations/booleans/union.js

  1. const flatten = require('../../utils/flatten')
  2. const areAllShapesTheSameType = require('../../utils/areAllShapesTheSameType')
  3. const geom2 = require('../../geometries/geom2')
  4. const geom3 = require('../../geometries/geom3')
  5. const unionGeom2 = require('./unionGeom2')
  6. const unionGeom3 = require('./unionGeom3')
  7. /**
  8. * Return a new geometry representing the total space in the given geometries.
  9. * The given geometries should be of the same type, either geom2 or geom3.
  10. *
  11. * @param {...Object} geometries - list of geometries
  12. * @returns {geom2|geom3} a new geometry
  13. * @alias module:modeling/booleans.union
  14. *
  15. * @example
  16. * let myshape = union(cube({size: 5}), cube({size: 5, center: [3,3,3]}))
  17. *
  18. * @example
  19. * +-------+ +-------+
  20. * | | | |
  21. * | A | | |
  22. * | +--+----+ = | +----+
  23. * +----+--+ | +----+ |
  24. * | B | | |
  25. * | | | |
  26. * +-------+ +-------+
  27. */
  28. const union = (...geometries) => {
  29. geometries = flatten(geometries)
  30. if (geometries.length === 0) throw new Error('wrong number of arguments')
  31. if (!areAllShapesTheSameType(geometries)) {
  32. throw new Error('only unions of the same type are supported')
  33. }
  34. const geometry = geometries[0]
  35. // if (path.isA(geometry)) return pathunion(matrix, geometries)
  36. if (geom2.isA(geometry)) return unionGeom2(geometries)
  37. if (geom3.isA(geometry)) return unionGeom3(geometries)
  38. return geometry
  39. }
  40. module.exports = union