ARKit: How do you iterate all detected feature points?

Viewed 2931

I'd like to do some processing for each of the detected feature points found in a single frame during an ARKit session. How can I iterate through each of the detected feature points, and get their world coordinates?

I'm using Swift, but an Objective-C answer will be fine as well.

2 Answers

As it took me a while to come up with a working sample of what it looks like to get a render of the feature points, here's a full working example:

SCNNode *ARPointCloudNode = nil;

- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame API_AVAILABLE(ios(11.0));
{

    NSInteger pointCount = frame.rawFeaturePoints.count;
    if (pointCount) {
        // We want a root node to work in, it's going to hold the all of the represented spheres
        // that come together to make the point cloud
        if (!ARPointCloudNode) {
            ARPointCloudNode = [SCNNode node];
            [self.sceneView.scene.rootNode addChildNode:ARPointCloudNode];
        }

        // It's going to need some colour
        SCNMaterial *whiteMaterial = [SCNMaterial material];
        whiteMaterial.diffuse.contents = [UIColor whiteColor];
        whiteMaterial.locksAmbientWithDiffuse = YES;

        // Remove the old point clouds (this happens per-frame
        for (SCNNode *child in ARPointCloudNode.childNodes) {
            [child removeFromParentNode];
        }

        // Use the frames point cloud to create a set of SCNSpheres
        // which live at the feature point in the AR world
        for (NSInteger i = 0; i < pointCount; i++) {
            vector_float3 point = frame.rawFeaturePoints.points[i];
            SCNVector3 vector = SCNVector3Make(point[0], point[1], point[2]);

            SCNSphere *pointSphere = [SCNSphere sphereWithRadius:0.001];
            pointSphere.materials = @[whiteMaterial];

            SCNNode *pointNode = [SCNNode nodeWithGeometry:pointSphere];
            pointNode.position = vector;

            [ARPointCloudNode addChildNode:pointNode];
        }
    }
}
Related