2017-09-05 4 views
0

Étant donné un SCNNode, comment puis-je déterminer si un point (spécifié dans le système de coordonnées du nœud) est contenu dans la géométrie de ce nœud?Point SCNNode à l'intérieur du test

Alternativement, si plus simple, comment puis-je tester si un point est contenu dans la boîte englobante du nœud?

Répondre

0

Je serais surpris s'il n'y a pas une meilleure réponse que celle-ci, mais je n'en ai pas trouvé une, alors voici ce que j'ai.

extension SCNNode { 
    func boundingBoxContains(point: SCNVector3, in node: SCNNode) -> Bool { 
    let localPoint = self.convertPosition(point, from: node) 
    return boundingBoxContains(point: localPoint) 
    } 

    func boundingBoxContains(point: SCNVector3) -> Bool { 
    return BoundingBox(self.boundingBox).contains(point) 
    } 
} 

struct BoundingBox { 
    let min: SCNVector3 
    let max: SCNVector3 

    init(_ boundTuple: (min: SCNVector3, max: SCNVector3)) { 
    min = boundTuple.min 
    max = boundTuple.max 
    } 

    func contains(_ point: SCNVector3) -> Bool { 
    let contains = 
    min.x <= point.x && 
    min.y <= point.y && 
    min.z <= point.z && 

    max.x > point.x && 
    max.y > point.y && 
    max.z > point.z 

    return contains 
    } 
}