How to measure distance using ARCore?

前端 未结 3 1018
太阳男子
太阳男子 2020-12-02 11:34

Is it possible to calculate distance between two HitResult `s ?

Or how we can calculate real distance (e.g. meters) using ARCore?

相关标签:
3条回答
  • 2020-12-02 12:08

    The answer is: Yes, of course, you definitely can calculate distance between two HitResult's. The grid's size for ARCore, as well as for ARKit framework, is meters. Sometimes, it's more useful to use centimetres. Here are a few ways how you do it with Java and great old 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)) {
            // 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
    

    Or, alternatively, you can use the following math:

    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;
    
    0 讨论(0)
  • 2020-12-02 12:11

    You can extract the two HitResult poses using getHitPose() and then compare their translation component (getTranslation()). The translation is defined as

    ...the position vector from the destination (usually world) coordinate frame to the local coordinate frame, expressed in destination (world) coordinates.

    As for the physical unit of this I could not find any remark. With a calibrated camera this should be mathematically possible but I don't know if they actually provide an API for this

    0 讨论(0)
  • 2020-12-02 12:12

    In Java ARCore world units are meters (I just realized we might not document this... aaaand looks like nope. Oops, bug filed). By subtracting the translation component of two Poses you can get the distance between them. Your code would look something like this:

    On first hit as hitResult:

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

    On second hit as 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);
    

    Assuming that these hit results don't happen on the same frame, creating an Anchor is important because the virtual world can be reshaped every time you call Session.update(). By holding that location with an anchor instead of just a Pose, its Pose will update to track the physical feature across those reshapings.

    0 讨论(0)
提交回复
热议问题