# [Interest] Rotating leaves

Igor Mironchik igor.mironchik at gmail.com
Sun Apr 1 12:56:04 CEST 2018

```Hi,

Sure, I know this...

const QVector3D branch(...);
const QVector3D leaf( 0.0f, 1.0f, 0.0f );
const QVector3D axis = QVector3D::crossProduct( branch, leaf );
const float cosPlainAngle = QVector3D::dotProduct( branch, leaf );
const float plainAngle = qRadiansToDegrees( std::acos( cosPlainAngle ) );
const QQuaternion quat = Qt3DCore::QTransform::fromAxisAndAngle( axis,
plainAngle );
m_transform->setRotation( quat );

But in a view of Qt 3D this is only a half of the solution. In a half of
cases this works, but in another cases I need -plainAngle.

So at this point I found the next solution:

static inline bool lessZero( const QVector3D & v )
{
return ( v.x() < 0.0f || v.y() < 0.0f || v.z() < 0.0f );
}

if( lessZero( branch ) )
plainAngle = -plainAngle;

So I actually asked not for the math as it is but for checking of my
solution for correctness.

On 01.04.2018 12:48, Konstantin Shegunov wrote:
> Hi Igor,
> What Bin Chen wrote is probably the most painless way of achieving
> what you want. If you are however interested in the math, here goes my
> stab:
> If I understand you correctly, you know the leaf normal, and the
> branch direction vector, then you're searching for the matrix that
> transforms the former to the latter.
> Basically you need to find the matrix that satisfies: b = A  * n (b is
> the branch direction, n is the leaf normal).
> This equation however is underdetemined, meaning you can have several
> rotations done in sequence that give you the same result, so you'd
> need to do some "trickery". One of the usual ways to solve such a
> problem is to use Euler angles[1], where the idea is to make elemental
> rotations with respect to the principle axes of the (global)
> coordinate systems. To that end you'd need to calculate the
> projections (i.e. dot products) of b and n to the principal axes and
> extract the angles of rotation from there, then construct each
> rotation matrix around a principal axis of the coordinate system and
> finally multiply them to obtain the final transformation.
>
> [1]: https://en.wikipedia.org/wiki/Euler_angles
> <https://en.wikipedia.org/wiki/Euler_angles>
>
> I hope that helps.
> Konstantin.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180401/52a45034/attachment.html>
```