// File: /workspace/github_cpp_makefile_star2_O2/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 0x3831c
leaq 0x18(%rbx), %r15
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3831c
movq %r13, %rdi
movq %rbp, %rsi
callq 0x38586
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x4f72c(%rip), %xmm2    # 0x93020
leaq 0x972ad(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm3
movapd 0x4f729(%rip), %xmm1    # 0x93030
xorpd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x4391b
ucomisd %xmm2, %xmm3
jae 0x43a82
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movapd %xmm2, (%rsp)
movapd %xmm0, 0x40(%rsp)
callq 0x212f8
leaq 0xc8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x212f8
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x38586
movsd %xmm0, 0x38(%rsp)
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38586
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x38586
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x38586
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 0x971bf(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm2
ucomisd %xmm0, %xmm2
jae 0x43a82
movapd 0x4f631(%rip), %xmm2    # 0x93030
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 0x38a02
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x37e13
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movapd (%rsp), %xmm0
callq 0x38a02
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x37e13
movq %r12, %rdi
movq %r15, %rsi
callq 0x4321a
jmp 0x43a8d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x43240
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq