2

On Qt3D, an entity can have these two components:

  1. Qt3DRender::QGeometryRenderer component which contains the mesh geometry data like vertex positions and normals.
  2. Qt3DCore::QTransform component which contains the rotations, translations and scalings.

I intend to export the mesh data as STL with all the transformations applied to mesh geometry. I mean, all rotations and translations need to be applied to vertex positions...

When I access the mesh geometry data, like below, the transformations are not applied to geometry.

mesh->geometry()->attributes().at(i)->buffer()->data();

How can I apply the transformations to the geometry?


The Qt3DCore::QTransform component gives me a 4x4 Matrix, but my vertex positions are 3x1, I don't know how to apply this 4x4 matrix into my 3x1 vertex positions.

Megidd
  • 7,089
  • 6
  • 65
  • 142
  • 3
    A faster solution than iterating over all vertices yourself and applying Mariam's solution could be to stack all vertices in the columns of a `QGenericMatrix` (i.e. [v1|v2|v3|...]) and then multiply your transformation from the left side: transform * vertMatrix. This should result in a matrix where each column is the transformed vertex. The guys at Qt probably implemented a faster matrix multiplication algorithm than simple iteration. – Florian Blume Aug 23 '18 at 10:13
  • @FlorianBlume Right, I'm going to try your solution. Thanks ☺ – Megidd Aug 23 '18 at 10:58

1 Answers1

3

From the math of transformations, to transform a vector with a 4x4 transformation matrix, everything has to be in homogeneous coordinates. You do this by adding a fourth component to the vector and setting it to one and then just multiply it with the (already homogeneous) 4x4 matrix. so:

QVector3D oldVec = QVector3D(x,y,z);     //this is your 3x1 vertex position
QVector4D newVec = QVector4D(oldVec.x(), oldVec.y(), oldVec.z(), 1);    //added 1 as fourth component
QVector4D transformedVec = matrix*newVec;    //matrix is your 4x4 transformation matrix
//transformedVec is now what you need but with the fourth component still being the 1, so we just leave it out:
QVector3D transformedVec3D = QVector3D(transformedVec.x(), transformedVec.y(), transformedVec.z());

To read more about the math behind it, you can check out this link.

Mariam
  • 342
  • 3
  • 18
  • 2
    I see I've got some competition now answering Qt3D questions, very nice! Even less agony for the rest trying to use it :) But maybe mention the term [homogeneous coordinates](https://en.wikipedia.org/wiki/Homogeneous_coordinates#Use_in_computer_graphics_and_computer_vision), that's what the vertices have to be in but are not when represented by a `3x1` vector. – Florian Blume Aug 23 '18 at 07:35
  • haha I'm trying to give something back as well :) and yes thanks for the addition! – Mariam Aug 23 '18 at 07:44