// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Plane const&, gaps::R3Plane const&, gaps::R3Line*)
RNClassID R3Intersects(const R3Plane& plane1, const R3Plane& plane2, 
    R3Line *result)
{
    // Check if planes are parallel
    if (R3Parallel(plane1, plane2)) {
	// Planes are parallel - check if planes are equal
	if (RNIsEqual(plane1.D(), plane2.D())) {
	    // Planes are coincident
	    return R3_PLANE_CLASS_ID;
	}
	else {
	    // Planes are parallel, but not coincident
	    return RN_NULL_CLASS_ID;
	}
    }
    else {
	// Planes are not parallel -- they must intersect
	if (result) {
	    // Compute direction vector of intersection line
	    R3Vector v = plane1.Normal();
	    v.Cross(plane2.Normal());

	    // Find v's largest dimension
	    RNDimension dim = v.MaxDimension();
	    RNDimension dim1 = (dim+1) % 3;
	    RNDimension dim2 = (dim+2) % 3;
	    assert(RNIsNotZero(v[dim]));

	    // Solve for point on zero plane of v's largest dimension
	    R3Point p;
	    p[dim] = 0.0;
	    p[dim1] = (plane1[dim2] * plane2.D() - plane2[dim2] * plane1.D()) / v[dim];
	    p[dim2] = (plane2[dim1] * plane1.D() - plane1[dim1] * plane2.D()) / v[dim];
	    *result = R3Line(p, v);
	}

	// Planes intersect in a line
	return R3_LINE_CLASS_ID;
    }
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq %rdx, 0xc8(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x8bcd0
cmpl $0x0, %eax
je 0x7f207
movq 0xd8(%rsp), %rdi
callq 0x4e330
movsd %xmm0, 0x50(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x4e330
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
leaq 0xf9307(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x37170
cmpl $0x0, %eax
je 0x7f1f7
movl $0x5, 0xe4(%rsp)
jmp 0x7f49d
movl $0x0, 0xe4(%rsp)
jmp 0x7f49d
cmpq $0x0, 0xc8(%rsp)
je 0x7f492
movq 0xd8(%rsp), %rdi
callq 0x13e60
movq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x6fcf0
movq 0xd0(%rsp), %rdi
callq 0x13e60
movq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x70500
leaq 0xb0(%rsp), %rdi
callq 0x6ffd0
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0xa8(%rsp)
movl 0xac(%rsp), %eax
addl $0x2, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, 0xa4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x6eb40
movl 0xac(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36be0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0xd8(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x81f40
movsd %xmm0, 0x10(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x4e330
movsd %xmm0, 0x8(%rsp)
movq 0xd0(%rsp), %rdi
movl 0xa4(%rsp), %esi
callq 0x81f40
movsd %xmm0, (%rsp)
movq 0xd8(%rsp), %rdi
callq 0x4e330
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
movaps %xmm0, %xmm3
movsd 0x10(%rsp), %xmm0
mulsd %xmm3, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0xac(%rsp), %esi
leaq 0xb0(%rsp), %rdi
callq 0x36c00
movsd 0x18(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movl 0xa8(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36be0
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0xd0(%rsp), %rdi
movl 0xa8(%rsp), %esi
callq 0x81f40
movsd %xmm0, 0x38(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x4e330
movsd %xmm0, 0x30(%rsp)
movq 0xd8(%rsp), %rdi
movl 0xa8(%rsp), %esi
callq 0x81f40
movsd %xmm0, 0x28(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x4e330
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movaps %xmm0, %xmm3
movsd 0x38(%rsp), %xmm0
mulsd %xmm3, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0xac(%rsp), %esi
leaq 0xb0(%rsp), %rdi
callq 0x36c00
movsd 0x40(%rsp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0xa4(%rsp), %esi
leaq 0x88(%rsp), %rdi
callq 0x36be0
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x58(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x6e720
movq 0xc8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3c140
movl $0x2, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
nopl (%rax)