swiftswiftuiarkitroomplan

How do I set the WorldAlignment for Apple's RoomPlan Session to be gravityAndHeading?


I am creating an app with swift that is using Apple's RoomPlan, but I am trying to set the orientation to the gravityAndHeading option. I am trying to create a custom ARSession and pass it into my RoomPlanSession in order to change the orientation, but it is not working.

class RoomCaptureController: RoomCaptureViewDelegate, RoomCaptureSessionDelegate, ObservableObject
{
  var roomCaptureView: RoomCaptureView
  private var arSession: ARSession
  private var arConfig: ARWorldTrackingConfiguration
  
  var sessionConfig: RoomCaptureSession.Configuration
  
  init() {
      let arConfig = ARWorldTrackingConfiguration()
      arConfig.worldAlignment = .gravityAndHeading
      arSession = ARSession()
    roomCaptureView = RoomCaptureView(frame: CGRect(x: 0, y: 0, width: 42, height: 42)) arSession: arSession)
    sessionConfig = RoomCaptureSession.Configuration()
    roomCaptureView.captureSession.delegate = self
    roomCaptureView.delegate = self
  }

Above is my current code, but I get an issue about self being used before all variables are declared at the line where roomCaptureView is assigned.

The line assigning RoomCaptureView has this error: 'self' used in property access 'arSession' before all stored properties are initialized

The two delegate lines have this error: 'self' used in property access 'roomCaptureView' before all stored properties are initialized

And the return says that I didn't initialise all the stored properties.

If I run it without the custom ARSession, my code works.

How can I fix it?


Solution

  • I figured it out, had to switch around some code. I think the issue was with declaring arSession before the init, and then assigning and using it as the input to the RoomCaptureView in the init.

    class RoomCaptureController: RoomCaptureViewDelegate, RoomCaptureSessionDelegate, ObservableObject{
        var roomCaptureView: RoomCaptureView
        var sessionConfig: RoomCaptureSession.Configuration
      
        init() {
            roomCaptureView = RoomCaptureView(frame: CGRect(x: 0, y: 0, width: 42, height: 42))
            sessionConfig = RoomCaptureSession.Configuration()
            let arConfig = ARWorldTrackingConfiguration()
            arConfig.worldAlignment = .gravityAndHeading
            roomCaptureView.captureSession.arSession.run(arConfig)
            roomCaptureView.captureSession.delegate = self
            roomCaptureView.delegate = self     
        }
    }