modeling/src/operations/transforms/transform.js

  1. const flatten = require('../../utils/flatten')
  2. const geom2 = require('../../geometries/geom2')
  3. const geom3 = require('../../geometries/geom3')
  4. const path2 = require('../../geometries/path2')
  5. /**
  6. * Transform the given objects using the given matrix.
  7. * @param {mat4} matrix - a transformation matrix
  8. * @param {...Object} objects - the objects to transform
  9. * @return {Object|Array} the transformed object, or a list of transformed objects
  10. * @alias module:modeling/transforms.transform
  11. *
  12. * @example
  13. * const newsphere = transform(mat4.rotateX(TAU / 8), sphere())
  14. */
  15. const transform = (matrix, ...objects) => {
  16. // TODO how to check that the matrix is REAL?
  17. objects = flatten(objects)
  18. if (objects.length === 0) throw new Error('wrong number of arguments')
  19. const results = objects.map((object) => {
  20. if (path2.isA(object)) return path2.transform(matrix, object)
  21. if (geom2.isA(object)) return geom2.transform(matrix, object)
  22. if (geom3.isA(object)) return geom3.transform(matrix, object)
  23. return object
  24. })
  25. return results.length === 1 ? results[0] : results
  26. }
  27. module.exports = transform