3
3
/*
4
4
Part of the Processing project - http://processing.org
5
5
6
- Copyright (c) 2019 The Processing Foundation
6
+ Copyright (c) 2019-23 The Processing Foundation
7
7
8
8
This library is free software; you can redistribute it and/or
9
9
modify it under the terms of the GNU Lesser General Public
@@ -75,18 +75,14 @@ public class ARGraphics extends PGraphics3D {
75
75
76
76
protected ArrayList <ARTracker > trackers = new ArrayList <ARTracker >();
77
77
protected ArrayList <Trackable > trackObjects = new ArrayList <Trackable >();
78
- protected HashMap <Plane , float []> trackMatrices = new HashMap <Plane , float []>();
79
- // protected HashMap<Plane , Integer> trackIds = new HashMap<Plane , Integer>();
78
+ protected HashMap <Trackable , float []> trackMatrices = new HashMap <Trackable , float []>();
79
+ protected HashMap <Trackable , Integer > trackIds = new HashMap <Trackable , Integer >();
80
80
protected HashMap <Integer , Integer > trackIdx = new HashMap <Integer , Integer >();
81
81
82
- protected ArrayList <Plane > newPlanes = new ArrayList <Plane >();
82
+ protected ArrayList <Trackable > newObjects = new ArrayList <Trackable >();
83
83
protected ArrayList <Integer > delAnchors = new ArrayList <Integer >();
84
84
85
85
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 >();
90
86
91
87
protected float [] pointIn = new float [3 ];
92
88
protected float [] pointOut = new float [3 ];
@@ -229,52 +225,60 @@ public int trackableCount() {
229
225
return trackObjects .size ();
230
226
}
231
227
228
+
232
229
public int trackableId (int i ) {
233
230
return trackIds .get (trackObjects .get (i ));
234
231
}
235
232
233
+
236
234
public int trackableIndex (int id ) {
237
235
return trackIdx .get (id );
238
236
}
239
237
238
+
240
239
public int trackableType (int i ) {
241
- Plane plane = (Plane )trackObjects .get (i );
242
- if (plane .getType () == Plane .Type .HORIZONTAL_UPWARD_FACING ) {
243
- return PLANE_FLOOR ;
244
- } else if (plane .getType () == Plane .Type .HORIZONTAL_DOWNWARD_FACING ) {
245
- return PLANE_CEILING ;
246
- } else if (plane .getType () == Plane .Type .VERTICAL ) {
247
- return PLANE_WALL ;
240
+ Trackable track = trackObjects .get (i );
241
+ if (track instanceof Plane ) {
242
+ Plane plane = (Plane )track ;
243
+ if (plane .getType () == Plane .Type .HORIZONTAL_UPWARD_FACING ) {
244
+ return PLANE_FLOOR ;
245
+ } else if (plane .getType () == Plane .Type .HORIZONTAL_DOWNWARD_FACING ) {
246
+ return PLANE_CEILING ;
247
+ } else if (plane .getType () == Plane .Type .VERTICAL ) {
248
+ return PLANE_WALL ;
249
+ }
250
+ } else if (track instanceof AugmentedImage ) {
251
+ return IMAGE ;
248
252
}
249
253
return UNKNOWN ;
250
254
}
251
255
256
+
252
257
public int trackableStatus (int i ) {
253
- Plane plane = ( Plane ) trackObjects .get (i );
254
- if (plane .getTrackingState () == TrackingState .PAUSED ) {
258
+ Trackable track = trackObjects .get (i );
259
+ if (track .getTrackingState () == TrackingState .PAUSED ) {
255
260
return PAUSED ;
256
- } else if (plane .getTrackingState () == TrackingState .TRACKING ) {
261
+ } else if (track .getTrackingState () == TrackingState .TRACKING ) {
257
262
return TRACKING ;
258
- } else if (plane .getTrackingState () == TrackingState .STOPPED ) {
263
+ } else if (track .getTrackingState () == TrackingState .STOPPED ) {
259
264
return STOPPED ;
260
265
}
261
266
return UNKNOWN ;
262
267
}
263
268
269
+
264
270
public boolean trackableNew (int i ) {
265
- Plane plane = ( Plane ) trackObjects .get (i );
266
- return newPlanes .contains (plane );
271
+ Trackable track = trackObjects .get (i );
272
+ return newObjects .contains (track );
267
273
}
268
274
275
+
269
276
public boolean trackableSelected (int i , int mx , int my ) {
270
- Plane planei = ( Plane ) trackObjects .get (i );
277
+ Trackable tracki = trackObjects .get (i );
271
278
for (HitResult hit : surfar .frame .hitTest (mx , my )) {
272
279
Trackable trackable = hit .getTrackable ();
273
- if (trackable instanceof Plane ) {
274
- Plane plane = (Plane )trackable ;
275
- if (planei .equals (plane ) && plane .isPoseInPolygon (hit .getHitPose ())) {
276
- return true ;
277
- }
280
+ if (tracki .equals (trackable ) && trackable .isPoseInPolygon (hit .getHitPose ())) {
281
+ return true ;
278
282
}
279
283
}
280
284
return false ;
@@ -284,29 +288,28 @@ public boolean trackableSelected(int i, int mx, int my) {
284
288
protected HitResult getHitResult (int mx , int my ) {
285
289
for (HitResult hit : surfar .frame .hitTest (mx , my )) {
286
290
Trackable trackable = hit .getTrackable ();
287
- if (trackable instanceof Plane ) {
288
- Plane plane = (Plane )trackable ;
289
- if (trackObjects .contains (plane ) && plane .isPoseInPolygon (hit .getHitPose ())) {
290
- return hit ;
291
- }
291
+ if (trackObjects .contains (trackable ) && trackable .isPoseInPolygon (hit .getHitPose ())) {
292
+ return hit ;
292
293
}
293
294
}
294
295
return null ;
295
296
}
296
297
298
+
297
299
protected int getTrackable (HitResult hit ) {
298
- Plane plane = ( Plane ) hit .getTrackable ();
299
- return trackObjects .indexOf (plane );
300
+ Trackable track = hit .getTrackable ();
301
+ return trackObjects .indexOf (track );
300
302
}
301
303
304
+
302
305
public float [] getTrackablePolygon (int i ) {
303
306
return getTrackablePolygon (i , null );
304
307
}
305
308
306
309
307
310
public float [] getTrackablePolygon (int i , float [] points ) {
308
- Plane plane = ( Plane ) trackObjects .get (i );
309
- FloatBuffer buffer = plane .getPolygon ();
311
+ Trackable track = trackObjects .get (i );
312
+ FloatBuffer buffer = track .getPolygon ();
310
313
buffer .rewind ();
311
314
if (points == null || points .length < buffer .capacity ()) {
312
315
points = new float [buffer .capacity ()];
@@ -317,14 +320,14 @@ public float[] getTrackablePolygon(int i, float[] points) {
317
320
318
321
319
322
public float getTrackableExtentX (int i ) {
320
- Plane plane = ( Plane ) trackObjects .get (i );
321
- return plane .getExtentX ();
323
+ Trackable track = trackObjects .get (i );
324
+ return track .getExtentX ();
322
325
}
323
326
324
327
325
328
public float getTrackableExtentZ (int i ) {
326
- Plane plane = ( Plane ) trackObjects .get (i );
327
- return plane .getExtentZ ();
329
+ Trackable track = trackObjects .get (i );
330
+ return track .getExtentZ ();
328
331
}
329
332
330
333
public PMatrix3D getTrackableMatrix (int i ) {
@@ -349,14 +352,14 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
349
352
350
353
351
354
public int createAnchor (int i , float x , float y , float z ) {
352
- Plane plane = ( Plane ) trackObjects .get (i );
353
- Pose planePose = plane .getCenterPose ();
355
+ Trackable track = trackObjects .get (i );
356
+ Pose trackPose = track .getCenterPose ();
354
357
pointIn [0 ] = x ;
355
358
pointIn [1 ] = y ;
356
359
pointIn [2 ] = z ;
357
- planePose .transformPoint (pointIn , 0 , pointOut , 0 );
360
+ trackPose .transformPoint (pointIn , 0 , pointOut , 0 );
358
361
Pose anchorPose = Pose .makeTranslation (pointOut );
359
- Anchor anchor = plane .createAnchor (anchorPose );
362
+ Anchor anchor = track .createAnchor (anchorPose );
360
363
anchors .put (++lastAnchorId , anchor );
361
364
return lastAnchorId ;
362
365
}
@@ -365,11 +368,8 @@ public int createAnchor(int i, float x, float y, float z) {
365
368
public int createAnchor (int mx , int my ) {
366
369
for (HitResult hit : surfar .frame .hitTest (mx , my )) {
367
370
Trackable trackable = hit .getTrackable ();
368
- if (trackable instanceof Plane ) {
369
- Plane plane = (Plane )trackable ;
370
- if (trackObjects .contains (plane ) && plane .isPoseInPolygon (hit .getHitPose ())) {
371
- return createAnchor (hit );
372
- }
371
+ if (trackObjects .contains (trackable ) && trackable .isPoseInPolygon (hit .getHitPose ())) {
372
+ return createAnchor (hit );
373
373
}
374
374
}
375
375
return 0 ;
@@ -441,10 +441,12 @@ protected void createBackgroundRenderer() {
441
441
backgroundRenderer = new BackgroundRenderer (surfar .getActivity ());
442
442
}
443
443
444
+
444
445
protected void setCameraTexture () {
445
446
surfar .session .setCameraTextureName (backgroundRenderer .getTextureId ());
446
447
}
447
448
449
+
448
450
protected void updateMatrices () {
449
451
surfar .camera .getProjectionMatrix (projMatrix , 0 , 0.1f , 100.0f );
450
452
surfar .camera .getViewMatrix (viewMatrix , 0 );
@@ -454,58 +456,59 @@ protected void updateMatrices() {
454
456
455
457
protected void updateTrackables () {
456
458
Collection <Plane > planes = surfar .frame .getUpdatedTrackables (Plane .class );
457
-
458
459
for (Plane plane : planes ) {
459
- if (plane .getSubsumedBy () != null ) continue ;
460
- float [] mat ;
461
- if (trackMatrices .containsKey (plane )) {
462
- mat = trackMatrices .get (plane );
463
- } else {
464
- mat = new float [16 ];
465
- trackMatrices .put (plane , mat );
466
- trackObjects .add (plane );
467
- trackIds .put (plane , ++lastTrackableId );
468
- newPlanes .add (plane );
469
- }
470
- Pose pose = plane .getCenterPose ();
471
- pose .toMatrix (mat , 0 );
460
+ addNewObject (plane );
472
461
}
473
462
463
+
464
+ Collection <AugmentedImage > images = surfar .frame .getUpdatedTrackables (AugmentedImage .class );
465
+ for (AugmentedImage image : images ) {
466
+ addNewObject (image );
467
+ }
468
+
469
+
474
470
// Remove stopped and subsummed trackables
475
471
for (int i = trackObjects .size () - 1 ; i >= 0 ; i --) {
476
- Plane plane = ( Plane ) trackObjects .get (i );
477
- if (plane .getTrackingState () == TrackingState .STOPPED || plane .getSubsumedBy () != null ) {
472
+ Trackable track = trackObjects .get (i );
473
+ if (track .getTrackingState () == TrackingState .STOPPED || track .getSubsumedBy () != null ) {
478
474
trackObjects .remove (i );
479
- trackMatrices .remove (plane );
480
- int pid = trackIds .remove (plane );
475
+ trackMatrices .remove (track );
476
+ int pid = trackIds .remove (track );
481
477
trackIdx .remove (pid );
482
478
for (ARTracker t : trackers ) t .remove (pid );
483
479
}
484
480
}
485
481
486
482
// Update indices
487
483
for (int i = 0 ; i < trackObjects .size (); i ++) {
488
- Plane plane = ( Plane ) trackObjects .get (i );
489
- int pid = trackIds .get (plane );
484
+ Trackable track = trackObjects .get (i );
485
+ int pid = trackIds .get (track );
490
486
trackIdx .put (pid , i );
491
- if (newPlanes .contains (plane )) {
487
+ if (newObjects .contains (track )) {
492
488
for (ARTracker t : trackers ) t .create (i );
493
489
}
494
490
}
495
- //Augmented Images
496
- Collection <AugmentedImage > images = surfar .frame .getUpdatedTrackables (AugmentedImage .class );
497
- for (AugmentedImage image : images ) {
498
- trackObjects .add (image );
491
+ }
492
+
493
+ protected void addNewObject (Trackable track ) {
494
+ if (track .getSubsumedBy () != null ) return ;
495
+ float [] mat ;
496
+ if (trackMatrices .containsKey (track )) {
497
+ mat = trackMatrices .get (track );
498
+ } else {
499
+ mat = new float [16 ];
500
+ trackMatrices .put (track , mat );
501
+ trackObjects .add (track );
502
+ trackIds .put (track , ++lastTrackableId );
503
+ newObjects .add (track );
499
504
}
505
+ Pose pose = track .getCenterPose ();
506
+ pose .toMatrix (mat , 0 );
500
507
}
501
508
502
- // public int trackableId(int i) {
503
- // return trackIds.get(trackObjects.get(i));
504
- // }
505
509
506
510
protected void cleanup () {
507
- newPlanes .clear ();
508
-
511
+ newObjects .clear ();
509
512
for (int id : delAnchors ) {
510
513
Anchor anchor = anchors .remove (id );
511
514
anchor .detach ();
0 commit comments