// File: /workspace/github_cpp_makefile_star2_O3/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 $0x218, %rsp            # imm = 0x218
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r12
callq 0x2574a
movq %r13, %rdi
movq %rax, %rsi
callq 0x1f492
movl $0x1, %r15d
testl %eax, %eax
je 0x15f17
movl %r15d, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq %r14, %rsi
callq 0x2574a
xorl %r15d, %r15d
movq %rax, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x1cf08
testl %eax, %eax
je 0x15f02
testb $0x4, 0xa8(%r14)
jne 0x15f50
movq (%r12), %rax
movq %r12, %rdi
movq %r14, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r14), %rdi
movq %r13, %rsi
callq 0x1c490
testl %eax, %eax
je 0x15f02
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
callq 0x4fd3c
addq $0x18, %rbx
cmpq $0x180, %rbx            # imm = 0x180
jne 0x15f62
movq 0x8(%r14), %rsi
addq $0x18, %rsi
leaq 0x90(%rsp), %rdi
callq 0x500a6
movq 0x10(%r14), %rsi
addq $0x18, %rsi
leaq 0xa8(%rsp), %rdi
callq 0x500a6
movq 0x18(%r14), %rsi
addq $0x18, %rsi
leaq 0xc0(%rsp), %rdi
callq 0x500a6
movb $0x1, %al
movl %eax, 0xc(%rsp)
movl $0x3, %ebx
leaq 0x20(%r13), %rax
movq %rax, 0x30(%rsp)
addq $0x8, %r13
movq %r13, 0x38(%rsp)
testb $0x1, 0xc(%rsp)
movq 0x30(%rsp), %rax
cmovneq 0x38(%rsp), %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
movl %ebx, %ebp
movslq %ebx, %rbx
leaq (%rbx,%rbx,2), %rax
leaq (%rsp,%rax,8), %rdi
addq $0x90, %rdi
leaq 0x90(%rsp), %rsi
callq 0x500a6
testl %ebx, %ebx
jle 0x161ff
xorl %r15d, %r15d
movl %ebp, %ebx
xorpd %xmm2, %xmm2
movslq %r15d, %r12
leaq (%r12,%r12,2), %rax
leaq (%rsp,%rax,8), %r14
addq $0x90, %r14
leal 0x1(%r12), %eax
cltq
leaq (%rax,%rax,2), %rax
leaq 0x90(%rsp,%rax,8), %rbp
movsd (%r14,%r13,8), %xmm3
movq 0x40(%rsp), %rax
movsd (%rax,%r13,8), %xmm1
subsd %xmm1, %xmm3
movsd (%rbp,%r13,8), %xmm0
subsd %xmm1, %xmm0
testb $0x1, 0xc(%rsp)
je 0x16081
movapd 0x93fb7(%rip), %xmm1    # 0xaa030
xorpd %xmm1, %xmm3
xorpd %xmm1, %xmm0
ucomisd %xmm3, %xmm2
jbe 0x16141
ucomisd %xmm2, %xmm0
jbe 0x161f1
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x93f85(%rip), %xmm3    # 0xaa030
movapd %xmm3, 0x10(%rsp)
divsd %xmm1, %xmm0
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x50782
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x78(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x50782
leaq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %r14, %rdx
callq 0x5073c
movl %ebx, 0x10(%rsp)
cmpl %r15d, %ebx
jle 0x16125
movslq 0x10(%rsp), %rbx
leaq (%rbx,%rbx,2), %rax
leaq (%rsp,%rax,8), %r14
addq $0x90, %r14
leaq 0x18(%r14), %rdi
movq %r14, %rsi
callq 0x500a6
decq %rbx
addq $-0x18, %r14
cmpq %r12, %rbx
jg 0x1610d
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x500a6
movl 0x10(%rsp), %ebx
incl %ebx
addl $0x2, %r15d
jmp 0x161ed
ucomisd %xmm2, %xmm3
jbe 0x161f1
ucomisd %xmm0, %xmm2
jbe 0x161bc
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x93ec9(%rip), %xmm0    # 0xaa030
divsd %xmm1, %xmm0
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
movapd %xmm3, 0x10(%rsp)
callq 0x50782
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x50782
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
leaq 0x48(%rsp), %rsi
movq %r12, %rdx
callq 0x5073c
movq %r14, %rdi
movq %rbp, %rsi
callq 0x500a6
jmp 0x161ed
movl %ebx, 0x10(%rsp)
cmpl %ebx, %r15d
jge 0x161e4
movslq 0x10(%rsp), %rbx
subq %r12, %rbx
leaq 0x18(%r14), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x500a6
movq %rbp, %r14
decq %rbx
jne 0x161cd
movl 0x10(%rsp), %ebx
decl %ebx
decl %r15d
xorpd %xmm2, %xmm2
incl %r15d
cmpl %ebx, %r15d
jl 0x16025
jmp 0x16201
movl %ebp, %ebx
testl %ebx, %ebx
je 0x16225
incq %r13
cmpq $0x3, %r13
jne 0x15ff3
testb $0x1, 0xc(%rsp)
movl $0x0, 0xc(%rsp)
jne 0x15fdb
xorl %r15d, %r15d
testl %ebx, %ebx
setne %r15b
jmp 0x15f02
nop