如何使用 ARKit 计算点 2 到点 3 和点 3 到点 4 的距离?

How to calculate distance from point 2 to point 3 and 3 to 4 so on using ARKit?

我正在制作一个应用程序来计算距离和面积,现在问题是我制作了一个数组并在其中附加了我的节点。

func calculate () {
    let start = dotNodes[0]
    let end = dotNodes[1]

    let a = end.position.x - start.position.x
    let b = end.position.y - start.position.y
    let c = end.position.z - start.position.z

    let distance =  sqrt(pow(a,2) + pow(b,2) + pow(c, 2))

    updateText(text:"\(abs( distance))", atPosition: (end.position))
}

现在起点是索引 0,终点是索引 1,但这只是两个点。我怎样才能计算出从 2 到 3 和 3 到 4 等等的距离,最后当最后一个点接触到点 1 时它应该给我面积?

性能最佳(也是最简单)的方法是使用 SIMD - https://developer.apple.com/documentation/accelerate/simd/working_with_vectors

let dist = simd_distance(start, end)

其中向量可能应该重新定义为 simd_float3(或 SIMD3<Float>,如果您使用的是 Swift 5)。

P.S。您需要先导入 simd 框架。

正如@Maxim 所说,您可以从简化计算开始^______^。

不过,我会尝试使用 GLK 数学辅助方法来回答您的问题,如果您对此感兴趣,可以在此处阅读更多相关信息:GLK Documentation

本质上你需要做的是遍历你的位置数组,并以 2 为单位计算这些位置之间的距离。当你的最后一次迭代只有一个元素时,你将计算这个和第一个元素之间的位置第一。

由于我的数学不是很好,所以我在 Whosebug 上快速搜索以找到解决方案,并利用了@Gasim 在 post .

由于我的尝试时间很长,我没有一步一步地逐一进行,而是提供了完整的答案,并希望能为您指明正确的方向。

一如既往,如果其他人可以帮助重构和/或改进代码,请随时:

//
//  ViewController.swift
//  Measuring Example
//
//  Created By Josh Robbins (∩`-´)⊃━☆゚.*・。゚* on 27/04/2019.
//  Copyright © 2019 BlackMirrorz. All rights reserved.
//

import UIKit
import ARKit

class ViewController: UIViewController {

  @IBOutlet weak var augmentedRealityView: ARSCNView!
  var augmentedRealityConfiguration = ARWorldTrackingConfiguration()
  var augmentedRealitySession = ARSession()

  var markerNodes = [SCNNode]()
  typealias NodeNameData = (name: String, node: SCNNode)
  typealias DistanceData = (distance: Float, positionA: GLKVector3, positionB: GLKVector3)

  //---------------------
  //MARK:- Initialization
  //---------------------

  override func viewDidLoad() {

    super.viewDidLoad()
    setupARSession()

  }

  /// Sets Up Our ARSession
  func setupARSession(){

    augmentedRealityView.session = augmentedRealitySession
    augmentedRealitySession.run(augmentedRealityConfiguration, options: [.removeExistingAnchors, .resetTracking])

  }

  /// Creates A Node To Mark The Touch Position In The Scene
  ///
  /// - Returns: SCNNode
  func markerNode() -> SCNNode{

    let node = SCNNode(geometry: SCNSphere(radius: 0.01))
    node.geometry?.firstMaterial?.diffuse.contents = UIColor.cyan
    return node

  }

  //------------------------
  //MARK:- Marker Placemenet
  //------------------------

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    //1. Get The Users Current Touch Point & Check We Have A Valid HitTest Result
    guard let touchPoint = touches.first?.location(in: self.augmentedRealityView),
          let hitTest = self.augmentedRealityView.hitTest(touchPoint, types: .featurePoint).first
    else { return }

    //2. Get The World Transorm & Create An SCNNode At The Converted Touch Position
    let transform = hitTest.worldTransform
    let node = markerNode()
    node.position = SCNVector3(transform.columns.3.x, transform.columns.3.y, transform.columns.3.z)
    self.augmentedRealityView.scene.rootNode.addChildNode(node)

    //3. Add The Node To Our Markers Array So We Can Calculate The Distance Later
    markerNodes.append(node)

    //4. If We Have 5 Marker Nodes Then Calculate The Distances Between Them & Join Them Together
    if markerNodes.count == 5{
      calculateMarkerNodeDistances()
      markerNodes.removeAll()
    }

  }

  //-------------------
  //MARK:- Calculations
  //-------------------

  /// Enemurates Our Marker Nodes & Creates A Joining Node Between Them
  func calculateMarkerNodeDistances(){

    var index = 0;

    while index < markerNodes.count {

      let nodeA = markerNodes[index];
      var nodeB : SCNNode? = nil;

      if index + 1 < markerNodes.count {
         nodeB = markerNodes[index+1];
      }

      //1. Create A Joining Node Between The Two Nodes And Calculate The Distance
      if let lastNode = nodeB{
        let nodeA = NodeNameData("Node \(index)", nodeA)
        let nodeB = NodeNameData("Node \(index+1)", lastNode)
        self.augmentedRealityView.scene.rootNode.addChildNode(joiningNode(between: [nodeA, nodeB]))

      }else{

        //2. Here We Can Assume The We Have Reached The Last Node So We Calculate The Distance Between The 1st & Last Nodes
        guard let initialNode = markerNodes.first, let lastNode = markerNodes.last else { return }
        let nodeA = NodeNameData("Node 0 ", initialNode)
        let nodeB = NodeNameData("Node \(markerNodes.count)", lastNode)
        self.augmentedRealityView.scene.rootNode.addChildNode(joiningNode(between: [nodeA, nodeB]))
      }

      //Increment By 1 So We Join The Nodes Together In The Correct Sequence e.g. (1, 2), (3, 4) And Not (1, 2), (3, 4)
      index += 1;
    }

  }


  /// Creates A Joining Node Between Two Names
  ///
  /// - Parameter nodes: [NodeNameData]
  /// - Returns: MeasuringLineNode
  func joiningNode(between nodes: [NodeNameData]) -> MeasuringLineNode{

    let distance = calculateDistanceBetweenNodes([nodes[0], nodes[1]])
    let joiner = MeasuringLineNode(startingVector: distance.positionA, endingVector: distance.positionB)
    return joiner

  }

  /// Calculates The Distance Between Two SCNNodes
  ///
  /// - Parameter nodes: [NodeNameData]
  /// - Returns: DistanceData
  func calculateDistanceBetweenNodes(_ nodes: [NodeNameData]) -> DistanceData{

    //1. Calculate The Distance
    let positionA = GLKVectorThreeFrom(nodes[0].node.position)
    let positionB = GLKVectorThreeFrom(nodes[1].node.position)
    let distance = GLKVector3Distance(positionA, positionB)
    let meters = Measurement(value: Double(distance), unit: UnitLength.meters)
    print("Distance Between Markers [ \(nodes[0].name) & \(nodes[1].name) ]  = \(String(format: "%.2f", meters.value))m")

    //2. Return The Distance A Positions Of The Nodes
    return (distance, positionA, positionB)

  }

  /// Creates A GLKVector3 From An SCNVectore3
  ///
  /// - Parameter vector3: SCNVector3
  /// - Returns: GLKVector3
  func GLKVectorThreeFrom(_ vector3: SCNVector3) -> GLKVector3 { return GLKVector3Make(vector3.x, vector3.y, vector3.z) }

}

//-------------------------
//MARK:- Mesuring Line Node
//-------------------------

class MeasuringLineNode: SCNNode{

  /// Creates A Line Between Two SCNNodes
  ///
  /// - Parameters:
  ///   - vectorA: GLKVector3
  ///   - vectorB: GLKVector3
  init(startingVector vectorA: GLKVector3, endingVector vectorB: GLKVector3) {

    super.init()

    let height = CGFloat(GLKVector3Distance(vectorA, vectorB))

    self.position = SCNVector3(vectorA.x, vectorA.y, vectorA.z)

    let nodeVectorTwo = SCNNode()
    nodeVectorTwo.position = SCNVector3(vectorB.x, vectorB.y, vectorB.z)

    let nodeZAlign = SCNNode()
    nodeZAlign.eulerAngles.x = Float.pi/2

    let box = SCNBox(width: 0.001, height: height, length: 0.001, chamferRadius: 0)
    let material = SCNMaterial()
    material.diffuse.contents = UIColor.white
    box.materials = [material]

    let nodeLine = SCNNode(geometry: box)
    nodeLine.position.y = Float(-height/2)
    nodeZAlign.addChildNode(nodeLine)

    self.addChildNode(nodeZAlign)

    self.constraints = [SCNLookAtConstraint(target: nodeVectorTwo)]
  }

  required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }

}

基于这个简单的(希望是准确的答案)结果是这样的:

Distance Between Markers [ Node 0 & Node 1 ]  = 0.14m
Distance Between Markers [ Node 1 & Node 2 ]  = 0.09m
Distance Between Markers [ Node 2 & Node 3 ]  = 0.09m
Distance Between Markers [ Node 3 & Node 4 ]  = 0.05m
Distance Between Markers [ Node 0  & Node 5 ]  = 0.36m

在我的示例中,我正在计算五个节点的距离,但您可以将其称为任意点。当然,您随后还需要使用公式来计算面积本身。然而,这应该足以为您指明正确的方向。

希望对您有所帮助...