Skip to content

Commit 2f4d446

Browse files
committed
use anchor matrix
1 parent 61a9935 commit 2f4d446

File tree

7 files changed

+157
-90
lines changed

7 files changed

+157
-90
lines changed
Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package arscene;
22

3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
36
import processing.ar.*;
47
import processing.core.PApplet;
5-
import processing.core.PMatrix3D;
68

79
public class Sketch extends PApplet {
8-
float angle = 0;
9-
float[] points;
10-
PMatrix3D mat = new PMatrix3D();
11-
int oldSelAnchor;
12-
int selAnchor;
10+
float angle;
11+
Anchor selAnchor;
12+
ArrayList<Anchor> regAnchors;
13+
14+
Tracker tracker;
1315

1416
public void settings() {
1517
fullScreen(AR);
1618
}
1719

1820
public void setup() {
19-
noStroke();
21+
tracker = new Tracker(this);
22+
tracker.start();
23+
regAnchors = new ArrayList<Anchor>();
2024
}
2125

2226
public void draw() {
@@ -28,32 +32,64 @@ public void draw() {
2832

2933
lights();
3034

35+
/*
36+
if (mousePressed) {
37+
// Create new anchor at the current touch point
38+
if (selAnchor != null) selAnchor.dispose();
39+
selAnchor = new Anchor(tracker, mouseX, mouseY);
40+
}
41+
*/
42+
43+
// Draw objects attached to each anchor
44+
for (Anchor anchor : regAnchors) {
45+
if (anchor.isTracking()) {
46+
drawBox(anchor, 255, 255, 255);
47+
}
48+
}
49+
50+
// for (int i = : regAnchors){
51+
// if (status == PAR.STOPPED) anchor.dispose();
52+
// }
3153

32-
for (int i = 0; i < trackableCount(); i++) {
33-
int status = trackableStatus(i);
34-
if (status == PAR.PAUSED || status == PAR.STOPPED) continue;
3554

36-
if (status == PAR.CREATED && trackableCount() < 10) {
37-
if (trackableType(i) == PAR.PLANE_WALL) {
38-
createAnchor(i, 0.3f, 0, 0);
55+
if (selAnchor != null) {
56+
drawBox(selAnchor, 255, 0, 0);
57+
}
58+
59+
60+
// Draw trackable planes
61+
for (int i = 0; i < tracker.count(); i++) {
62+
Trackable trackable = tracker.get(i);
63+
64+
if (trackable.isNew()) {
65+
println("IS NEW");
66+
}
67+
68+
if (!trackable.isTracking()) continue;
69+
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);
3975
} else {
40-
createAnchor(i, 0, 0.3f, 0);
76+
anchor = new Anchor(trackable, 0, 0.3f, 0);
4177
}
78+
regAnchors.add(anchor);
4279
}
4380

44-
points = getTrackablePolygon(i, points);
81+
float[] points = trackable.getPolygon();
4582

46-
getTrackableMatrix(i, mat);
4783
pushMatrix();
48-
applyMatrix(mat);
49-
if (trackableSelected(i, mouseX, mouseY)) {
84+
trackable.transform();
85+
if (mousePressed && trackable.selected(mouseX, mouseY)) {
5086
fill(255, 0, 0, 100);
5187
} else {
5288
fill(255, 100);
5389
}
5490

5591
beginShape();
56-
for (int n = 0; n < points.length/2; n++) {
92+
for (int n = 0; n < points.length / 2; n++) {
5793
float x = points[2 * n];
5894
float z = points[2 * n + 1];
5995
vertex(x, 0, z);
@@ -62,38 +98,14 @@ public void draw() {
6298
popMatrix();
6399
}
64100

65-
if (mousePressed) {
66-
oldSelAnchor = selAnchor;
67-
selAnchor = createAnchor(mouseX, mouseY);
68-
}
69-
70-
for (int i = 0; i < anchorCount(); i++) {
71-
int id = anchorId(i);
72-
if (oldSelAnchor == id) {
73-
deleteAnchor(i);
74-
continue;
75-
}
76-
77-
int status = anchorStatus(i);
78-
if (status == PAR.PAUSED || status == PAR.STOPPED) {
79-
if (status == PAR.STOPPED) deleteAnchor(i);
80-
continue;
81-
}
82-
83-
pushMatrix();
84-
anchor(i);
85-
86-
if (selAnchor == id) {
87-
fill(255, 0, 0);
88-
} else {
89-
fill(255);
90-
}
91-
92-
rotateY(angle);
93-
box(0.15f);
94-
popMatrix();
95-
}
96-
97101
angle += 0.1;
98102
}
103+
104+
public void drawBox(Anchor anchor, int r, int g, int b) {
105+
anchor.attach();
106+
fill(r, g, b);
107+
rotateY(angle);
108+
box(0.15f);
109+
anchor.detach();
110+
}
99111
}

mode/libraries/ar/examples/Cubes/Cubes.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void draw() {
5757
int status = trackable.status();
5858
if (status == PAR.PAUSED || status == PAR.STOPPED) continue;
5959

60-
if (status == PAR.CREATED && regAnchor.size() < 10) {
60+
if (status == PAR.CREATED && regAnchors.size() < 10) {
6161
// Add new anchor associated to this trackable, 0.3 meters above it
6262
Anchor anchor;
6363
if (trackable.type() == PAR.PLANE_WALL) {

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class Anchor implements PAR {
66
protected PGraphicsAR g;
7+
private boolean disposed = false;
78

89
private int id;
910
private PMatrix3D m;
@@ -22,26 +23,40 @@ public Anchor(Tracker tracker, int mx, int my) {
2223

2324
public void dispose() {
2425
g.deleteAnchor(id);
26+
disposed = true;
2527
}
2628

2729
public String id() {
2830
return String.valueOf(id);
2931
}
3032

31-
public int status() {
32-
return g.anchorStatus(id);
33-
}
34-
3533
public PMatrix3D matrix() {
34+
m = g.getTrackableMatrix(id, m);
3635
return m;
3736
}
3837

3938
public void attach() {
4039
g.pushMatrix();
41-
g.applyMatrix(matrix());
40+
g.anchor(id);
4241
}
4342

4443
public void detach() {
4544
g.popMatrix();
4645
}
46+
47+
public boolean isTracking() {
48+
return g.anchorStatus(id) == TRACKING;
49+
}
50+
51+
public boolean isPaused() {
52+
return g.anchorStatus(id) == PAUSED;
53+
}
54+
55+
public boolean isStopped() {
56+
return g.anchorStatus(id) == STOPPED;
57+
}
58+
59+
public boolean isDisposed() {
60+
return disposed;
61+
}
4762
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ public interface PAR {
88
static final int PLANE_WALL = 2;
99
static final int POINT = 3;
1010

11-
static final int CREATED = 0;
12-
static final int UPDATED = 1;
13-
static final int TRACKING = 2;
14-
static final int PAUSED = 3;
15-
static final int STOPPED = 4;
11+
static final int TRACKING = 0;
12+
static final int PAUSED = 1;
13+
static final int STOPPED = 2;
1614
}

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class PGraphicsAR extends PGraphics3D {
6565
protected HashMap<Integer, Integer> trackIdx = new HashMap<Integer, Integer>();
6666

6767
protected ArrayList<Plane> newPlanes = new ArrayList<Plane>();
68-
protected ArrayList<Plane> updatedPlanes = new ArrayList<Plane>();
6968
protected ArrayList<Integer> delAnchors = new ArrayList<Integer>();
7069

7170
protected HashMap<Integer, Anchor> anchors = new HashMap<Integer, Anchor>();
@@ -214,7 +213,6 @@ public int trackableIndex(int id) {
214213
return trackIdx.get(id);
215214
}
216215

217-
218216
public int trackableType(int i) {
219217
Plane plane = trackPlanes.get(i);
220218
if (plane.getType() == Plane.Type.HORIZONTAL_UPWARD_FACING) {
@@ -227,23 +225,22 @@ public int trackableType(int i) {
227225
return PAR.UNKNOWN;
228226
}
229227

230-
231228
public int trackableStatus(int i) {
232229
Plane plane = trackPlanes.get(i);
233-
if (newPlanes.contains(plane)) {
234-
return PAR.CREATED;
235-
} else if (updatedPlanes.contains(plane)) {
236-
return PAR.UPDATED;
230+
if (plane.getTrackingState() == TrackingState.PAUSED) {
231+
return PAR.PAUSED;
237232
} else if (plane.getTrackingState() == TrackingState.TRACKING) {
238233
return PAR.TRACKING;
239-
} else if (plane.getTrackingState() == TrackingState.PAUSED) {
240-
return PAR.PAUSED;
241234
} else if (plane.getTrackingState() == TrackingState.STOPPED) {
242235
return PAR.STOPPED;
243236
}
244237
return PAR.UNKNOWN;
245238
}
246239

240+
public boolean trackableNew(int i) {
241+
Plane plane = trackPlanes.get(i);
242+
return newPlanes.contains(plane);
243+
}
247244

248245
public boolean trackableSelected(int i, int mx, int my) {
249246
Plane planei = trackPlanes.get(i);
@@ -413,10 +410,9 @@ protected void updateTrackables() {
413410
}
414411
Pose pose = plane.getCenterPose();
415412
pose.toMatrix(mat, 0);
416-
updatedPlanes.add(plane);
417413
}
418414

419-
// Remove stopped and subsummed trackables, and update indices.
415+
// Remove stopped and subsummed trackables
420416
for (int i = trackPlanes.size() - 1; i >= 0; i--) {
421417
Plane plane = trackPlanes.get(i);
422418
if (plane.getTrackingState() == TrackingState.STOPPED || plane.getSubsumedBy() != null) {
@@ -425,16 +421,19 @@ protected void updateTrackables() {
425421
int pid = trackIds.remove(plane);
426422
trackIdx.remove(pid);
427423
for (Tracker t: trackers) t.remove(pid);
428-
} else {
429-
int pid = trackIds.get(plane);
430-
trackIdx.put(pid, i);
431424
}
432425
}
426+
427+
// Update indices
428+
for (int i = 0; i < trackPlanes.size(); i++) {
429+
Plane plane = trackPlanes.get(i);
430+
int pid = trackIds.get(plane);
431+
trackIdx.put(pid, i);
432+
}
433433
}
434434

435435

436436
protected void cleanup() {
437-
updatedPlanes.clear();
438437
newPlanes.clear();
439438

440439
for (int id: delAnchors) {

0 commit comments

Comments
 (0)