-
Notifications
You must be signed in to change notification settings - Fork 5
/
RectangleToolsTest.java
153 lines (122 loc) · 5.11 KB
/
RectangleToolsTest.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
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
package org.genericsystem.reinforcer;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.genericsystem.reinforcer.tools.GSPoint;
import org.genericsystem.reinforcer.tools.GSRect;
import org.genericsystem.reinforcer.tools.RectangleTools;
import org.testng.annotations.Test;
public class RectangleToolsTest {
private GSRect rect1 = new GSRect(0, 0, 10, 10);
private GSRect rect2 = new GSRect(1, 1, 20, 10);
private GSRect rect3 = new GSRect(20, 10, 10, 10);
private GSRect rect4 = new GSRect(1, 1, 1, 1);
@Test
public void nonMaximumSuppression() {
// Test with empty / null
List<GSRect> list = RectangleTools.nonMaximumSuppression(Collections.emptyList(), 0.3);
assertTrue(list.isEmpty());
list = RectangleTools.nonMaximumSuppression(null, 0.3);
assertTrue(list.isEmpty());
// Test case 1: should return 1 rect
GSRect r1 = new GSRect(new GSPoint(12, 84), new GSPoint(140, 212));
GSRect r2 = new GSRect(new GSPoint(24, 84), new GSPoint(152, 212));
GSRect r3 = new GSRect(new GSPoint(36, 84), new GSPoint(164, 212));
GSRect r4 = new GSRect(new GSPoint(12, 96), new GSPoint(140, 224));
GSRect r5 = new GSRect(new GSPoint(24, 96), new GSPoint(152, 224));
GSRect r6 = new GSRect(new GSPoint(24, 108), new GSPoint(152, 236));
GSRect simplified1 = new GSRect(24, 108, 128, 128);
list = RectangleTools.nonMaximumSuppression(Arrays.asList(r1, r2, r3, r4, r5, r6), 0.3);
assertTrue(!list.isEmpty());
assertEquals(list, Arrays.asList(simplified1));
// Test 2: should return 1 rect
r1 = new GSRect(new GSPoint(114, 60), new GSPoint(178, 124));
r2 = new GSRect(new GSPoint(120, 60), new GSPoint(184, 124));
r3 = new GSRect(new GSPoint(114, 66), new GSPoint(178, 130));
GSRect simplified2 = new GSRect(114, 66, 64, 64);
list = RectangleTools.nonMaximumSuppression(Arrays.asList(r1, r2, r3), 0.3);
assertTrue(!list.isEmpty());
assertEquals(list, Arrays.asList(simplified2));
// Test case 3: should return 2 rects
r1 = new GSRect(new GSPoint(12, 30), new GSPoint(76, 94));
r2 = new GSRect(new GSPoint(12, 36), new GSPoint(76, 100));
r3 = new GSRect(new GSPoint(72, 36), new GSPoint(200, 164));
r4 = new GSRect(new GSPoint(84, 48), new GSPoint(212, 176));
GSRect simplified31 = new GSRect(12, 36, 64, 64);
GSRect simplified32 = new GSRect(84, 48, 128, 128);
list = RectangleTools.nonMaximumSuppression(Arrays.asList(r1, r2, r3, r4), 0.3);
assertTrue(!list.isEmpty());
assertEquals(list, Arrays.asList(simplified31, simplified32));
}
@Test
public void commonArea() {
double[] res = RectangleTools.commonArea(rect1, rect2);
assertEquals(res[0], 0.81, 0.001);
assertEquals(res[1], 0.405, 0.001);
res = RectangleTools.commonArea(rect1, rect1);
assertEquals(res[0], 1.0, 0.001);
assertEquals(res[1], 1.0, 0.001);
res = RectangleTools.commonArea(rect1, rect3);
assertEquals(res[0], 0, 0.001);
assertEquals(res[1], 0, 0.001);
res = RectangleTools.commonArea(rect1, rect4);
assertEquals(res[0], 0.01, 0.001);
assertEquals(res[1], 1.0, 0.001);
res = RectangleTools.commonArea(rect4, rect1);
assertEquals(res[0], 1.0, 0.001);
assertEquals(res[1], 0.01, 0.001);
}
@Test
public void inclusiveArea() {
GSRect r1 = new GSRect(0, 0, 100, 100);
GSRect r2 = new GSRect(49, 0, 50, 100);
GSRect r3 = new GSRect(100, 0, 100, 100);
assertEquals(RectangleTools.inclusiveArea(r1, r1), 1, 0.001);
assertEquals(RectangleTools.inclusiveArea(r1, r2), 0.5, 0.001);
assertEquals(RectangleTools.inclusiveArea(r1, r3), 0, 0.001);
}
@Test
public void getIntersection() {
GSRect inter12 = new GSRect(1, 1, 9, 9);
Optional<GSRect> inter = RectangleTools.getIntersection(rect1, rect1);
assertTrue(inter.isPresent());
assertEquals(inter.get(), rect1);
inter = RectangleTools.getIntersection(rect1, rect2);
assertTrue(inter.isPresent());
assertEquals(inter.get(), inter12);
inter = RectangleTools.getIntersection(rect1, rect3);
assertFalse(inter.isPresent());
assertTrue(Optional.empty().equals(inter));
inter = RectangleTools.getIntersection(rect1, rect4);
assertTrue(inter.isPresent());
assertEquals(inter.get(), rect4);
}
@Test
public void getUnion() {
GSRect union12 = new GSRect(0, 0, 21, 11);
GSRect union13 = new GSRect(0, 0, 30, 20);
GSRect union = RectangleTools.getUnion(rect1, rect1);
assertEquals(union, rect1);
union = RectangleTools.getUnion(rect1, rect2);
assertEquals(union, union12);
union = RectangleTools.getUnion(rect1, rect3);
assertEquals(union, union13);
union = RectangleTools.getUnion(rect1, rect4);
assertEquals(union, rect1);
}
@Test
public void isOverlapping() {
assertTrue(RectangleTools.isOverlapping(rect1, rect1));
assertTrue(RectangleTools.isOverlapping(rect1, rect2));
assertFalse(RectangleTools.isOverlapping(rect1, rect3));
assertTrue(RectangleTools.isOverlapping(rect1, rect4));
}
@Test(expectedExceptions = { IllegalArgumentException.class })
public void isOverlappingNull() {
RectangleTools.isOverlapping(null, null);
}
}