Skip to content

Commit 073b32b

Browse files
authored
Merge pull request #103 from strykeforce/target-rect
Replace TargetListTargetData Target with Rect in /client
2 parents 4cf9c2a + b7b4b9c commit 073b32b

File tree

5 files changed

+224
-169
lines changed

5 files changed

+224
-169
lines changed

client/src/main/java/org/strykeforce/deadeye/Point.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.strykeforce.deadeye;
22

33
import java.util.Objects;
4+
import org.jetbrains.annotations.NotNull;
45

56
/**
67
* A point representing a location in (x,y) coordinate space, specified in integer precision.
@@ -18,8 +19,9 @@ public final class Point {
1819

1920
/**
2021
* Constructs and initializes a point at the specified (x,y) location in the coordinate space.
21-
* @param x the X coordinate of the newly constructed {@code Point}
22-
* @param y the y coordinate of the newly constructed {@code Point}
22+
*
23+
* @param x the X coordinate of the newly constructed {@code Point}
24+
* @param y the y coordinate of the newly constructed {@code Point}
2325
*/
2426
public Point(int x, int y) {
2527
this.x = x;
@@ -33,8 +35,9 @@ public Point(int x, int y) {
3335
* @param bottomRight the bottom right corner of the bounding box.
3436
* @return the {@code Point} at the center of this bounding box.
3537
*/
36-
public static Point boundingBoxCenterFrom(Point topLeft, Point bottomRight) {
37-
return new Point((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
38+
public static Point boundingBoxCenterFrom(@NotNull Point topLeft, @NotNull Point bottomRight) {
39+
// Rect constructor checks for nulls
40+
return new Rect(topLeft, bottomRight).center();
3841
}
3942

4043
/**
@@ -61,7 +64,7 @@ public double theta() {
6164
* @param that the point to measure distance from.
6265
* @return the distance to the other {@code Point}.
6366
*/
64-
public double distanceTo(Point that) {
67+
public double distanceTo(@NotNull Point that) {
6568
double dx = this.x - that.x;
6669
double dy = this.y - that.y;
6770
return Math.sqrt(dx * dx + dy * dy);
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package org.strykeforce.deadeye;
2+
3+
import java.util.Objects;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
/**
7+
* A rectangle in (x, y) coordinate space, specified in integer precision.
8+
*/
9+
public class Rect {
10+
11+
/**
12+
* The coordinates of the top left corner of this rectangle.
13+
*/
14+
@NotNull
15+
public final Point topLeft;
16+
17+
/**
18+
* The coordinates of the bottom right corner of this rectangle.
19+
*/
20+
@NotNull
21+
public final Point bottomRight;
22+
23+
/**
24+
* Constructs a rectangle with the specified (x, y) corner points.
25+
*
26+
* @param topLeft the coordinates of the top left corner
27+
* @param bottomRight the coordinates of the bottom right corner
28+
*/
29+
public Rect(@NotNull Point topLeft, @NotNull Point bottomRight) {
30+
Objects.requireNonNull(topLeft);
31+
Objects.requireNonNull(bottomRight);
32+
this.topLeft = topLeft;
33+
this.bottomRight = bottomRight;
34+
}
35+
36+
/**
37+
* Return the center of this rectangle.
38+
*
39+
* @return the center (x, y) point
40+
*/
41+
public Point center() {
42+
return new Point((topLeft.x + bottomRight.x) / 2, (topLeft.y + bottomRight.y) / 2);
43+
}
44+
45+
/**
46+
* Return the height of this rectangle.
47+
*
48+
* @return the height in units of (x, y) coordinate system
49+
*/
50+
public int height() {
51+
return bottomRight.y - topLeft.y;
52+
}
53+
54+
/**
55+
* Return the width of this rectangle.
56+
*
57+
* @return the width in units of (x, y) coordinate system
58+
*/
59+
public int width() {
60+
return bottomRight.x - topLeft.x;
61+
}
62+
63+
/**
64+
* Return the size of this rectangle.
65+
*
66+
* @return the height x width in units of (x, y) coordinate system
67+
*/
68+
public int size() {
69+
return height() * width();
70+
}
71+
72+
/**
73+
* Checks whether this rectangle contains the point.
74+
*
75+
* <p>For example:
76+
*
77+
* <code><pre>
78+
* assertTrue(rect.contains(rect.topLeft));
79+
* assertFalse(rect.contains(rect.bottomRight));
80+
* assertTrue(rect.contains(rect.center()));
81+
* </pre></code>
82+
*
83+
* @param point the (x, y) point to check
84+
* @return true if point is contained in rectangle
85+
*/
86+
public boolean contains(@NotNull Point point) {
87+
return topLeft.x <= point.x && point.x < bottomRight.x && topLeft.y <= point.y
88+
&& point.y < bottomRight.y;
89+
}
90+
91+
/**
92+
* Returns true if rectangle is empty, i.e. height or width is less than or equal to zero.
93+
*
94+
* @return true if empty
95+
*/
96+
public boolean isEmpty() {
97+
return height() <= 0 || width() <= 0;
98+
}
99+
100+
@Override
101+
public boolean equals(Object o) {
102+
if (this == o) {
103+
return true;
104+
}
105+
if (o == null || getClass() != o.getClass()) {
106+
return false;
107+
}
108+
Rect rect = (Rect) o;
109+
return topLeft.equals(rect.topLeft) && bottomRight.equals(rect.bottomRight);
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hash(topLeft, bottomRight);
115+
}
116+
117+
@Override
118+
public String toString() {
119+
return "Rect{" + "topLeft=" + topLeft + ", bottomRight=" + bottomRight + '}';
120+
}
121+
}

client/src/main/java/org/strykeforce/deadeye/TargetListTargetData.java

Lines changed: 7 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class TargetListTargetData extends TargetData {
2020
static final int DATA_LENGTH = 5;
2121

2222
/**
23-
* A <code>List</code> containing all <code>Target</code> instances identified by the
23+
* A <code>List</code> containing all {@code Rect}instances identified by the
2424
* <code>TargetListPipeline</code>.
2525
*/
2626
@NotNull
27-
public final List<Target> targets;
27+
public final List<Rect> targets;
2828

2929
/**
3030
* Constructs and initializes an invalid <code>TargetListTargetData</code> with no id, serial 0,
@@ -43,7 +43,7 @@ public TargetListTargetData() {
4343
* @param targets the list of <code>Target</code> instances identified.
4444
*/
4545
public TargetListTargetData(
46-
@NotNull String id, int serial, boolean valid, @NotNull List<Target> targets) {
46+
@NotNull String id, int serial, boolean valid, @NotNull List<Rect> targets) {
4747
super(id, serial, valid);
4848
this.targets = targets;
4949
}
@@ -80,112 +80,6 @@ public String toString() {
8080
return "TargetListTargetData{" + "targets=" + targets + "} " + super.toString();
8181
}
8282

83-
/**
84-
* A <code>Target</code> represents a single target returned in a <code>TargetListTargetData</code>.
85-
*/
86-
public static class Target {
87-
88-
/**
89-
* Top left corner <code>Point</code> of the upright bounding box enclosing this target.
90-
*/
91-
@NotNull
92-
public final Point topLeft;
93-
/**
94-
* Bottom right corner <code>Point</code> of the upright bounding box enclosing this target.
95-
*/
96-
@NotNull
97-
public final Point bottomRight;
98-
/**
99-
* Center <code>Point</code> of the upright bounding box enclosing this target.
100-
*/
101-
@NotNull
102-
public final Point center;
103-
/**
104-
* Gets the area of the contour enclosing the target.
105-
*/
106-
public final int contourArea;
107-
108-
/**
109-
* Constructs and initializes a <code>Target</code> with the specified values.
110-
*
111-
* @param topLeft the top left corner of the bounding box enclosing this target.
112-
* @param bottomRight the top left corner of the bounding box enclosing this target.
113-
* @param center the center of the bounding box enclosing this target.
114-
* @param contourArea the area of the contour enclosing the target.
115-
*/
116-
public Target(
117-
@NotNull Point topLeft,
118-
@NotNull Point bottomRight,
119-
@NotNull Point center,
120-
int contourArea) {
121-
this.topLeft = topLeft;
122-
this.bottomRight = bottomRight;
123-
this.center = center;
124-
this.contourArea = contourArea;
125-
}
126-
127-
/**
128-
* Gets the area of the upright bounding box surrounding this target.
129-
*
130-
* @return the area of the bounding box.
131-
*/
132-
public int area() {
133-
return width() * height();
134-
}
135-
136-
/**
137-
* Gets the width of the upright bounding box surrounding this target.
138-
*
139-
* @return the width of the bounding box.
140-
*/
141-
public int width() {
142-
return bottomRight.x - topLeft.x;
143-
}
144-
145-
/**
146-
* Gets the height of the upright bounding box surrounding this target.
147-
*
148-
* @return the height of the bounding box.
149-
*/
150-
public int height() {
151-
return bottomRight.y - topLeft.y;
152-
}
153-
154-
@Override
155-
public boolean equals(Object o) {
156-
if (this == o) {
157-
return true;
158-
}
159-
if (o == null || getClass() != o.getClass()) {
160-
return false;
161-
}
162-
Target target = (Target) o;
163-
return contourArea == target.contourArea
164-
&& topLeft.equals(target.topLeft)
165-
&& bottomRight.equals(target.bottomRight)
166-
&& center.equals(target.center);
167-
}
168-
169-
@Override
170-
public int hashCode() {
171-
return Objects.hash(topLeft, bottomRight, center, contourArea);
172-
}
173-
174-
@Override
175-
public String toString() {
176-
return "Target{"
177-
+ "topLeft="
178-
+ topLeft
179-
+ ", bottomRight="
180-
+ bottomRight
181-
+ ", center="
182-
+ center
183-
+ ", contourArea="
184-
+ contourArea
185-
+ '}';
186-
}
187-
}
188-
18983
private static class JsonAdapterImpl implements DeadeyeJsonAdapter<TargetListTargetData> {
19084

19185
// json d field: bb.tl().x, bb.tl().y, bb.br().x, bb.br().y, center.x, center.y
@@ -197,7 +91,7 @@ public TargetListTargetData fromJson(BufferedSource source) throws IOException {
19791
String id = null;
19892
int serial = -1;
19993
boolean valid = false;
200-
List<Target> targets = new ArrayList<>();
94+
List<Rect> targets = new ArrayList<>();
20195

20296
reader.beginObject();
20397
while (reader.hasNext()) {
@@ -223,8 +117,7 @@ public TargetListTargetData fromJson(BufferedSource source) throws IOException {
223117
// bb.x, bb.y, bb.width, bb.height, area
224118
Point topLeft = new Point(data[0], data[1]);
225119
Point bottomRight = new Point(data[0] + data[2], data[1] + data[3]);
226-
Point center = Point.boundingBoxCenterFrom(topLeft, bottomRight);
227-
targets.add(new Target(topLeft, bottomRight, center, data[4]));
120+
targets.add(new Rect(topLeft, bottomRight));
228121
}
229122
reader.endArray();
230123
break;
@@ -246,11 +139,11 @@ public String toJson(TargetListTargetData targetData) throws IOException {
246139
writer.name("v").value(targetData.valid);
247140

248141
writer.name("d").beginArray();
249-
for (Target t : targetData.targets) {
142+
for (Rect t : targetData.targets) {
250143
writer.beginArray();
251144
writer.value(t.topLeft.x).value(t.topLeft.y);
252145
writer.value(t.width()).value(t.height());
253-
writer.value(t.contourArea);
146+
writer.value(0); // contour area not in Rect
254147
writer.endArray();
255148
}
256149
writer.endArray();
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.strykeforce.deadeye;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotSame;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
class RectTest {
11+
12+
Rect rect = new Rect(new Point(0, 0), new Point(10, 20));
13+
14+
@Test
15+
void center() {
16+
assertEquals(new Point(5, 10), rect.center());
17+
}
18+
19+
@Test
20+
void height() {
21+
assertEquals(20, rect.height());
22+
}
23+
24+
@Test
25+
void width() {
26+
assertEquals(10, rect.width());
27+
}
28+
29+
@Test
30+
void size() {
31+
assertEquals(200, rect.size());
32+
}
33+
34+
@Test
35+
void contains() {
36+
assertTrue(rect.contains(rect.topLeft));
37+
assertFalse(rect.contains(rect.bottomRight));
38+
assertTrue(rect.contains(rect.center()));
39+
}
40+
41+
@Test
42+
void isEmpty() {
43+
assertFalse(rect.isEmpty());
44+
assertTrue(new Rect(rect.bottomRight, rect.topLeft).isEmpty());
45+
assertTrue(new Rect(new Point(10,10), new Point(10,20)).isEmpty());
46+
assertTrue(new Rect(new Point(10,10), new Point(9,20)).isEmpty());
47+
assertTrue(new Rect(new Point(0,10), new Point(10,10)).isEmpty());
48+
assertTrue(new Rect(new Point(0,10), new Point(10,9)).isEmpty());
49+
}
50+
51+
@Test
52+
void equality() {
53+
Rect expected = new Rect(new Point(0, 0), new Point(10, 20));
54+
assertNotSame(expected, rect);
55+
assertEquals(expected, rect);
56+
}
57+
58+
@Test
59+
void hashCodeEquality() {
60+
Rect expected = new Rect(new Point(0, 0), new Point(10, 20));
61+
assertEquals(expected.hashCode(), rect.hashCode());
62+
}
63+
}

0 commit comments

Comments
 (0)