// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Ray const&, gaps::R3Box const&, gaps::R3Point*, gaps::R3Vector*, double*)
RNClassID R3Intersects(const R3Ray& ray, const R3Box& box, 
    R3Point *hit_point1, R3Vector *hit_normal1, RNScalar *hit_t1)
{
    // Check box 
    if (box.IsEmpty()) {
        // Box is empty
        return RN_NULL_CLASS_ID;
    }
    else {
        R3Point point1;
        RNScalar tval1;

        // Check if ray start is inside box
        RNBoolean start_inside = R3Contains(box, ray.Start());
        if (start_inside) {
	  if (hit_point1) *hit_point1 = ray.Start();
	  if (hit_normal1) *hit_normal1 = -ray.Vector();
  	  if (hit_t1) *hit_t1 = 0;
          return R3_SPAN_CLASS_ID;
        }

	// Find parametric distance to front plane in each dimension
	for (RNDimension dim = RN_X; dim <= RN_Z; dim++) {
	    if (RNIsPositive(ray.Vector()[dim])) {
                RNCoord box_coord = (start_inside) ? box.Max()[dim] : box.Min()[dim];
		RNScalar delta = box_coord - ray.Start()[dim];
		if (delta < 0.0) continue;
		tval1 = delta / ray.Vector()[dim];
	    }
	    else if (RNIsNegative(ray.Vector()[dim])) {
                RNCoord box_coord = (start_inside) ? box.Min()[dim] : box.Max()[dim];
		RNScalar delta = box_coord - ray.Start()[dim];
		if (delta > 0.0) continue;
		tval1 = delta / ray.Vector()[dim];
	    }
	    else {
		continue;
	    }
	    
	    // Compute intersection point on plane
	    RNDimension dim1 = (dim + 1) % 3;
	    RNDimension dim2 = (dim + 2) % 3;
	    point1[dim] = ray.Start()[dim] + ray.Vector()[dim] * tval1;
	    point1[dim1] = ray.Start()[dim1] + ray.Vector()[dim1] * tval1;
	    point1[dim2] = ray.Start()[dim2] + ray.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 
  	        if (hit_t1) *hit_t1 = tval1;
	        if (hit_point1) *hit_point1 = point1;
	        if (hit_normal1) {
	    	    if (RNIsNegative(ray.Vector()[dim])) *hit_normal1 = R3xyz_triad.Axis(dim);
		    else *hit_normal1 = -(R3xyz_triad.Axis(dim));
	        }
		return R3_SPAN_CLASS_ID;
	    }
	}

	// Return no intersection 
	return RN_NULL_CLASS_ID;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r8, %rbp
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r14
movq %rsi, %rdi
callq 0x257ce
xorl %r12d, %r12d
testl %eax, %eax
je 0x3dff3
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%rsp), %rdi
callq 0x37b4a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x419e0
testl %eax, %eax
je 0x3e050
testq %r13, %r13
je 0x3e01c
movq %r13, %rdi
movq %r14, %rsi
callq 0x37e36
testq %r15, %r15
je 0x3e040
addq $0x18, %r14
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x38c72
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3887c
pushq $0x4
popq %r12
testq %rbp, %rbp
je 0x3dfe1
andq $0x0, (%rbp)
jmp 0x3dfe1
movq %rbp, %rsi
movq %r15, (%rsp)
leaq 0x9cb4a(%rip), %rdi     # 0xdaba8
movsd (%rdi), %xmm0
movapd 0x54fc6(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
leaq 0x9d153(%rip), %rbp     # 0xdb1c8
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
pushq $0x2
popq %rax
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x3dfe1
movsd 0x18(%r14,%r15,8), %xmm4
ucomisd %xmm0, %xmm4
ja 0x3e0bf
ucomisd %xmm4, %xmm1
jbe 0x3e175
movsd 0x20(%rbx,%r15,8), %xmm3
movsd (%r14,%r15,8), %xmm5
subsd %xmm5, %xmm3
ucomisd %xmm2, %xmm3
jbe 0x3e0da
jmp 0x3e175
movsd 0x8(%rbx,%r15,8), %xmm3
movsd (%r14,%r15,8), %xmm5
subsd %xmm5, %xmm3
ucomisd %xmm3, %xmm2
ja 0x3e175
divsd %xmm4, %xmm3
leaq 0x1(%r15), %rcx
cmpq $0x2, %r15
movl $0x0, %edx
cmovneq %rcx, %rdx
movq %r15, %rcx
mulsd %xmm3, %xmm4
addsd %xmm5, %xmm4
movsd %xmm4, 0x8(%rsp,%r15,8)
movsd 0x18(%r14,%rdx,8), %xmm4
mulsd %xmm3, %xmm4
addsd (%r14,%rdx,8), %xmm4
subq $0x1, %rcx
movsd %xmm4, 0x8(%rsp,%rdx,8)
cmovbq %rax, %rcx
movsd 0x18(%r14,%rcx,8), %xmm4
mulsd %xmm3, %xmm4
addsd (%r14,%rcx,8), %xmm4
movsd %xmm4, 0x8(%rsp,%rcx,8)
movsd 0x8(%rsp,%rdx,8), %xmm5
movapd %xmm5, %xmm6
subsd 0x20(%rbx,%rdx,8), %xmm6
ucomisd %xmm6, %xmm0
jb 0x3e175
subsd 0x8(%rbx,%rdx,8), %xmm5
ucomisd %xmm1, %xmm5
jb 0x3e175
movapd %xmm4, %xmm5
subsd 0x20(%rbx,%rcx,8), %xmm5
ucomisd %xmm5, %xmm0
jb 0x3e175
subsd 0x8(%rbx,%rcx,8), %xmm4
ucomisd %xmm1, %xmm4
jae 0x3e181
incq %r15
addq $0x18, %rbp
jmp 0x3e082
movq %rdi, %rbx
testq %rsi, %rsi
je 0x3e18d
movsd %xmm3, (%rsi)
testq %r13, %r13
je 0x3e19f
leaq 0x8(%rsp), %rsi
movq %r13, %rdi
callq 0x37e36
pushq $0x4
popq %r12
cmpq $0x0, (%rsp)
je 0x3dfe1
movsd (%rbx), %xmm0
xorpd 0x54e76(%rip), %xmm0    # 0x93030
ucomisd 0x18(%r14,%r15,8), %xmm0
ja 0x3e1dc
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x38c72
movq (%rsp), %rdi
movq %rbx, %rsi
jmp 0x3e1e3
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x3887c
jmp 0x3dfe1