如何使用ARCore测量距离?

Ale*_*ian 17 java android augmented-reality arcore

有可能计算两个之间的距离HitResult吗?

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

Ian*_*n M 27

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

首次点击为hitResult:

startAnchor = session.addAnchor(hitResult.getHitPose());
Run Code Online (Sandbox Code Playgroud)

第二次打击为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);
Run Code Online (Sandbox Code Playgroud)

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


Phi*_*Lab 5

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

...从目标(通常是世界)坐标系到本地坐标系的位置向量,以目标(世界)坐标表示。

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


ARG*_*Geo 5

答案是:是的,你绝对可以计算出两个HitResult人之间的距离。的工作单位ARCore以及ARKitmeters。有时,使用 更有用centimetres。以下是使用 Java 和伟大的旧Pythagorean theorem.

在此输入图像描述

import com.google.ar.core.HitResult

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

// 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
Run Code Online (Sandbox Code Playgroud)

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

Pose pose0 = firstAnchor.getPose()     // first pose
Pose pose1 = secondAnchor.getPose()    // second pose

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;
Run Code Online (Sandbox Code Playgroud)