// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movq %rcx, %r12
movq %rdx, 0x10(%rsp)
movq %rsi, %r13
movq %rdi, %r14
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x680a4
movsd 0x8(%r13), %xmm0
xorl %r15d, %r15d
ucomisd 0x18(%r13), %xmm0
ja 0x58601
movq %rbp, (%rsp)
movq %r12, 0x8(%rsp)
movsd 0x10(%r13), %xmm0
ucomisd 0x20(%r13), %xmm0
ja 0x58601
leaq 0x28(%r14), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x576c8
testl %eax, %eax
je 0x58527
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x584f6
movq %rbp, %rsi
callq 0x681de
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x58508
movq %r14, %rsi
callq 0x68720
movl $0x4, %r15d
movq (%rsp), %rax
testq %rax, %rax
je 0x58601
movq $0x0, (%rax)
jmp 0x58601
leaq 0x20(%rsp), %rax
leaq 0x8d665(%rip), %rdi     # 0xe5b98
movsd (%rdi), %xmm0
movapd 0x49ae1(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
movb $0x1, %cl
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
movsd (%r14,%r12,8), %xmm4
ucomisd %xmm0, %xmm4
ja 0x5857b
ucomisd %xmm4, %xmm1
jbe 0x585ea
movsd 0x18(%r13,%r12,8), %xmm3
subsd (%rbp,%r12,8), %xmm3
ucomisd %xmm2, %xmm3
jbe 0x5858f
jmp 0x585ea
movsd 0x8(%r13,%r12,8), %xmm3
subsd (%rbp,%r12,8), %xmm3
ucomisd %xmm3, %xmm2
ja 0x585ea
divsd %xmm4, %xmm3
movq %r12, %rdx
xorq $0x1, %rdx
xorl %esi, %esi
ucomisd %xmm2, %xmm4
setbe %sil
shll $0x4, %esi
addq %r13, %rsi
movsd 0x8(%rsi,%r12,8), %xmm4
movsd %xmm4, (%rbx)
movsd (%r14,%rdx,8), %xmm4
mulsd %xmm3, %xmm4
addsd 0x28(%r14,%rdx,8), %xmm4
movsd %xmm4, 0x18(%rsp,%rdx,8)
movapd %xmm4, %xmm5
subsd 0x18(%r13,%rdx,8), %xmm5
ucomisd %xmm5, %xmm0
jb 0x585ea
subsd 0x8(%r13,%rdx,8), %xmm4
ucomisd %xmm1, %xmm4
jae 0x58613
movl $0x1, %r12d
movq %rax, %rbx
testb $0x1, %cl
movl $0x0, %ecx
jne 0x5854f
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
movq (%rsp), %rax
testq %rax, %rax
movq 0x8(%rsp), %r13
je 0x58628
movsd %xmm3, (%rax)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5863c
leaq 0x18(%rsp), %rsi
callq 0x681de
movl $0x4, %r15d
testq %r13, %r13
je 0x58601
leaq 0x513e2(%rip), %rsi     # 0xa9a30
movq %r13, %rdi
callq 0x68720
movsd (%rbx), %xmm0
xorpd 0x499be(%rip), %xmm0    # 0xa2020
ucomisd (%r14,%r12,8), %xmm0
ja 0x5867b
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, (%r13,%r12,8)
jmp 0x58601
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r13,%r12,8)
leaq 0x28(%rsp), %rbx
movsd 0x499a1(%rip), %xmm0    # 0xa2038
movq %rbx, %rdi
callq 0x683f0
movq %r13, %rdi
movq %rbx, %rsi
callq 0x68720
jmp 0x58601