intersection.py (1846B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\ballparkCommon\intersection.py 2 import geo2 3 import math 4 5 def GetClosestIntersection(origin, direction, spheres): 6 direction = geo2.Vec3NormalizeD(direction) 7 closest = -1.0 8 intersection = None 9 theID = None 10 for sphereID, center, radius in spheres: 11 delta = geo2.Vec3SubtractD(origin, center) 12 b = geo2.Vec3DotD(geo2.Vec3ScaleD(direction, 2.0), delta) 13 if b > 0: 14 continue 15 c = geo2.Vec3LengthSqD(delta) - radius ** 2 16 det = b * b - 4.0 * c 17 if det < 0.0: 18 continue 19 det = math.sqrt(det) 20 t0 = (-b - det) * 0.5 21 t1 = (-b + det) * 0.5 22 if t0 < t1: 23 t = t0 24 else: 25 t = t1 26 p = geo2.Vec3AddD(origin, geo2.Vec3ScaleD(direction, t)) 27 d2 = geo2.Vec3DistanceSqD(origin, p) 28 if closest < 0.0: 29 closest = d2 30 theID = sphereID 31 intersection = p 32 elif d2 < closest: 33 closest = d2 34 intersection = p 35 theID = sphereID 36 37 return (theID, intersection) 38 39 40 def GetClosestIntersectionForRay(ballpark, origin, direction, maxDistance = None): 41 spheres = [ (eachBall.id, (eachBall.x, eachBall.y, eachBall.z), eachBall.radius) for eachBall in ballpark.balls.values() if eachBall.isGlobal ] 42 if maxDistance: 43 spheres = FilterOutVectorsOutsideRange(spheres, origin, maxDistance) 44 return GetClosestIntersection(origin, direction, spheres) 45 46 47 def FilterOutVectorsOutsideRange(spheres, origin, maxDistance): 48 maxDistanceSquared = maxDistance ** 2 49 return [ (sphereID, sphereCenterVector, sphereRadius) for sphereID, sphereCenterVector, sphereRadius in spheres if geo2.Vec3DistanceSqD(sphereCenterVector, origin) <= maxDistanceSquared ]