Skip to content

Commit a4c219b

Browse files
committed
code cleanup, added example
1 parent b7f6500 commit a4c219b

File tree

6 files changed

+94
-33
lines changed

6 files changed

+94
-33
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import processing.ar.*;
2+
3+
ARTracker tracker;
4+
ARAnchor anchor;
5+
PShape earth;
6+
7+
void setup() {
8+
fullScreen(AR);
9+
10+
tracker = new ARTracker(this);
11+
12+
PImage earthImg = loadImage("earth.jpg");
13+
tracker.start();
14+
tracker.addImage("earth", earthImg);
15+
16+
earth = createShape(SPHERE, 0.5);
17+
}
18+
19+
void draw() {
20+
lights();
21+
22+
if (mousePressed) {
23+
// Create new anchor at the current touch point
24+
if (anchor != null) anchor.dispose();
25+
ARTrackable hit = tracker.get(mouseX, mouseY);
26+
if (hit != null && hit.isImage() && hit.getName().equals("earth")) anchor = new ARAnchor(hit);
27+
else anchor = null;
28+
}
29+
30+
if (anchor != null) {
31+
anchor.attach();
32+
shape(earth);
33+
anchor.detach();
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
component=ar
230 KB
Loading

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public class ARGraphics extends PGraphics3D {
102102
protected PShader arLightShader;
103103
protected PShader arTexlightShader;
104104

105+
105106
public ARGraphics() {
106107
}
107108

@@ -236,6 +237,16 @@ public int trackableIndex(int id) {
236237
}
237238

238239

240+
public String trackableName(int i) {
241+
Trackable track = trackObjects.get(i);
242+
if (track instanceof AugmentedImage) {
243+
AugmentedImage img = ((AugmentedImage)track);
244+
return img.getName();
245+
}
246+
return null;
247+
}
248+
249+
239250
public int trackableType(int i) {
240251
Trackable track = trackObjects.get(i);
241252
if (track instanceof Plane) {
@@ -558,13 +569,12 @@ protected void updateMatrices() {
558569
protected void updateTrackables() {
559570
Collection<Plane> planes = surfar.frame.getUpdatedTrackables(Plane.class);
560571
for (Plane plane: planes) {
561-
addNewObject(plane);
572+
addNewPlane(plane);
562573
}
563574

564-
565575
Collection<AugmentedImage> images = surfar.frame.getUpdatedTrackables(AugmentedImage.class);
566576
for (AugmentedImage image: images) {
567-
addNewObject(image);
577+
addNewImage(image);
568578
}
569579

570580
// Remove stopped and subsummed trackables
@@ -593,23 +603,34 @@ protected void updateTrackables() {
593603
}
594604
}
595605

596-
protected void addNewObject(Trackable track) {
597-
boolean isPlane = track instanceof Plane;
598-
if (isPlane && ((Plane)track).getSubsumedBy() != null) return;
599-
float[] mat;
600-
if (trackMatrices.containsKey(track)) {
601-
mat = trackMatrices.get(track);
606+
607+
protected void addNewPlane(Plane plane) {
608+
if (plane.getSubsumedBy() != null) return;
609+
float[] mat = addNewMatrix(plane);
610+
Pose pose = plane.getCenterPose();
611+
if (pose != null) pose.toMatrix(mat, 0);
612+
}
613+
614+
615+
protected void addNewImage(AugmentedImage image) {
616+
float[] mat = addNewMatrix(image);
617+
Pose pose = image.getCenterPose();
618+
if (pose != null) pose.toMatrix(mat, 0);
619+
}
620+
621+
622+
protected float[] addNewMatrix(Trackable obj) {
623+
float[] mat;
624+
if (trackMatrices.containsKey(obj)) {
625+
mat = trackMatrices.get(obj);
602626
} else {
603627
mat = new float[16];
604-
trackMatrices.put(track, mat);
605-
trackObjects.add(track);
606-
trackIds.put(track, ++lastTrackableId);
607-
newObjects.add(track);
628+
trackMatrices.put(obj, mat);
629+
trackObjects.add(obj);
630+
trackIds.put(obj, ++lastTrackableId);
631+
newObjects.add(obj);
608632
}
609-
if (isPlane) {
610-
Pose pose = ((Plane)track).getCenterPose();
611-
pose.toMatrix(mat, 0);
612-
}
633+
return mat;
613634
}
614635

615636

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ public class ARTrackable {
3030
protected ARGraphics g;
3131
protected HitResult hit;
3232

33+
private String name;
3334
private int id;
3435
private PMatrix3D m;
3536
private float[] points;
3637

37-
public ARTrackable(ARGraphics g, int id) {
38+
public ARTrackable(ARGraphics g, int id, String name) {
3839
this.g = g;
3940
this.id = id;
41+
this.name = name;
4042
}
41-
43+
4244
public String id() {
4345
return String.valueOf(id);
4446
}
@@ -48,7 +50,7 @@ public PMatrix3D matrix() {
4850
m = g.getTrackableMatrix(idx, m);
4951
return m;
5052
}
51-
53+
5254
public void transform() {
5355
g.applyMatrix(matrix());
5456
}
@@ -59,23 +61,23 @@ public float[] getPolygon() {
5961
return points;
6062
}
6163

62-
6364
public float lengthX() {
6465
int idx = g.trackableIndex(id);
6566
return g.getTrackableExtentX(idx);
6667
}
6768

68-
6969
public float lengthY() {
7070
return 0;
7171
}
7272

73-
7473
public float lengthZ() {
7574
int idx = g.trackableIndex(id);
7675
return g.getTrackableExtentZ(idx);
7776
}
7877

78+
public String getName() {
79+
return name;
80+
}
7981

8082
public boolean isSelected(int mx, int my) {
8183
int idx = g.trackableIndex(id);

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class ARTracker {
4242
protected PApplet p;
4343
protected ARGraphics g;
44-
protected AugmentedImageDatabase imgDB;
44+
protected AugmentedImageDatabase db;
4545

4646
private HashMap<String, ARTrackable> trackables = new HashMap<String, ARTrackable>();
4747
private ArrayList<ARAnchor> toRemove = new ArrayList<ARAnchor>();
@@ -50,20 +50,22 @@ public class ARTracker {
5050

5151
public ARTracker(PApplet parent) {
5252
this.p = parent;
53-
this.g = (ARGraphics) p.g;
53+
this.g = (ARGraphics)p.g;
5454
setEventHandler();
55-
ARGraphics arg = (ARGraphics)parent.g;
56-
this.imgDB = new AugmentedImageDatabase(arg.surfar.session); //A new Database has been created.
5755
}
5856

5957
public void addImage(String name, PImage img) {
58+
if (db == null) {
59+
// Creating a new database of augmented images.
60+
db = new AugmentedImageDatabase(g.surfar.session);
61+
}
62+
6063
Bitmap bitmap = (Bitmap)img.getNative();
61-
62-
imgDB.addImage(name, bitmap); // User-provided or auto-generated name here, physical size argument seems optional
64+
db.addImage(name, bitmap);
6365

64-
// Re-set the session config with the updated image database
66+
// Reset the session config with the updated image database
6567
Config config = new Config(session);
66-
config.setAugmentedImageDatabase(imgDB);
68+
config.setAugmentedImageDatabase(db);
6769
session.configure(config);
6870
}
6971

@@ -82,9 +84,10 @@ public int count() {
8284

8385
public ARTrackable get(int idx) {
8486
int id = g.trackableId(idx);
87+
String name = g.trackableName(idx);
8588
String sid = String.valueOf(id);
8689
if (!trackables.containsKey(sid)) {
87-
ARTrackable t = new ARTrackable(g, id);
90+
ARTrackable t = new ARTrackable(g, id, name);
8891
trackables.put(sid, t);
8992
}
9093
return get(sid);
@@ -149,7 +152,6 @@ protected void remove(String id) {
149152
trackables.remove(id);
150153
}
151154

152-
153155
protected void setEventHandler() {
154156
try {
155157
trackableEventMethod = p.getClass().getMethod("trackableEvent", ARTrackable.class);

0 commit comments

Comments
 (0)