如何使用ARCore测量距离?

时间:2017-08-31 13:07:06

标签: java android augmented-reality arcore

是否可以计算两个HitResult之间的距离?

或者我们如何使用ARCore计算实际距离(例如米)?

3 个答案:

答案 0 :(得分:24)

在Java ARCore中,世界单位是米(我刚刚意识到我们可能没有记录这个... aaaand看起来像nope。糟糕,提交了错误)。通过减去两个Pose的平移分量,您可以得到它们之间的距离。您的代码看起来像这样:

首次点击hitResult

startAnchor = session.addAnchor(hitResult.getHitPose());

第二次点击hitResult

Pose startPose = startAnchor.getPose();
Pose endPose = hitResult.getHitPose();

// Clean up the anchor
session.removeAnchors(Collections.singleton(startAnchor));
startAnchor = null;

// Compute the difference vector between the two hit locations.
float dx = startPose.tx() - endPose.tx();
float dy = startPose.ty() - endPose.ty();
float dz = startPose.tz() - endPose.tz();

// Compute the straight-line distance.
float distanceMeters = (float) Math.sqrt(dx*dx + dy*dy + dz*dz);

假设这些命中结果不会在同一帧上发生,创建Anchor非常重要,因为每次调用Session.update()时都可以重新塑造虚拟世界。通过使用锚点而不仅仅是姿势来保持该位置,其姿势将更新以跟踪这些重新调整中的物理特征。

答案 1 :(得分:3)

您可以使用getHitPose()提取两个HitResult姿势,然后比较它们的翻译组件(getTranslation())。 翻译定义为

  

...来自目的地的位置矢量(通常是   世界)坐标框到本地坐标系,表示为   目的地(世界)坐标。

至于这个物理单位,我找不到任何评论。使用经过校准的相机,这在数学上是可行的,但我不知道它们是否真的为此提供了一个API

答案 2 :(得分:2)

答案是:是的,您当然可以计算两个HitResult之间的距离。 ARCoreARKit框架的网格大小为meters。有时,使用centimetres更有用。这是使用Java和古老的Pythagorean theorem的几种方法:

enter image description here

import com.google.ar.core.HitResult

MotionEvent tap = queuedSingleTaps.poll();
if (tap != null && camera.getTrackingState() == TrackingState.TRACKING) {
    for (HitResult hit : frame.hitTest(tap)) {
        // Blah-blah-blah...
    }
}

// Here's the principle how you can calculate the distance  
// between two anchors in 3D space using Java:

private double getDistanceMeters(Pose pose0, Pose pose1) {

    float distanceX = pose0.tx() - pose1.tx();
    float distanceY = pose0.ty() - pose1.ty();
    float distanceZ = pose0.tz() - pose1.tz();

    return Math.sqrt(distanceX * distanceX + 
                     distanceY * distanceY + 
                     distanceZ * distanceZ);
} 

// Convert Meters into Centimetres

double distanceCm = ((int)(getDistanceMeters(pose0, pose1) * 1000))/10.0f;

// pose0 is the location of first Anchor
// pose1 is the location of second Anchor
  

或者,您也可以使用以下数学公式:

Pose pose0 = // first HitResult's Anchor
Pose pose1 = // second HitResult's Anchor

double distanceM = Math.sqrt(Math.pow((pose0.tx() - pose1.tx()), 2) + 
                             Math.pow((pose0.ty() - pose1.ty()), 2) +
                             Math.pow((pose0.tz() - pose1.tz()), 2));

double distanceCm = ((int)(distanceM * 1000))/10.0f;