Measuring the distance between two points on a 3D-Obj file in SceneKit/modelIO and displaying them

528 views Asked by At

I am completely new to IOS development and Swift. At present I am working on an IOS app that involves scanning a room using LiDAR sensor of IPad and later when I load the 3D Obj file and touch two arbitrary points, the length between two points should be displayed. Something similar to 3D scanner App, Canvas app.

So far I am able to export the mesh data in to an Obj file and save it to the device. I have tried for a while, but I think am kind of stuck at this point as I do not know how to proceed further for the measuring part.

The end result should look something like this.

an exported obj file with the distance label

Looking for any guidance/suggestions.

1

There are 1 answers

0
Voltan On

Scenekit uses meters, just fyi. You may have to experiment with scaling on this, I kind of doubt it will match out of the box. This assumes you have the nodes to compare distances, otherwise it's a different deal.

You can use GLKVector3Distance, or just roll your own:

func distance3D(vector1: SCNVector3, vector2: SCNVector3) -> Float
{
    let x: Float = (vector1.x - vector2.x) * (vector1.x - vector2.x)
    let y: Float = (vector1.y - vector2.y) * (vector1.y - vector2.y)
    let z: Float = (vector1.z - vector2.z) * (vector1.z - vector2.z)
    
    let temp = x + y + z
    return Float(sqrtf(Float(temp)))
}

or:

extension SCNVector3 {
     func distance(to vector: SCNVector3) -> Float {
         return simd_distance(simd_float3(self), simd_float3(vector))
     }
 }