This repository has been archived by the owner on Apr 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
show-roi.coffee
57 lines (43 loc) · 1.9 KB
/
show-roi.coffee
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
$ = window.jQuery
project = require 'zooniverse-readymade/current-project'
classifyPage = project.classifyPages[0]
rois = $.get('./roi.tsv').pipe (content) ->
result = {}
rows = content.split('\n').slice(1).filter Boolean
rows.forEach (line) ->
fields = line.split '\t'
result[fields[0]] = fields[1...].map (both) ->
coords = both.split /\,\s*/
coords if coords.length is 2
result[fields[0]] = result[fields[0]].filter Boolean
result
preScaledROIs = [] # Not currently used.
nestingOutline = null
stopPropagation = (e) ->
e.stopPropagation()
classifyPage.on classifyPage.LOAD_SUBJECT, (e, subject) ->
nestingOutline?.destroy()
nestingOutline = null
$.when(rois).then (rois) ->
site = subject.metadata.path.split('/')[1].split('_')[0]
{width, height} = classifyPage.subjectViewer.markingSurface.svg.el.viewBox.animVal
if site in preScaledROIs
[scaleX, scaleY] = [1, 1]
else
scaleX = subject.metadata.original_size.width / width
scaleY = subject.metadata.original_size.height / height
# Reverse the points so they're counterclockwise and knock out the clockwise outer shape.
nestingPoints = rois[site]?.slice(0).reverse()
if nestingPoints?
nestingOutline = classifyPage.subjectViewer.markingSurface.svg.addShape 'path.nesting-area',
d: """
M 0 0 L #{width + 0} 0 L #{width + 0} #{height + 0} L 0 #{height + 0}
L 0 0
M #{nestingPoints[0][0] / scaleX}, #{nestingPoints[0][1] / scaleY}
#{("L #{[x / scaleX, y / scaleY]}" for [x, y] in nestingPoints[1...]).join '\n'}
L #{nestingPoints[0][0] / scaleX}, #{nestingPoints[0][1] / scaleY}
"""
nestingOutline.addEvent 'mousedown', stopPropagation
nestingOutline.addEvent 'touchstart', stopPropagation
nestingOutline.addEvent 'mousemove', stopPropagation
nestingOutline.addEvent 'touchmove', stopPropagation