Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

swift - Flip ARFaceAnchor from left-handed to right-handed coordinate system

After some testings by printing faceAnchor.transform.columns.3, digging in SO: ARFaceAnchor have negative Z position? and Apple's documentation: ARFaceAnchor, I realized that the z axis is actually flipped and it is not the right-handed coordinate system as in the documentation. The ARFaceAnchor claims the coordinate:

enter image description here

the positive x direction points to the viewer’s right (that is, the face’s own left), the positive y direction points up (relative to the face itself, not to the world), and the positive z direction points outward from the face (toward the viewer)


Sarasvati has given a suggestion on doing the transformation:

    func faceAnchorPoseToRHS(_ mat: float4x4) -> float4x4 {
    let correctedPos = float4(x: mat.columns.3.x, y: mat.columns.3.y, z: -mat.columns.3.z, w: 1)

    let quat = simd_quatf(mat)
    let newQuat = simd_quatf(angle: -quat.angle, axis: float3(quat.axis.x, quat.axis.y, -quat.axis.z))
    var newPose = float4x4(newQuat)
    newPose.columns.3 = correctedPos

    return newPose
}

Updates based on Andy Fedoroff:

extension ViewController: ARSCNViewDelegate {
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard let faceAnchor = anchor as? ARFaceAnchor else { return }
        currentFaceAnchor = faceAnchor
        if node.childNodes.isEmpty, let contentNode = selectedContentController.renderer(renderer, nodeFor: faceAnchor) {
            node.addChildNode(contentNode)
        }
        selectedContentController.session = sceneView?.session
        selectedContentController.sceneView = sceneView
    }
    /// - Tag: ARFaceGeometryUpdate
    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard anchor == currentFaceAnchor,
            let contentNode = selectedContentController.contentNode,
            contentNode.parent == node
            else { return }
        sceneView.session.currentFrame!.anchors.first!.transform
        print(currentFaceAnchor!.transform.columns.3)
        selectedContentController.session = sceneView?.session
        selectedContentController.sceneView = sceneView
        selectedContentController.renderer(renderer, didUpdate: contentNode, for: anchor)
    }
}

However, I printed faceAnchor.transform.columns.3 and obtained the z negative with True Depth camera. Below show x,y,z,w axis:

enter image description here

Any suggestions on how to correct the z-axis? Thank you in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

At first, let's see what a default matrix values are.


Identity matrix

Here's an Identity 4x4 matrix with default values.

If you need additional info on elements of 4x4 matrices read this post.

 // 0  1  2  3
 ┌              ┐
 |  1  0  0  0  |  x
 |  0  1  0  0  |  y
 |  0  0  1  0  |  z
 |  0  0  0  1  |
 └              ┘


Position Mirroring

To flip entity's Z axis and X axis, in order to reorient axis from Face tracking environment to World tracking environment, use the following form of 4x4 transform matrix.

 // 0  1  2  3
 ┌              ┐
 | -1  0  0  0  |  x
 |  0  1  0  0  |  y
 |  0  0 -1  0  |  z
 |  0  0  0  1  |
 └              ┘


Rotation Mirroring

In 4x4 transform matrices rotation is a combination of scale and skew. To choose a right rotation's direction, clockwise (CW) or counter-clockwise (CCW) use + or - for cos expressions.

Here's a positive Z rotation expression (CCW).

enter image description here

Positive +cos(a):

 ┌                    ┐
 | cos -sin   0    0  |
 | sin  cos   0    0  |
 |  0    0    1    0  |
 |  0    0    0    1  |
 └                    ┘


And here's a negative Z rotation expression (CW).

enter image description here

Negative -cos(a):

 ┌                    ┐
 |-cos -sin   0    0  |
 | sin -cos   0    0  |
 |  0    0    1    0  |
 |  0    0    0    1  |
 └                    ┘

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...