Skip to content

Commit b7b4b9c

Browse files
committed
Replace Target with Rect in TargetListTargetData
1 parent e64939e commit b7b4b9c

File tree

2 files changed

+32
-164
lines changed

2 files changed

+32
-164
lines changed

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();

client/src/test/java/org/strykeforce/deadeye/TargetDataTest.java

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
import java.io.IOException;
77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.Objects;
109
import okio.Buffer;
1110
import org.junit.jupiter.api.Test;
12-
import org.strykeforce.deadeye.TargetListTargetData.Target;
1311

1412
class TargetDataTest {
1513

@@ -19,8 +17,7 @@ void testTargetDataDeserialization() throws IOException {
1917
buffer.writeUtf8("{\"id\":\"Z1\",\"sn\":1,\"v\":true}");
2018

2119
TargetData exp = new TargetData("Z1", 1, true);
22-
@SuppressWarnings("unchecked")
23-
DeadeyeJsonAdapter<TargetData> jsonAdapter = new TargetData().getJsonAdapter();
20+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<TargetData> jsonAdapter = new TargetData().getJsonAdapter();
2421
TargetData td = jsonAdapter.fromJson(buffer);
2522
assertEquals(exp, td);
2623

@@ -33,8 +30,7 @@ void testTargetDataDeserialization() throws IOException {
3330
@Test
3431
void testTargetDataSerialization() throws IOException {
3532
TargetData expected = new TargetData("Z1", 1, true);
36-
@SuppressWarnings("unchecked")
37-
DeadeyeJsonAdapter<TargetData> jsonAdapter = new TargetData().getJsonAdapter();
33+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<TargetData> jsonAdapter = new TargetData().getJsonAdapter();
3834
String json = jsonAdapter.toJson(expected);
3935
Buffer buffer = new Buffer();
4036
buffer.writeUtf8(json);
@@ -51,9 +47,7 @@ void testUprightRectTargetDataDeserialization() throws IOException {
5147
Point br = new Point(3, 4);
5248
Point c = new Point(5, 6);
5349
UprightRectTargetData exp = new UprightRectTargetData("Z1", 3, true, tl, br, c);
54-
@SuppressWarnings("unchecked")
55-
DeadeyeJsonAdapter<UprightRectTargetData> jsonAdapter =
56-
new UprightRectTargetData().getJsonAdapter();
50+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<UprightRectTargetData> jsonAdapter = new UprightRectTargetData().getJsonAdapter();
5751
UprightRectTargetData td = jsonAdapter.fromJson(buffer);
5852
assertEquals(exp, td);
5953

@@ -75,9 +69,7 @@ void testUprightRectTargetDataSerialization() throws IOException {
7569
Point c = new Point(5, 6);
7670
UprightRectTargetData expected = new UprightRectTargetData("Z1", 3, true, tl, br, c);
7771

78-
@SuppressWarnings("unchecked")
79-
DeadeyeJsonAdapter<UprightRectTargetData> jsonAdapter =
80-
new UprightRectTargetData().getJsonAdapter();
72+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<UprightRectTargetData> jsonAdapter = new UprightRectTargetData().getJsonAdapter();
8173
String json = jsonAdapter.toJson(expected);
8274
Buffer buffer = new Buffer();
8375
buffer.writeUtf8(json);
@@ -89,23 +81,18 @@ void testUprightRectTargetDataSerialization() throws IOException {
8981
@Test
9082
void testMinAreaRectTargetDataDeserialization() throws IOException {
9183
Buffer buffer = new Buffer();
92-
buffer.writeUtf8(
93-
"{\"id\":\"Z1\",\"d\":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,"
94-
+ "12.0,13.0],\"sn\":3,\"v\":true}");
84+
buffer.writeUtf8("{\"id\":\"Z1\",\"d\":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,"
85+
+ "12.0,13.0],\"sn\":3,\"v\":true}");
9586

9687
double angle = 1;
9788
Point2D center = new Point2D(2, 3);
9889
double height = 4;
9990
double width = 5;
100-
Point2D[] points =
101-
new Point2D[] {
102-
new Point2D(6, 7), new Point2D(8, 9), new Point2D(10, 11), new Point2D(12, 13),
103-
};
104-
MinAreaRectTargetData exp =
105-
new MinAreaRectTargetData("Z1", 3, true, angle, center, width, height, points);
106-
@SuppressWarnings("unchecked")
107-
DeadeyeJsonAdapter<MinAreaRectTargetData> jsonAdapter =
108-
new MinAreaRectTargetData().getJsonAdapter();
91+
Point2D[] points = new Point2D[]{new Point2D(6, 7), new Point2D(8, 9), new Point2D(10, 11),
92+
new Point2D(12, 13),};
93+
MinAreaRectTargetData exp = new MinAreaRectTargetData("Z1", 3, true, angle, center, width,
94+
height, points);
95+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<MinAreaRectTargetData> jsonAdapter = new MinAreaRectTargetData().getJsonAdapter();
10996
MinAreaRectTargetData td = jsonAdapter.fromJson(buffer);
11097
assertEquals(exp, td);
11198
}
@@ -116,16 +103,12 @@ void testMinAreaRectTargetDataSerialization() throws IOException {
116103
Point2D center = new Point2D(2, 3);
117104
double height = 4;
118105
double width = 5;
119-
Point2D[] points =
120-
new Point2D[] {
121-
new Point2D(6, 7), new Point2D(8, 9), new Point2D(10, 11), new Point2D(12, 13),
122-
};
123-
MinAreaRectTargetData expected =
124-
new MinAreaRectTargetData("Z1", 3, true, angle, center, width, height, points);
125-
126-
@SuppressWarnings("unchecked")
127-
DeadeyeJsonAdapter<MinAreaRectTargetData> jsonAdapter =
128-
new MinAreaRectTargetData().getJsonAdapter();
106+
Point2D[] points = new Point2D[]{new Point2D(6, 7), new Point2D(8, 9), new Point2D(10, 11),
107+
new Point2D(12, 13),};
108+
MinAreaRectTargetData expected = new MinAreaRectTargetData("Z1", 3, true, angle, center, width,
109+
height, points);
110+
111+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<MinAreaRectTargetData> jsonAdapter = new MinAreaRectTargetData().getJsonAdapter();
129112
String json = jsonAdapter.toJson(expected);
130113
Buffer buffer = new Buffer();
131114
buffer.writeUtf8(json);
@@ -140,45 +123,37 @@ void testTargetListTargetDataDeserialization() throws IOException {
140123
buffer.writeUtf8(
141124
"{\"id\":\"Z1\",\"d\":[[0,0,10,10,85],[10,10,100,100,900]],\"sn\":3,\"v\":true}");
142125

143-
List<Target> targets = new ArrayList<>();
126+
List<Rect> targets = new ArrayList<>();
144127
Point tl = new Point(0, 0);
145128
Point br = new Point(10, 10);
146-
Point c = new Point(5, 5);
147-
targets.add(new Target(tl, br, c, 85));
129+
targets.add(new Rect(tl, br));
148130

149131
tl = new Point(10, 10);
150132
br = new Point(110, 110);
151-
c = new Point(60, 60);
152-
targets.add(new Target(tl, br, c, 900));
133+
targets.add(new Rect(tl, br));
153134

154135
TargetListTargetData exp = new TargetListTargetData("Z1", 3, true, targets);
155136

156-
@SuppressWarnings("unchecked")
157-
DeadeyeJsonAdapter<TargetListTargetData> jsonAdapter =
158-
new TargetListTargetData().getJsonAdapter();
137+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<TargetListTargetData> jsonAdapter = new TargetListTargetData().getJsonAdapter();
159138
TargetListTargetData td = jsonAdapter.fromJson(buffer);
160139
assertEquals(exp, td);
161140
}
162141

163142
@Test
164143
void testTargetListTargetDataSerialization() throws IOException {
165144

166-
List<Target> targets = new ArrayList<>();
145+
List<Rect> targets = new ArrayList<>();
167146
Point tl = new Point(0, 0);
168147
Point br = new Point(10, 10);
169-
Point c = new Point(5, 5);
170-
targets.add(new Target(tl, br, c, 85));
148+
targets.add(new Rect(tl, br));
171149

172150
tl = new Point(10, 10);
173151
br = new Point(110, 110);
174-
c = new Point(60, 60);
175-
targets.add(new Target(tl, br, c, 900));
152+
targets.add(new Rect(tl, br));
176153

177154
TargetListTargetData expected = new TargetListTargetData("Z1", 5, true, targets);
178155

179-
@SuppressWarnings("unchecked")
180-
DeadeyeJsonAdapter<TargetListTargetData> jsonAdapter =
181-
new TargetListTargetData().getJsonAdapter();
156+
@SuppressWarnings("unchecked") DeadeyeJsonAdapter<TargetListTargetData> jsonAdapter = new TargetListTargetData().getJsonAdapter();
182157
String json = jsonAdapter.toJson(expected);
183158
assertNotNull(json);
184159

0 commit comments

Comments
 (0)