Skip to content

Commit 81a2cd6

Browse files
authored
Merge pull request #745 from p4puniya/ar-image-markers-gsoc2023-v0.2
Adding Augmented Image Dataset( Work in progress. This PR is for code review only.)
2 parents a585e55 + 5257c9a commit 81a2cd6

File tree

4 files changed

+66
-26
lines changed

4 files changed

+66
-26
lines changed

mode/libraries/ar/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
compileOnly name: "android"
1010
compileOnly "org.p5android:processing-core:${modeVersion}"
1111
implementationAar "com.google.ar:core:${garVersion}"
12+
implementation 'com.google.ar:core:1.37.0'
1213
}
1314

1415
task sourceJar(type: Jar) {

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

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import android.view.SurfaceHolder;
2626

2727
import com.google.ar.core.Anchor;
28+
import com.google.ar.core.AugmentedImage;
2829
import com.google.ar.core.HitResult;
2930
import com.google.ar.core.Plane;
3031
import com.google.ar.core.Pose;
3132
import com.google.ar.core.Trackable;
3233
import com.google.ar.core.TrackingState;
34+
import com.google.ar.core.Config;
35+
import com.google.ar.core.Session;
3336

3437
import java.net.URL;
3538
import java.nio.FloatBuffer;
@@ -54,6 +57,7 @@ public class ARGraphics extends PGraphics3D {
5457
static protected final int PLANE_CEILING = 1;
5558
static protected final int PLANE_WALL = 2;
5659
static protected final int POINT = 3;
60+
static protected final int IMAGE = 4;
5761

5862
static protected final int TRACKING = 0;
5963
static protected final int PAUSED = 1;
@@ -70,15 +74,19 @@ public class ARGraphics extends PGraphics3D {
7074
protected float[] colorCorrection = new float[4];
7175

7276
protected ArrayList<ARTracker> trackers = new ArrayList<ARTracker>();
73-
protected ArrayList<Plane> trackPlanes = new ArrayList<Plane>();
77+
protected ArrayList<Trackable> trackObjects = new ArrayList<Trackable>();
7478
protected HashMap<Plane, float[]> trackMatrices = new HashMap<Plane, float[]>();
75-
protected HashMap<Plane, Integer> trackIds = new HashMap<Plane, Integer>();
79+
// protected HashMap<Plane, Integer> trackIds = new HashMap<Plane, Integer>();
7680
protected HashMap<Integer, Integer> trackIdx = new HashMap<Integer, Integer>();
7781

7882
protected ArrayList<Plane> newPlanes = new ArrayList<Plane>();
7983
protected ArrayList<Integer> delAnchors = new ArrayList<Integer>();
8084

8185
protected HashMap<Integer, Anchor> anchors = new HashMap<Integer, Anchor>();
86+
// protected ArrayList<Plane> trackObjects = new ArrayList<Trackable>(); // replace trackPlanes with this
87+
88+
// Use Trackable as the the key's type to it hand hold both Plane and AugmentedImage objects trackImages
89+
protected HashMap<Trackable, Integer> trackIds = new HashMap<Trackable, Integer>();
8290

8391
protected float[] pointIn = new float[3];
8492
protected float[] pointOut = new float[3];
@@ -218,22 +226,19 @@ public void removeTracker(ARTracker tracker) {
218226

219227

220228
public int trackableCount() {
221-
return trackPlanes.size();
229+
return trackObjects.size();
222230
}
223231

224-
225-
226232
public int trackableId(int i) {
227-
return trackIds.get(trackPlanes.get(i));
233+
return trackIds.get(trackObjects.get(i));
228234
}
229235

230-
231236
public int trackableIndex(int id) {
232237
return trackIdx.get(id);
233238
}
234239

235240
public int trackableType(int i) {
236-
Plane plane = trackPlanes.get(i);
241+
Plane plane = (Plane)trackObjects.get(i);
237242
if (plane.getType() == Plane.Type.HORIZONTAL_UPWARD_FACING) {
238243
return PLANE_FLOOR;
239244
} else if (plane.getType() == Plane.Type.HORIZONTAL_DOWNWARD_FACING) {
@@ -245,7 +250,7 @@ public int trackableType(int i) {
245250
}
246251

247252
public int trackableStatus(int i) {
248-
Plane plane = trackPlanes.get(i);
253+
Plane plane = (Plane)trackObjects.get(i);
249254
if (plane.getTrackingState() == TrackingState.PAUSED) {
250255
return PAUSED;
251256
} else if (plane.getTrackingState() == TrackingState.TRACKING) {
@@ -257,12 +262,12 @@ public int trackableStatus(int i) {
257262
}
258263

259264
public boolean trackableNew(int i) {
260-
Plane plane = trackPlanes.get(i);
265+
Plane plane = (Plane)trackObjects.get(i);
261266
return newPlanes.contains(plane);
262267
}
263268

264269
public boolean trackableSelected(int i, int mx, int my) {
265-
Plane planei = trackPlanes.get(i);
270+
Plane planei = (Plane)trackObjects.get(i);
266271
for (HitResult hit : surfar.frame.hitTest(mx, my)) {
267272
Trackable trackable = hit.getTrackable();
268273
if (trackable instanceof Plane) {
@@ -281,7 +286,7 @@ protected HitResult getHitResult(int mx, int my) {
281286
Trackable trackable = hit.getTrackable();
282287
if (trackable instanceof Plane) {
283288
Plane plane = (Plane)trackable;
284-
if (trackPlanes.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
289+
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
285290
return hit;
286291
}
287292
}
@@ -291,7 +296,7 @@ protected HitResult getHitResult(int mx, int my) {
291296

292297
protected int getTrackable(HitResult hit) {
293298
Plane plane = (Plane) hit.getTrackable();
294-
return trackPlanes.indexOf(plane);
299+
return trackObjects.indexOf(plane);
295300
}
296301

297302
public float[] getTrackablePolygon(int i) {
@@ -300,7 +305,7 @@ public float[] getTrackablePolygon(int i) {
300305

301306

302307
public float[] getTrackablePolygon(int i, float[] points) {
303-
Plane plane = trackPlanes.get(i);
308+
Plane plane = (Plane)trackObjects.get(i);
304309
FloatBuffer buffer = plane.getPolygon();
305310
buffer.rewind();
306311
if (points == null || points.length < buffer.capacity()) {
@@ -312,13 +317,13 @@ public float[] getTrackablePolygon(int i, float[] points) {
312317

313318

314319
public float getTrackableExtentX(int i) {
315-
Plane plane = trackPlanes.get(i);
320+
Plane plane = (Plane)trackObjects.get(i);
316321
return plane.getExtentX();
317322
}
318323

319324

320325
public float getTrackableExtentZ(int i) {
321-
Plane plane = trackPlanes.get(i);
326+
Plane plane = (Plane)trackObjects.get(i);
322327
return plane.getExtentZ();
323328
}
324329

@@ -332,7 +337,7 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
332337
target = new PMatrix3D();
333338
}
334339

335-
Plane plane = trackPlanes.get(i);
340+
Plane plane = (Plane)trackObjects.get(i);
336341
float[] mat = trackMatrices.get(plane);
337342
target.set(mat[0], mat[4], mat[8], mat[12],
338343
mat[1], mat[5], mat[9], mat[13],
@@ -344,7 +349,7 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
344349

345350

346351
public int createAnchor(int i, float x, float y, float z) {
347-
Plane plane = trackPlanes.get(i);
352+
Plane plane = (Plane)trackObjects.get(i);
348353
Pose planePose = plane.getCenterPose();
349354
pointIn[0] = x;
350355
pointIn[1] = y;
@@ -362,7 +367,7 @@ public int createAnchor(int mx, int my) {
362367
Trackable trackable = hit.getTrackable();
363368
if (trackable instanceof Plane) {
364369
Plane plane = (Plane)trackable;
365-
if (trackPlanes.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
370+
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
366371
return createAnchor(hit);
367372
}
368373
}
@@ -449,6 +454,7 @@ protected void updateMatrices() {
449454

450455
protected void updateTrackables() {
451456
Collection<Plane> planes = surfar.frame.getUpdatedTrackables(Plane.class);
457+
452458
for (Plane plane: planes) {
453459
if (plane.getSubsumedBy() != null) continue;
454460
float[] mat;
@@ -457,7 +463,7 @@ protected void updateTrackables() {
457463
} else {
458464
mat = new float[16];
459465
trackMatrices.put(plane, mat);
460-
trackPlanes.add(plane);
466+
trackObjects.add(plane);
461467
trackIds.put(plane, ++lastTrackableId);
462468
newPlanes.add(plane);
463469
}
@@ -466,10 +472,10 @@ protected void updateTrackables() {
466472
}
467473

468474
// Remove stopped and subsummed trackables
469-
for (int i = trackPlanes.size() - 1; i >= 0; i--) {
470-
Plane plane = trackPlanes.get(i);
475+
for (int i = trackObjects.size() - 1; i >= 0; i--) {
476+
Plane plane = (Plane)trackObjects.get(i);
471477
if (plane.getTrackingState() == TrackingState.STOPPED || plane.getSubsumedBy() != null) {
472-
trackPlanes.remove(i);
478+
trackObjects.remove(i);
473479
trackMatrices.remove(plane);
474480
int pid = trackIds.remove(plane);
475481
trackIdx.remove(pid);
@@ -478,16 +484,24 @@ protected void updateTrackables() {
478484
}
479485

480486
// Update indices
481-
for (int i = 0; i < trackPlanes.size(); i++) {
482-
Plane plane = trackPlanes.get(i);
487+
for (int i = 0; i < trackObjects.size(); i++) {
488+
Plane plane = (Plane)trackObjects.get(i);
483489
int pid = trackIds.get(plane);
484490
trackIdx.put(pid, i);
485491
if (newPlanes.contains(plane)) {
486492
for (ARTracker t: trackers) t.create(i);
487493
}
488494
}
495+
//Augmented Images
496+
Collection<AugmentedImage> images = surfar.frame.getUpdatedTrackables(AugmentedImage.class);
497+
for (AugmentedImage image: images) {
498+
trackObjects.add(image);
499+
}
489500
}
490501

502+
// public int trackableId(int i) {
503+
// return trackIds.get(trackObjects.get(i));
504+
// }
491505

492506
protected void cleanup() {
493507
newPlanes.clear();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public boolean isFloorPlane() {
115115
return g.trackableType(idx) == ARGraphics.PLANE_FLOOR;
116116
}
117117

118+
public boolean isImage(){
119+
int idx = g.trackableIndex(id);
120+
return g.trackableType(idx)== ARGraphics.IMAGE;
121+
}
122+
118123
public boolean isCeilingPlane() {
119124
int idx = g.trackableIndex(id);
120125
return g.trackableType(idx) == ARGraphics.PLANE_CEILING;

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222

2323
package processing.ar;
2424

25+
import android.graphics.Bitmap;
26+
2527
import com.google.ar.core.HitResult;
28+
import com.google.ar.core.AugmentedImageDatabase;
29+
import com.google.ar.core.Config;
30+
import com.google.ar.core.Session;
2631

2732
import java.lang.reflect.Method;
2833
import java.util.ArrayList;
@@ -31,19 +36,34 @@
3136
import java.util.Set;
3237

3338
import processing.core.PApplet;
39+
import processing.core.PImage;
3440

3541
public class ARTracker {
3642
protected PApplet p;
37-
protected ARGraphics g;
43+
protected ARGraphics g,arg;
44+
protected AugmentedImageDatabase imgDB;
3845

3946
private HashMap<String, ARTrackable> trackables = new HashMap<String, ARTrackable>();
4047
private ArrayList<ARAnchor> toRemove = new ArrayList<ARAnchor>();
4148
private Method trackableEventMethod;
49+
private Session session;
4250

4351
public ARTracker(PApplet parent) {
4452
this.p = parent;
4553
this.g = (ARGraphics) p.g;
4654
setEventHandler();
55+
this.arg = (ARGraphics)parent.g;
56+
this.imgDB = new AugmentedImageDatabase(arg.surfar.session); //A new Database has been created.
57+
}
58+
59+
public void addImage(String name, PImage img) {
60+
Bitmap bitmap = (Bitmap)img.getNative();
61+
int imgIndex = imgDB.addImage(name,bitmap); // User-provided or auto-generated name here, physical size argument seems optional
62+
63+
// Re-set the session config with the updated image database
64+
Config config = new Config(session);
65+
config.setAugmentedImageDatabase(imgDB);
66+
session.configure(config);
4767
}
4868

4969
public void start() {

0 commit comments

Comments
 (0)