Changeset View
Changeset View
Standalone View
Standalone View
src/assets/keyframes/model/rotoscoping/bpoint.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2011 by Till Theato (root@ttill.de) * | ||||
3 | * This file is part of Kdenlive (www.kdenlive.org). * | ||||
4 | * * | ||||
5 | * Kdenlive is free software: you can redistribute it and/or modify * | ||||
6 | * it under the terms of the GNU General Public License as published by * | ||||
7 | * the Free Software Foundation, either version 2 of the License, or * | ||||
8 | * (at your option) any later version. * | ||||
9 | * * | ||||
10 | * Kdenlive is distributed in the hope that it will be useful, * | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
13 | * GNU General Public License for more details. * | ||||
14 | * * | ||||
15 | * You should have received a copy of the GNU General Public License * | ||||
16 | * along with Kdenlive. If not, see <http://www.gnu.org/licenses/>. * | ||||
17 | ***************************************************************************/ | ||||
18 | | ||||
19 | #include "bpoint.h" | ||||
20 | | ||||
21 | #include <QLineF> | ||||
22 | | ||||
23 | BPoint::BPoint() | ||||
24 | : h1(-1, -1) | ||||
25 | , p(-1, -1) | ||||
26 | , h2(-1, -1) | ||||
27 | , handlesLinked(true) | ||||
28 | { | ||||
29 | } | ||||
30 | | ||||
31 | BPoint::BPoint(const QPointF &handle1, const QPointF &point, const QPointF &handle2) | ||||
32 | : h1(handle1) | ||||
33 | , p(point) | ||||
34 | , h2(handle2) | ||||
35 | { | ||||
36 | autoSetLinked(); | ||||
37 | } | ||||
38 | | ||||
39 | QPointF &BPoint::operator[](int i) | ||||
40 | { | ||||
41 | return i == 0 ? h1 : (i == 1 ? p : h2); | ||||
42 | } | ||||
43 | | ||||
44 | const QPointF &BPoint::operator[](int i) const | ||||
45 | { | ||||
46 | return i == 0 ? h1 : (i == 1 ? p : h2); | ||||
47 | } | ||||
48 | | ||||
49 | bool BPoint::operator==(const BPoint &point) const | ||||
50 | { | ||||
51 | return point.h1 == h1 && point.p == p && point.h2 == h2; | ||||
52 | } | ||||
53 | | ||||
54 | void BPoint::setP(const QPointF &point, bool updateHandles) | ||||
55 | { | ||||
56 | QPointF offset = point - p; | ||||
57 | p = point; | ||||
58 | if (updateHandles) { | ||||
59 | h1 += offset; | ||||
60 | h2 += offset; | ||||
61 | } | ||||
62 | } | ||||
63 | | ||||
64 | void BPoint::setH1(const QPointF &handle1) | ||||
65 | { | ||||
66 | h1 = handle1; | ||||
67 | if (handlesLinked) { | ||||
68 | qreal angle = QLineF(h1, p).angle(); | ||||
69 | QLineF l = QLineF(p, h2); | ||||
70 | l.setAngle(angle); | ||||
71 | h2 = l.p2(); | ||||
72 | } | ||||
73 | } | ||||
74 | | ||||
75 | void BPoint::setH2(const QPointF &handle2) | ||||
76 | { | ||||
77 | h2 = handle2; | ||||
78 | if (handlesLinked) { | ||||
79 | qreal angle = QLineF(h2, p).angle(); | ||||
80 | QLineF l = QLineF(p, h1); | ||||
81 | l.setAngle(angle); | ||||
82 | h1 = l.p2(); | ||||
83 | } | ||||
84 | } | ||||
85 | | ||||
86 | void BPoint::autoSetLinked() | ||||
87 | { | ||||
88 | // sometimes the angle is returned as 360° | ||||
89 | // due to rounding problems the angle is sometimes not quite 0 | ||||
90 | qreal angle = QLineF(h1, p).angleTo(QLineF(p, h2)); | ||||
91 | handlesLinked = angle < 1e-3 || qRound(angle) == 360; | ||||
92 | } | ||||
93 | | ||||
94 | void BPoint::setHandlesLinked(bool linked) | ||||
95 | { | ||||
96 | handlesLinked = linked; | ||||
97 | if (linked) { | ||||
98 | // we force recomputing one of the handles | ||||
99 | setH1(h1); | ||||
100 | } | ||||
101 | } |