modeling/src/operations/hulls/hull.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 path2 = require('../../geometries/path2')
  6. const hullPath2 = require('./hullPath2')
  7. const hullGeom2 = require('./hullGeom2')
  8. const hullGeom3 = require('./hullGeom3')
  9. /**
  10. * Create a convex hull of the given geometries.
  11. * The given geometries should be of the same type, either geom2 or geom3 or path2.
  12. * @param {...Objects} geometries - list of geometries from which to create a hull
  13. * @returns {geom2|geom3} new geometry
  14. * @alias module:modeling/hulls.hull
  15. *
  16. * @example
  17. * let myshape = hull(rectangle({center: [-5,-5]}), ellipse({center: [5,5]}))
  18. *
  19. * @example
  20. * +-------+ +-------+
  21. * | | | \
  22. * | A | | \
  23. * | | | \
  24. * +-------+ + \
  25. * = \ \
  26. * +-------+ \ +
  27. * | | \ |
  28. * | B | \ |
  29. * | | \ |
  30. * +-------+ +-------+
  31. */
  32. const hull = (...geometries) => {
  33. geometries = flatten(geometries)
  34. if (geometries.length === 0) throw new Error('wrong number of arguments')
  35. if (!areAllShapesTheSameType(geometries)) {
  36. throw new Error('only hulls of the same type are supported')
  37. }
  38. const geometry = geometries[0]
  39. if (path2.isA(geometry)) return hullPath2(geometries)
  40. if (geom2.isA(geometry)) return hullGeom2(geometries)
  41. if (geom3.isA(geometry)) return hullGeom3(geometries)
  42. // FIXME should this throw an error for unknown geometries?
  43. return geometry
  44. }
  45. module.exports = hull