Skip to content

Commit d471ead

Browse files
committed
refining AR API with Tracker and Trackable classes
1 parent cac5c64 commit d471ead

File tree

5 files changed

+145
-13
lines changed

5 files changed

+145
-13
lines changed

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

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

3-
import com.google.ar.core.Plane;
4-
53
public interface PAR {
64
static final int UNKNOWN = -1;
75

@@ -15,6 +13,4 @@ public interface PAR {
1513
static final int TRACKING = 2;
1614
static final int PAUSED = 3;
1715
static final int STOPPED = 4;
18-
19-
2016
}

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@
2222

2323
package processing.ar;
2424

25-
import android.view.MotionEvent;
2625
import android.view.SurfaceHolder;
2726

2827
import com.google.ar.core.Anchor;
29-
import com.google.ar.core.Camera;
30-
import com.google.ar.core.Frame;
3128
import com.google.ar.core.HitResult;
3229
import com.google.ar.core.Plane;
3330
import com.google.ar.core.Pose;
34-
import com.google.ar.core.Session;
3531
import com.google.ar.core.Trackable;
3632
import com.google.ar.core.TrackingState;
3733

@@ -45,7 +41,6 @@
4541
import processing.core.PGraphics;
4642
import processing.core.PMatrix3D;
4743
import processing.core.PSurface;
48-
import processing.event.TouchEvent;
4944
import processing.opengl.PGL;
5045
import processing.opengl.PGLES;
5146
import processing.opengl.PGraphics3D;
@@ -63,9 +58,11 @@ public class PGraphicsAR extends PGraphics3D {
6358
protected float[] anchorMatrix = new float[16];
6459
protected float[] colorCorrection = new float[4];
6560

61+
protected ArrayList<Tracker> trackers = new ArrayList<Tracker>();
6662
protected ArrayList<Plane> trackPlanes = new ArrayList<Plane>();
6763
protected HashMap<Plane, float[]> trackMatrices = new HashMap<Plane, float[]>();
6864
protected HashMap<Plane, Integer> trackIds = new HashMap<Plane, Integer>();
65+
protected HashMap<Integer, Integer> trackIdx = new HashMap<Integer, Integer>();
6966

7067
protected ArrayList<Plane> newPlanes = new ArrayList<Plane>();
7168
protected ArrayList<Plane> updatedPlanes = new ArrayList<Plane>();
@@ -96,6 +93,10 @@ public PGraphicsAR() {
9693
}
9794

9895

96+
static processing.ar.Trackable[] getTrackables() {
97+
return null;
98+
}
99+
99100
@Override
100101
public PSurface createSurface(AppComponent appComponent, SurfaceHolder surfaceHolder, boolean reset) {
101102
if (reset) pgl.resetFBOLayer();
@@ -189,6 +190,15 @@ protected void updateView() {
189190
}
190191

191192

193+
public void addTracker(Tracker tracker) {
194+
trackers.add(tracker);
195+
}
196+
197+
198+
public void removeTracker(Tracker tracker) {
199+
trackers.remove(tracker);
200+
}
201+
192202
@Override
193203
public int trackableCount() {
194204
return trackPlanes.size();
@@ -201,6 +211,11 @@ public int trackableId(int i) {
201211
}
202212

203213

214+
public int trackableIndex(int id) {
215+
return trackIdx.get(id);
216+
}
217+
218+
204219
@Override
205220
public int trackableType(int i) {
206221
Plane plane = trackPlanes.get(i);
@@ -425,13 +440,18 @@ protected void updateTrackables() {
425440
updatedPlanes.add(plane);
426441
}
427442

428-
// Remove stopped and subsummed trackables
443+
// Remove stopped and subsummed trackables, and update indices.
429444
for (int i = trackPlanes.size() - 1; i >= 0; i--) {
430445
Plane plane = trackPlanes.get(i);
431446
if (plane.getTrackingState() == TrackingState.STOPPED || plane.getSubsumedBy() != null) {
432447
trackPlanes.remove(i);
433448
trackMatrices.remove(plane);
434-
trackIds.remove(plane);
449+
int pid = trackIds.remove(plane);
450+
trackIdx.remove(pid);
451+
for (Tracker t: trackers) t.remove(pid);
452+
} else {
453+
int pid = trackIds.get(plane);
454+
trackIdx.put(pid, i);
435455
}
436456
}
437457
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package processing.ar;
2+
3+
import processing.core.PMatrix3D;
4+
5+
public class Trackable {
6+
private PGraphicsAR g;
7+
private int id;
8+
private PMatrix3D m;
9+
10+
public Trackable(PGraphicsAR g, int id) {
11+
this.g = g;
12+
this.id = id;
13+
}
14+
15+
public String id() {
16+
return String.valueOf(id);
17+
}
18+
19+
public int status() {
20+
int idx = g.trackableIndex(id);
21+
return g.trackableStatus(idx);
22+
}
23+
24+
public int type() {
25+
int idx = g.trackableIndex(id);
26+
return g.trackableType(idx);
27+
}
28+
29+
public PMatrix3D matrix() {
30+
int idx = g.trackableIndex(id);
31+
m = g.getTrackableMatrix(idx, m);
32+
return m;
33+
}
34+
35+
public void pushTransform() {
36+
g.push();
37+
transform();
38+
}
39+
40+
public void transform() {
41+
g.applyMatrix(matrix());
42+
}
43+
44+
public void popTransform() {
45+
g.pop();
46+
}
47+
48+
boolean selected(int mx, int my) {
49+
int idx = g.trackableIndex(id);
50+
return g.trackableSelected(idx, mx, my);
51+
}
52+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package processing.ar;
2+
3+
import java.util.HashMap;
4+
import java.util.Set;
5+
6+
import processing.core.PApplet;
7+
8+
public class Tracker implements PAR {
9+
private PApplet p;
10+
private PGraphicsAR g;
11+
12+
private HashMap<String, Trackable> trackables = new HashMap<String, Trackable>();
13+
14+
public Tracker(PApplet parent) {
15+
this.p = parent;
16+
this.g = (PGraphicsAR) p.g;
17+
}
18+
19+
public void start() {
20+
cleanup();
21+
g.addTracker(this);
22+
}
23+
24+
public void stop() {
25+
g.removeTracker(this);
26+
}
27+
28+
public int count() {
29+
return g.trackableCount();
30+
}
31+
32+
public Trackable get(int idx) {
33+
int id = g.trackableId(idx);
34+
String sid = String.valueOf(id);
35+
if (!trackables.containsKey(sid)) {
36+
Trackable t = new Trackable(g, id);
37+
trackables.put(sid, t);
38+
}
39+
return get(sid);
40+
}
41+
42+
public Trackable get(String id) {
43+
return trackables.get(id);
44+
}
45+
46+
protected void cleanup() {
47+
// Remove any inactive trackables left over in the tracker.
48+
Set<String> ids = trackables.keySet();
49+
for (String id: ids) {
50+
Trackable t = trackables.get(id);
51+
if (t.status() == STOPPED || t.status() == UNKNOWN) trackables.remove(id);
52+
}
53+
}
54+
55+
protected void remove(int idx) {
56+
int id = g.trackableId(idx);
57+
String sid = String.valueOf(id);
58+
remove(sid);
59+
}
60+
61+
protected void remove(String id) {
62+
trackables.remove(id);
63+
}
64+
}

mode/mode.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ url = http://android.processing.org
44
sentence = Create projects with Processing for Android devices
55
paragraph = This version of the Android Mode is for Processing 3.1+
66
imports=processing.mode.java.JavaMode
7-
version = 274
8-
prettyVersion = 4.1.0-beta3
7+
version = 275
8+
prettyVersion = 4.1.0-beta4
99
minRevision = 249
1010
maxRevision = 0

0 commit comments

Comments
 (0)