const flatten = require('../../utils/flatten')
const mat4 = require('../../maths/mat4')
const geom2 = require('../../geometries/geom2')
const geom3 = require('../../geometries/geom3')
const path2 = require('../../geometries/path2')
/**
* Translate the given objects using the given options.
* @param {Array} offset - offset (vector) of which to translate the objects
* @param {...Object} objects - the objects to translate
* @return {Object|Array} the translated object, or a list of translated objects
* @alias module:modeling/transforms.translate
*
* @example
* const newsphere = translate([5, 0, 10], sphere())
*/
const translate = (offset, ...objects) => {
if (!Array.isArray(offset)) throw new Error('offset must be an array')
objects = flatten(objects)
if (objects.length === 0) throw new Error('wrong number of arguments')
// adjust the offset if necessary
offset = offset.slice() // don't modify the original
while (offset.length < 3) offset.push(0)
const matrix = mat4.fromTranslation(mat4.create(), offset)
const results = objects.map((object) => {
if (path2.isA(object)) return path2.transform(matrix, object)
if (geom2.isA(object)) return geom2.transform(matrix, object)
if (geom3.isA(object)) return geom3.transform(matrix, object)
return object
})
return results.length === 1 ? results[0] : results
}
/**
* Translate the given objects along the X axis using the given options.
* @param {Number} offset - X offset of which to translate the objects
* @param {...Object} objects - the objects to translate
* @return {Object|Array} the translated object, or a list of translated objects
* @alias module:modeling/transforms.translateX
*/
const translateX = (offset, ...objects) => translate([offset, 0, 0], objects)
/**
* Translate the given objects along the Y axis using the given options.
* @param {Number} offset - Y offset of which to translate the geometries
* @param {...Object} objects - the objects to translate
* @return {Object|Array} the translated object, or a list of translated objects
* @alias module:modeling/transforms.translateY
*/
const translateY = (offset, ...objects) => translate([0, offset, 0], objects)
/**
* Translate the given objects along the Z axis using the given options.
* @param {Number} offset - Z offset of which to translate the geometries
* @param {...Object} objects - the objects to translate
* @return {Object|Array} the translated object, or a list of translated objects
* @alias module:modeling/transforms.translateZ
*/
const translateZ = (offset, ...objects) => translate([0, 0, offset], objects)
module.exports = {
translate,
translateX,
translateY,
translateZ
}