modeling/src/geometries/geom2/transform.js

  1. const mat4 = require('../../maths/mat4')
  2. const reverse = require('./reverse.js')
  3. /**
  4. * Transform the given geometry using the given matrix.
  5. * This is a lazy transform of the sides, as this function only adjusts the transforms.
  6. * The transforms are applied when accessing the sides via toSides().
  7. * @param {mat4} matrix - the matrix to transform with
  8. * @param {geom2} geometry - the geometry to transform
  9. * @returns {geom2} a new geometry
  10. * @alias module:modeling/geometries/geom2.transform
  11. *
  12. * @example
  13. * let newgeometry = transform(fromZRotation(degToRad(90)), geometry)
  14. */
  15. const transform = (matrix, geometry) => {
  16. const transforms = mat4.multiply(mat4.create(), matrix, geometry.transforms)
  17. const transformed = Object.assign({}, geometry, { transforms })
  18. // determine if the transform is mirroring in 2D
  19. if (matrix[0] * matrix[5] - matrix[4] * matrix[1] < 0) {
  20. // reverse the order to preserve the orientation
  21. return reverse(transformed)
  22. }
  23. return transformed
  24. }
  25. module.exports = transform