// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Triangle const&, gaps::R3Box const&)
RNClassID R3Intersects(const R3Triangle& triangle, const R3Box& box)
{
  // Check triangle bounding box and plane
  if (!R3Intersects(triangle.BBox(), box)) return RN_NULL_CLASS_ID;
  if (!R3Intersects(triangle.Plane(), box)) return RN_NULL_CLASS_ID;

  // Make polygon
  R3Point points[16];
  points[0] = triangle.Vertex(0)->Position();
  points[1] = triangle.Vertex(1)->Position();
  points[2] = triangle.Vertex(2)->Position();
  int npoints = 3;

  // Clip polygon against each side
  for (RNDirection dir = RN_LO; dir <= RN_HI; dir++) {
    for (RNDimension dim = RN_X; dim <= RN_Z; dim++) {
      points[npoints] = points[0];
      for (int i = 0; i < npoints; i++) {
        R3Point& p1 = points[i];
        R3Point& p2 = points[i+1];
        RNScalar d1 = p1[dim] - box[dir][dim];
        RNScalar d2 = p2[dim] - box[dir][dim];
        if (dir == RN_LO) { d1 = -d1; d2 = -d2; }
        if (d1 < 0) { // Inside
          if (d2 > 0) { // Outside
            // Insert a point at crossing
            RNScalar denom = d2 + -d1;
            R3Point p = (d2/denom)*p1 + (-d1/denom)*p2;
            for (int j = npoints; j > i; j--) points[j+1] = points[j];
            points[i+1] = p;
            npoints++;
            i += 2;
          }
        }
        else if (d1 > 0) { // Outside
          if (d2 < 0) { // Inside
            // Replace p1 with point at crossing
            RNScalar denom = -d2 + d1;
            R3Point p = (-d2/denom)*p1 + (d1/denom)*p2;
            points[i] = p;
          }
          else {
            // Remove p1
            for (int j = i; j < npoints; j++) points[j] = points[j+1];
            npoints--;
            i--;
          }
        }
      }

      // Check number of points
      if (npoints == 0) return RN_NULL_CLASS_ID;
      assert(npoints < 16);
      assert(npoints > 0);
    }
  }

  // Triangle survived all clips
  if (npoints <= 0) return RN_NULL_CLASS_ID;
  else if (npoints == 1) return R3_POINT_CLASS_ID;
  else if (npoints == 2) return R3_SPAN_CLASS_ID;
  else return R3_POLYGON_CLASS_ID;
}

// Assembly:
subq $0x308, %rsp            # imm = 0x308
movq %rdi, 0x2f8(%rsp)
movq %rsi, 0x2f0(%rsp)
movq 0x2f8(%rsp), %rsi
movq (%rsi), %rax
movq 0x88(%rax), %rax
leaq 0x2b8(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq *%rax
movq 0x38(%rsp), %rdi
movq 0x2f0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x80e10
movl %eax, 0x40(%rsp)
jmp 0x80714
movl 0x40(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, 0x37(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0x36a80
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x8073b
jmp 0x80771
movl $0x0, 0x304(%rsp)
jmp 0x80deb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2b0(%rsp)
movl %eax, 0x2ac(%rsp)
leaq 0x2b8(%rsp), %rdi
callq 0x36a80
jmp 0x80dfa
movq 0x2f8(%rsp), %rdi
callq 0x81b30
movq %rax, %rdi
movq 0x2f0(%rsp), %rsi
callq 0x7f8e0
cmpl $0x0, %eax
jne 0x807a3
movl $0x0, 0x304(%rsp)
jmp 0x80deb
leaq 0x120(%rsp), %rax
movq %rax, %rcx
addq $0x180, %rcx            # imm = 0x180
movq %rcx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x6eb40
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x28(%rsp)
jne 0x807bf
movq 0x2f8(%rsp), %rdi
xorl %esi, %esi
callq 0x81eb0
movq %rax, %rdi
callq 0x78950
movq %rax, %rsi
leaq 0x120(%rsp), %rdi
callq 0x6f1e0
movq 0x2f8(%rsp), %rdi
movl $0x1, %esi
callq 0x81eb0
movq %rax, %rdi
callq 0x78950
movq %rax, %rsi
leaq 0x120(%rsp), %rdi
addq $0x18, %rdi
callq 0x6f1e0
movq 0x2f8(%rsp), %rdi
movl $0x2, %esi
callq 0x81eb0
movq %rax, %rdi
callq 0x78950
movq %rax, %rsi
leaq 0x120(%rsp), %rdi
addq $0x30, %rdi
callq 0x6f1e0
movl $0x3, 0x11c(%rsp)
movl $0x0, 0x118(%rsp)
cmpl $0x1, 0x118(%rsp)
jg 0x80d9b
movl $0x0, 0x114(%rsp)
cmpl $0x2, 0x114(%rsp)
jg 0x80d83
leaq 0x120(%rsp), %rsi
movslq 0x11c(%rsp), %rax
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x6f1e0
movl $0x0, 0x110(%rsp)
movl 0x110(%rsp), %eax
cmpl 0x11c(%rsp), %eax
jge 0x80d51
movslq 0x110(%rsp), %rcx
leaq 0x120(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x108(%rsp)
movl 0x110(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x120(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x2f0(%rsp), %rdi
movl 0x118(%rsp), %esi
callq 0x44f00
movq %rax, %rdi
movl 0x114(%rsp), %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0xf8(%rsp)
movq 0x100(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x2f0(%rsp), %rdi
movl 0x118(%rsp), %esi
callq 0x44f00
movq %rax, %rdi
movl 0x114(%rsp), %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
cmpl $0x0, 0x118(%rsp)
jne 0x80a36
movsd 0xf8(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd 0xf0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0xf0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xf8(%rsp), %xmm0
jbe 0x80bcd
movsd 0xf0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x80bc8
movsd 0xf0(%rsp), %xmm0
movsd 0xf8(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0xf0(%rsp), %xmm0
divsd 0xe8(%rsp), %xmm0
movq 0x108(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x36b10
movsd 0xf8(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd 0xe8(%rsp), %xmm0
movq 0x100(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x36b10
leaq 0xd0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x6fa40
movl 0x11c(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x110(%rsp), %eax
jle 0x80b7e
movslq 0x9c(%rsp), %rax
leaq 0x120(%rsp), %rsi
imulq $0x18, %rax, %rax
addq %rax, %rsi
movl 0x9c(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x6f1e0
movl 0x9c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x80b24
movl 0x110(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x6f1e0
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
movl 0x110(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x110(%rsp)
jmp 0x80d39
movsd 0xf8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x80d37
xorps %xmm0, %xmm0
ucomisd 0xf0(%rsp), %xmm0
jbe 0x80cbd
movsd 0xf0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
addsd 0xf8(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xf0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd 0x90(%rsp), %xmm0
movq 0x108(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x36b10
movsd 0xf8(%rsp), %xmm0
divsd 0x90(%rsp), %xmm0
movq 0x100(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x36b10
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x6fa40
movslq 0x110(%rsp), %rax
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0x78(%rsp), %rsi
callq 0x6f1e0
jmp 0x80d35
movl 0x110(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x11c(%rsp), %eax
jge 0x80d13
movl 0x44(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x120(%rsp), %rsi
imulq $0x18, %rax, %rax
addq %rax, %rsi
movslq 0x44(%rsp), %rax
leaq 0x120(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x6f1e0
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x80cc8
movl 0x11c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x11c(%rsp)
movl 0x110(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x110(%rsp)
jmp 0x80d37
jmp 0x80d39
jmp 0x80d3b
movl 0x110(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x110(%rsp)
jmp 0x808d5
cmpl $0x0, 0x11c(%rsp)
jne 0x80d6b
movl $0x0, 0x304(%rsp)
jmp 0x80deb
jmp 0x80d6d
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x80898
jmp 0x80d85
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
jmp 0x8087f
cmpl $0x0, 0x11c(%rsp)
jg 0x80db2
movl $0x0, 0x304(%rsp)
jmp 0x80deb
cmpl $0x1, 0x11c(%rsp)
jne 0x80dc9
movl $0x1, 0x304(%rsp)
jmp 0x80deb
cmpl $0x2, 0x11c(%rsp)
jne 0x80de0
movl $0x4, 0x304(%rsp)
jmp 0x80deb
movl $0x6, 0x304(%rsp)
movl 0x304(%rsp), %eax
addq $0x308, %rsp            # imm = 0x308
retq
movq 0x2b0(%rsp), %rdi
callq 0xf890
nopw (%rax,%rax)