Skip to content

Commit b7f6500

Browse files
committed
implemented all cases for AugmentedImage
1 parent ab906ab commit b7f6500

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

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

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,20 @@ public boolean trackableSelected(int i, int mx, int my) {
277277
Trackable tracki = trackObjects.get(i);
278278
for (HitResult hit : surfar.frame.hitTest(mx, my)) {
279279
Trackable trackable = hit.getTrackable();
280+
Pose hitPose = hit.getHitPose();
280281
if (trackable instanceof Plane) {
281282
Plane plane = (Plane)trackable;
282-
if (tracki.equals(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
283+
if (tracki.equals(plane) && plane.isPoseInPolygon(hitPose)) {
283284
return true;
284285
}
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+
}
286294
}
287295
return false;
288296
}
@@ -291,17 +299,65 @@ public boolean trackableSelected(int i, int mx, int my) {
291299
protected HitResult getHitResult(int mx, int my) {
292300
for (HitResult hit : surfar.frame.hitTest(mx, my)) {
293301
Trackable trackable = hit.getTrackable();
302+
Pose hitPose = hit.getHitPose();
294303
if (trackable instanceof Plane) {
295304
Plane plane = (Plane)trackable;
296-
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
305+
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hitPose)) {
297306
return hit;
298307
}
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+
}
299315
}
300316
}
301317
return null;
302318
}
303319

304320

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+
305361
protected int getTrackable(HitResult hit) {
306362
Trackable track = hit.getTrackable();
307363
return trackObjects.indexOf(track);
@@ -323,6 +379,10 @@ public float[] getTrackablePolygon(int i, float[] points) {
323379
points = new float[buffer.capacity()];
324380
}
325381
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);
326386
}
327387
return points;
328388
}
@@ -373,17 +433,23 @@ public PMatrix3D getTrackableMatrix(int i, PMatrix3D target) {
373433

374434
public int createAnchor(int i, float x, float y, float z) {
375435
Trackable track = trackObjects.get(i);
436+
Pose centerPose = null;
376437
if (track instanceof Plane) {
377438
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) {
379445
pointIn[0] = x;
380446
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);
383449
Pose anchorPose = Pose.makeTranslation(pointOut);
384-
Anchor anchor = plane.createAnchor(anchorPose);
450+
Anchor anchor = track.createAnchor(anchorPose);
385451
anchors.put(++lastAnchorId, anchor);
386-
return lastAnchorId;
452+
return lastAnchorId;
387453
}
388454
return -1;
389455
}
@@ -392,12 +458,20 @@ public int createAnchor(int i, float x, float y, float z) {
392458
public int createAnchor(int mx, int my) {
393459
for (HitResult hit : surfar.frame.hitTest(mx, my)) {
394460
Trackable trackable = hit.getTrackable();
461+
Pose hitPose = hit.getHitPose();
395462
if (trackable instanceof Plane) {
396463
Plane plane = (Plane)trackable;
397-
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hit.getHitPose())) {
464+
if (trackObjects.contains(plane) && plane.isPoseInPolygon(hitPose)) {
398465
return createAnchor(hit);
399466
}
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+
}
401475
}
402476
return 0;
403477
}

0 commit comments

Comments
 (0)