Exemple #1
0
def intersectSphere(origin, ray, sphere):

    center = np.array(sphere["center"])
    radius = float(sphere["radius"])

    closestIntersect = Intersection.worstCase()

    a = np.dot(ray, ray)
    b = np.dot(2*ray, (origin - center))
    c = np.dot((origin - center), (origin - center)) - (radius * radius)

    delta = b*b - 4*a*c
    if(delta > 0):

        t1 = (-b - math.sqrt(delta)) / (2*a)
        t2 = (-b + math.sqrt(delta)) / (2*a)
        ti = min(t1, t2)

        if(ti > 0):

            pi = origin + ray*ti
            n = (pi - center)
            n /= np.linalg.norm(n)

            return Intersection(ti, n, pi, sphere)

    return None
Exemple #2
0
def intersectCube(origin, ray, cube):

    minBound = np.array(cube["min"])
    maxBound = np.array(cube["max"])

    closestIntersect = Intersection.worstCase()

    # Front side
    p1 = np.array([minBound[0], minBound[1], minBound[2]])
    p2 = np.array([maxBound[0], minBound[1], minBound[2]])
    p3 = np.array([maxBound[0], maxBound[1], minBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    p1 = np.array([minBound[0], minBound[1], minBound[2]])
    p2 = np.array([maxBound[0], maxBound[1], minBound[2]])
    p3 = np.array([minBound[0], maxBound[1], minBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    # Back side
    p1 = np.array([minBound[0], minBound[1], maxBound[2]])
    p2 = np.array([minBound[0], maxBound[1], maxBound[2]])
    p3 = np.array([maxBound[0], maxBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    p1 = np.array([minBound[0], minBound[1], maxBound[2]])
    p2 = np.array([minBound[0], maxBound[1], maxBound[2]])
    p3 = np.array([maxBound[0], maxBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    # Right side
    p1 = np.array([maxBound[0], minBound[1], minBound[2]])
    p2 = np.array([maxBound[0], maxBound[1], minBound[2]])
    p3 = np.array([maxBound[0], maxBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    p1 = np.array([maxBound[0], minBound[1], minBound[2]])
    p2 = np.array([maxBound[0], maxBound[1], maxBound[2]])
    p3 = np.array([maxBound[0], minBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    # Left side
    p1 = np.array([minBound[0], minBound[1], minBound[2]])
    p2 = np.array([minBound[0], maxBound[1], minBound[2]])
    p3 = np.array([minBound[0], maxBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    p1 = np.array([minBound[0], minBound[1], minBound[2]])
    p2 = np.array([minBound[0], maxBound[1], maxBound[2]])
    p3 = np.array([minBound[0], maxBound[1], minBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection
    
    # Up side
    p1 = np.array([minBound[0], maxBound[1], minBound[2]])
    p2 = np.array([maxBound[0], maxBound[1], maxBound[2]])
    p3 = np.array([maxBound[0], maxBound[1], minBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    p1 = np.array([minBound[0], maxBound[1], minBound[2]])
    p2 = np.array([minBound[0], maxBound[1], maxBound[2]])
    p3 = np.array([maxBound[0], maxBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    # Down side
    p1 = np.array([minBound[0], minBound[1], minBound[2]])
    p2 = np.array([maxBound[0], minBound[1], minBound[2]])
    p3 = np.array([maxBound[0], minBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    p1 = np.array([minBound[0], minBound[1], minBound[2]])
    p2 = np.array([maxBound[0], minBound[1], maxBound[2]])
    p3 = np.array([minBound[0], minBound[1], maxBound[2]])
    intersection = intersectTriangle(p1, p2, p3, origin, ray)
    if(intersection != None and intersection < closestIntersect):
        intersection.obj = cube
        closestIntersect = intersection

    if(closestIntersect.distance < 1000):
        return closestIntersect

    return None