-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAnnotationStore.js
192 lines (159 loc) · 5.72 KB
/
AnnotationStore.js
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
import RBush from 'rbush';
import { SVG_NAMESPACE } from '@recogito/annotorious/src/util/SVG';
import { drawShape, shapeArea, svgFragmentToShape, parseRectFragment } from '@recogito/annotorious/src/selectors';
import { WebAnnotation } from '@recogito/recogito-client-core';
import {
pointInCircle,
pointInEllipse,
pointInPolygon,
svgPathToPolygons,
pointInLine
} from '@recogito/annotorious/src/util/Geom2D';
/**
* Computes the bounding box of an annotation. WARNING:
* this is an expensive operation which parses the annotation,
* creates a temporary SVG element and attaches it to the DOM,
* uses .getBBox() and then removes the temporary SVG element.
*/
const getBounds = (annotation, image) => {
const isBox = annotation.targets[0].selector.type === 'FragmentSelector';
if (isBox) {
const {x,y,w,h} = parseRectFragment(annotation, image);
return {
minX: x,
minY: y,
maxX: x + w,
maxY: y + h
};
} else {
const shape = drawShape(annotation, image);
// A temporary SVG buffer, so we can use .getBBox()
const svg = document.createElementNS(SVG_NAMESPACE, 'svg');
svg.style.position = 'absolute';
svg.style.opacity = 0;
svg.style.top = 0;
svg.style.left = 0;
svg.appendChild(shape);
document.body.appendChild(svg);
const { x, y, width, height } = shape.getBBox();
document.body.removeChild(svg);
return {
minX: x,
minY: y,
maxX: x + width,
maxY: y + height
};
}
}
const getSelectorType = annotation => {
const firstTarget = annotation.targets[0];
return Array.isArray(firstTarget.selector) ?
firstTarget.selector[0].type : firstTarget.selector?.type;
}
/**
* Checks if a point is inside the annotation shape.
* WARNING: this is only for internal use ONLY. It pre-assumes
* an annotation with an SVGSelector (not a FragmentSelector)!
* @param {number} x point x coordinate
* @param {number} y point y coordinate
* @param {WebAnnotation} annotation annotation (with an SVG Selector!)
*/
const pointInSVGShape = (x, y, annotation, buffer) => {
const svg = svgFragmentToShape(annotation);
const nodeName = svg.nodeName.toLowerCase();
const pt = [x, y];
if (nodeName === 'polygon') {
const points = Array.from(svg.points).map(pt => [pt.x, pt.y]);
return pointInPolygon(pt, points);
} else if (nodeName === 'circle') {
const cx = svg.getAttribute('cx');
const cy = svg.getAttribute('cy');
const r = svg.getAttribute('r');
return pointInCircle(pt, cx, cy, r);
} else if (nodeName === 'ellipse') {
const cx = svg.getAttribute('cx');
const cy = svg.getAttribute('cy');
const rx = svg.getAttribute('rx');
const ry = svg.getAttribute('ry');
return pointInEllipse(pt, cx, cy, rx, ry);
} else if (nodeName === 'path') {
const polygons = svgPathToPolygons(svg);
return polygons.find(polygon => pointInPolygon(pt, polygon));
} else if (nodeName === 'line') {
const x1 = parseInt(svg.getAttribute('x1'));
const y1 = parseInt(svg.getAttribute('y1'));
const x2 = parseInt(svg.getAttribute('x2'));
const y2 = parseInt(svg.getAttribute('y2'));
return pointInLine(pt, x1, y1, x2, y2, buffer);
} else {
throw `Unsupported SVG shape type: ${nodeName}`;
}
}
export default class AnnotationStore {
constructor(env) {
this.env = env;
// Hacky... the store exposes itself to the environment, so that tools
// have access to it
env.store = this;
this.spatial_index = new RBush();
}
clear = () =>
this.spatial_index.clear();
getAnnotationAt = (x, y, scale) => {
// 5 pixel buffer, so we reliably catch point
// annotations (optionally with scale applied)
const buffer = scale ? 5 / scale : 5;
// Fast hit test in index (bounds only!)
const idxHits = this.spatial_index.search({
minX: x - buffer,
minY: y - buffer,
maxX: x + buffer,
maxY: y + buffer
}).map(item => item.annotation);
// Exact hit test on shape (needed for SVG fragments only!)
const exactHits = idxHits.filter(annotation => {
const selectorType = getSelectorType(annotation);
if (selectorType === 'FragmentSelector') {
return true; // For FragmentSelectors, shape is always equal to bounds!
} else if (selectorType === 'SvgSelector') {
return pointInSVGShape(x, y, annotation, buffer);
} else {
throw `Unsupported selector type: ${selectorType}`;
}
});
// Get smallest annotation
if (exactHits.length > 0) {
exactHits.sort((a, b) => shapeArea(a, this.env.image) - shapeArea(b, this.env.image));
return exactHits[0];
}
}
getAnnotationsIntersecting = annotationOrBounds => {
const isBounds = annotationOrBounds.minX; // Bit of a naive test...
const bounds = isBounds ? annotationOrBounds : getBounds(annotationOrBounds, this.env.image);
const intersecting = this.spatial_index.search(bounds).map(item => item.annotation);
return isBounds ? intersecting :
intersecting.filter(a => !a.isEqual(annotationOrBounds));
}
insert = arg => {
const annotations = Array.isArray(arg) ? arg : [ arg ];
annotations.forEach(annotation => {
this.spatial_index.insert({
...getBounds(annotation, this.env.image), annotation
})
});
}
getBounds = annotation => {
return this.getBounds(annotation, this.env.image);
}
remove = annotation => {
// Unfortunately, .remove currently requires bounds,
// therefore we need to re-compute. See:
// https://github.com/mourner/rbush/issues/95
const item = {
...getBounds(annotation, this.env.image),
annotation
};
this.spatial_index.remove(item, (a, b) =>
a.annotation.id === b.annotation.id);
}
}