Warning, file /graphics/glaxnimate/src/core/model/transform.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "transform.hpp"
0008 #include "math/math.hpp"
0009 
0010 namespace {
0011 
0012 QTransform make_transform(
0013     const QPointF& anchor_point,
0014     const QPointF& position,
0015     double rotation,
0016     QVector2D scale,
0017     const std::optional<QPointF>& pos_derivative
0018 )
0019 {
0020     QTransform trans;
0021     trans.translate(position.x(), position.y());
0022     trans.rotate(rotation);
0023     trans.scale(scale.x(), scale.y());
0024     trans.translate(-anchor_point.x(), -anchor_point.y());
0025     if ( pos_derivative )
0026         trans.rotate(glaxnimate::math::rad2deg(glaxnimate::math::atan2(pos_derivative->y(), pos_derivative->x())));
0027     return trans;
0028 }
0029 
0030 } // namespace
0031 
0032 GLAXNIMATE_OBJECT_IMPL(glaxnimate::model::Transform)
0033 
0034 QTransform glaxnimate::model::Transform::transform_matrix(FrameTime f, bool auto_orient) const
0035 {
0036     std::optional<QPointF> pos_derivative;
0037     if ( auto_orient )
0038         pos_derivative = position.derivative_at(f);
0039 
0040     return make_transform(
0041         anchor_point.get_at(f),
0042         position.get_at(f),
0043         rotation.get_at(f),
0044         scale.get_at(f),
0045         pos_derivative
0046     );
0047 }
0048 
0049 void glaxnimate::model::Transform::set_transform_matrix(const QTransform& t)
0050 {
0051     qreal a = t.m11();
0052     qreal b = t.m12();
0053     qreal c = t.m21();
0054     qreal d = t.m22();
0055     qreal tx = t.m31();
0056     qreal ty = t.m32();
0057 
0058     position.set(QPointF(tx, ty));
0059     qreal delta = a * d - b * c;
0060     qreal sx = 1;
0061     qreal sy = 1;
0062     if ( a != 0 || b != 0 )
0063     {
0064         qreal r = math::hypot(a, b);
0065         rotation.set(-math::rad2deg(-math::sign(b) * math::acos(a/r)));
0066         sx = r;
0067         sy = delta / r;
0068     }
0069     else
0070     {
0071         qreal r = math::hypot(c, d);
0072         rotation.set(-math::rad2deg(math::pi / 2 + math::sign(d) * math::acos(c / r)));
0073         sx = delta / r;
0074         sy = r;
0075     }
0076 
0077     scale.set(QVector2D(sx, sy));
0078 }
0079 
0080 void glaxnimate::model::Transform::copy(glaxnimate::model::Transform* other)
0081 {
0082     other->clone_into(this);
0083 }