// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Span const&, gaps::R3Triangle const&, gaps::R3Point*, double*)
RNClassID R3Intersects(const R3Span& span, const R3Triangle& triangle, 
    R3Point *hit_point1, RNScalar *hit_t1)
{
    // Find intersection of span and plane
    R3Point p;
    RNScalar t;
    switch (R3Intersects(span, triangle.Plane(), &p, &t)) {
    case RN_NULL_CLASS_ID:
	// Span does not intersect triangle plane
	return RN_NULL_CLASS_ID;
	
    case R3_POINT_CLASS_ID:
	// Span intersects triangle plane in a single point
	// Check whether triangle contains intersection point
	if (!R3Contains(triangle, p)) return RN_NULL_CLASS_ID;
	if (hit_point1) *hit_point1 = p;
	if (hit_t1) *hit_t1 = t;
	return R3_POINT_CLASS_ID;

    case R3_SPAN_CLASS_ID: 
	// Span lies in triangle plane 
	{
	    // Clip span to all edges of triangle
	    R3Span s(span);
	    const R3Point *p1 = &(triangle.Vertex(2)->Position());
	    for (int i = 0; i < 3; i++) {
		// Get kth point
		const R3Point *p2 = &(triangle.Vertex(i)->Position());
		
		// Construct edge plane (containing edge and normal to triangle)
		R3Plane edge_plane(*p2, triangle.Normal(), *p2 - *p1);
		
		// Clip span to edge plane
		RNClassID s_type = s.Clip(edge_plane);
		if (s_type == RN_NULL_CLASS_ID) {
		    return RN_NULL_CLASS_ID;
		}
		else if (s_type == R3_POINT_CLASS_ID) {
		    if (hit_point1) *hit_point1 = s.Start();
		    if (hit_t1) *hit_t1 = span.T(s.Start());
		    return R3_POINT_CLASS_ID;
		}
		
		// Remember previous point
		p1 = p2;
	    }
	    
	    // Span intersects triangle in span
	    if (hit_point1) *hit_point1 = s.Start();
	    if (hit_t1) *hit_t1 = span.T(s.Start());
	    // if (hit_point2) *hit_point2 = s.End();
	    // if (hit_t2) *hit_t2 = span.T(s.End());
	    return R3_SPAN_CLASS_ID;
        }
	break;

    default:
	RNAbort("Invalid intersection result");
	break;
    }

    // Should never get here
    RNAbort("Should never get here");
    return RN_NULL_CLASS_ID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbp
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x37b4a
leaq 0x20(%r12), %r13
leaq 0x20(%rsp), %rcx
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x3f506
movl %eax, %ebp
testl %eax, %eax
je 0x3f7fb
cmpl $0x4, %ebp
je 0x3f6f1
cmpl $0x1, %ebp
jne 0x3f76d
leaq 0x28(%rsp), %rsi
movq %r12, %rdi
callq 0x4182a
testl %eax, %eax
je 0x3f78d
testq %r15, %r15
je 0x3f6d5
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x37e36
pushq $0x1
popq %rbp
testq %r14, %r14
je 0x3f7fb
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%r14)
jmp 0x3f7fb
movq %r15, 0x18(%rsp)
movq %r14, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x36f48
movq 0x18(%r12), %rdx
pushq $0x1
popq %r14
leaq 0xa8(%rsp), %r15
cmpq $0x4, %r14
je 0x3f791
movq (%r12,%r14,8), %rbx
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x37d29
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x36892
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x374d6
movl %eax, %ebp
testl %eax, %eax
je 0x3f7fb
cmpl $0x1, %ebp
je 0x3f7c7
incq %r14
movq %rbx, %rdx
jmp 0x3f71b
leaq 0x5a833(%rip), %rdi     # 0x99fa7
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x680f0
leaq 0x5a80d(%rip), %rdi     # 0x99f91
xorl %eax, %eax
callq 0x680f0
jmp 0x3f7fb
xorl %ebp, %ebp
jmp 0x3f7fb
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x3f7a5
leaq 0x40(%rsp), %rsi
callq 0x37e36
pushq $0x4
popq %rbp
movq 0x8(%rsp), %rbx
testq %rbx, %rbx
je 0x3f7fb
leaq 0x40(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x37276
movsd %xmm0, (%rbx)
jmp 0x3f7fb
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x3f7db
leaq 0x40(%rsp), %rsi
callq 0x37e36
cmpq $0x0, 0x8(%rsp)
je 0x3f7fb
leaq 0x40(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x37276
movq 0x8(%rsp), %rax
movsd %xmm0, (%rax)
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq