如何使用ARKit检测平面

时间:2019-04-22 07:39:51

标签: ios swift augmented-reality arkit measure

我正在创建一个使用ARKit测量物体高度的应用程序。对于瓶子,钢笔和显示器之类的小物体,它几乎可以显示准确的结果。但是当我尝试一些大的物体时,例如人的身高,门的高度,每次测量都显示不同的结果。我认为这是由于表面平面检测。

有什么更好的解决方案来检测平面?或其他任何寻找身高的选项?

还有关于ARKit视觉框架的信息吗?

1 个答案:

答案 0 :(得分:0)

请查看以下代码,我认为它将帮助您了解测量的工作原理:

class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate {

    @IBOutlet weak var resultLabel: UILabel!
    @IBOutlet weak var aimLabel: UILabel!
    @IBOutlet weak var notReadyLabel: UILabel!
    @IBOutlet var sceneView: ARSCNView!

    let session = ARSession()
    let vectorZero = SCNVector3()
    let sessionConfig = ARWorldTrackingSessionConfiguration()
    var measuring = false
    var startValue = SCNVector3()
    var endValue = SCNVector3()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupScene()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIApplication.shared.isIdleTimerDisabled = true
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        session.pause()
    }
    func setupScene() {
        sceneView.delegate = self
        sceneView.session = session
        session.run(sessionConfig, options: [.resetTracking, 
                                             .removeExistingAnchors])
        resetValues()
    }
    func resetValues() {
        measuring = false
        startValue = SCNVector3()
        endValue =  SCNVector3()
        updateResultLabel(0.0)
    }
    func updateResultLabel(_ value: Float) {
        let cm = value * 100.0
        let inch = cm * 0.3937007874
        resultLabel.text = String(format: "%.2f cm / %.2f\"", cm, inch)
    }
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        DispatchQueue.main.async {
            self.detectObjects()
        }
    }  
    func detectObjects() {
        if let worldPos = sceneView.realWorldVector(screenPos: view.center) {
            aimLabel.isHidden = false
            notReadyLabel.isHidden = true
            if measuring {
                if startValue == vectorZero {
                    startValue = worldPos
                }
                endValue = worldPos
                updateResultLabel(startValue.distance(from: endValue))
            }
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        resetValues()
        measuring = true
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        measuring = false
    }
}

您会看到没有.planeDetection结构。