2
* Copyright 2016 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by Florian Boucault <florian.boucault@canonical.com>
22
id: segmentBoundingBoxes
24
property string source
25
onSourceChanged: parseBoundingBoxes(source)
27
property var boundingBoxes: []
30
property int count: boundingBoxes.length
32
// The API cannot be used reliably before this signal has been emitted.
35
function parseBoundingBoxes(source) {
36
var xhr = new XMLHttpRequest;
37
xhr.open("GET", source);
38
xhr.onreadystatechange = function() {
39
if (xhr.readyState == XMLHttpRequest.DONE) {
41
var json = JSON.parse(xhr.responseText);
42
boundingBoxes = json["boxes"];
43
width = json["width"];
44
height = json["height"];
51
function intersects(box1, box2) {
55
var x12 = box1[0] + box1[2];
56
var y12 = box1[1] + box1[3];
59
var x22 = box2[0] + box2[2];
60
var y22 = box2[1] + box2[3];
61
var x_overlap = Math.max(0, Math.min(x12,x22) - Math.max(x11,x21));
62
var y_overlap = Math.max(0, Math.min(y12,y22) - Math.max(y11,y21));
63
return (x_overlap / Math.min(box1[2], box2[2]) > 0.25
64
&& y_overlap / Math.min(box1[3], box2[3]) > 0.25);
67
function computeIntersections(hitBox) {
68
var absoluteHitBox = [hitBox[0] * width, hitBox[1] * height,
69
hitBox[2] * width, hitBox[3] * height];
71
var intersections = [];
72
for (var i in boundingBoxes) {
73
var boundingBox = boundingBoxes[i];
74
if (intersects(absoluteHitBox, boundingBox)) {
75
intersections.push(i);