// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Isect.cpp
// Function: gaps::R2Intersects(gaps::R2Ray const&, gaps::R2Box const&, gaps::R2Point*, gaps::R2Vector*, double*)
RNClassID R2Intersects(const R2Ray& ray, const R2Box& box, 
    R2Point *hit_point1, R2Vector *hit_normal1, RNScalar *hit_t1)
{
    RNClassID id = RN_NULL_CLASS_ID;
    R2Point point1;
    RNScalar tval1;

    // Check box 
    if (box.IsEmpty()) {
        // Box is empty
        return RN_NULL_CLASS_ID;
    }
    else if (R2Contains(box, ray.Start())) {
        // Box contains ray start point
	if (hit_point1) *hit_point1 = ray.Start();
	if (hit_normal1) *hit_normal1 = ray.Vector();
	if (hit_t1) *hit_t1 = 0.0;
	return R2_SPAN_CLASS_ID;
    }
    else {
	// Find parametric distance to front plane in each dimension
	for (RNDimension dim = RN_X; dim <= RN_Y; dim++) {
	    if (RNIsPositive(ray.Vector()[dim])) {
		RNScalar delta = box.Min()[dim] - ray.Start()[dim];
		if (delta < 0.0) continue;
		tval1 = delta / ray.Vector()[dim];
	    }
	    else if (RNIsNegative(ray.Vector()[dim])) {
		RNScalar delta = box.Max()[dim] - ray.Start()[dim];
		if (delta > 0.0) continue;
		tval1 = delta / ray.Vector()[dim];
	    }
	    else {
		continue;
	    }
	    
	    // Compute intersection point on plane
	    RNDimension dim1 = 1 - dim;
	    point1[dim] = (ray.Vector()[dim] > 0.0) ? box.Min()[dim] : box.Max()[dim];
	    point1[dim1] = ray.Start()[dim1] + ray.Vector()[dim1] * tval1;
	    // assert(RNIsEqual(point1[dim], ray.Start()[dim] + ray.Vector()[dim] * tval1));
	    
	    // Check if intersection point on plane is inside box
	    if ((RNIsLessOrEqual(point1[dim1], box.Max()[dim1])) &&
		(RNIsGreaterOrEqual(point1[dim1], box.Min()[dim1]))) {
		// Found first intersection point 
		id = R2_SPAN_CLASS_ID;
                if (hit_t1) *hit_t1 = tval1;
                if (hit_point1) *hit_point1 = point1;
                if (hit_normal1) {
	            *hit_normal1 = R2zero_vector;
		    if (RNIsNegative(ray.Vector()[dim])) *hit_normal1 = (*hit_normal1)[dim] = 1.0;
		    else (*hit_normal1)[dim] = -1.0;
	        }
		break;
	    }
	}

	// Return intersection 
	return id;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r14
leaq 0x18(%rsp), %rdi
callq 0x5f58a
movq %rbp, %rdi
callq 0x50606
xorl %r12d, %r12d
testl %eax, %eax
je 0x50c4b
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x8(%rsp)
leaq 0x28(%r14), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x4ff30
testl %eax, %eax
je 0x50c97
testq %r13, %r13
je 0x50c73
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5f6c4
testq %r15, %r15
je 0x50c83
movq %r15, %rdi
movq %r14, %rsi
callq 0x5fbfa
pushq $0x4
popq %r12
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x50c39
andq $0x0, (%rax)
jmp 0x50c39
movq %r15, 0x10(%rsp)
leaq 0x89f05(%rip), %rsi     # 0xdaba8
movsd (%rsi), %xmm0
movapd 0x42381(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
pushq $0x1
popq %rax
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x50c39
movsd (%r14,%r15,8), %xmm4
ucomisd %xmm0, %xmm4
ja 0x50cf5
ucomisd %xmm4, %xmm1
jbe 0x50d69
movsd 0x18(%rbp,%r15,8), %xmm3
subsd (%rbx,%r15,8), %xmm3
ucomisd %xmm2, %xmm3
jbe 0x50d08
jmp 0x50d69
movsd 0x8(%rbp,%r15,8), %xmm3
subsd (%rbx,%r15,8), %xmm3
ucomisd %xmm3, %xmm2
ja 0x50d69
movq %rax, %rcx
subq %r15, %rcx
xorl %edx, %edx
ucomisd %xmm2, %xmm4
setbe %dl
divsd %xmm4, %xmm3
shll $0x4, %edx
addq %rbp, %rdx
movsd 0x8(%rdx,%r15,8), %xmm4
movsd %xmm4, 0x18(%rsp,%r15,8)
leal (,%rcx,8), %edx
movsd (%r14,%rdx), %xmm4
mulsd %xmm3, %xmm4
addsd 0x28(%r14,%rdx), %xmm4
movsd %xmm4, 0x18(%rsp,%rdx)
movapd %xmm4, %xmm5
subsd 0x18(%rbp,%rdx), %xmm5
ucomisd %xmm5, %xmm0
jb 0x50d69
subsd 0x8(%rbp,%rcx,8), %xmm4
ucomisd %xmm1, %xmm4
jae 0x50d71
incq %r15
jmp 0x50cc0
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x50d7f
movsd %xmm3, (%rax)
testq %r13, %r13
je 0x50d97
movq %rsi, %rbx
leaq 0x18(%rsp), %rsi
movq %r13, %rdi
callq 0x5f6c4
movq %rbx, %rsi
pushq $0x4
popq %r12
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x50c39
movq %rsi, %rbx
leaq 0x49c05(%rip), %rsi     # 0x9a9b8
movq %r13, %rdi
callq 0x5fbfa
movsd (%rbx), %xmm0
xorpd 0x42269(%rip), %xmm0    # 0x93030
ucomisd (%r14,%r15,8), %xmm0
ja 0x50de3
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, (%r13,%r15,8)
jmp 0x50c39
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13,%r15,8)
leaq 0x28(%rsp), %rbx
movsd 0x42209(%rip), %xmm0    # 0x93008
movq %rbx, %rdi
callq 0x5f8d2
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5fbfa
jmp 0x50c39