modeling/src/maths/plane/transform.js

  1. const mat4 = require('../mat4')
  2. const vec3 = require('../vec3')
  3. const fromPoints = require('./fromPoints')
  4. const flip = require('./flip')
  5. /**
  6. * Transform the given plane using the given matrix
  7. *
  8. * @param {plane} out - receiving plane
  9. * @param {plane} plane - plane to transform
  10. * @param {mat4} matrix - matrix to transform with
  11. * @return {plane} out
  12. * @alias module:modeling/maths/plane.transform
  13. */
  14. const transform = (out, plane, matrix) => {
  15. const ismirror = mat4.isMirroring(matrix)
  16. // get two vectors in the plane:
  17. const r = vec3.orthogonal(vec3.create(), plane)
  18. const u = vec3.cross(r, plane, r)
  19. const v = vec3.cross(vec3.create(), plane, u)
  20. // get 3 points in the plane:
  21. let point1 = vec3.fromScalar(vec3.create(), plane[3])
  22. vec3.multiply(point1, point1, plane)
  23. let point2 = vec3.add(vec3.create(), point1, u)
  24. let point3 = vec3.add(vec3.create(), point1, v)
  25. // transform the points:
  26. point1 = vec3.transform(point1, point1, matrix)
  27. point2 = vec3.transform(point2, point2, matrix)
  28. point3 = vec3.transform(point3, point3, matrix)
  29. // and create a new plane from the transformed points:
  30. fromPoints(out, point1, point2, point3)
  31. if (ismirror) {
  32. // the transform is mirroring so flip the plane
  33. flip(out, out)
  34. }
  35. return out
  36. }
  37. module.exports = transform