1
votes

I am trying to have my code detect horizontal planes in order to find the floor. My floor is the biggest horizontal surface below the device at starting point. My current algorithm is working to find the biggest horizontal plane and identifies it as floor, but it also counts the ceiling as a horizontal plane - which it shouldn't. I have been looking around for a way around this, but no dice.

Any help is appreciated.

This is my code:

var areas = [Float]()

for grid1 in grids {
    let a = grid1.planeGeometry.width * grid1.planeGeometry.height
    areas.append(Float(a))
}

for grid2 in grids {
    let area = grid2.planeGeometry.width * grid2.planeGeometry.height

     if (Float(area)==areas.max()) {
         grid2.floored(state: true)
     } else {
         grid2.floored(state: false)
     }
}

EDIT

To clarify the definition of the floor, it is the biggest, lowest plane detected from the device's initial starting point. So, for example, if two planes (say a carpet and the floor itself) are detected separately and are of the same size, the lower one would be the floor. But, if the carpet covers more space then it is the floor.

1

1 Answers

1
votes

In ARKit 4.0 it's still a gettable-only property – it says you how ARKit classifies a surface:

public var classification: ARPlaneAnchor.Classification { get }


Use a RealityKit's property instead that is settable and conforms to OptionSet protocol:

let surfaceClassification: AnchoringComponent.Target.Classification = [.floor, 
                                                                       .seat]

In a real code it may look like this:

import RealityKit

let arView = ARView(frame: cgRect)

let box = MeshResource.generateBox(size: 0.25)
let entity = ModelEntity(mesh: box)

let anchor = AnchorEntity(plane: .horizontal,
                 classification: .floor,
                  minimumBounds: [2.0, 2.0])

anchor.addChild(entity)
arView.scene.anchors.append(anchor)