Visual SLAM

Related documentations
Map Manager
Tracker Coordinate System
Visual SLAM Learning Guide

The Visual SLAM(Simultaneous Localization and Mapping) creates a map by scanning 3D space.

Please refer Visual SLAM Learning Guide to create a map more precisely while scanning 3D space.

Create Instants
Start / Stop Tracker
Start / Stop Map Creation & Map Saving
Set Rendering Options

Create Instants

>SlamViewController.swift

var cameraDevice:MasCameraDevice = MasCameraDevice()
var sensorDevice:MasSensorDevice = MasSensorDevice()
var trackingManager:MasTrackerManager = MasTrackerManager()

Starting / Stopping the Tracker

To start / stop Tracker, refer to the following code.

>SlamViewController.swift

@objc func resumeAR()
{
    ...
    trackingManager.start(.TRACKER_TYPE_SLAM)
}

@objc func pauseAR()
{
    trackingManager.stopTracker()
    ...
}

Start / Stop Map Creation & Map Saving

  • To start a map generation, refer to the following code.
trackingManager.findSurface()
  • To stop generating a map, refer to the following code.
trackingManager.quitFindingSurface()
  • Saving map is only possible while tracking. Refer to the following code for the file storage location when you save the generated map data as a file.

>SlamViewController.swift

func getFilePath() -> String {
    let documentPaths:[String] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentPath:String = documentPaths[0]
    let contentsPath:String = "\(documentPath)/3DMap"
    
    let fileManager:FileManager = FileManager.default
    
    var isDir:ObjCBool = false
    let exists = fileManager.fileExists(atPath: contentsPath, isDirectory: &isDir)
    
    if exists == false {
        try! fileManager.createDirectory(atPath: contentsPath, withIntermediateDirectories: true, attributes: [:])
    }
    
    let filePath:String = "\(contentsPath)/\(getDateForFileName())"
    
    return filePath
}

Set Rendering Options

Feature points, SLAM initialization progress, and axis can be created with 'FeaturePoint', 'Axis' class. Refer to the following code.

You must use Convert Matrix for Metal.('metalMatrixConverter')

>SlamViewController.swift

func setupMetal() {
    ...
    featurePoint = FeaturePointCPU(device: self.device)
    axis = Axis(device: self.device)
    ...
}

func draw(in view: MTKView) {
    ...
    featurePoint.draw(commandEncoder: commandEncoder, trackingManager: trackingManager, projectionMatrix: projectionMatrix)
        
    let trackingCount:Int32 = result.getCount()
    
    for i in stride(from: 0, to: trackingCount, by: 1) {
        let trackable:MasTrackable = result.getTrackable(i)
        let poseMatrix:matrix_float4x4 = trackable.getPose() * metalMatrixConverter

        axis!.setProjectionMatrix(projectionMatrix: projectionMatrix)
        axis.setPoseMatrix(poseMatrix: poseMatrix)
        axis.setTranslation(x: 0.0, y: 0.0, z: 0.0)
        axis.setScale(x: 0.5, y: 0.5, z: 0.5)
        axis.draw(commandEncoder: commandEncoder)
    }
    ...
}

Preferably, use landscape left mode (screen orientation) for Visual SLAM.