Swift ARKit: Get face anchor transform relative to camera

Viewed 878

My Swift ARKit app needs the position and orientation of the face relative to the front-facing camera. If I set ARConfiguration.worldAlignment = .camera all I need to do is call for the faceAnchor.transform, which works perfectly; but I need to run in the default worldAlignment = .gravity. In this mode I can get faceAnchor.transform and camera.transform, which are both supplied in world coordinates. How can I use those transforms to get the face anchor in camera coordinates? I've tried multiplying those together as well as multiplying one by the other's inverse, in all four order combinations, but none of these results works. I just don't understand matrix operations well enough to succeed here. Can someone shed light on this for me?

1 Answers

I finally figured this out using SceneKit functions!

    let currentFaceTransform = currentFaceAnchor!.transform

    let currentCameraTransform = frame.camera.transform

    let newFaceMatrix = SCNMatrix4.init(currentFaceTransform)

    let newCameraMatrix = SCNMatrix4.init(currentCameraTransform)
    let cameraNode = SCNNode()
    cameraNode.transform = newCameraMatrix

    let originNode = SCNNode()
    originNode.transform = SCNMatrix4Identity

    //Converts a transform from the node’s local coordinate space to that of another node.
    let transformInCameraSpace = originNode.convertTransform(newFaceMatrix, to: cameraNode)

    let faceTransformFromCamera = simd_float4x4(transformInCameraSpace)

Hope this helps some others out there!

Related