ios – Viewing Frustum utilizing ARKit

0
5
ios – Viewing Frustum utilizing ARKit


I’ve been making an attempt 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 perform 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, remodel: currentFrame.digicam.remodel, arView: arView)
    }
}

func calculateFOVAndFrustum(body: ARFrame, remodel: 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.peak)
    
    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: (remodel.getPosition())")
    print("Rotation: (remodel.getRotation())")
    // Get digicam place and orientation from the remodel
    let camPos = SIMD3(remodel.columns.3.x,
                             remodel.columns.3.y,
                             remodel.columns.3.z)
    
    // Digital camera axes in ARKit (ahead is -Z)
    let camForward = -SIMD3(remodel.columns.2.x,
                                  remodel.columns.2.y,
                                  remodel.columns.2.z)
    let camUp = SIMD3(remodel.columns.1.x,
                            remodel.columns.1.y,
                            remodel.columns.1.z)
    let camRight = SIMD3(remodel.columns.0.x,
                               remodel.columns.0.y,
                               remodel.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 perform creates and visualizes the frustum planes, and likewise the close to and much facilities as spheres within the AR scene
func visualizeFrustumPlanes(corners: [SIMD3], nearCenter: SIMD3, farCenter: SIMD3, arView: ARView) {
    
    // Outline the aircraft supplies
    let nearPlaneMaterial = SimpleMaterial(shade: .blue, isMetallic: true) // Close to aircraft (blue)
    let farPlaneMaterial = SimpleMaterial(shade: .crimson, isMetallic: true)  // Far aircraft (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 accurately in heart of what my system digicam can see when it comes to close to and much viewing distance, BUT after the primary body every time i attempt to change my digicam orientation the NEAR and FAR heart factors do get positioned in world house however not within the view of WHAT MY CAMERA IS SEEING.

I might actually respect if anyone may also help me with it.

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here