// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
callq 0x2286c
movq %r13, %rdi
movq %rax, %rsi
callq 0x1d36e
testl %eax, %eax
je 0x14a6d
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq %r14, %rsi
callq 0x2286c
xorl %r15d, %r15d
movq %rax, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x1b4d8
testl %eax, %eax
je 0x14a58
movq %r12, %rdi
movq %r14, %rsi
callq 0x1375e
movq %rax, %rdi
movq %r13, %rsi
callq 0x1aadd
testl %eax, %eax
je 0x14a58
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0xa0, %rdi
callq 0x48bde
addq $0x18, %rbx
cmpq $0x180, %rbx            # imm = 0x180
jne 0x14aa8
movq 0x8(%r14), %rsi
addq $0x18, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x48eca
movq 0x10(%r14), %rsi
addq $0x18, %rsi
leaq 0xb8(%rsp), %rdi
callq 0x48eca
movq 0x18(%r14), %rsi
addq $0x18, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x48eca
pushq $0x3
popq %rdx
xorl %r15d, %r15d
leaq 0x20(%r13), %rax
movq %rax, 0x38(%rsp)
addq $0x8, %r13
movq %r13, 0x40(%rsp)
cmpl $0x2, %r15d
je 0x14d63
testl %r15d, %r15d
movq 0x38(%rsp), %rax
cmoveq 0x40(%rsp), %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
movq %r15, 0x50(%rsp)
cmpq $0x3, %r13
je 0x14d5b
movslq %edx, %rax
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0xa0, %rdi
leaq 0xa0(%rsp), %rsi
movl %edx, %ebx
callq 0x48eca
xorpd %xmm2, %xmm2
movl %ebx, %edx
xorl %ebx, %ebx
cmpl %edx, %ebx
jge 0x14d4e
movslq %ebx, %r12
imulq $0x18, %r12, %rax
leal 0x1(%rbx), %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
leaq (%rsp,%rax), %r14
addq $0xa0, %r14
leaq 0xa0(%rsp,%rcx), %rbp
movsd (%r14,%r13,8), %xmm3
movq 0x48(%rsp), %rax
movsd (%rax,%r13,8), %xmm1
subsd %xmm1, %xmm3
movsd (%rbp,%r13,8), %xmm0
subsd %xmm1, %xmm0
testl %r15d, %r15d
jne 0x14bd5
movapd 0x854a3(%rip), %xmm1    # 0x9a070
xorpd %xmm1, %xmm3
xorpd %xmm1, %xmm0
ucomisd %xmm3, %xmm2
movslq %edx, %r15
jbe 0x14c7b
ucomisd %xmm2, %xmm0
jbe 0x14d42
movl %edx, 0xc(%rsp)
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movapd %xmm1, 0x10(%rsp)
xorpd 0x8546a(%rip), %xmm3    # 0x9a070
movapd %xmm3, 0x20(%rsp)
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
callq 0x49354
movapd 0x20(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x49354
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %r14, %rdx
callq 0x4930e
imulq $0x18, %r15, %rax
leaq (%rsp,%rax), %r14
addq $0xa0, %r14
cmpq %r12, %r15
jle 0x14d1e
leaq 0x18(%r14), %rdi
movq %r14, %rsi
callq 0x48eca
decq %r15
addq $-0x18, %r14
jmp 0x14c5d
ucomisd %xmm2, %xmm3
jbe 0x14d42
movl %edx, 0xc(%rsp)
ucomisd %xmm0, %xmm2
jbe 0x14d02
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
xorpd 0x853cb(%rip), %xmm0    # 0x9a070
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movapd %xmm3, 0x20(%rsp)
callq 0x49354
movapd 0x20(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x49354
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4930e
movq %r14, %rdi
movq %rbp, %rsi
callq 0x48eca
movl 0xc(%rsp), %edx
jmp 0x14d3e
cmpq %r15, %r12
jge 0x14d36
incq %r12
leaq 0x18(%r14), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x48eca
movq %rbp, %r14
jmp 0x14d02
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x48eca
movl 0xc(%rsp), %edx
incl %edx
addl $0x2, %ebx
jmp 0x14d3e
movl 0xc(%rsp), %edx
decl %edx
decl %ebx
xorpd %xmm2, %xmm2
incl %ebx
movq 0x50(%rsp), %r15
jmp 0x14b74
incq %r13
testl %edx, %edx
jne 0x14b41
jmp 0x14d63
incl %r15d
jmp 0x14b1c
movq %r15, %rax
xorl %r15d, %r15d
cmpl $0x2, %eax
setae %r15b
jmp 0x14a58
nop