modeling/src/maths/mat4/rightMultiplyVec2.js

  1. const fromValues = require('../vec2/fromValues')
  2. /**
  3. * Multiply a 2D vector by a matrix (interpreted as 2 row, 1 column).
  4. *
  5. * Calculation: result = v*M, where the fourth element is set to 1.
  6. * @param {vec2} vector - input vector
  7. * @param {mat4} matrix - input matrix
  8. * @returns {vec2} a new vector
  9. * @alias module:modeling/maths/mat4.rightMultiplyVec2
  10. */
  11. const rightMultiplyVec2 = (vector, matrix) => {
  12. const [v0, v1] = vector
  13. const v2 = 0
  14. const v3 = 1
  15. let x = v0 * matrix[0] + v1 * matrix[1] + v2 * matrix[2] + v3 * matrix[3]
  16. let y = v0 * matrix[4] + v1 * matrix[5] + v2 * matrix[6] + v3 * matrix[7]
  17. const w = v0 * matrix[12] + v1 * matrix[13] + v2 * matrix[14] + v3 * matrix[15]
  18. // scale such that fourth element becomes 1:
  19. if (w !== 1) {
  20. const invw = 1.0 / w
  21. x *= invw
  22. y *= invw
  23. }
  24. return fromValues(x, y)
  25. }
  26. module.exports = rightMultiplyVec2