How to save a LiDAR depthMap as a LAS/LAZ file in Swift?

52 Views Asked by At

On iOS 17.2 platform, I am trying to save a LiDAR image in such a way that can be visualised in a cloud point viewer which reads LAS/LAZ files.

Here is my attempt which saves the depth map as a TIFF, but I would not know how to save it as a LAS/LAZ file.

    func session(_ session: ARSession, didUpdate frame: ARFrame) {
        if(frame.sceneDepth != nil) && (frame.smoothedSceneDepth != nil) {
            
            // Create CIImage from the depth map
            let depthImage = CIImage(cvPixelBuffer: frame.sceneDepth!.depthMap)

            let context = CIContext()
            guard let cgImage = context.createCGImage(depthImage, from: depthImage.extent) else {
                print("Failed to create CGImage")
                return
            }
            
            
            // Convert the CGImage to NSData with TIFF representation
            let tiffData = NSMutableData()
            guard let destination = CGImageDestinationCreateWithData(tiffData as CFMutableData, kUTTypeTIFF, 1, nil) else {
                    fatalError("Failed to create CGImageDestination")
            }
            CGImageDestinationAddImage(destination, cgImage, nil)

            // Finalize and save the TIFF data
            if savedImagesCount < 3 {
                print("Saving image...")
                if CGImageDestinationFinalize(destination) {
                    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                    let fileURL = documentsDirectory.appendingPathComponent("depthMap.tiff")
                    tiffData.write(to: fileURL, atomically: true)
                    print("Depth map saved to: \(fileURL)")
                } else {
                    print("Failed to finalize TIFF image")
                }
                savedImagesCount += 1
            }
}
0

There are 0 best solutions below