// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
leaq 0x38(%rsp), %rdi
callq 0x68e2c
movsd 0x8(%r13), %xmm0
xorl %eax, %eax
ucomisd 0x18(%r13), %xmm0
ja 0x593c9
movsd 0x10(%r13), %xmm0
ucomisd 0x20(%r13), %xmm0
ja 0x593c9
leaq 0x28(%r15), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x58377
testl %eax, %eax
je 0x591ca
testq %r14, %r14
je 0x59199
movq %r14, %rdi
movq %rbp, %rsi
callq 0x68f66
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x591ab
movq %r15, %rsi
callq 0x6950a
movl $0x4, %eax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x593c9
movq $0x0, (%rcx)
jmp 0x593c9
xorl %ebx, %ebx
movl $0x20, %r12d
leaq 0x8c9bf(%rip), %rcx     # 0xe5b98
movapd 0x48e3f(%rip), %xmm4    # 0xa2020
xorpd %xmm5, %xmm5
xorl %eax, %eax
movq %r14, 0x8(%rsp)
movsd (%r15,%rbx), %xmm1
movsd (%rcx), %xmm0
ucomisd %xmm0, %xmm1
ja 0x59244
movapd %xmm0, %xmm2
xorpd %xmm4, %xmm2
ucomisd %xmm1, %xmm2
jbe 0x593b7
movsd 0x18(%r13,%rbx), %xmm2
subsd 0x28(%r15,%rbx), %xmm2
ucomisd %xmm5, %xmm2
xorpd %xmm3, %xmm3
cmpnltsd %xmm2, %xmm3
divsd %xmm1, %xmm2
andpd %xmm3, %xmm2
andnpd %xmm6, %xmm3
orpd %xmm2, %xmm3
movapd %xmm3, %xmm6
jbe 0x59279
jmp 0x593b7
movsd 0x8(%r13,%rbx), %xmm2
subsd 0x28(%r15,%rbx), %xmm2
ucomisd %xmm2, %xmm5
movapd %xmm2, %xmm3
divsd %xmm1, %xmm3
cmpnltsd %xmm5, %xmm2
andpd %xmm2, %xmm3
andnpd %xmm6, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, %xmm6
ja 0x593b7
xorl %edx, %edx
ucomisd %xmm5, %xmm1
setbe %dl
shll $0x4, %edx
addq %r13, %rdx
movsd 0x8(%rbx,%rdx), %xmm1
movsd %xmm1, 0x38(%rsp,%rbx)
movsd -0x18(%r15,%r12), %xmm1
mulsd %xmm6, %xmm1
addsd 0x10(%r15,%r12), %xmm1
movsd %xmm1, 0x20(%rsp,%r12)
movapd %xmm1, %xmm2
subsd (%r13,%r12), %xmm2
movb $0x1, %dl
ucomisd %xmm2, %xmm0
jb 0x593b3
subsd -0x10(%r13,%r12), %xmm1
xorpd %xmm4, %xmm0
ucomisd %xmm0, %xmm1
jb 0x593b3
movq %rcx, %rbp
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x592ea
movsd %xmm6, (%rax)
testq %r14, %r14
movapd %xmm6, 0x20(%rsp)
je 0x59314
movq %r14, %rdi
leaq 0x38(%rsp), %rsi
callq 0x68f66
movapd 0x20(%rsp), %xmm6
xorpd %xmm5, %xmm5
movapd 0x48d0c(%rip), %xmm4    # 0xa2020
movl $0x4, %eax
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5935e
leaq 0x506ee(%rip), %rsi     # 0xa9a18
movq %rdi, %r14
callq 0x6950a
movapd 0x48ce6(%rip), %xmm4    # 0xa2020
movsd (%rbp), %xmm0
xorpd %xmm4, %xmm0
ucomisd (%r15,%rbx), %xmm0
ja 0x59365
movq %rbp, %rcx
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, (%r14,%rbx)
jmp 0x5939d
xorl %edx, %edx
movq %rbp, %rcx
jmp 0x593b3
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r14,%rbx)
leaq 0x48(%rsp), %rdi
movsd 0x48cb8(%rip), %xmm0    # 0xa2038
callq 0x69178
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
callq 0x6950a
movapd 0x48c86(%rip), %xmm4    # 0xa2020
movq %rbp, %rcx
xorl %edx, %edx
movq 0x8(%rsp), %r14
xorpd %xmm5, %xmm5
movl $0x4, %eax
movapd 0x20(%rsp), %xmm6
testb %dl, %dl
je 0x593c9
addq $0x8, %rbx
addq $-0x8, %r12
cmpq $0x8, %rbx
je 0x591ec
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq