// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Dist.cpp
// Function: gaps::R3Distance(gaps::R3Line const&, gaps::R3Ray const&)
RNLength R3Distance(const R3Line& line, const R3Ray& ray)
{
    // NOTE: THIS HAS NOT BEEN TESTED, BUT SHOULD WORK
  
    // Get vectors in more convenient form
    const R3Vector v1 = ray.Vector();
    const R3Vector v2 = line.Vector();

    // Compute useful intermediate values
    const RNScalar v1v1 = 1.0;  // v1.Dot(v1);
    const RNScalar v2v2 = 1.0;  // v2.Dot(v2);
    RNScalar v1v2 = v1.Dot(v2);
    RNScalar denom = v1v2*v1v2 - v1v1*v2v2;

    // Check if parallel
    if (RNIsZero(denom)) {
        // Return distance to any point on ray
        return R3Distance(line, ray.Start());
    }
    else {
	// Find point on ray closest to line
	const R3Vector p1 = ray.Start().Vector();
	const R3Vector p2 = line.Point().Vector();
	RNScalar p1v1 = v1.Dot(p1);
	RNScalar p2v2 = v2.Dot(p2);
	RNScalar p1v2 = v2.Dot(p1);
	RNScalar p2v1 = v1.Dot(p2);
	RNScalar t1 = (v1v2*p2v2 + v2v2*p1v1 - v1v2*p1v2 - v2v2*p2v1) / denom;
        if (RNIsNegativeOrZero(t1)) {
            // Closest point is behind start of ray, return distance to start
            return R3Distance(ray.Start(), line); 
        }
        else {
            // Return distance to closest point
            RNScalar t2 = (v1v2*p1v1 + v1v1*p2v2 - v1v2*p2v1 - v1v1*p1v2) / denom;
            R3Point point1 = ray.Start() + t1*ray.Vector();
            R3Point point2 = line.Point() + t2*line.Vector();
            return R3Distance(point1, point2);
        }
    }
    
    // Commented out because compiler complains :(
    // RNAbort("Should not get here");
    // return 0.0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rsi), %r12
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3cfd6
leaq 0x18(%rbx), %r15
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3cfd6
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3d252
movapd %xmm0, %xmm4
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
addsd 0x584d5(%rip), %xmm3    # 0xa2048
leaq 0x9c01e(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x5849a(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm3
jb 0x49b9a
ucomisd %xmm3, %xmm0
jae 0x49d2a
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
movapd %xmm3, (%rsp)
movapd %xmm4, 0x40(%rsp)
callq 0x3cfc6
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
leaq 0xc8(%rsp), %rbp
movq %rbp, %rdi
callq 0x3cfc6
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x3d252
movsd %xmm0, 0x38(%rsp)
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3d252
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3d252
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3d252
movsd 0x28(%rsp), %xmm7
movsd 0x30(%rsp), %xmm6
movsd 0x38(%rsp), %xmm5
movapd 0x40(%rsp), %xmm4
movapd (%rsp), %xmm3
movapd %xmm0, %xmm1
movapd %xmm4, %xmm0
mulsd %xmm6, %xmm0
addsd %xmm5, %xmm0
movapd %xmm4, %xmm2
mulsd %xmm7, %xmm2
subsd %xmm2, %xmm0
subsd %xmm1, %xmm0
divsd %xmm3, %xmm0
leaq 0x9bf17(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm2
ucomisd %xmm0, %xmm2
jae 0x49d2a
movapd 0x58389(%rip), %xmm2    # 0xa2020
xorpd %xmm4, %xmm2
mulsd %xmm4, %xmm5
addsd %xmm6, %xmm5
mulsd %xmm1, %xmm2
addsd %xmm5, %xmm2
subsd %xmm7, %xmm2
divsd %xmm3, %xmm2
movapd %xmm2, (%rsp)
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x3d962
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3c877
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movapd (%rsp), %xmm0
callq 0x3d962
leaq 0x98(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3c877
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3c78e
movq %rbx, %rdi
callq 0x3d0cc
jmp 0x49d35
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4942a
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq