// Auxiliary function to make String from depth map array
func getStringFrom2DimArray(array: [[Float32]], height: Int, width: Int) -> String
{
var arrayStr: String = ""
for y in 0...height - 1
{
var lineStr = ""
for x in 0...width - 1
{
lineStr += String(array[y][x])
if x != width - 1
{
lineStr += ","
}
}
lineStr += "\n"
arrayStr += lineStr
}
return arrayStr
}
let depthWidth2 = CVPixelBufferGetWidth(depthMap)
let depthHeight2 = CVPixelBufferGetHeight(depthMap)
CVPixelBufferLockBaseAddress(depthMap, CVPixelBufferLockFlags(rawValue: 0))
let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(depthMap), to: UnsafeMutablePointer<Float32>.self)
var depthArray = [[Float32]]()
for y in 0...depthHeight2 - 1
{
var distancesLine = [Float32]()
for x in 0...depthWidth2 - 1
{
let distanceAtXYPoint = floatBuffer[y * depthWidth2 + x]
distancesLine.append(distanceAtXYPoint)
print("Depth in (\(x), \(y)): \(distanceAtXYPoint)")
}
depthArray.append(distancesLine)
}
let textDepthUrl = documentsDirectory.appendingPathComponent("depth_text_\(dateString).txt")
let depthString: String = getStringFrom2DimArray(array: depthArray, height: depthHeight2, width: depthWidth2)
try depthString.write(to: textDepthUrl, atomically: false, encoding: .utf8)