Hi all,

for your reference, here's my current solution to the problem. Can anyone do it better (shorter/faster/nicer and I mean not by removing the comments :-) ?

Qt Code:
  1. Qt3DCore::QEntity * createTransformedPlane(Qt3DCore::QEntity * parent, QVector3D p0, QVector3D p1, QVector3D p3) {
  2.  
  3. // vectors that span the plane
  4. QVector3D v1 = p1-p0;
  5. QVector3D v2 = p3-p0;
  6.  
  7. // create standard mesh in xz plane
  8. Qt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh();
  9. float wx = v1.length();
  10. float wz = v2.length();
  11. planeMesh->setWidth(wx);
  12. planeMesh->setHeight(wz);
  13.  
  14. // Plane entity
  15. Qt3DCore::QEntity * planeEntity = new Qt3DCore::QEntity(parent);
  16. // add the mesh to the entity
  17. planeEntity->addComponent(planeMesh);
  18.  
  19.  
  20. // transformation
  21.  
  22. // compute normal vector
  23. QVector3D n1 = QVector3D::crossProduct(v1,v2);
  24.  
  25. // determine rotation matrix to align normal vector of plane with normal vector of generated plane (i.e. y-axis)
  26. QVector3D n2(0,1,0);
  27.  
  28. QQuaternion rot = QQuaternion::rotationTo(n2, n1);
  29.  
  30. // now we transform the w1 vector (corresponding to the v1 vector) into the target plane
  31. QVector3D w1(-1,0,0);
  32. // Mind: PlaneMesh is generated in x-z plane, with bottom-left point at q0=(w/2, 0, -h/2)
  33. // If numbered counter clock-wise, the next point is at q1=(-w/2, 0, -h/2) and the normalized vector w1 between q0 and q1
  34. // is w1=(-1,0,0)
  35.  
  36. QVector3D w1Rotated = rot.rotatedVector(w1);
  37.  
  38. // now we need to compute the rotation matrix to align the w1 vector with the v1 vector
  39. QQuaternion rot2 = QQuaternion::rotationTo(w1Rotated, v1);
  40.  
  41. // combine both rotations (mind the matrix multiplication order!)
  42. QQuaternion planeRotation = rot2*rot;
  43.  
  44. // anchor point of generated mesh
  45. QVector3D q0(wx/2, 0, -wz/2);
  46. // Point q0 after rotation
  47. QVector3D q0rot = planeRotation.rotatedVector(q0);
  48.  
  49. //translation to target anchor point
  50. QVector3D transVec = p0 - q0rot;
  51.  
  52. // create transformation
  53. Qt3DCore::QTransform *transform = new Qt3DCore::QTransform();
  54. transform->setTranslation(transVec);
  55. transform->setRotation(planeRotation);
  56.  
  57. // and set it as component
  58. planeEntity->addComponent(transform);
  59.  
  60. return planeEntity;
  61. }
To copy to clipboard, switch view to plain text mode