Skip to content

Commit ee72172

Browse files
committed
better touch anchor creation
1 parent c96efaa commit ee72172

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,25 @@ public void draw() {
3333

3434
lights();
3535

36-
/*
3736
if (mousePressed) {
3837
// Create new anchor at the current touch point
3938
if (selAnchor != null) selAnchor.dispose();
40-
selAnchor = new Anchor(tracker, mouseX, mouseY);
39+
Trackable hit = tracker.get(mouseX, mouseY);
40+
if (hit != null) selAnchor = new Anchor(hit);
41+
else selAnchor = null;
4142
}
42-
*/
4343

4444
// Draw objects attached to each anchor
4545
for (Anchor anchor : regAnchors) {
4646
if (anchor.isTracking()) drawBox(anchor, 255, 255, 255);
47+
48+
// It is very important to dispose anchors once they are no longer tracked.
4749
if (anchor.isStopped()) anchor.dispose();
4850
}
49-
tracker.clearAnchors(regAnchors);
51+
if (selAnchor != null) drawBox(selAnchor, 255, 0, 0);
5052

51-
if (selAnchor != null) {
52-
drawBox(selAnchor, 255, 0, 0);
53-
}
53+
// Conveniency function in the tracker object to remove disposed anchors from a list
54+
tracker.clearAnchors(regAnchors);
5455

5556
// Draw trackable planes
5657
for (int i = 0; i < tracker.count(); i++) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public Anchor(Trackable trackable, float x, float y, float z) {
1616
id = g.createAnchor(idx, x, y, z);
1717
}
1818

19-
public Anchor(Tracker tracker, int mx, int my) {
20-
this.g = tracker.g;
21-
id = g.createAnchor(mx, my);
19+
public Anchor(Trackable trackable) {
20+
this.g = trackable.g;
21+
id = g.createAnchor(trackable.hit);
22+
trackable.hit = null;
2223
}
2324

2425
public void dispose() {

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ public boolean trackableSelected(int i, int mx, int my) {
257257
}
258258

259259

260+
protected HitResult getHitResult(int mx, int my) {
261+
for (HitResult hit : surfar.frame.hitTest(mx, my)) {
262+
Trackable trackable = hit.getTrackable();
263+
if (trackable instanceof Plane) {
264+
Plane plane = (Plane)trackable;
265+
if (trackPlanes.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
266+
return hit;
267+
}
268+
}
269+
}
270+
return null;
271+
}
272+
273+
protected int getTrackable(HitResult hit) {
274+
Plane plane = (Plane) hit.getTrackable();
275+
return trackPlanes.indexOf(plane);
276+
}
277+
260278
public float[] getTrackablePolygon(int i) {
261279
return getTrackablePolygon(i, null);
262280
}
@@ -315,16 +333,21 @@ public int createAnchor(int mx, int my) {
315333
if (trackable instanceof Plane) {
316334
Plane plane = (Plane)trackable;
317335
if (trackPlanes.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
318-
Anchor anchor = hit.createAnchor();
319-
anchors.put(++lastAnchorId, anchor);
320-
return lastAnchorId;
336+
return createAnchor(hit);
321337
}
322338
}
323339
}
324340
return 0;
325341
}
326342

327343

344+
protected int createAnchor(HitResult hit) {
345+
Anchor anchor = hit.createAnchor();
346+
anchors.put(++lastAnchorId, anchor);
347+
return lastAnchorId;
348+
}
349+
350+
328351
public void deleteAnchor(int id) {
329352
delAnchors.add(id);
330353
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import processing.core.PMatrix3D;
44

5+
import com.google.ar.core.HitResult;
6+
57
public class Trackable implements PAR {
68
protected PGraphicsAR g;
9+
protected HitResult hit;
710

811
private int id;
912
private PMatrix3D m;

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

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

3+
import com.google.ar.core.HitResult;
4+
35
import java.lang.reflect.Method;
46
import java.util.ArrayList;
57
import java.util.Collection;
@@ -49,6 +51,18 @@ public Trackable get(String id) {
4951
return trackables.get(id);
5052
}
5153

54+
public Trackable get(int mx, int my) {
55+
HitResult hit = g.getHitResult(mx, my);
56+
if (hit != null) {
57+
int idx = g.getTrackable(hit);
58+
Trackable t = get(idx);
59+
t.hit = hit;
60+
return t;
61+
} else {
62+
return null;
63+
}
64+
}
65+
5266
protected void create(int idx) {
5367
if (trackableEventMethod != null) {
5468
try {

0 commit comments

Comments
 (0)