summaryrefslogtreecommitdiffstats
path: root/plugin/spatialnavigation360.cpp
blob: 12f19eee5edb2c255af685b63c5437b47fd09771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/****************************************************************************
**
** Copyright (C) 2018, 2019 Luxoft Sweden AB. All rights reserved.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the cursor management module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "spatialnavigation360.h"
#include <QQuickItem>
#include "cursornavigationattached.h"
#include <algorithm>
#include <QtMath>

SpatialNavigation360::SpatialNavigation360()
{

}

//test if point is contained in at least one of the given quadrants
bool isPointIncluded(const std::vector<bool> &q, const QPointF &p, const QPointF &o)
{
    return (q[0] && (p.x() > o.x()) && (p.y() > o.y())) ||
           (q[1] && (p.x() < o.x()) && (p.y() > o.y())) ||
           (q[2] && (p.x() < o.x()) && (p.y() < o.y())) ||
           (q[3] && (p.x() > o.x()) && (p.y() < o.y()));
}

//test if rect is contained in at least one of the given quadrants
bool isRectIncluded(const std::vector<bool> &q, const QRectF &rect, const QPointF &origin)
{
    return  !rect.contains(origin) &&
            (isPointIncluded(q, rect.topLeft(), origin) ||
            isPointIncluded(q, rect.bottomRight(), origin));
}

//test if angle is between start and end angles
bool angleIsBetween(qreal angle, qreal begin, qreal end)
{
    if (begin > end)
        return angle >= begin || angle <= end;
    else
        return angle >= begin && angle <= end;
}

//test if 2 sectors overlap
bool sectorsOverlap(qreal begin1, qreal end1,
                    qreal begin2, qreal end2)
{
    return angleIsBetween(begin1, begin2, end2) ||
           angleIsBetween(end1, begin2, end2) ||
           angleIsBetween(begin2, begin1, end1) ||
           angleIsBetween(end2, begin1, end1);
}

qreal pointDistance(const QPointF& p1, const QPointF& p2)
{
    qreal dx=p1.x()-p2.x();
    qreal dy=p1.y()-p2.y();
    return qSqrt(qPow(dx,2)+qPow(dy,2));
}

//minimum distance between 2 rects, calculated from their edges
qreal rectDistance(const QRectF& rect1, const QRectF& rect2)
{
    bool left   = rect2.bottomRight().x() < rect1.topLeft().x();
    bool right  = rect2.topLeft().x() > rect1.bottomRight().x();
    bool bottom = rect2.topLeft().y() > rect1.bottomRight().y();
    bool top    = rect2.bottomRight().y() < rect1.topLeft().y();

    if (top && left)
        return pointDistance(rect1.topLeft(), rect2.bottomRight());
    else if (left && bottom)
        return pointDistance(rect1.bottomLeft(), rect2.topRight());
    else if (bottom && right)
        return pointDistance(rect1.bottomRight(), rect2.topLeft());
    else if (right && top)
        return pointDistance(rect1.topRight(), rect2.bottomRight());
    else if (left)
        return rect1.x() - rect2.bottomRight().x();
    else if (right)
        return rect2.x() - rect1.bottomRight().x();
    else if (bottom)
        return rect2.y() - rect1.bottomRight().y();
    else if (top)
        return rect1.y() - rect2.bottomRight().y();//y2 - y1b
    else //rectangles overlap
        return 0;
}

//get the widest arc that is less than 180 degrees, that this item covers, clockwise around the origin
std::pair<qreal, qreal> getSector(const QRectF &rect, const QPointF &origin)
{
    std::pair<qreal, qreal> angles;
    angles = std::minmax({std::atan2(rect.topLeft().y()-origin.y(), rect.topLeft().x()-origin.x()),
                          std::atan2(rect.topRight().y()-origin.y(), rect.topRight().x()-origin.x()),
                          std::atan2(rect.bottomRight().y()-origin.y(), rect.bottomRight().x()-origin.x()),
                          std::atan2(rect.bottomLeft().y()-origin.y(), rect.bottomLeft().x()-origin.x())
                         });

    //if delta larger than 180, invert min and max
    if (angles.second-angles.first > M_PI) {
        qreal temp = angles.first;
        angles.first = angles.second;
        angles.second = temp;
    }

    return angles;
}

CursorNavigationAttached* SpatialNavigation360::getNextCandidate(
                        const QList<CursorNavigationAttached*> &candidates,
                        const CursorNavigationAttached *currentItem,
                        const CursorNavigationCommand& cmd)
{
    /* -angle should be between -180 and 180 in relation to the x axis, clockwise around the origin
     * -depending on the input angle, find min and max x and y values which items must have
     * -calculate angles for items points that are within the limits. from those angles, pick min and max (widest sector the item covers)
     * -items that overlap the current item center, should be ignored
     * -items that are within seaqrch beams angle limits, are stored along the angle ranges they have
     * -if there are more than 1 stored items, we select the item that cuts the exact selection vector.
     *  if no item overlaps the center, pick the one closest to the current item
     * -remember to use current item's coord system as the reference!!!
     */

    //qWarning() << "##### navigation360: start, angle = " << cmd.angle << " tolerance = " << cmd.angleTolerance;

    if (candidates.isEmpty())
        return nullptr;

    if (!currentItem && candidates.size()) {
        return candidates.first();
    }

    //booleans for quadrants
    std::vector<bool> quadrants(4);

    //define selector beam sector
    qreal angle1, angle2;

    angle1 = CursorNavigationCommand::fitAngle(cmd.angle - cmd.angleTolerance);
    angle2 = CursorNavigationCommand::fitAngle(cmd.angle + cmd.angleTolerance);

    quadrants[0] = sectorsOverlap(angle1, angle2, 0, M_PI_2);
    quadrants[1] = sectorsOverlap(angle1, angle2, M_PI_2, M_PI);
    quadrants[2] = sectorsOverlap(angle1, angle2, -M_PI, -M_PI_2);
    quadrants[3] = sectorsOverlap(angle1, angle2, -M_PI_2, 0);

    const QRectF currentItemSceneRect = currentItem->item()->mapRectToScene(
                QRectF( 0, 0, currentItem->item()->width(), currentItem->item()->height() ));

    const QPointF origin = currentItemSceneRect.center();

    //item that overlaps the center of the selector beam
    CursorNavigationAttached* directHitItem = nullptr;
    qreal directHitDistance = -1;
    //item that overlaps selector beam, but does not overlap with the center
    CursorNavigationAttached* withinToleranceItem = nullptr;
    qreal withinToleranceDistance = -1;


    for (auto iter: candidates)
    {
        if (!iter->available() || iter == currentItem)
            continue;

        const QRectF itemSceneRect = iter->item()->mapRectToScene(
                    QRectF( 0, 0, iter->item()->width(), iter->item()->height() ));

        if (isRectIncluded(quadrants, itemSceneRect, origin)) {

            std::pair<qreal,qreal> sector = getSector(itemSceneRect, origin);

            if (angleIsBetween(cmd.angle, sector.first, sector.second)) {
                qreal dist = rectDistance(itemSceneRect, currentItemSceneRect);
                if (!directHitItem || dist < directHitDistance) {
                    directHitDistance = dist;
                    directHitItem = iter;
                }
            } else if (!directHitItem && sectorsOverlap(angle1, angle2, sector.first, sector.second)) {
                qreal dist = rectDistance(itemSceneRect, currentItemSceneRect);
                if (!withinToleranceItem || dist < withinToleranceDistance) {
                    withinToleranceDistance = dist;
                    withinToleranceItem = iter;
                }
            }
        }
    }

    //qWarning() << "##### end, directHit = " <<
      //            directHitItem << "  withinTolerances = " << withinToleranceItem;

    if (directHitItem)
        return directHitItem;

    return withinToleranceItem;

}