从 ARKit 中的 faceAnchor 中提取 yaw/pitch/roll

Extract yaw/pitch/roll from faceAnchor in ARKit

使用 ARKit 进行面部跟踪,一旦检测到面部,我就会得到 faceAnchor (ARFaceAnchor),它提供了一个 simd_float4x4 矩阵。我知道变换矩阵,并且我也知道该主题已部分解决(此处: , and here: simd_float4x4 Columns),但是是否有直接的方法从面部锚点获取 yaw/pitch/rool 值? (为了在下面的代码中提供我的 y/p/r 值)。

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        let faceAnchor = anchor as? ARFaceAnchor
        let data = faceAnchor?.transform
        print(data!)
        let message = OSCMessage(
            OSCAddressPattern("/orientation"),
            yawValue,
            pitchValue,
            rollValue
        )
        client.send(message)
        print(message)
        
    }

仅供参考,OSCMessage 来自我项目中嵌入的 SwiftOSC 框架。

由于 Apple 显然(还)没有为此目的提供的功能,因此先验地需要实现四元数到欧拉角的计算。加上these mathematical resources,加上弧度转度的功能,可以作为扩展实现,如下:

extension matrix_float4x4 {
    // Function to convert rad to deg
    func radiansToDegress(radians: Float32) -> Float32 {
        return radians * 180 / (Float32.pi)
    }
    var translation: SCNVector3 {
       get {
           return SCNVector3Make(columns.3.x, columns.3.y, columns.3.z)
       }
    }
    // Retrieve euler angles from a quaternion matrix
    var eulerAngles: SCNVector3 {
        get {
            // Get quaternions
            let qw = sqrt(1 + self.columns.0.x + self.columns.1.y + self.columns.2.z) / 2.0
            let qx = (self.columns.2.y - self.columns.1.z) / (qw * 4.0)
            let qy = (self.columns.0.z - self.columns.2.x) / (qw * 4.0)
            let qz = (self.columns.1.x - self.columns.0.y) / (qw * 4.0)

            // Deduce euler angles
            /// yaw (z-axis rotation)
            let siny = +2.0 * (qw * qz + qx * qy)
            let cosy = +1.0 - 2.0 * (qy * qy + qz * qz)
            let yaw = radiansToDegress(radians:atan2(siny, cosy))
            // pitch (y-axis rotation)
            let sinp = +2.0 * (qw * qy - qz * qx)
            var pitch: Float
            if abs(sinp) >= 1 {
                pitch = radiansToDegress(radians:copysign(Float.pi / 2, sinp))
            } else {
                pitch = radiansToDegress(radians:asin(sinp))
            }
            /// roll (x-axis rotation)
            let sinr = +2.0 * (qw * qx + qy * qz)
            let cosr = +1.0 - 2.0 * (qx * qx + qy * qy)
            let roll = radiansToDegress(radians:atan2(sinr, cosr))
            
            /// return array containing ypr values
            return SCNVector3(yaw, pitch, roll)
            }
    }
}