Wrong value transposing matrix itself

Version: 6.27.1 (latest when making post)
Browser: Chrome and Firefox
OS: Windows 10

Playground:

The transposeFromThreeJs is a port of three.js Matrix4.transpose here
There is nothing in docs in case of transposing matrix itself.

Expected:
Getting the exactly same result of matrix.transposeToRef and transposeFromThreeJs.

Actual:

matrix.transposeToRef  [
  1,
  5,
  9,
  13,
  5,
  6,
  10,
  14,
  9,
  10,
  11,
  15,
  13,
  14,
  15,
  16
]
transposeFromThreeJs  [
  1,
  5,
  9,
  13,
  2,
  6,
  10,
  14,
  3,
  7,
  11,
  15,
  4,
  8,
  12,
  16
]

Hello!

Calling transposeToRef with the matrix itself as argument will lead to incorrect behavior, as you’re storing the transposed values on the same matrix you’re transposing. The expected use is to call transposeToRef with a different matrix, I just created one here and they have the same result: matrix transpose self | Babylon.js Playground (babylonjs.com)

1 Like

It’s a waste of memory creating Matrix forced by the internal impl of transposeToRef. And there is nothing in the docs saying that it’s forbidden to apply transposeToRef on the matrix itself.
If it is not accepted to fix this in transposeToRef, a transposeInplace method could be added as an alternative, with docs about how to choose which one to use.

Agree it is a bug, do you want to make a PR to fix transposeToRef ?

PR is here Allow matrix transposeToRef to work in place by sebavan · Pull Request #14485 · BabylonJS/Babylon.js · GitHub

2 Likes