Skip to content

Commit 50b8728

Browse files
author
gaurav
committed
Adding Augmented Image Dataset
1 parent fc0dc61 commit 50b8728

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
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: 38 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;
@@ -70,15 +73,19 @@ public class ARGraphics extends PGraphics3D {
7073
protected float[] colorCorrection = new float[4];
7174

7275
protected ArrayList<ARTracker> trackers = new ArrayList<ARTracker>();
73-
protected ArrayList<Plane> trackPlanes = new ArrayList<Plane>();
76+
protected ArrayList<Trackable> trackObjects = new ArrayList<Trackable>();
7477
protected HashMap<Plane, float[]> trackMatrices = new HashMap<Plane, float[]>();
75-
protected HashMap<Plane, Integer> trackIds = new HashMap<Plane, Integer>();
78+
// protected HashMap<Plane, Integer> trackIds = new HashMap<Plane, Integer>();
7679
protected HashMap<Integer, Integer> trackIdx = new HashMap<Integer, Integer>();
7780

7881
protected ArrayList<Plane> newPlanes = new ArrayList<Plane>();
7982
protected ArrayList<Integer> delAnchors = new ArrayList<Integer>();
8083

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

8390
protected float[] pointIn = new float[3];
8491
protected float[] pointOut = new float[3];
@@ -218,22 +225,19 @@ public void removeTracker(ARTracker tracker) {
218225

219226

220227
public int trackableCount() {
221-
return trackPlanes.size();
228+
return trackObjects.size();
222229
}
223230

224-
225-
226231
public int trackableId(int i) {
227-
return trackIds.get(trackPlanes.get(i));
232+
return trackIds.get(trackObjects.get(i));
228233
}
229234

230-
231235
public int trackableIndex(int id) {
232236
return trackIdx.get(id);
233237
}
234238

235239
public int trackableType(int i) {
236-
Plane plane = trackPlanes.get(i);
240+
Plane plane = (Plane)trackObjects.get(i);
237241
if (plane.getType() == Plane.Type.HORIZONTAL_UPWARD_FACING) {
238242
return PLANE_FLOOR;
239243
} else if (plane.getType() == Plane.Type.HORIZONTAL_DOWNWARD_FACING) {
@@ -245,7 +249,7 @@ public int trackableType(int i) {
245249
}
246250

247251
public int trackableStatus(int i) {
248-
Plane plane = trackPlanes.get(i);
252+
Plane plane = (Plane)trackObjects.get(i);
249253
if (plane.getTrackingState() == TrackingState.PAUSED) {
250254
return PAUSED;
251255
} else if (plane.getTrackingState() == TrackingState.TRACKING) {
@@ -257,12 +261,12 @@ public int trackableStatus(int i) {
257261
}
258262

259263
public boolean trackableNew(int i) {
260-
Plane plane = trackPlanes.get(i);
264+
Plane plane = (Plane)trackObjects.get(i);
261265
return newPlanes.contains(plane);
262266
}
263267

264268
public boolean trackableSelected(int i, int mx, int my) {
265-
Plane planei = trackPlanes.get(i);
269+
Plane planei = (Plane)trackObjects.get(i);
266270
for (HitResult hit : surfar.frame.hitTest(mx, my)) {
267271
Trackable trackable = hit.getTrackable();
268272
if (trackable instanceof Plane) {
@@ -281,7 +285,7 @@ protected HitResult getHitResult(int mx, int my) {
281285
Trackable trackable = hit.getTrackable();
282286
if (trackable instanceof Plane) {
283287
Plane plane = (Plane)trackable;
284-
if (trackPlanes.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
288+
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
285289
return hit;
286290
}
287291
}
@@ -291,7 +295,7 @@ protected HitResult getHitResult(int mx, int my) {
291295

292296
protected int getTrackable(HitResult hit) {
293297
Plane plane = (Plane) hit.getTrackable();
294-
return trackPlanes.indexOf(plane);
298+
return trackObjects.indexOf(plane);
295299
}
296300

297301
public float[] getTrackablePolygon(int i) {
@@ -300,7 +304,7 @@ public float[] getTrackablePolygon(int i) {
300304

301305

302306
public float[] getTrackablePolygon(int i, float[] points) {
303-
Plane plane = trackPlanes.get(i);
307+
Plane plane = (Plane)trackObjects.get(i);
304308
FloatBuffer buffer = plane.getPolygon();
305309
buffer.rewind();
306310
if (points == null || points.length < buffer.capacity()) {
@@ -312,13 +316,13 @@ public float[] getTrackablePolygon(int i, float[] points) {
312316

313317

314318
public float getTrackableExtentX(int i) {
315-
Plane plane = trackPlanes.get(i);
319+
Plane plane = (Plane)trackObjects.get(i);
316320
return plane.getExtentX();
317321
}
318322

319323

320324
public float getTrackableExtentZ(int i) {
321-
Plane plane = trackPlanes.get(i);
325+
Plane plane = (Plane)trackObjects.get(i);
322326
return plane.getExtentZ();
323327
}
324328

@@ -332,7 +336,7 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
332336
target = new PMatrix3D();
333337
}
334338

335-
Plane plane = trackPlanes.get(i);
339+
Plane plane = (Plane)trackObjects.get(i);
336340
float[] mat = trackMatrices.get(plane);
337341
target.set(mat[0], mat[4], mat[8], mat[12],
338342
mat[1], mat[5], mat[9], mat[13],
@@ -344,7 +348,7 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
344348

345349

346350
public int createAnchor(int i, float x, float y, float z) {
347-
Plane plane = trackPlanes.get(i);
351+
Plane plane = (Plane)trackObjects.get(i);
348352
Pose planePose = plane.getCenterPose();
349353
pointIn[0] = x;
350354
pointIn[1] = y;
@@ -362,7 +366,7 @@ public int createAnchor(int mx, int my) {
362366
Trackable trackable = hit.getTrackable();
363367
if (trackable instanceof Plane) {
364368
Plane plane = (Plane)trackable;
365-
if (trackPlanes.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
369+
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
366370
return createAnchor(hit);
367371
}
368372
}
@@ -449,6 +453,7 @@ protected void updateMatrices() {
449453

450454
protected void updateTrackables() {
451455
Collection<Plane> planes = surfar.frame.getUpdatedTrackables(Plane.class);
456+
452457
for (Plane plane: planes) {
453458
if (plane.getSubsumedBy() != null) continue;
454459
float[] mat;
@@ -457,7 +462,7 @@ protected void updateTrackables() {
457462
} else {
458463
mat = new float[16];
459464
trackMatrices.put(plane, mat);
460-
trackPlanes.add(plane);
465+
trackObjects.add(plane);
461466
trackIds.put(plane, ++lastTrackableId);
462467
newPlanes.add(plane);
463468
}
@@ -466,10 +471,10 @@ protected void updateTrackables() {
466471
}
467472

468473
// Remove stopped and subsummed trackables
469-
for (int i = trackPlanes.size() - 1; i >= 0; i--) {
470-
Plane plane = trackPlanes.get(i);
474+
for (int i = trackObjects.size() - 1; i >= 0; i--) {
475+
Plane plane = (Plane)trackObjects.get(i);
471476
if (plane.getTrackingState() == TrackingState.STOPPED || plane.getSubsumedBy() != null) {
472-
trackPlanes.remove(i);
477+
trackObjects.remove(i);
473478
trackMatrices.remove(plane);
474479
int pid = trackIds.remove(plane);
475480
trackIdx.remove(pid);
@@ -478,16 +483,24 @@ protected void updateTrackables() {
478483
}
479484

480485
// Update indices
481-
for (int i = 0; i < trackPlanes.size(); i++) {
482-
Plane plane = trackPlanes.get(i);
486+
for (int i = 0; i < trackObjects.size(); i++) {
487+
Plane plane = (Plane)trackObjects.get(i);
483488
int pid = trackIds.get(plane);
484489
trackIdx.put(pid, i);
485490
if (newPlanes.contains(plane)) {
486491
for (ARTracker t: trackers) t.create(i);
487492
}
488493
}
494+
//Augmented Images
495+
Collection<AugmentedImage> images = surfar.frame.getUpdatedTrackables(AugmentedImage.class);
496+
for (AugmentedImage image: images) {
497+
trackObjects.add(image);
498+
}
489499
}
490500

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

492505
protected void cleanup() {
493506
newPlanes.clear();

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

Lines changed: 20 additions & 0 deletions
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;
3743
protected ARGraphics g;
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+
ARGraphics arg = (ARGraphics)parent.g;
56+
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)