@@ -277,12 +277,20 @@ public boolean trackableSelected(int i, int mx, int my) {
277
277
Trackable tracki = trackObjects .get (i );
278
278
for (HitResult hit : surfar .frame .hitTest (mx , my )) {
279
279
Trackable trackable = hit .getTrackable ();
280
+ Pose hitPose = hit .getHitPose ();
280
281
if (trackable instanceof Plane ) {
281
282
Plane plane = (Plane )trackable ;
282
- if (tracki .equals (plane ) && plane .isPoseInPolygon (hit . getHitPose () )) {
283
+ if (tracki .equals (plane ) && plane .isPoseInPolygon (hitPose )) {
283
284
return true ;
284
285
}
285
- }
286
+ } else if (trackable instanceof AugmentedImage ) {
287
+ AugmentedImage image = (AugmentedImage )trackable ;
288
+ Pose anchorPose = image .getCenterPose ();
289
+ Pose localHitPose = anchorPose .compose (hitPose );
290
+ if (tracki .equals (image ) && isPoseInsideAugmentedImage (localHitPose , image )) {
291
+ return true ;
292
+ }
293
+ }
286
294
}
287
295
return false ;
288
296
}
@@ -291,17 +299,65 @@ public boolean trackableSelected(int i, int mx, int my) {
291
299
protected HitResult getHitResult (int mx , int my ) {
292
300
for (HitResult hit : surfar .frame .hitTest (mx , my )) {
293
301
Trackable trackable = hit .getTrackable ();
302
+ Pose hitPose = hit .getHitPose ();
294
303
if (trackable instanceof Plane ) {
295
304
Plane plane = (Plane )trackable ;
296
- if (trackObjects .contains (plane ) && plane .isPoseInPolygon (hit . getHitPose () )) {
305
+ if (trackObjects .contains (plane ) && plane .isPoseInPolygon (hitPose )) {
297
306
return hit ;
298
307
}
308
+ } else if (trackable instanceof AugmentedImage ) {
309
+ AugmentedImage image = (AugmentedImage )trackable ;
310
+ Pose anchorPose = image .getCenterPose ();
311
+ Pose localHitPose = anchorPose .compose (hitPose );
312
+ if (trackObjects .contains (image ) && isPoseInsideAugmentedImage (localHitPose , image )) {
313
+ return hit ;
314
+ }
299
315
}
300
316
}
301
317
return null ;
302
318
}
303
319
304
320
321
+ private boolean isPoseInsideAugmentedImage (Pose pose , AugmentedImage image ) {
322
+ // Get the four corners of the AugmentedImage's defining rectangle
323
+ float [] corners = new float [8 ];
324
+ image .getExtentX ();
325
+ image .getExtentZ ();
326
+ image .getCenterPose ().toMatrix (corners , 0 );
327
+
328
+ // Define the vertices of the rectangle in 2D (assuming the image is flat on the XZ plane)
329
+ float imageMinX = Float .POSITIVE_INFINITY ;
330
+ float imageMaxX = Float .NEGATIVE_INFINITY ;
331
+ float imageMinZ = Float .POSITIVE_INFINITY ;
332
+ float imageMaxZ = Float .NEGATIVE_INFINITY ;
333
+
334
+ // Extract the X and Z coordinates of the corners
335
+ for (int i = 0 ; i < 8 ; i += 2 ) {
336
+ float cornerX = corners [i ];
337
+ float cornerZ = corners [i + 2 ];
338
+
339
+ if (cornerX < imageMinX ) {
340
+ imageMinX = cornerX ;
341
+ }
342
+ if (cornerX > imageMaxX ) {
343
+ imageMaxX = cornerX ;
344
+ }
345
+ if (cornerZ < imageMinZ ) {
346
+ imageMinZ = cornerZ ;
347
+ }
348
+ if (cornerZ > imageMaxZ ) {
349
+ imageMaxZ = cornerZ ;
350
+ }
351
+ }
352
+
353
+ // Check if the Pose's position (X, Z) is within the bounds of the AugmentedImage's rectangle
354
+ float poseX = pose .tx ();
355
+ float poseZ = pose .tz ();
356
+
357
+ return (poseX >= imageMinX && poseX <= imageMaxX && poseZ >= imageMinZ && poseZ <= imageMaxZ );
358
+ }
359
+
360
+
305
361
protected int getTrackable (HitResult hit ) {
306
362
Trackable track = hit .getTrackable ();
307
363
return trackObjects .indexOf (track );
@@ -323,6 +379,10 @@ public float[] getTrackablePolygon(int i, float[] points) {
323
379
points = new float [buffer .capacity ()];
324
380
}
325
381
buffer .get (points , 0 , buffer .capacity ());
382
+ } else if (track instanceof AugmentedImage ) {
383
+ AugmentedImage image = (AugmentedImage )track ;
384
+ points = new float [8 ];
385
+ image .getCenterPose ().toMatrix (points , 0 );
326
386
}
327
387
return points ;
328
388
}
@@ -373,17 +433,23 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
373
433
374
434
public int createAnchor (int i , float x , float y , float z ) {
375
435
Trackable track = trackObjects .get (i );
436
+ Pose centerPose = null ;
376
437
if (track instanceof Plane ) {
377
438
Plane plane = (Plane )track ;
378
- Pose trackPose = plane .getCenterPose ();
439
+ centerPose = plane .getCenterPose ();
440
+ } else if (track instanceof AugmentedImage ) {
441
+ AugmentedImage img = (AugmentedImage )track ;
442
+ centerPose = img .getCenterPose ();
443
+ }
444
+ if (centerPose != null ) {
379
445
pointIn [0 ] = x ;
380
446
pointIn [1 ] = y ;
381
- pointIn [2 ] = z ;
382
- trackPose .transformPoint (pointIn , 0 , pointOut , 0 );
447
+ pointIn [2 ] = z ;
448
+ centerPose .transformPoint (pointIn , 0 , pointOut , 0 );
383
449
Pose anchorPose = Pose .makeTranslation (pointOut );
384
- Anchor anchor = plane .createAnchor (anchorPose );
450
+ Anchor anchor = track .createAnchor (anchorPose );
385
451
anchors .put (++lastAnchorId , anchor );
386
- return lastAnchorId ;
452
+ return lastAnchorId ;
387
453
}
388
454
return -1 ;
389
455
}
@@ -392,12 +458,20 @@ public int createAnchor(int i, float x, float y, float z) {
392
458
public int createAnchor (int mx , int my ) {
393
459
for (HitResult hit : surfar .frame .hitTest (mx , my )) {
394
460
Trackable trackable = hit .getTrackable ();
461
+ Pose hitPose = hit .getHitPose ();
395
462
if (trackable instanceof Plane ) {
396
463
Plane plane = (Plane )trackable ;
397
- if (trackObjects .contains (plane ) && plane .isPoseInPolygon (hit . getHitPose () )) {
464
+ if (trackObjects .contains (plane ) && plane .isPoseInPolygon (hitPose )) {
398
465
return createAnchor (hit );
399
466
}
400
- }
467
+ } else if (trackable instanceof AugmentedImage ) {
468
+ AugmentedImage image = (AugmentedImage )trackable ;
469
+ Pose anchorPose = image .getCenterPose ();
470
+ Pose localHitPose = anchorPose .compose (hitPose );
471
+ if (trackObjects .contains (image ) && isPoseInsideAugmentedImage (localHitPose , image )) {
472
+ return createAnchor (hit );
473
+ }
474
+ }
401
475
}
402
476
return 0 ;
403
477
}
0 commit comments