Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?
QtWS25 Last Chance

QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Igor Baidiuk
    wrote on 13 Apr 2021, 10:40 last edited by
    #1

    Hello!

    All docs I've seen about affine transformations say that if one wants to combine two affine matrices A then B, he needs to compute matrix M = BA, not AB. I.e. order of matrix multiplication is inverted.

    Although, methods of QMatrix4x4 like translate, rotate, scale which apply additional transformations to existing matrix perform multiplication left-to-right. As an example, sources of QMatrix4x4::rotate(const QQuaternion&) end with *this *= m; instead of *this = m * (*this).

    So my question is. Does Qt's matrix class implement transformation combination incorrectly? Or am I getting it completely wrong?

    Thanks

    P K 2 Replies Last reply 13 Apr 2021, 11:23
    0
    • I Igor Baidiuk
      13 Apr 2021, 10:40

      Hello!

      All docs I've seen about affine transformations say that if one wants to combine two affine matrices A then B, he needs to compute matrix M = BA, not AB. I.e. order of matrix multiplication is inverted.

      Although, methods of QMatrix4x4 like translate, rotate, scale which apply additional transformations to existing matrix perform multiplication left-to-right. As an example, sources of QMatrix4x4::rotate(const QQuaternion&) end with *this *= m; instead of *this = m * (*this).

      So my question is. Does Qt's matrix class implement transformation combination incorrectly? Or am I getting it completely wrong?

      Thanks

      P Offline
      P Offline
      Pl45m4
      wrote on 13 Apr 2021, 11:23 last edited by
      #2

      @Igor-Baidiuk said in QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?:

      Although, methods of QMatrix4x4 like translate, rotate, scale which apply additional transformations to existing matrix perform multiplication left-to-right. As an example, sources of QMatrix4x4::rotate(const QQuaternion&) end with *this *= m; instead of *this = m * (*this).

      What's wrong about that?

      Assuming A = this and given another B:

      *this *= m is equal to *this = *this * m, so like newA = A * B


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      I 1 Reply Last reply 13 Apr 2021, 11:55
      0
      • P Pl45m4
        13 Apr 2021, 11:23

        @Igor-Baidiuk said in QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?:

        Although, methods of QMatrix4x4 like translate, rotate, scale which apply additional transformations to existing matrix perform multiplication left-to-right. As an example, sources of QMatrix4x4::rotate(const QQuaternion&) end with *this *= m; instead of *this = m * (*this).

        What's wrong about that?

        Assuming A = this and given another B:

        *this *= m is equal to *this = *this * m, so like newA = A * B

        I Offline
        I Offline
        Igor Baidiuk
        wrote on 13 Apr 2021, 11:55 last edited by
        #3

        @Pl45m4 As I noted in my post, the right order of multiplication should be m * (*this). Transformation matrices are combined by multiplying in reverse order.

        O 1 Reply Last reply 13 Apr 2021, 16:34
        0
        • I Igor Baidiuk
          13 Apr 2021, 11:55

          @Pl45m4 As I noted in my post, the right order of multiplication should be m * (*this). Transformation matrices are combined by multiplying in reverse order.

          O Offline
          O Offline
          ofmrew
          wrote on 13 Apr 2021, 16:34 last edited by
          #4

          @Igor-Baidiuk This issue arises because some authors use the (row) vector * matrix notation and others use the matrix * (column) vector form. The matrix in the first case is the inverse of the matrix in the second case.

          O 1 Reply Last reply 13 Apr 2021, 17:02
          0
          • O ofmrew
            13 Apr 2021, 16:34

            @Igor-Baidiuk This issue arises because some authors use the (row) vector * matrix notation and others use the matrix * (column) vector form. The matrix in the first case is the inverse of the matrix in the second case.

            O Offline
            O Offline
            ofmrew
            wrote on 13 Apr 2021, 17:02 last edited by
            #5

            @ofmrew Correction. Change inverse to transpose. Sorry.

            K 1 Reply Last reply 13 Apr 2021, 18:18
            0
            • I Igor Baidiuk
              13 Apr 2021, 10:40

              Hello!

              All docs I've seen about affine transformations say that if one wants to combine two affine matrices A then B, he needs to compute matrix M = BA, not AB. I.e. order of matrix multiplication is inverted.

              Although, methods of QMatrix4x4 like translate, rotate, scale which apply additional transformations to existing matrix perform multiplication left-to-right. As an example, sources of QMatrix4x4::rotate(const QQuaternion&) end with *this *= m; instead of *this = m * (*this).

              So my question is. Does Qt's matrix class implement transformation combination incorrectly? Or am I getting it completely wrong?

              Thanks

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 13 Apr 2021, 18:17 last edited by
              #6

              @Igor-Baidiuk said in QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?:

              All docs I've seen about affine transformations say that if one wants to combine two affine matrices A then B, he needs to compute matrix M = BA, not AB. I.e. order of matrix multiplication is inverted.

              This makes no sense. Firstly I assume you mean affine transformations represented by a matrix; there are no affine matrices as such. And secondly, and much more importantly, there's no such thing as an "inverted" order. Matrix multiplication is non-commutative, so as AB != BA in the general case, there can be defined no "right" and "inverted" order.

              Matrices are operators (or rather representations of linear operators), so they operate to the right (generally speaking).
              The hermitian adjoint of a matrix can be thought of operating on the left, however. So, it is all about the way you structure the matrix and what you're doing, and how you think about it ...

              In the cited source about QMatrix4x4::rotate, did you notice the matrix was constructed as a transposed one, before actually multiplying this from the right?

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2
              • O ofmrew
                13 Apr 2021, 17:02

                @ofmrew Correction. Change inverse to transpose. Sorry.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 13 Apr 2021, 18:18 last edited by
                #7

                @ofmrew said in QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?:

                @ofmrew Correction. Change inverse to transpose. Sorry.

                Inverse is the same as the transpose in this case. Rotation matrices are orthogonal.

                Read and abide by the Qt Code of Conduct

                O 1 Reply Last reply 13 Apr 2021, 19:08
                1
                • K kshegunov
                  13 Apr 2021, 18:18

                  @ofmrew said in QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?:

                  @ofmrew Correction. Change inverse to transpose. Sorry.

                  Inverse is the same as the transpose in this case. Rotation matrices are orthogonal.

                  O Offline
                  O Offline
                  ofmrew
                  wrote on 13 Apr 2021, 19:08 last edited by
                  #8

                  @kshegunov You are missing the point. I am not saying that the order of multiplication does not matter: it does. What I am saying is that the pre- or post-multiplication is dictated by where your vector, whether it is a row or column vector. What you are looking at is referred to as composition. For example, if you want to rotate a position vector you must first translate its center to the origin, then perform the rotation and finally translate it back to its original position. If you have column vectors, the it would be (TbRTo)v, but if the vectors are row vectors it would be v(ToRTb). Note the order change.

                  K 1 Reply Last reply 13 Apr 2021, 21:42
                  0
                  • O ofmrew
                    13 Apr 2021, 19:08

                    @kshegunov You are missing the point. I am not saying that the order of multiplication does not matter: it does. What I am saying is that the pre- or post-multiplication is dictated by where your vector, whether it is a row or column vector. What you are looking at is referred to as composition. For example, if you want to rotate a position vector you must first translate its center to the origin, then perform the rotation and finally translate it back to its original position. If you have column vectors, the it would be (TbRTo)v, but if the vectors are row vectors it would be v(ToRTb). Note the order change.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 13 Apr 2021, 21:42 last edited by kshegunov
                    #9

                    @ofmrew said in QMatrix4x4: Why methods like `translate`, `rotate` etc. perform left-to-right multiplication?:

                    You are missing the point. I am not saying that the order of multiplication does not matter: it does. What I am saying is that the pre- or post-multiplication is dictated by where your vector, whether it is a row or column vector. What you are looking at is referred to as composition.

                    I'm pretty sure I'm not missing the point, I have an idea what I'm talking about being a physicist and all. The point is that the order of the matrices follows the order of operations, which is up to the user.

                    For example, if you want to rotate a position vector you must first translate its center to the origin, then perform the rotation and finally translate it back to its original position.

                    You may (not must) want to do that if you want to rotate in a local coordinate system tied to the mentioned vector. You can, however, rotate as much as you want in any coordinate system you want; you just may not get exactly what you expect, but that's another matter.

                    If you have column vectors, the it would be (TbRTo)v, but if the vectors are row vectors it would be v(ToRTb). Note the order change.

                    The order change is not enough. What you're citing is the adjoint distributive rule. That is: (AB)* = B*A* where * is the hermitian conjugate, or in the field of real numbers it's simply a transposition. This extends to vectors and scalars, for obvious reasons. Your reordering of the matrices is not enough to get the desired result - your Tb from the first case is not the same as the Tb from the second case, more specifically they're transposed to one another.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on 14 Apr 2021, 07:53 last edited by
                      #10

                      If you have a transformation matrix A and a vector x you get the same result if you do A*x or x*(A^T) (A^T meaning A transposed). (If you want to be really precise in distinguishing row- and column-vectors you need to write x^T as well.)

                      Now, if you have two transformation matrices A and B you want to apply successively, then it holds:
                      B*A*x = x*(A^T)*(B^T)

                      The reason why Qt multiplies the matrices in reverse order is because 1) their matrices are the transposed matrices and 2) they multiply the vector from the right as well. Mathematically speaking, the two are equivalent (assuming you construct the right matrix for your transformation): either chain your multiplications to the left or to the right. Just make it consistent and do not mix the two.

                      1 Reply Last reply
                      1

                      1/10

                      13 Apr 2021, 10:40

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved