Accessing and displaying LiDAR depth data

Hello!

I'm new to Swift and currently trying to get and display the distance from the lidar sensor to the middle of the screen, however, I'm having difficulty accessing LiDAR data.

I'm using the sample code from: https://developer.apple.com/documentation/arkit/environmental_analysis/displaying_a_point_cloud_using_scene_depth that uses ARKit, and I believe that I should be accessing ARDepthData's depthMap property to get the distances but I'm unable to do so.

Does anyone know how to do this or have any advice in general?

Thanks!

Hi James,

The source code of an ARKit demo App from us may be helpful.

Once you understand and can access the depthMapp, you may like to pick a point around the screen center.

The red circle at the screen center defines the search area for the point you look for. The radius of the red circle is to be given in pixel (of camera image) and to be converted as the vertex angle of a search cone.

func pickPoint(rayDirection ray_dir: simd_float3, rayPosition ray_pos: simd_float3, vertices list: UnsafePointer<simd_float4>, count: Int, _ unitRadius: Float) -> Int {
    let UR_SQ_PLUS_ONE = unitRadius * unitRadius + 1.0
    var minLen: Float = Float.greatestFiniteMagnitude
    var maxCos: Float = -Float.greatestFiniteMagnitude
    
    var pickIdx   : Int = -1
    var pickIdxExt: Int = -1
    
    for idx in 0..<count {
        let sub = simd_make_float3(list[idx]) - ray_pos
        let len1 = simd_dot( ray_dir, sub )
        
        if len1 < Float.ulpOfOne { continue; } // Float.ulpOfOne == FLT_EPSILON
        // 1. Inside ProbeRadius (Picking Cylinder Radius)
        if simd_length_squared(sub) < UR_SQ_PLUS_ONE * (len1 * len1) {
            if len1 < minLen { // find most close point to camera (in z-direction distance)
                minLen = len1
                pickIdx = idx
            }
        }
        // 2. Outside ProbeRadius
        else {
            let cosine = len1 / simd_length(sub)
            if cosine > maxCos { // find most close point to probe radius
                maxCos = cosine
                pickIdxExt = idx
            }
        }
    }
    
    return pickIdx < 0 ? pickIdxExt : pickIdx
}

There are 3 cases:

  1. If there are at least 1 depthMap points inside the view cone, the top most point to the camera COP will be chosen.
  2. Else if there are at least 1 depthMap points outside the view cone, the closest point to the red circle in screen will be chosen.
  3. Otherwise, there is no point from depthMap (empty depthMap).

By adjusting the radius of the red circle, you can control the precision of picking a point.

CurvSurf

Hi CurvSurf,

Thanks for your reply, although for some reason it isn't recognising 'FindSurfaceFramework' when running the project (I've followed the steps to link it as given in the README and it looks like it should work, i.e. it's set in search paths etc, but it still doesn't unfortunately). Any ideas on how to fix this?

Thanks!

Hi James,

We are afraid that we couldn’t reproduce what you have experienced. It might be useful for us to help you if you provide more descriptions such as error messages. Do you see any error messages?

Meanwhile, there is a checklist that might be helpful to deal with your issue:

  1. Download the released library framework (https://github.com/CurvSurf/FindSurface-iOS/releases).
  2. Unzip the downloaded file. In the unzipped content, what you need is a directory named “FindSurfaceFramework.framework” containing files and folders such as “Modules (folder), Info.plist, Headers (folder), and FindSurfaceFramework (no extension)”. The directory itself acts as the framework package file.
  3. While you import the framework to your project by following the instructions (https://github.com/CurvSurf/FindSurface-iOS/blob/master/How-to-import-FindSurface-Framework-to-your-project.md):

3.1 In Step 2, after clicked “Add Files…”: Click “Open” while you selected the directory “FindSurfaceFramework.framework”. Remember, the directory itself acts as the framework file.

3.2 In Step 3: Add the path where the framework directory exists. It might look like it already has some paths in the items, but you must manually add the path if you haven’t. For example, if the path goes like “/Users/username/Downloads/FindSurfaceFramework.framework” then you have to set it to be “/Users/username/Downloads/”.

  1. Just in case, check the following documents if those are relevant to your issue:

4.1 https://developer.apple.com/documentation/xcode/enabling-developer-mode-on-a-device.

4.2 https://support.apple.com/en-us/HT204460

Accessing and displaying LiDAR depth data
 
 
Q