Anony mouse
Anony mouse

Reputation: 23

How to identify relevant anchors within a specified FOV in visionOS?

I am currently developing a project for visionOS. This project involves getting valid PlaneAnchors within a certain FOV of the device anchor. Essentially we are given a 4x4 transformation matrix for the device (headset) anchor relative to a world origin. We also have a list of PlaneAnchors detected via Apple's Plane Detection package. These PlaneAnchors also give a 4x4 transformation matrix relative to the same world origin.

What I am trying to do is write code that can check how many of these PlaneAnchors fall within the visible boundary of the user given the device anchor. How could I go about doing this? I know the VisionPro has a vertical FOV of 90° and a horizontal FOV of around 110° and this is the region within which I want to select valid PlaneAnchors that have been detected.

I am currently using this code but for some reason only detecting PlaneAnchors behind my device, so out of my FOV.

final class FOVUtils {
    // Define FOV and Clipping Planes
    let fovHorizontal: Float = .pi / 3 // 60 degrees
    let fovVertical: Float = .pi / 4 // 45 degrees
    let nearPlane: Float = 0.1
    let farPlane: Float = 10.0
    
    // Helper Function: Convert to WorldAnchor's Local Coordinates
    func convertToWorldAnchorCoordinates(anchorPosition: SIMD3<Float>, devicePosition: SIMD3<Float>, inverseOrientation: simd_quatf) -> SIMD3<Float> {
        let relativePosition = anchorPosition - devicePosition
        let transformedPosition = inverseOrientation.act(relativePosition)
        return transformedPosition
    }

    // Helper Function: Check if Inside Field of View
    func isInFieldOfView(anchorPosition: SIMD3<Float>, devicePosition: SIMD3<Float>, inverseOrientation: simd_quatf) -> Bool {
        print("DEBUG: anchor pos: ", anchorPosition)
        print("DEBUG: devicePosition: ", devicePosition)
        print("DEBUG: inverseOrientation: ", inverseOrientation)
        let transformedPosition = convertToWorldAnchorCoordinates(anchorPosition: anchorPosition, devicePosition: devicePosition, inverseOrientation: inverseOrientation)
        print("DEBUG: transformed position: ",  transformedPosition)
        let zDistance = transformedPosition.z
        if zDistance < nearPlane || zDistance > farPlane {
            return false
        }
        print("DEBUG: passed 1")

        let tanHalfHorizontalFOV = tan(fovHorizontal)
        let tanHalfVerticalFOV = tan(fovVertical)

        let xLimit = zDistance * tanHalfHorizontalFOV
        let yLimit = zDistance * tanHalfVerticalFOV

        // let passed = abs(transformedPosition.x) <= xLimit && abs(transformedPosition.y) <= yLimit
        // print("DEBUG: passed 2 is", passed)
        return true
    }
    
}

If anyone has any techniques regarding how to do this, or can offer some insight into how to leverage the Vision Pro in solving this problem, I would highly appreciate it. Thank you!

Upvotes: 1

Views: 93

Answers (0)

Related Questions