-
Notifications
You must be signed in to change notification settings - Fork 5
/
Labels.java
71 lines (60 loc) · 1.76 KB
/
Labels.java
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
package org.genericsystem.ir.reinforcer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
public class Labels implements Iterable<Label> {
private final Set<Label> labels = new HashSet<>();
public boolean addLabel(double tlx, double tly, double brx, double bry, String candidateLabel) {
Label candidate = new Label(tlx, tly, brx, bry, candidateLabel);
return addLabel(candidate);
}
public boolean addLabel(Label candidate) {
for (Label label : labels)
if (label.intersectWith(candidate))
throw new IllegalStateException(label + " intersect with : " + candidate);
return labels.add(candidate);
}
@Override
public String toString() {
return labels.toString();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Labels))
return false;
return labels.equals(((Labels) obj).labels);
}
@Override
public int hashCode() {
return labels.hashCode();
}
public List<Label> normalizeLabels() {
double mintlx = Double.MAX_VALUE, mintly = Double.MAX_VALUE, maxbrx = 0, maxbry = 0;
for (Label label : labels) {
if (label.getTlx() < mintlx)
mintlx = label.getTlx();
if (label.getTly() < mintly)
mintly = label.getTly();
if (label.getBrx() > maxbrx)
maxbrx = label.getBrx();
if (label.getBry() > maxbry)
maxbry = label.getBry();
}
double width = maxbrx - mintlx;
double height = maxbry - mintly;
List<Label> normalized = new ArrayList<>();
for (Label label : labels)
normalized.add(label.normalize(mintlx, mintly, width, height));
return normalized;
}
@Override
public Iterator<Label> iterator() {
return labels.iterator();
}
public Stream<Label> stream() {
return labels.stream();
}
}