HomeiOS Developmentios - Viewing Frustum utilizing ARKit

ios – Viewing Frustum utilizing ARKit


I’ve been attempting to calculate and visualize frustum in SwiftUI utilizing ARKit for a number of days now, however to no avail. I’ve been utilizing the next code

    // This operate handles a faucet gesture to provoke the frustum calculation
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
    guard let arView = sender?.view as? ARView,
          let tapLocation = sender?.location(in: arView) else {
        print("Faucet or ARView not discovered")
        return
    }
    
    // Get the present digicam place from ARKit
    if let currentFrame = arView.session.currentFrame {

        calculateFOVAndFrustum(body: currentFrame, rework: currentFrame.digicam.rework, arView: arView)
    }
}

func calculateFOVAndFrustum(body: ARFrame, rework: simd_float4x4, arView: ARView) {
    let intrinsics = body.digicam.intrinsics
    let fx = intrinsics[0, 0]
    let fy = intrinsics[1, 1]
    
    let imageWidth = Float(body.digicam.imageResolution.width)
    let imageHeight = Float(body.digicam.imageResolution.top)
    
    let aspectRatio = imageWidth / imageHeight
    
    // Calculate FOV
    let verticalFOV = 2 * atan(imageHeight / (2 * fy))
    let horizontalFOV = 2 * atan(imageWidth / (2 * fx))
    
    let nearDistance: Float = 0.03
    let farDistance: Float = 0.1
    
    let nearHeight = 2 * tan(verticalFOV / 2) * nearDistance
    let farHeight = 2 * tan(verticalFOV / 2) * farDistance
    let nearWidth = nearHeight * aspectRatio
    let farWidth = farHeight * aspectRatio
    print("Place: (rework.getPosition())")
    print("Rotation: (rework.getRotation())")
    // Get digicam place and orientation from the rework
    let camPos = SIMD3(rework.columns.3.x,
                             rework.columns.3.y,
                             rework.columns.3.z)
    
    // Digicam axes in ARKit (ahead is -Z)
    let camForward = -SIMD3(rework.columns.2.x,
                                  rework.columns.2.y,
                                  rework.columns.2.z)
    let camUp = SIMD3(rework.columns.1.x,
                            rework.columns.1.y,
                            rework.columns.1.z)
    let camRight = SIMD3(rework.columns.0.x,
                               rework.columns.0.y,
                               rework.columns.0.z)
    
    print("Cam Pos: (camPos)")
    print("Cam Ahead: (camForward)")
    print("Cam Up: (camUp)")
    print("Cam Proper: (camRight)")
    
    // Calculate facilities (in entrance of digicam)
    let nearCenter = camPos + camForward * nearDistance
    let farCenter = camPos + camForward * farDistance
    
    // Calculate frustum corners
    let farTopLeft = farCenter + (camUp * (farHeight * 0.5)) - (camRight * (farWidth * 0.5))
    let farTopRight = farCenter + (camUp * (farHeight * 0.5)) + (camRight * (farWidth * 0.5))
    let farBottomLeft = farCenter - (camUp * (farHeight * 0.5)) - (camRight * (farWidth * 0.5))
    let farBottomRight = farCenter - (camUp * (farHeight * 0.5)) + (camRight * (farWidth * 0.5))
    
    let nearTopLeft = nearCenter + (camUp * (nearHeight * 0.5)) - (camRight * (nearWidth * 0.5))
    let nearTopRight = nearCenter + (camUp * (nearHeight * 0.5)) + (camRight * (nearWidth * 0.5))
    let nearBottomLeft = nearCenter - (camUp * (nearHeight * 0.5)) - (camRight * (nearWidth * 0.5))
    let nearBottomRight = nearCenter - (camUp * (nearHeight * 0.5)) + (camRight * (nearWidth * 0.5))
    
    visualizeFrustumPlanes(
        corners: [nearTopLeft, nearTopRight, nearBottomLeft, nearBottomRight,
                 farTopLeft, farTopRight, farBottomLeft, farBottomRight],
        nearCenter: nearCenter,
        farCenter: farCenter,
        arView: arView
    )
}

// This operate creates and visualizes the frustum planes, and in addition the close to and much facilities as spheres within the AR scene
func visualizeFrustumPlanes(corners: [SIMD3], nearCenter: SIMD3, farCenter: SIMD3, arView: ARView) {
    
    // Outline the airplane supplies
    let nearPlaneMaterial = SimpleMaterial(shade: .blue, isMetallic: true) // Close to airplane (blue)
    let farPlaneMaterial = SimpleMaterial(shade: .crimson, isMetallic: true)  // Far airplane (crimson)
    let sidePlaneMaterial = SimpleMaterial(shade: .inexperienced, isMetallic: true) // Facet planes (inexperienced)
    
    // Outline the sphere materials for the facilities
    let centerMaterial = SimpleMaterial(shade: .yellow, isMetallic: true) // Yellow for facilities
    
    let centerMaterial2 = SimpleMaterial(shade: .inexperienced, isMetallic: true) // Yellow for facilities
    
    // Create a small sphere mesh for visualization
    let sphereMesh = MeshResource.generateSphere(radius: 0.01)

    // Create ModelEntities for close to and much facilities (visualize them as spheres)
    let nearCenterEntity = ModelEntity(mesh: sphereMesh, supplies: [centerMaterial])
    nearCenterEntity.place = nearCenter
    let farCenterEntity = ModelEntity(mesh: sphereMesh, supplies: [centerMaterial2])
    farCenterEntity.place = farCenter

    let nearCenterAnchor = AnchorEntity(world: nearCenter)
    nearCenterAnchor.addChild(nearCenterEntity, preservingWorldTransform: true)
    
    let farCenterAnchor = AnchorEntity(world: farCenter)
    farCenterAnchor.addChild(farCenterEntity, preservingWorldTransform: true)

     arView.scene.addAnchor(nearCenterAnchor)
     arView.scene.addAnchor(farCenterAnchor)
}

The issue is that when i attempt to visualize the frustum NEAR and FAR facilities, they seem appropriately in middle of what my machine digicam can see by way of close to and much viewing distance, BUT after the primary body each time i attempt to change my digicam orientation the NEAR and FAR middle factors do get positioned in world area however not within the view of WHAT MY CAMERA IS SEEING.

I’d actually respect if someone might help me with it.

I anticipate the NEAR and FAR facilities of the frustum to seem on this planet area however within the view of what my digicam is definitely seeing.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments