2017-09-05 51 views
0

給定SCNNode,如何確定點(在節點的座標系中指定)是否包含在該節點的幾何內?SCNNode點內部測試

或者,如果更簡單,我該如何測試一個點是否包含在節點的邊界框內?

回答

0

如果沒有比這更好的答案,我會感到驚訝,但我沒有找到一個,所以這就是我所得到的。

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 
    } 
}