Skip to content

Commit 10b825b

Browse files
committed
implemented trackableEvent
1 parent 2f4d446 commit 10b825b

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

debug/apps/arscene/src/main/java/arscene/Sketch.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package arscene;
22

33
import java.util.ArrayList;
4-
import java.util.Iterator;
54

65
import processing.ar.*;
76
import processing.core.PApplet;
@@ -42,9 +41,8 @@ public void draw() {
4241

4342
// Draw objects attached to each anchor
4443
for (Anchor anchor : regAnchors) {
45-
if (anchor.isTracking()) {
46-
drawBox(anchor, 255, 255, 255);
47-
}
44+
if (anchor.isTracking()) drawBox(anchor, 255, 255, 255);
45+
if (anchor.isStopped()) anchor.dispose();
4846
}
4947

5048
// for (int i = : regAnchors){
@@ -56,39 +54,20 @@ public void draw() {
5654
drawBox(selAnchor, 255, 0, 0);
5755
}
5856

59-
6057
// Draw trackable planes
6158
for (int i = 0; i < tracker.count(); i++) {
6259
Trackable trackable = tracker.get(i);
63-
64-
if (trackable.isNew()) {
65-
println("IS NEW");
66-
}
67-
6860
if (!trackable.isTracking()) continue;
6961

70-
if (trackable.isNew() && regAnchors.size() < 10) {
71-
// Add new anchor associated to this trackable, 0.3 meters above it
72-
Anchor anchor;
73-
if (trackable.isWallPlane()) {
74-
anchor = new Anchor(trackable, 0.3f, 0, 0);
75-
} else {
76-
anchor = new Anchor(trackable, 0, 0.3f, 0);
77-
}
78-
regAnchors.add(anchor);
79-
}
80-
81-
float[] points = trackable.getPolygon();
82-
8362
pushMatrix();
8463
trackable.transform();
85-
if (mousePressed && trackable.selected(mouseX, mouseY)) {
64+
if (mousePressed && trackable.isSelected(mouseX, mouseY)) {
8665
fill(255, 0, 0, 100);
8766
} else {
8867
fill(255, 100);
8968
}
90-
9169
beginShape();
70+
float[] points = trackable.getPolygon();
9271
for (int n = 0; n < points.length / 2; n++) {
9372
float x = points[2 * n];
9473
float z = points[2 * n + 1];
@@ -108,4 +87,21 @@ public void drawBox(Anchor anchor, int r, int g, int b) {
10887
box(0.15f);
10988
anchor.detach();
11089
}
90+
91+
public void trackableEvent(Trackable t) {
92+
if (regAnchors.size() < 10) {
93+
float x0 = 0, y0 = 0;
94+
if (t.isWallPlane()) {
95+
// The new trackable is a wall, so adding the anchor 0.3 meters to its side
96+
x0 = 0.3f;
97+
} else if (t.isFloorPlane()) {
98+
// The new trackable is a floor plane, so adding the anchor 0.3 meters above it
99+
y0 = 0.3f;
100+
} else {
101+
// The new trackable is a floor plane, so adding the anchor 0.3 meters below it
102+
y0 = -0.3f;
103+
}
104+
regAnchors.add(new Anchor(t, x0, y0, 0));
105+
}
106+
}
111107
}

mode/libraries/ar/src/processing/ar/PGraphicsAR.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ protected void updateTrackables() {
429429
Plane plane = trackPlanes.get(i);
430430
int pid = trackIds.get(plane);
431431
trackIdx.put(pid, i);
432+
if (newPlanes.contains(plane)) {
433+
for (Tracker t: trackers) t.create(i);
434+
}
432435
}
433436
}
434437

mode/libraries/ar/src/processing/ar/Trackable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public float[] getPolygon() {
3434
return points;
3535
}
3636

37-
public boolean selected(int mx, int my) {
37+
public boolean isSelected(int mx, int my) {
3838
int idx = g.trackableIndex(id);
3939
return g.trackableSelected(idx, mx, my);
4040
}

mode/libraries/ar/src/processing/ar/Tracker.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package processing.ar;
22

3+
import java.lang.reflect.Method;
34
import java.util.HashMap;
45
import java.util.Set;
56

@@ -9,11 +10,14 @@ public class Tracker implements PAR {
910
protected PApplet p;
1011
protected PGraphicsAR g;
1112

13+
private Method trackableEventMethod;
14+
1215
private HashMap<String, Trackable> trackables = new HashMap<String, Trackable>();
1316

1417
public Tracker(PApplet parent) {
1518
this.p = parent;
1619
this.g = (PGraphicsAR) p.g;
20+
setEventHandler();
1721
}
1822

1923
public void start() {
@@ -43,6 +47,19 @@ public Trackable get(String id) {
4347
return trackables.get(id);
4448
}
4549

50+
protected void create(int idx) {
51+
if (trackableEventMethod != null) {
52+
try {
53+
Trackable t = get(idx);
54+
trackableEventMethod.invoke(p, t);
55+
} catch (Exception e) {
56+
System.err.println("error, disabling trackableEventMethod() for AR tracker");
57+
e.printStackTrace();
58+
trackableEventMethod = null;
59+
}
60+
}
61+
}
62+
4663
protected void cleanup() {
4764
// Remove any inactive trackables left over in the tracker.
4865
Set<String> ids = trackables.keySet();
@@ -61,4 +78,23 @@ protected void remove(int idx) {
6178
protected void remove(String id) {
6279
trackables.remove(id);
6380
}
81+
82+
83+
protected void setEventHandler() {
84+
try {
85+
trackableEventMethod = p.getClass().getMethod("trackableEvent", Trackable.class);
86+
return;
87+
} catch (Exception e) {
88+
// no such method, or an error... which is fine, just ignore
89+
}
90+
91+
// trackableEvent can alternatively be defined as receiving an Object, to allow
92+
// Processing mode implementors to support the video library without linking
93+
// to it at build-time.
94+
try {
95+
trackableEventMethod = p.getClass().getMethod("trackableEvent", Object.class);
96+
} catch (Exception e) {
97+
// no such method, or an error... which is fine, just ignore
98+
}
99+
}
64100
}

0 commit comments

Comments
 (0)