1

I'm trying to calculate surface area and volume of a 3D mesh/object that I'm loading in scene view.

I tried some solutions from stackoverflow but the actual surface area is way less than the calculated one.

Here's my code.

private func calculateSuperficialAreaOfMesh() -> Float {        
    let points = modelNode.childNodes[0].geometry!.vertices() ?? []
    
    var len = points.count
    var area = 0.0
    
    if(len == 0) {
        return 0.0
    }
    
    var i = 0
    var va: SCNVector3
    var vb: SCNVector3
    var vc: SCNVector3
    
    repeat {
        va = points[i]
        vb = points[i + 2]
        vc = points[i + 3]
        
        
        let ab = SCNVector3(x: vb.x - va.x, y: vb.y - va.y, z: vb.z - va.z)
        let ac = SCNVector3(x: vc.x - va.x, y: vc.y - va.y, z: va.z - vc.z)
        
        let cross = ab.cross(vector: ac)
        area += Double(sqrt(pow(cross.x, 2) + pow(cross.y , 2) + pow(cross.z, 2))) / 2
        
        i += 3
    } while (i < points.count - 3)
    
    
    return Float(area)
}

And getting vertices from

extension SCNGeometry {


/**
 Get the vertices (3d points coordinates) of the geometry.
 
 - returns: An array of SCNVector3 containing the vertices of the geometry.
 */
func vertices() -> [SCNVector3]? {
    
    let sources = self.sources(for: .vertex)
    
    guard let source  = sources.first else{return nil}
    
    let stride = source.dataStride / source.bytesPerComponent
    let offset = source.dataOffset / source.bytesPerComponent
    let vectorCount = source.vectorCount
    
    return source.data.withUnsafeBytes{ (buffer : UnsafePointer<Float>) -> [SCNVector3] in
        
        var result = Array<SCNVector3>()
        for i in 0...vectorCount - 1 {
            let start = i * stride + offset
            let x = buffer[start]
            let y = buffer[start + 1]
            let z = buffer[start + 2]
            result.append(SCNVector3(x, y, z))
        }
        return result
    }
}
}
Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50

1 Answers1

0

I have searched for your solution. I found that you can measure distance between two 3d points, by using that I think you can achieve the surface area also. This tutorial can guide you: Link to ARKit measuring object

  • Thanks but I believe this won't work, I can calculate distance between any 2 points, not an issue. Here I need to calculate the total surface area. I cannot access triangle data as SceneKit doesn't provide it. Neither I can access the object faces. I only have vertices. If I try to calculate distance between 2 points and keep on repeating it just think of the complexity and inaccuracy and amount of processing the system will need to do. – Jaswant Singh Nov 23 '22 at 09:07
  • Can you have a look at this also https://stackoverflow.com/questions/58995442/detect-a-object-using-camera-and-position-a-3d-object-using-arkit-in-ios : this measures the real object shape and place a virtual object of that shape inside that. – Mostafa Shamin Yeasar Nov 23 '22 at 09:18