modeling/src/operations/transforms/translate.js

  1. const flatten = require('../../utils/flatten')
  2. const mat4 = require('../../maths/mat4')
  3. const geom2 = require('../../geometries/geom2')
  4. const geom3 = require('../../geometries/geom3')
  5. const path2 = require('../../geometries/path2')
  6. /**
  7. * Translate the given objects using the given options.
  8. * @param {Array} offset - offset (vector) of which to translate the objects
  9. * @param {...Object} objects - the objects to translate
  10. * @return {Object|Array} the translated object, or a list of translated objects
  11. * @alias module:modeling/transforms.translate
  12. *
  13. * @example
  14. * const newsphere = translate([5, 0, 10], sphere())
  15. */
  16. const translate = (offset, ...objects) => {
  17. if (!Array.isArray(offset)) throw new Error('offset must be an array')
  18. objects = flatten(objects)
  19. if (objects.length === 0) throw new Error('wrong number of arguments')
  20. // adjust the offset if necessary
  21. offset = offset.slice() // don't modify the original
  22. while (offset.length < 3) offset.push(0)
  23. const matrix = mat4.fromTranslation(mat4.create(), offset)
  24. const results = objects.map((object) => {
  25. if (path2.isA(object)) return path2.transform(matrix, object)
  26. if (geom2.isA(object)) return geom2.transform(matrix, object)
  27. if (geom3.isA(object)) return geom3.transform(matrix, object)
  28. return object
  29. })
  30. return results.length === 1 ? results[0] : results
  31. }
  32. /**
  33. * Translate the given objects along the X axis using the given options.
  34. * @param {Number} offset - X offset of which to translate the objects
  35. * @param {...Object} objects - the objects to translate
  36. * @return {Object|Array} the translated object, or a list of translated objects
  37. * @alias module:modeling/transforms.translateX
  38. */
  39. const translateX = (offset, ...objects) => translate([offset, 0, 0], objects)
  40. /**
  41. * Translate the given objects along the Y axis using the given options.
  42. * @param {Number} offset - Y offset of which to translate the geometries
  43. * @param {...Object} objects - the objects to translate
  44. * @return {Object|Array} the translated object, or a list of translated objects
  45. * @alias module:modeling/transforms.translateY
  46. */
  47. const translateY = (offset, ...objects) => translate([0, offset, 0], objects)
  48. /**
  49. * Translate the given objects along the Z axis using the given options.
  50. * @param {Number} offset - Z offset of which to translate the geometries
  51. * @param {...Object} objects - the objects to translate
  52. * @return {Object|Array} the translated object, or a list of translated objects
  53. * @alias module:modeling/transforms.translateZ
  54. */
  55. const translateZ = (offset, ...objects) => translate([0, 0, offset], objects)
  56. module.exports = {
  57. translate,
  58. translateX,
  59. translateY,
  60. translateZ
  61. }