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. Help multiplying QMatrix4x4 * QVector3D

Help multiplying QMatrix4x4 * QVector3D

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 8.4k 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.
  • K Offline
    K Offline
    klox
    wrote on last edited by
    #1

    Hi all,

    I don't really understand the behavior of a QMatrix4x4 * QVector3D. I really just want a usable 3x3 matrix. I've figured out that setting the last element (4,4) to 1.0 basically turns QMatrix4x4 into a 3x3, but why is it that this returns inf:

    @
    QMatrix4x4 A(1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 0.0, 0.0, 0.0);
    QVector3D B(1.0, 2.0, 3.0);

    qDebug() << A*B;@

    returns QVector3D(inf, inf, inf).

    And similarly,
    if (4,4) = 2 the output is QVector3D(1.0, 2.0, 3.0).
    if (4,4) = 2 the output is QVector3D(0.5, 1.0, 1.5).
    if (4,4) = 0.5 the output is QVector3D(2.0, 4.0, 6.0).

    So clearly (4,4) is some kind of inverse length and the last row/column also do some kind of skew or manipulate the geometry somehow.

    Any information on this available?

    Thanks!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      The 4x4 matrix when used with Vector3D in this way represents a compound affine transformation as an augmented matrix. A correct augmented matrix has zeros in the bottom row and right column and 1.0 in the bottom right cell. The input vector is also augmented with an extra 1 to allow matrix multiplication (which would otherwise be impossible).

      The three values in the vector resulting from applying the transform are normalised by the value in the additional cell of the augmented vector result:
      [code]
      w = vector.x() * matrix.m[3][0] +
      vector.y() * matrix.m[3][1] +
      vector.z() * matrix.m[3][2] +
      matrix.m[3][3];
      [/code]
      (from src/gui/math3d/qmatrix4x4.h). In your example this evaluates to 0 and dividing by zero gives you infinite rather than an exception.

      You want QGenericMatrix:
      @
      QGenericMatrix<3,3,qreal> m1;
      int n = 0;
      for (int r = 0; r < 3; ++r)
      for (int c = 0; c < 3; ++c)
      m1(r, c) = ++n;

      QGenericMatrix<1,3,qreal> m2;
      m2(0, 0) = 1.0;
      m2(1, 0) = 3.0;
      m2(2, 0) = 6.0;
      
      
      QGenericMatrix<1,3,qreal> result = m1 * m2;
      for (int r = 0; r < 3; ++r)
          qDebug() << result(r, 0);
      

      @

      1 Reply Last reply
      1
      • K Offline
        K Offline
        klox
        wrote on last edited by
        #3

        Thank you very much. It's much clearer now; I was a bit confused by the GenericMatrix documentation at first. Thanks!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          [quote author="klox" date="1358409388"]I was a bit confused by the GenericMatrix documentation at first.[/quote]

          You are not alone. Took a while for me to realise the column count came before the row count in the template instantiation.

          1 Reply Last reply
          0

          • Login

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