I'm using ARKit and need to count physical size of captured image, for now I'm only have distance from camera to object.
One of my idea was to have Max top point and Max bottom so then with hitTest I will be able count distance between this two point, but how correctly create this two point?
func calculateHeightMmByTopAndBottomPoints() {
let topPoint = CGPoint(x: 0, y: 0)
let bottomPoint = CGPoint(x: 0, y: self.bounds.maxY)
let topPosition = getPosition(point: topPoint)
let bottomPosition = getPosition(point: bottomPoint)
let distanceMm = round(topPosition!.distance(to: bottomPosition!) * 1000)
debugPrint("Heignt By Top And Bottom Points, mm - \(distanceMm)")
}
func getPosition(point:CGPoint) -> SCNVector3?{
let hitTestResults = self.hitTest(point, types: .featurePoint)
if let result = hitTestResults.first {
let position = SCNVector3.positionFrom(matrix: result.worldTransform)
return position
}
return nil
}
func distance(to destination: SCNVector3) -> CGFloat {
let dx = destination.x - x
let dy = destination.y - y
let dz = destination.z - z
return CGFloat(sqrt(dx*dx + dy*dy + dz*dz))
}
static func positionFrom(matrix: matrix_float4x4) -> SCNVector3 {
let column = matrix.columns.3
return SCNVector3(column.x, column.y, column.z)
}
Thanks for any ideas.