Advertisement
TiyasAria

RoomController

Nov 17th, 2023
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.16 KB | None | 0 0
  1. //
  2. //  RoomController.swift
  3. //  keluarga-cemara
  4. //
  5. //  Created by tiyas aria on 18/10/23.
  6. //
  7.  
  8. import RoomPlan
  9. import SceneKit
  10. import UIKit
  11.  
  12.  
  13. class RoomController : ObservableObject,RoomCaptureViewDelegate, RoomCaptureSessionDelegate{
  14.     func encode(with coder: NSCoder) {
  15.         fatalError("Not Needed")
  16.     }
  17.    
  18.    
  19.     required dynamic init?(coder: NSCoder) {
  20.         fatalError("Not Needed")
  21.     }
  22.    
  23.    
  24.     //    MARK: Making properties
  25.     static var instance = RoomController()
  26.     @Published var roomCaptureView : RoomCaptureView
  27.     var sessionConfig : RoomCaptureSession.Configuration
  28.     var finalResults : CapturedRoom?
  29.    
  30.     var sceneView : SCNView?
  31.    
  32.     init(){
  33.         roomCaptureView = RoomCaptureView(frame: .zero)
  34.         sessionConfig = RoomCaptureSession.Configuration()
  35.         roomCaptureView.captureSession.delegate = self
  36.         roomCaptureView.delegate = self
  37.     }
  38.    
  39.     //    MARK: func to start and stop scanning
  40.    
  41.     func startSession() {
  42.         roomCaptureView.captureSession.run(configuration: sessionConfig)
  43.     }
  44.    
  45.     func stopSession() {
  46.         roomCaptureView.captureSession.stop()
  47.     }
  48.    
  49.    
  50.     //    MARK: func captureview (post-processing scan result)
  51.    
  52.     func captureView(shouldPresent roomDataForProcessing: CapturedRoomData, error: (Error)?) -> Bool {
  53.         return true
  54.     }
  55.    
  56.     func captureView(didPresent processedResult: CapturedRoom, error: (Error)?) {
  57. //        if let error = error as? RoomCaptureSession.CaptureError, error == .worldTrackingFailure {
  58. //            let alert = UIAlertController(title: "World Tracking Failure", message: "An unexpected error occurred during world tracking.Please click re-scan to start scan again", preferredStyle: .alert)
  59. //            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  60. //            UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
  61. //            return
  62. //        }
  63. //        guard error == nil else {
  64. //            print("Error when capturing room: \(error!.localizedDescription)")
  65. //            return
  66. //        }
  67.         Task{
  68.             finalResults = processedResult
  69.             export()
  70.             return
  71.         }
  72.     }
  73.    
  74.     //    MARK: func for export file
  75.    
  76.     func export(){
  77.         if let finalResults {
  78.             let fm = FileManager.default
  79.             var path = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
  80.             let fileName = "room.usdz"
  81.             path.appendPathComponent(fileName)
  82.             print("file name \(path)")
  83.            
  84.             do {
  85.                 try  finalResults.export(to: path.absoluteURL)
  86.                 print("Berhasil export ")
  87.             }
  88.             catch{
  89.                 print(error)
  90.             }
  91.         }
  92.     }
  93.    
  94.    
  95.    
  96.     //    MARK: func for scenekit detect dimension of surface
  97.     func getAllNodes(for surfaces : [CapturedRoom.Surface], contents : Any?) -> [SCNNode] {
  98.         var nodes : [SCNNode] = []
  99.        
  100.         surfaces.forEach { surface in
  101.             let width = CGFloat(surface.dimensions.x)
  102.             let height = CGFloat(surface.dimensions.y)
  103.             let depth = CGFloat(surface.dimensions.z)
  104.            
  105.             print("Surface with category \(surface.category), have a width \(width), height \(height), and depth \(depth)")
  106.            
  107.             let node = SCNNode()
  108.             node.geometry = SCNBox(width: width, height: height, length: depth, chamferRadius: 0.0)
  109.             node.geometry?.firstMaterial?.diffuse.contents = contents
  110.             node.transform = SCNMatrix4(surface.transform)
  111.             nodes.append(node)
  112.         }
  113.        
  114.         return nodes
  115.     }
  116.    
  117.    
  118.    
  119.     func onModelReady(model : CapturedRoom) {
  120.         let walls = getAllNodes(for: model.walls , contents:  UIColor.red)
  121.         walls.forEach { sceneView?.scene?.rootNode.addChildNode($0)}
  122.        
  123.         let doors = getAllNodes(for: model.doors , contents:  UIColor.blue)
  124.         doors.forEach { sceneView?.scene?.rootNode.addChildNode($0)}
  125.        
  126.         let windows = getAllNodes(for: model.windows , contents:  UIColor.gray)
  127.         windows.forEach { sceneView?.scene?.rootNode.addChildNode($0)}
  128.        
  129.         let openings = getAllNodes(for: model.openings , contents:  UIColor.green)
  130.         openings.forEach { sceneView?.scene?.rootNode.addChildNode($0)}
  131.        
  132.         if #available(iOS 17.0, *) {
  133.             let floors = getAllNodes(for: model.floors , contents:  UIColor.blue.withAlphaComponent(0.6))
  134.             floors.forEach { sceneView?.scene?.rootNode.addChildNode($0)}
  135.         } else {
  136.             // Fallback on earlier versions
  137.         }
  138.        
  139.         //         looping for object
  140.         for object in model.objects {
  141.             let uuidString = object.identifier.uuidString
  142.             let category = object.category
  143.             let dimesion = object.dimensions
  144.            
  145.             print("Object with id \(uuidString) with category \(category), with width \(dimesion.x), height \(dimesion.y) and depth \(dimesion.z)")
  146.         }
  147.     }
  148.    
  149.    
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement