// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3Intersects(gaps::R3Mesh*, gaps::R3MeshFace*, gaps::R3Box const&)
static RNBoolean
R3Intersects(R3Mesh *mesh, R3MeshFace *face, const R3Box& box)
{
  // Check triangle bounding box and plane
  if (R3Contains(box, mesh->FaceBBox(face))) return TRUE;
  if (!R3Intersects(mesh->FaceBBox(face), box)) return FALSE;
  if (!R3Intersects(mesh->FacePlane(face), box)) return FALSE;

  // Make polygon
  R3Point points[16];
  points[0] = mesh->VertexPosition(mesh->VertexOnFace(face, 0));
  points[1] = mesh->VertexPosition(mesh->VertexOnFace(face, 1));
  points[2] = mesh->VertexPosition(mesh->VertexOnFace(face, 2));
  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++) {
      assert(npoints < 15);
      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 FALSE; 
      assert(npoints < 16);
      assert(npoints > 0);
    }
  }

  // Triangle survived all clips
  return TRUE;
}

// Assembly:
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rdi, 0x2c8(%rsp)
movq %rsi, 0x2c0(%rsp)
movq %rdx, 0x2b8(%rsp)
movq 0x2b8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x2c8(%rsp), %rdi
movq 0x2c0(%rsp), %rsi
callq 0x3a630
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x2e4c0
cmpl $0x0, %eax
je 0x1b393
movl $0x1, 0x2d4(%rsp)
jmp 0x1ba5f
movq 0x2c8(%rsp), %rdi
movq 0x2c0(%rsp), %rsi
callq 0x3a630
movq %rax, %rdi
movq 0x2b8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x29bd0
cmpl $0x0, %eax
jne 0x1b3d1
movl $0x0, 0x2d4(%rsp)
jmp 0x1ba5f
movq 0x2c8(%rsp), %rdi
movq 0x2c0(%rsp), %rsi
callq 0x18070
movq %rax, %rdi
movq 0x2b8(%rsp), %rsi
callq 0x286a0
cmpl $0x0, %eax
jne 0x1b40b
movl $0x0, 0x2d4(%rsp)
jmp 0x1ba5f
leaq 0x130(%rsp), %rax
movq %rax, %rcx
addq $0x180, %rcx            # imm = 0x180
movq %rcx, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x92f00
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x40(%rsp)
jne 0x1b427
movq 0x2c8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x2c8(%rsp), %rdi
movq 0x2c0(%rsp), %rsi
xorl %edx, %edx
callq 0x17a60
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x17a40
movq %rax, %rsi
leaq 0x130(%rsp), %rdi
callq 0x935a0
movq 0x2c8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x2c8(%rsp), %rdi
movq 0x2c0(%rsp), %rsi
movl $0x1, %edx
callq 0x17a60
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x17a40
movq %rax, %rsi
leaq 0x130(%rsp), %rdi
addq $0x18, %rdi
callq 0x935a0
movq 0x2c8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x2c8(%rsp), %rdi
movq 0x2c0(%rsp), %rsi
movl $0x2, %edx
callq 0x17a60
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x17a40
movq %rax, %rsi
leaq 0x130(%rsp), %rdi
addq $0x30, %rdi
callq 0x935a0
movl $0x3, 0x12c(%rsp)
movl $0x0, 0x128(%rsp)
cmpl $0x1, 0x128(%rsp)
jg 0x1ba54
movl $0x0, 0x124(%rsp)
cmpl $0x2, 0x124(%rsp)
jg 0x1ba3c
leaq 0x130(%rsp), %rsi
movslq 0x12c(%rsp), %rax
leaq 0x130(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x935a0
movl $0x0, 0x120(%rsp)
movl 0x120(%rsp), %eax
cmpl 0x12c(%rsp), %eax
jge 0x1ba0d
movslq 0x120(%rsp), %rcx
leaq 0x130(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x118(%rsp)
movl 0x120(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x130(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x110(%rsp)
movq 0x118(%rsp), %rdi
movl 0x124(%rsp), %esi
callq 0x17800
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x2b8(%rsp), %rdi
movl 0x128(%rsp), %esi
callq 0x21790
movq %rax, %rdi
movl 0x124(%rsp), %esi
callq 0x17e60
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x108(%rsp)
movq 0x110(%rsp), %rdi
movl 0x124(%rsp), %esi
callq 0x17800
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x2b8(%rsp), %rdi
movl 0x128(%rsp), %esi
callq 0x21790
movq %rax, %rdi
movl 0x124(%rsp), %esi
callq 0x17e60
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
cmpl $0x0, 0x128(%rsp)
jne 0x1b6ec
movsd 0x108(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x108(%rsp)
movsd 0x100(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x100(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x108(%rsp), %xmm0
jbe 0x1b883
movsd 0x100(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1b87e
movsd 0x100(%rsp), %xmm0
movsd 0x108(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd 0x100(%rsp), %xmm0
divsd 0xf8(%rsp), %xmm0
movq 0x118(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x18320
movsd 0x108(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd 0xf8(%rsp), %xmm0
movq 0x110(%rsp), %rsi
leaq 0xb0(%rsp), %rdi
callq 0x18320
leaq 0xe0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
callq 0x93e00
movl 0x12c(%rsp), %eax
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0x120(%rsp), %eax
jle 0x1b834
movslq 0xac(%rsp), %rax
leaq 0x130(%rsp), %rsi
imulq $0x18, %rax, %rax
addq %rax, %rsi
movl 0xac(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x130(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x935a0
movl 0xac(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x1b7da
movl 0x120(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x130(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x935a0
movl 0x12c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12c(%rsp)
movl 0x120(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x120(%rsp)
jmp 0x1b9f5
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1b9f3
xorps %xmm0, %xmm0
ucomisd 0x100(%rsp), %xmm0
jbe 0x1b979
movsd 0x100(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
addsd 0x108(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0x100(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd 0xa0(%rsp), %xmm0
movq 0x118(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x18320
movsd 0x108(%rsp), %xmm0
divsd 0xa0(%rsp), %xmm0
movq 0x110(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x18320
leaq 0x88(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x93e00
movslq 0x120(%rsp), %rax
leaq 0x130(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0x88(%rsp), %rsi
callq 0x935a0
jmp 0x1b9f1
movl 0x120(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x12c(%rsp), %eax
jge 0x1b9cf
movl 0x54(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x130(%rsp), %rsi
imulq $0x18, %rax, %rax
addq %rax, %rsi
movslq 0x54(%rsp), %rax
leaq 0x130(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x935a0
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x1b984
movl 0x12c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x12c(%rsp)
movl 0x120(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x120(%rsp)
jmp 0x1b9f3
jmp 0x1b9f5
jmp 0x1b9f7
movl 0x120(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x120(%rsp)
jmp 0x1b58b
cmpl $0x0, 0x12c(%rsp)
jne 0x1ba24
movl $0x0, 0x2d4(%rsp)
jmp 0x1ba5f
jmp 0x1ba26
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x1b54e
jmp 0x1ba3e
movl 0x128(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x128(%rsp)
jmp 0x1b535
movl $0x1, 0x2d4(%rsp)
movl 0x2d4(%rsp), %eax
addq $0x2d8, %rsp            # imm = 0x2D8
retq
nop