// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Span.cpp
// Function: gaps::R3Span::Clip(gaps::R3Plane const&)
RNClassID R3Span::
Clip(const R3Plane& plane)
{
    // Characterize endpoints with respect to plane
    RNScalar d1 = R3SignedDistance(plane, Start());
    RNScalar d2 = R3SignedDistance(plane, End());

    // Clip span to plane
    if (RNIsNegative(d1)) {
	if (RNIsNegative(d2)) {
	    // Both points are below plane ???
	    Reset(Start(), Start());
	    return RN_NULL_CLASS_ID;
	}
	else if (RNIsPositive(d2)) {
	    // Start is below, end is above -- move start to plane
	    Reset((Start() * d2 + End() * -d1) / (d2 - d1), End());
	    return R3_SPAN_CLASS_ID;
	}
	else {
	    // Start is below, end is on -- move start to end
	    Reset(End(), End());
	    return R3_POINT_CLASS_ID;
	}
    }
    else if (RNIsPositive(d1)) {
	if (RNIsNegative(d2)) {
	    // Start is above, end is below -- move end to plane
	    Reset(Start(), (Start() * -d2 + End() * d1) / (d1 - d2));
	    return R3_SPAN_CLASS_ID;
	}
	else {
	    // Start is above, end is on or above
	    return R3_SPAN_CLASS_ID;
	}
    }
    else {
	if (RNIsNegative(d2)) {
	    // Start is on, end is below -- move end to start
	    Reset(Start(), Start());
	    return R3_POINT_CLASS_ID;
	}
	else if (RNIsPositive(d2)){
	    // Start is on, End is above
	    return R3_SPAN_CLASS_ID;
	}
	else {
	    // Start is on, end is on
	    return R3_POINT_CLASS_ID;
	}
    }
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movq 0x118(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x28(%rsp)
callq 0x39250
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x86f90
movq 0x30(%rsp), %rdi
movsd %xmm0, 0x108(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x38(%rsp)
callq 0x39260
movq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x86f90
movsd %xmm0, 0x100(%rsp)
movsd 0x108(%rsp), %xmm0
leaq 0x10a727(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x6df51
movsd 0x100(%rsp), %xmm0
leaq 0x10a705(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x6de24
movq 0x30(%rsp), %rdi
callq 0x39250
movq 0x30(%rsp), %rdi
movq %rax, 0x20(%rsp)
callq 0x39250
movq 0x30(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rax, %rdx
callq 0x6dcb0
movl $0x0, 0x124(%rsp)
jmp 0x6e0f6
movsd 0x100(%rsp), %xmm0
leaq 0x10a6ac(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x6df16
movq 0x30(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
movsd 0x100(%rsp), %xmm0
leaq 0xb8(%rsp), %rdi
callq 0x6fb80
movq 0x30(%rsp), %rdi
callq 0x39260
movq %rax, %rsi
movsd 0x108(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0xa0(%rsp), %rdi
callq 0x6fb80
leaq 0xd0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x6fa40
movsd 0x100(%rsp), %xmm0
subsd 0x108(%rsp), %xmm0
leaq 0xe8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x6fc00
movq 0x30(%rsp), %rdi
callq 0x39260
movq 0x30(%rsp), %rdi
movq %rax, %rdx
leaq 0xe8(%rsp), %rsi
callq 0x6dcb0
movl $0x4, 0x124(%rsp)
jmp 0x6e0f6
movq 0x30(%rsp), %rdi
callq 0x39260
movq 0x30(%rsp), %rdi
movq %rax, 0x18(%rsp)
callq 0x39260
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
callq 0x6dcb0
movl $0x1, 0x124(%rsp)
jmp 0x6e0f6
movsd 0x108(%rsp), %xmm0
leaq 0x10a57f(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x6e06a
movsd 0x100(%rsp), %xmm0
leaq 0x10a55d(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x6e05a
movq 0x30(%rsp), %rdi
callq 0x39250
movq 0x30(%rsp), %rdi
movq %rax, 0x10(%rsp)
callq 0x39250
movq %rax, %rsi
movsd 0x100(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
leaq 0x58(%rsp), %rdi
callq 0x6fb80
movq 0x30(%rsp), %rdi
callq 0x39260
movq %rax, %rsi
movsd 0x108(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
callq 0x6fb80
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x6fa40
movsd 0x108(%rsp), %xmm0
subsd 0x100(%rsp), %xmm0
leaq 0x88(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x6fc00
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x6dcb0
movl $0x4, 0x124(%rsp)
jmp 0x6e0f6
movl $0x4, 0x124(%rsp)
jmp 0x6e0f6
movsd 0x100(%rsp), %xmm0
leaq 0x10a466(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x6e0c0
movq 0x30(%rsp), %rdi
callq 0x39250
movq 0x30(%rsp), %rdi
movq %rax, 0x8(%rsp)
callq 0x39250
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x6dcb0
movl $0x1, 0x124(%rsp)
jmp 0x6e0f6
movsd 0x100(%rsp), %xmm0
leaq 0x10a410(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x6e0eb
movl $0x4, 0x124(%rsp)
jmp 0x6e0f6
movl $0x1, 0x124(%rsp)
movl 0x124(%rsp), %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopw %cs:(%rax,%rax)