// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Line const&, gaps::R3Box const&, gaps::R3Point*)
RNClassID R3Intersects(const R3Line& line, const R3Box& box, 
    R3Point *hit_point)
{
    RNClassID id = RN_NULL_CLASS_ID;
    R3Point point1;
    RNScalar tval1;

    // Check if box contains point on line
    if (R3Contains(box, line.Point())) {
	if (hit_point) *hit_point = line.Point();
	return R3_SPAN_CLASS_ID;
    }
    else {
	// Find parametric distance to front plane in each dimension
	for (RNDimension dim = RN_X; dim <= RN_Z; dim++) {
	    if (RNIsPositive(line.Vector()[dim])) {
		RNScalar delta = box.Min()[dim] - line.Point()[dim];
		tval1 = delta / line.Vector()[dim];
	    }
	    else if (RNIsNegative(line.Vector()[dim])) {
		RNScalar delta = box.Max()[dim] - line.Point()[dim];
		tval1 = delta / line.Vector()[dim];
	    }
	    else {
		continue;
	    }
	    
	    // Compute intersection point on plane
	    RNDimension dim1 = (dim + 1) % 3;
	    RNDimension dim2 = (dim + 2) % 3;
	    point1[dim] = (line.Vector()[dim] > 0.0) ? box.Min()[dim] : box.Max()[dim];
	    assert(RNIsEqual(point1[dim], line.Point()[dim] + line.Vector()[dim] * tval1));
	    point1[dim1] = line.Point()[dim1] + line.Vector()[dim1] * tval1;
	    point1[dim2] = line.Point()[dim2] + line.Vector()[dim2] * tval1;
	    
	    // Check if intersection point on plane is inside box
	    if ((RNIsLessOrEqual(point1[dim1], box.Max()[dim1])) &&
		(RNIsGreaterOrEqual(point1[dim1], box.Min()[dim1])) &&
		(RNIsLessOrEqual(point1[dim2], box.Max()[dim2])) &&
		(RNIsGreaterOrEqual(point1[dim2], box.Min()[dim2]))) {
		// Found first intersection point 
		id = R3_SPAN_CLASS_ID;
		break;
	    }
	}

	// Fill in results
	if (id != RN_NULL_CLASS_ID) {
	    if (hit_point) *hit_point = point1;
	}
	
	// Return intersection 
	return id;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r12
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x3ca24
movq %r13, %rdi
movq %r12, %rsi
callq 0x47df8
testl %eax, %eax
je 0x42f1c
movl $0x4, %r15d
movq %r12, %r14
testq %rbx, %rbx
je 0x42f0a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3cd94
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x10(%rsp)
leaq 0xa2c70(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x5f0ec(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
leaq 0x28(%r12), %rbx
leaq 0x30(%rsp), %r15
leaq 0x18(%r13), %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movl $0x1, %r9d
movl $0x2, %ecx
movabsq $-0x5555555555555555, %r10 # imm = 0xAAAAAAAAAAAAAAAB
leaq 0x20(%r13), %r11
leaq 0x8(%r13), %rbp
xorpd %xmm2, %xmm2
movq %rcx, %rax
mulq %r10
movsd 0x10(%r12,%r9,8), %xmm4
movl $0x8, %r8d
ucomisd %xmm0, %xmm4
ja 0x42f9e
movl $0x20, %r8d
ucomisd %xmm4, %xmm1
jbe 0x4306d
shlq $0x2, %rdx
andq $-0x8, %rdx
leaq (%rdx,%rdx,2), %rax
movq %rbx, %rsi
subq %rax, %rsi
movq %r15, %rdi
subq %rax, %rdi
addq %r13, %r8
movsd -0x8(%r8,%r9,8), %xmm3
subsd -0x8(%r12,%r9,8), %xmm3
ucomisd %xmm2, %xmm4
movq %r11, %rdx
cmovaq %rbp, %rdx
divsd %xmm4, %xmm3
cmpq $0x3, %r9
movsd -0x8(%rdx,%r9,8), %xmm4
movsd %xmm4, 0x18(%rsp,%r9,8)
movl $0x0, %edx
cmovneq %r9, %rdx
movsd 0x18(%r12,%rdx,8), %xmm4
mulsd %xmm3, %xmm4
addsd (%r12,%rdx,8), %xmm4
movsd %xmm4, 0x20(%rsp,%rdx,8)
mulsd -0x8(%rsi,%r9,8), %xmm3
addsd -0x20(%rsi,%r9,8), %xmm3
movsd %xmm3, -0x8(%rdi,%r9,8)
movsd 0x20(%rsp,%rdx,8), %xmm4
movapd %xmm4, %xmm5
subsd 0x20(%r13,%rdx,8), %xmm5
ucomisd %xmm5, %xmm0
jb 0x43067
subsd 0x8(%r13,%rdx,8), %xmm4
ucomisd %xmm1, %xmm4
jb 0x43067
movq 0x18(%rsp), %rdx
subq %rax, %rdx
movapd %xmm3, %xmm4
subsd 0x10(%rdx,%r9,8), %xmm4
ucomisd %xmm4, %xmm0
jb 0x43067
subsd -0x8(%rdx,%r9,8), %xmm3
ucomisd %xmm1, %xmm3
jae 0x4307f
movb $0x1, %al
testb %al, %al
je 0x4308d
incq %r9
incq %rcx
cmpq $0x4, %r9
jne 0x42f75
jmp 0x4308d
movl $0x4, %eax
movq %rax, 0x8(%rsp)
xorl %eax, %eax
jmp 0x43069
movq 0x8(%rsp), %r15
testl %r15d, %r15d
sete %al
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
sete %cl
orb %al, %cl
je 0x42eff
jmp 0x42f0a