File size: 30,534 Bytes
5fb2d50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfcc093
 
5fb2d50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//
//  SHARPModelRunner.swift
//  SHARP Model Inference and PLY Export
//
//  Loads a SHARP Core ML model, runs inference on an image,
//  and saves the 3D Gaussian splat output as a PLY file.
// 
//  Usage:
//    swiftc -O -o sharp_runner sharp.swift -framework CoreML -framework CoreImage -framework AppKit
//    ./sharp_runner sharp.mlpackage test.png output.ply -d 0.5

import Foundation
import CoreML
import CoreImage
import AppKit  // For NSImage on macOS; use UIKit for iOS

// MARK: - Gaussians3D Structure

/// Represents the output of the SHARP model - a collection of 3D Gaussians
struct Gaussians3D {
    let meanVectors: MLMultiArray      // Shape: (1, N, 3) - 3D positions
    let singularValues: MLMultiArray   // Shape: (1, N, 3) - scales
    let quaternions: MLMultiArray      // Shape: (1, N, 4) - rotations
    let colors: MLMultiArray           // Shape: (1, N, 3) - RGB colors (linear)
    let opacities: MLMultiArray        // Shape:  (1, N) - opacity values
    
    var count: Int {
        return meanVectors.shape[1].intValue
    }
    
    /// Compute importance scores for each Gaussian.
    /// Higher scores = more important (larger and more opaque).
    func computeImportanceScores() -> [Float] {
        let n = count
        var scores = [Float](repeating: 0, count: n)
        
        let scalePtr = singularValues.dataPointer.assumingMemoryBound(to: Float.self)
        let opacityPtr = opacities.dataPointer.assumingMemoryBound(to: Float.self)
        
        for i in 0..<n {
            // Sum of log scales (singular values are already in linear space, not log)
            // To match Python:  scales = exp(scale_0 + scale_1 + scale_2)
            // But our singularValues are already exp(log_scale), so we need log them first
            let s0 = scalePtr[i * 3 + 0]
            let s1 = scalePtr[i * 3 + 1]
            let s2 = scalePtr[i * 3 + 2]
            
            // Product of scales (equivalent to exp(log_s0 + log_s1 + log_s2))
            let scaleProduct = s0 * s1 * s2
            
            // Opacity is already in [0, 1] range (after sigmoid in model)
            let opacity = opacityPtr[i]
            
            scores[i] = scaleProduct * opacity
        }
        
        return scores
    }
    
    /// Decimate the Gaussians by keeping only a fraction based on importance.
    /// Returns indices of Gaussians to keep, sorted for spatial coherence.
    func decimationIndices(keepRatio: Float) -> [Int] {
        let n = count
        let keepCount = max(1, Int(Float(n) * keepRatio))
        
        // Compute importance scores
        let scores = computeImportanceScores()
        
        // Create array of (index, score) pairs and sort by score descending
        var indexedScores = scores.enumerated().map { ($0.offset, $0.element) }
        indexedScores.sort { $0.1 > $1.1 }
        
        // Get top keepCount indices
        var keepIndices = indexedScores.prefix(keepCount).map { $0.0 }
        
        // Sort indices to maintain spatial coherence
        keepIndices.sort()
        
        return keepIndices
    }
}

// MARK: - Color Space Utilities

/// Convert linear RGB to sRGB color space
func linearRGBToSRGB(_ linear: Float) -> Float {
    if linear <= 0.0031308 {
        return linear * 12.92
    } else {
        return 1.055 * pow(linear, 1.0 / 2.4) - 0.055
    }
}

/// Convert RGB to degree-0 spherical harmonics
func rgbToSphericalHarmonics(_ rgb: Float) -> Float {
    let coeffDegree0 = sqrt(1.0 / (4.0 * Float.pi))
    return (rgb - 0.5) / coeffDegree0
}

/// Inverse sigmoid function
func inverseSigmoid(_ x: Float) -> Float {
    let clamped = min(max(x, 1e-6), 1.0 - 1e-6)
    return log(clamped / (1.0 - clamped))
}

// MARK: - SHARP Model Wrapper

class SHARPModelRunner {
    private let model: MLModel
    private let inputHeight: Int
    private let inputWidth: Int
    
    init(modelPath: URL, inputHeight: Int = 1536, inputWidth: Int = 1536) throws {
        let config = MLModelConfiguration()
        config.computeUnits = .all
        
        // Compile the model if needed
        let compiledModelURL = try SHARPModelRunner.compileModelIfNeeded(at: modelPath)
        
        self.model = try MLModel(contentsOf: compiledModelURL, configuration:  config)
        self.inputHeight = inputHeight
        self.inputWidth = inputWidth
        
        // Print model description for debugging
        print("Model inputs: \(model.modelDescription.inputDescriptionsByName.keys.joined(separator: ", "))")
        print("Model outputs:  \(model.modelDescription.outputDescriptionsByName.keys.joined(separator: ", "))")
    }
    
    /// Compile the model if it's not already compiled
    private static func compileModelIfNeeded(at modelPath: URL) throws -> URL {
        let fileManager = FileManager.default
        let pathExtension = modelPath.pathExtension.lowercased()
        
        // If already compiled (.mlmodelc), return as-is
        if pathExtension == "mlmodelc" {
            print("Model is already compiled.")
            return modelPath
        }
        
        // Check if it's an .mlpackage or .mlmodel that needs compilation
        guard pathExtension == "mlpackage" || pathExtension == "mlmodel" else {
            throw NSError(domain: "SHARPModelRunner", code: 10,
                         userInfo: [NSLocalizedDescriptionKey: "Unsupported model format:  \(pathExtension).Use .mlpackage, .mlmodel, or .mlmodelc"])
        }
        
        // Create a cache directory for compiled models
        let cacheDir = fileManager.temporaryDirectory.appendingPathComponent("SHARPModelCache")
        try?  fileManager.createDirectory(at: cacheDir, withIntermediateDirectories:  true)
        
        // Generate a unique name for the compiled model based on the source path
        let modelName = modelPath.deletingPathExtension().lastPathComponent
        let compiledPath = cacheDir.appendingPathComponent("\(modelName).mlmodelc")
        
        // Check if we have a cached compiled version
        if fileManager.fileExists(atPath: compiledPath.path) {
            // Verify the cached version is newer than the source
            let sourceAttrs = try fileManager.attributesOfItem(atPath:  modelPath.path)
            let cachedAttrs = try fileManager.attributesOfItem(atPath: compiledPath.path)
            
            if let sourceDate = sourceAttrs[.modificationDate] as?  Date,
               let cachedDate = cachedAttrs[.modificationDate] as? Date,
               cachedDate >= sourceDate {
                print("Using cached compiled model at \(compiledPath.path)")
                return compiledPath
            } else {
                // Source is newer, remove old cached version
                try? fileManager.removeItem(at: compiledPath)
            }
        }
        
        // Compile the model
        print("Compiling model (this may take a moment)...")
        let startTime = CFAbsoluteTimeGetCurrent()
        
        let temporaryCompiledURL = try MLModel.compileModel(at: modelPath)
        
        let compileTime = CFAbsoluteTimeGetCurrent() - startTime
        print("✓ Model compiled in \(String(format: "%.1f", compileTime))s")
        
        // Move to our cache directory
        try?  fileManager.removeItem(at: compiledPath)
        try fileManager.moveItem(at: temporaryCompiledURL, to: compiledPath)
        
        print("Compiled model cached at \(compiledPath.path)")
        return compiledPath
    }
    
    /// Load and preprocess an image for model input
    func preprocessImage(at imagePath: URL) throws -> MLMultiArray {
        guard let nsImage = NSImage(contentsOf: imagePath) else {
            throw NSError(domain: "SHARPModelRunner", code: 1,
                         userInfo: [NSLocalizedDescriptionKey:  "Failed to load image from \(imagePath.path)"])
        }
        
        guard let cgImage = nsImage.cgImage(forProposedRect: nil, context: nil, hints:  nil) else {
            throw NSError(domain: "SHARPModelRunner", code: 2,
                         userInfo: [NSLocalizedDescriptionKey: "Failed to convert to CGImage"])
        }
        
        // Create CIImage and resize
        let ciImage = CIImage(cgImage: cgImage)
        let context = CIContext()
        
        // Scale to target size
        let scaleX = CGFloat(inputWidth) / ciImage.extent.width
        let scaleY = CGFloat(inputHeight) / ciImage.extent.height
        let scaledImage = ciImage.transformed(by: CGAffineTransform(scaleX: scaleX, y:  scaleY))
        
        // Render to bitmap
        guard let resizedCGImage = context.createCGImage(scaledImage, from:  CGRect(x: 0, y: 0,
                                                                                    width: inputWidth,
                                                                                    height: inputHeight)) else {
            throw NSError(domain: "SHARPModelRunner", code: 3,
                         userInfo: [NSLocalizedDescriptionKey: "Failed to resize image"])
        }
        
        // Convert to MLMultiArray (1, 3, H, W) normalized to [0, 1]
        let imageArray = try MLMultiArray(shape: [1, 3, NSNumber(value: inputHeight), NSNumber(value: inputWidth)],
                                          dataType: .float32)
        
        let width = resizedCGImage.width
        let height = resizedCGImage.height
        let bytesPerPixel = 4
        let bytesPerRow = bytesPerPixel * width
        var pixelData = [UInt8](repeating: 0, count: height * bytesPerRow)
        
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        guard let cgContext = CGContext(data: &pixelData,
                                        width: width,
                                        height:  height,
                                        bitsPerComponent: 8,
                                        bytesPerRow: bytesPerRow,
                                        space: colorSpace,
                                        bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
            throw NSError(domain: "SHARPModelRunner", code: 4,
                         userInfo:  [NSLocalizedDescriptionKey: "Failed to create bitmap context"])
        }
        
        cgContext.draw(resizedCGImage, in: CGRect(x: 0, y: 0, width: width, height: height))
        
        // Copy pixel data to MLMultiArray in CHW format
        // Use pointer access for better performance
        let ptr = imageArray.dataPointer.assumingMemoryBound(to: Float.self)
        let channelStride = inputHeight * inputWidth
        
        for y in 0..<height {
            for x in 0..<width {
                let pixelIndex = y * bytesPerRow + x * bytesPerPixel
                let r = Float(pixelData[pixelIndex]) / 255.0
                let g = Float(pixelData[pixelIndex + 1]) / 255.0
                let b = Float(pixelData[pixelIndex + 2]) / 255.0
                
                let spatialIndex = y * inputWidth + x
                ptr[0 * channelStride + spatialIndex] = r
                ptr[1 * channelStride + spatialIndex] = g
                ptr[2 * channelStride + spatialIndex] = b
            }
        }
        
        return imageArray
    }
    
    /// Run inference on the model
    func predict(image: MLMultiArray, focalLengthPx: Float) throws -> Gaussians3D {
        // Calculate disparity factor:  focal_length / image_width
        let disparityFactor = focalLengthPx / Float(inputWidth)
        
        // Create disparity factor input
        let disparityArray = try MLMultiArray(shape: [1], dataType: .float32)
        disparityArray[0] = NSNumber(value: disparityFactor)
        
        // Create feature provider
        let inputFeatures = try MLDictionaryFeatureProvider(dictionary: [
            "image":  MLFeatureValue(multiArray: image),
            "disparity_factor": MLFeatureValue(multiArray:  disparityArray)
        ])
        
        // Run prediction
        let output = try model.prediction(from: inputFeatures)
        
        // Try to find outputs by checking available names
        let outputNames = Array(model.modelDescription.outputDescriptionsByName.keys)
        
        // Helper function to find output by partial name match
        func findOutput(containing keywords: [String]) -> MLMultiArray? {
            for name in outputNames {
                let lowercaseName = name.lowercased()
                for keyword in keywords {
                    if lowercaseName.contains(keyword.lowercased()) {
                        return output.featureValue(for:  name)?.multiArrayValue
                    }
                }
            }
            return nil
        }
        
        // Try to match outputs - first try exact names, then partial matches
        let meanVectors = output.featureValue(for: "mean_vectors_3d_positions")?.multiArrayValue
            ?? findOutput(containing: ["mean", "position", "xyz"])
        
        let singularValues = output.featureValue(for: "singular_values_scales")?.multiArrayValue
            ?? findOutput(containing: ["singular", "scale"])
        
        let quaternions = output.featureValue(for: "quaternions_rotations")?.multiArrayValue
            ??  findOutput(containing:  ["quaternion", "rotation", "rot"])
        
        let colors = output.featureValue(for: "colors_rgb_linear")?.multiArrayValue
            ?? findOutput(containing: ["color", "rgb"])
        
        let opacities = output.featureValue(for: "opacities_alpha_channel")?.multiArrayValue
            ?? findOutput(containing: ["opacity", "alpha"])
        
        // If we still couldn't find outputs, try by index order
        if meanVectors == nil || singularValues == nil || quaternions == nil || colors == nil || opacities == nil {
            print("Warning: Could not match all outputs by name.Available outputs: \(outputNames)")
            
            // Try to get outputs by index if we have exactly 5
            if outputNames.count >= 5 {
                let sortedNames = outputNames.sorted()
                guard let mv = output.featureValue(for: sortedNames[0])?.multiArrayValue,
                      let sv = output.featureValue(for: sortedNames[1])?.multiArrayValue,
                      let q = output.featureValue(for: sortedNames[2])?.multiArrayValue,
                      let c = output.featureValue(for: sortedNames[3])?.multiArrayValue,
                      let o = output.featureValue(for: sortedNames[4])?.multiArrayValue else {
                    throw NSError(domain:  "SHARPModelRunner", code:  5,
                                 userInfo:  [NSLocalizedDescriptionKey: "Failed to extract model outputs. Available:  \(outputNames)"])
                }
                
                print("Using outputs by sorted order: \(sortedNames)")
                return Gaussians3D(
                    meanVectors: mv,
                    singularValues:  sv,
                    quaternions: q,
                    colors:  c,
                    opacities: o
                )
            }
            
            throw NSError(domain: "SHARPModelRunner", code: 5,
                         userInfo: [NSLocalizedDescriptionKey:  "Failed to extract model outputs.Available: \(outputNames)"])
        }
        
        return Gaussians3D(
            meanVectors: meanVectors!,
            singularValues: singularValues!,
            quaternions: quaternions!,
            colors: colors!,
            opacities:  opacities! 
        )
    }
    
    /// Save Gaussians to PLY file (matching Python save_ply format exactly)
    /// - Parameters:
    ///   - gaussians:  The Gaussians to save
    ///   - focalLengthPx:  Focal length in pixels
    ///   - imageShape: Image dimensions (height, width)
    ///   - outputPath: Output file path
    ///   - decimation: Optional decimation ratio (0.0-1.0).1.0 = keep all, 0.5 = keep 50%
    func savePLY(gaussians: Gaussians3D,
                 focalLengthPx: Float,
                 imageShape: (height: Int, width: Int),
                 to outputPath:  URL,
                 decimation: Float = 1.0) throws {
        
        let imageHeight = imageShape.height
        let imageWidth = imageShape.width
        
        // Determine which indices to keep based on decimation
        let keepIndices:  [Int]
        let originalCount = gaussians.count
        
        if decimation < 1.0 {
            keepIndices = gaussians.decimationIndices(keepRatio: decimation)
            print("Decimating:  keeping \(keepIndices.count) of \(originalCount) Gaussians (\(String(format: "%.1f", decimation * 100))%)")
        } else {
            keepIndices = Array(0..<originalCount)
        }
        
        let numGaussians = keepIndices.count
        
        var fileContent = Data()
        
        // Helper to append string
        func appendString(_ str: String) {
            fileContent.append(str.data(using: .ascii)!)
        }
        
        // Helper to append float32 in little-endian
        func appendFloat32(_ value: Float) {
            var v = value
            fileContent.append(Data(bytes: &v, count: 4))
        }
        
        // Helper to append int32 in little-endian
        func appendInt32(_ value: Int32) {
            var v = value
            fileContent.append(Data(bytes: &v, count: 4))
        }
        
        // Helper to append uint32 in little-endian
        func appendUInt32(_ value: UInt32) {
            var v = value
            fileContent.append(Data(bytes: &v, count: 4))
        }
        
        // Helper to append uint8
        func appendUInt8(_ value:  UInt8) {
            var v = value
            fileContent.append(Data(bytes: &v, count: 1))
        }
        
        // ===== PLY Header =====
        appendString("ply\n")
        appendString("format binary_little_endian 1.0\n")
        
        // Vertex element
        appendString("element vertex \(numGaussians)\n")
        appendString("property float x\n")
        appendString("property float y\n")
        appendString("property float z\n")
        appendString("property float f_dc_0\n")
        appendString("property float f_dc_1\n")
        appendString("property float f_dc_2\n")
        appendString("property float opacity\n")
        appendString("property float scale_0\n")
        appendString("property float scale_1\n")
        appendString("property float scale_2\n")
        appendString("property float rot_0\n")
        appendString("property float rot_1\n")
        appendString("property float rot_2\n")
        appendString("property float rot_3\n")
        
        // Extrinsic element (16 floats for 4x4 identity matrix)
        appendString("element extrinsic 16\n")
        appendString("property float extrinsic\n")
        
        // Intrinsic element (9 floats for 3x3 matrix)
        appendString("element intrinsic 9\n")
        appendString("property float intrinsic\n")
        
        // Image size element
        appendString("element image_size 2\n")
        appendString("property uint image_size\n")
        
        // Frame element
        appendString("element frame 2\n")
        appendString("property int frame\n")
        
        // Disparity element
        appendString("element disparity 2\n")
        appendString("property float disparity\n")
        
        // Color space element
        appendString("element color_space 1\n")
        appendString("property uchar color_space\n")
        
        // Version element
        appendString("element version 3\n")
        appendString("property uchar version\n")
        
        appendString("end_header\n")
        
        // ===== Vertex Data =====
        // Compute disparity quantiles for later
        var disparities: [Float] = []
        
        // Get pointers for faster access
        let meanPtr = gaussians.meanVectors.dataPointer.assumingMemoryBound(to: Float.self)
        let scalePtr = gaussians.singularValues.dataPointer.assumingMemoryBound(to: Float.self)
        let quatPtr = gaussians.quaternions.dataPointer.assumingMemoryBound(to: Float.self)
        let colorPtr = gaussians.colors.dataPointer.assumingMemoryBound(to:  Float.self)
        let opacityPtr = gaussians.opacities.dataPointer.assumingMemoryBound(to: Float.self)
        
        for i in keepIndices {
            // Position (x, y, z)
            let x = meanPtr[i * 3 + 0]
            let y = meanPtr[i * 3 + 1]
            let z = meanPtr[i * 3 + 2]
            appendFloat32(x)
            appendFloat32(y)
            appendFloat32(z)
            
            // Compute disparity for quantiles
            if z > 1e-6 {
                disparities.append(1.0 / z)
            }
            
            // Colors:  Convert linearRGB -> sRGB -> spherical harmonics
            // Model outputs linearRGB colors for proper alpha blending
            // We convert to sRGB for compatibility with public renderers
            let colorR = colorPtr[i * 3 + 0]
            let colorG = colorPtr[i * 3 + 1]
            let colorB = colorPtr[i * 3 + 2]
            
            let srgbR = linearRGBToSRGB(colorR)
            let srgbG = linearRGBToSRGB(colorG)
            let srgbB = linearRGBToSRGB(colorB)
            
            let sh0 = rgbToSphericalHarmonics(srgbR)
            let sh1 = rgbToSphericalHarmonics(srgbG)
            let sh2 = rgbToSphericalHarmonics(srgbB)
            
            appendFloat32(sh0)
            appendFloat32(sh1)
            appendFloat32(sh2)
            
            // Opacity:  Convert to logits using inverse sigmoid
            let opacity = opacityPtr[i]
            let opacityLogit = inverseSigmoid(opacity)
            appendFloat32(opacityLogit)
            
            // Scales:  Convert to log scale
            let scale0 = scalePtr[i * 3 + 0]
            let scale1 = scalePtr[i * 3 + 1]
            let scale2 = scalePtr[i * 3 + 2]
            
            appendFloat32(log(max(scale0, 1e-10)))
            appendFloat32(log(max(scale1, 1e-10)))
            appendFloat32(log(max(scale2, 1e-10)))
            
            // Quaternions (w, x, y, z)
            let q0 = quatPtr[i * 4 + 0]
            let q1 = quatPtr[i * 4 + 1]
            let q2 = quatPtr[i * 4 + 2]
            let q3 = quatPtr[i * 4 + 3]
            
            appendFloat32(q0)
            appendFloat32(q1)
            appendFloat32(q2)
            appendFloat32(q3)
        }
        
        // ===== Extrinsic Data (4x4 identity matrix) =====
        let identity:  [Float] = [
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
        ]
        for val in identity {
            appendFloat32(val)
        }
        
        // ===== Intrinsic Data (3x3 matrix) =====
        let intrinsic: [Float] = [
            focalLengthPx, 0, Float(imageWidth) * 0.5,
            0, focalLengthPx, Float(imageHeight) * 0.5,
            0, 0, 1
        ]
        for val in intrinsic {
            appendFloat32(val)
        }
        
        // ===== Image Size Data =====
        appendUInt32(UInt32(imageWidth))
        appendUInt32(UInt32(imageHeight))
        
        // ===== Frame Data =====
        appendInt32(1)  // Number of frames
        appendInt32(Int32(numGaussians))  // Particles per frame
        
        // ===== Disparity Data (quantiles) =====
        disparities.sort()
        let q10Index = Int(Float(disparities.count) * 0.1)
        let q90Index = Int(Float(disparities.count) * 0.9)
        let disparity10 = disparities.isEmpty ? 0.0 : disparities[min(q10Index, disparities.count - 1)]
        let disparity90 = disparities.isEmpty ?  1.0 : disparities[min(q90Index, disparities.count - 1)]
        appendFloat32(disparity10)
        appendFloat32(disparity90)
        
        // ===== Color Space Data (sRGB = 1) =====
        appendUInt8(1)
        
        // ===== Version Data =====
        appendUInt8(1)  // Major
        appendUInt8(5)  // Minor
        appendUInt8(0)  // Patch
        
        // Write to file
        try fileContent.write(to: outputPath)
        
        print("✓ Saved PLY with \(numGaussians) Gaussians to \(outputPath.path)")
    }
}

// MARK: - Command Line Argument Parsing

struct CommandLineArgs {
    let modelPath: URL
    let imagePath: URL
    let outputPath: URL
    let focalLength: Float
    let decimation: Float
    
    static func parse() -> CommandLineArgs?  {
        let args = CommandLine.arguments
        
        var modelPath: URL? 
        var imagePath: URL?
        var outputPath: URL?
        var focalLength: Float = 1536.0
        var decimation: Float = 1.0
        
        var i = 1
        while i < args.count {
            let arg = args[i]
            
            switch arg {
            case "-m", "--model":
                i += 1
                if i < args.count {
                    modelPath = URL(fileURLWithPath: args[i])
                }
                
            case "-i", "--input":
                i += 1
                if i < args.count {
                    imagePath = URL(fileURLWithPath: args[i])
                }
                
            case "-o", "--output":
                i += 1
                if i < args.count {
                    outputPath = URL(fileURLWithPath:  args[i])
                }
                
            case "-f", "--focal-length": 
                i += 1
                if i < args.count {
                    focalLength = Float(args[i]) ?? 1536.0
                }
                
            case "-d", "--decimation": 
                i += 1
                if i < args.count {
                    if let value = Float(args[i]) {
                        // Accept both percentage (0-100) and ratio (0-1)
                        if value > 1.0 {
                            decimation = value / 100.0
                        } else {
                            decimation = value
                        }
                        decimation = max(0.01, min(1.0, decimation))
                    }
                }
                
            case "-h", "--help": 
                printUsage()
                return nil
                
            default:
                // Handle positional arguments for backward compatibility
                if modelPath == nil {
                    modelPath = URL(fileURLWithPath: arg)
                } else if imagePath == nil {
                    imagePath = URL(fileURLWithPath: arg)
                } else if outputPath == nil {
                    outputPath = URL(fileURLWithPath: arg)
                } else if focalLength == 1536.0 {
                    focalLength = Float(arg) ?? 1536.0
                }
            }
            
            i += 1
        }
        
        guard let model = modelPath, let image = imagePath, let output = outputPath else {
            printUsage()
            return nil
        }
        
        return CommandLineArgs(
            modelPath: model,
            imagePath: image,
            outputPath: output,
            focalLength: focalLength,
            decimation:  decimation
        )
    }
    
    static func printUsage() {
        let execName = CommandLine.arguments[0].components(separatedBy:  "/").last ?? "sharp_runner"
        print("""
        Usage: \(execName) [OPTIONS] <model> <input_image> <output.ply>
        
        SHARP Model Inference - Generate 3D Gaussian Splats from a single image
        
        Arguments:
          model              Path to the SHARP Core ML model (.mlpackage, .mlmodel, or .mlmodelc)
          input_image        Path to input image (PNG, JPEG, etc.)
          output.ply         Path for output PLY file
        
        Options: 
          -m, --model PATH           Path to Core ML model
          -i, --input PATH           Path to input image
          -o, --output PATH          Path for output PLY file
          -f, --focal-length FLOAT   Focal length in pixels (default: 1536)
          -d, --decimation FLOAT     Decimation ratio 0.0-1.0 or percentage 1-100 (default:  1.0 = keep all)
                                     Example: 0.5 or 50 keeps 50% of Gaussians
          -h, --help                 Show this help message
        
        Examples:
          # Basic usage
          \(execName) sharp.mlpackage photo.jpg output.ply
        
          # With focal length
          \(execName) sharp.mlpackage photo.jpg output.ply 768
        
          # With decimation (keep 50% of points)
          \(execName) -m sharp.mlpackage -i photo.jpg -o output.ply -d 0.5
        
          # With decimation as percentage
          \(execName) -m sharp.mlpackage -i photo.jpg -o output.ply -d 25
        
        The model will be automatically compiled on first use and cached for subsequent runs.
        Decimation keeps the most important Gaussians based on scale and opacity.
        """)
    }
}

// MARK:  - Main Entry Point

func main() {
    guard let args = CommandLineArgs.parse() else {
        exit(1)
    }
    
    do {
        print("Loading SHARP model from \(args.modelPath.path)...")
        let runner = try SHARPModelRunner(modelPath:  args.modelPath)
        
        print("Preprocessing image \(args.imagePath.path)...")
        let imageArray = try runner.preprocessImage(at: args.imagePath)
        
        print("Running inference...")
        let startTime = CFAbsoluteTimeGetCurrent()
        let gaussians = try runner.predict(image: imageArray, focalLengthPx: args.focalLength)
        let inferenceTime = CFAbsoluteTimeGetCurrent() - startTime
        
        print("✓ Generated \(gaussians.count) Gaussians in \(String(format: "%.2f", inferenceTime))s")
        
        print("Saving PLY file...")
        try runner.savePLY(
            gaussians: gaussians,
            focalLengthPx: args.focalLength,
            imageShape: (height: 1536, width: 1536),
            to: args.outputPath,
            decimation:  args.decimation
        )
        
        print("✓ Complete!")
        
    } catch {
        print("Error: \(error.localizedDescription)")
        if let nsError = error as NSError? {
            print("Domain: \(nsError.domain), Code: \(nsError.code)")
            if let underlyingError = nsError.userInfo[NSUnderlyingErrorKey] as?  Error {
                print("Underlying error: \(underlyingError)")
            }
        }
        exit(1)
    }
}

main()