// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movq %rsi, %rbx
movq %rdi, %r15
callq 0x25d8e
movq %r12, %rdi
movq %rax, %rsi
callq 0x1f954
movl $0x1, %r14d
testl %eax, %eax
je 0x15fdf
movl %r14d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
movq %rbx, %rsi
callq 0x25d8e
xorl %r14d, %r14d
movq %rax, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x1d238
testl %eax, %eax
je 0x15fca
testb $0x4, 0xa8(%rbx)
jne 0x16016
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x2c8(%rax)
leaq 0x38(%rbx), %rdi
movq %r12, %rsi
callq 0x1c70d
testl %eax, %eax
je 0x15fca
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0xa0, %rdi
callq 0x508cc
addq $0x18, %r14
cmpq $0x180, %r14            # imm = 0x180
jne 0x16029
movq 0x8(%rbx), %rsi
addq $0x18, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x50c3c
movq 0x10(%rbx), %rsi
addq $0x18, %rsi
leaq 0xb8(%rsp), %rdi
callq 0x50c3c
movq 0x18(%rbx), %rsi
addq $0x18, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x50c3c
movl $0x0, 0xc(%rsp)
movl $0x3, %r14d
leaq 0x20(%r12), %rax
movq %rax, 0x38(%rsp)
addq $0x8, %r12
xorl %edx, %edx
movq %r12, 0x40(%rsp)
testl %edx, %edx
movq 0x38(%rsp), %rax
cmoveq %r12, %rax
movq %rax, 0x50(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
movq %rdx, (%rsp)
movq %rcx, 0x48(%rsp)
movslq %r14d, %rbx
leaq (%rbx,%rbx,2), %rax
leaq (%rsp,%rax,8), %rdi
addq $0xa0, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x50c3c
testl %ebx, %ebx
jle 0x162de
xorl %r15d, %r15d
xorpd %xmm2, %xmm2
movq (%rsp), %rdx
leal 0x1(%r15), %eax
movslq %r15d, %r12
leaq (%r12,%r12,2), %rcx
leaq (%rsp,%rcx,8), %rbx
addq $0xa0, %rbx
cltq
leaq (%rax,%rax,2), %rax
leaq 0xa0(%rsp,%rax,8), %rbp
movsd (%rbx,%r13,8), %xmm3
movq 0x50(%rsp), %rax
movsd (%rax,%r13,8), %xmm1
subsd %xmm1, %xmm3
movsd (%rbp,%r13,8), %xmm0
subsd %xmm1, %xmm0
testl %edx, %edx
jne 0x16150
movapd 0x93ee8(%rip), %xmm1    # 0xaa030
xorpd %xmm1, %xmm3
xorpd %xmm1, %xmm0
ucomisd %xmm3, %xmm2
jbe 0x16216
ucomisd %xmm2, %xmm0
jbe 0x162d0
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x93eb6(%rip), %xmm3    # 0xaa030
movapd %xmm3, 0x10(%rsp)
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x51318
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x51318
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x512d2
movl %r14d, 0x10(%rsp)
cmpl %r15d, %r14d
jle 0x161f8
movslq 0x10(%rsp), %r14
leaq (%r14,%r14,2), %rax
leaq (%rsp,%rax,8), %rbx
addq $0xa0, %rbx
leaq 0x18(%rbx), %rdi
movq %rbx, %rsi
callq 0x50c3c
decq %r14
addq $-0x18, %rbx
cmpq %r12, %r14
jg 0x161e0
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x50c3c
movl 0x10(%rsp), %r14d
incl %r14d
addl $0x2, %r15d
jmp 0x162c8
ucomisd %xmm2, %xmm3
jbe 0x162d0
ucomisd %xmm0, %xmm2
jbe 0x16294
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x93df4(%rip), %xmm0    # 0xaa030
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
movapd %xmm3, 0x10(%rsp)
callq 0x51318
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x51318
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
movq %r12, %rdx
callq 0x512d2
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x50c3c
jmp 0x162c8
movl %r14d, 0x10(%rsp)
cmpl %r14d, %r15d
jge 0x162bd
movslq 0x10(%rsp), %r14
subq %r12, %r14
leaq 0x18(%rbx), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x50c3c
movq %rbp, %rbx
decq %r14
jne 0x162a6
movl 0x10(%rsp), %r14d
decl %r14d
decl %r15d
xorpd %xmm2, %xmm2
movq (%rsp), %rdx
incl %r15d
cmpl %r14d, %r15d
jl 0x160f8
jmp 0x162e2
movq (%rsp), %rdx
testl %r14d, %r14d
je 0x16301
cmpq $0x2, %r13
leaq 0x1(%r13), %rax
setae %cl
movq %rax, %r13
cmpq $0x3, %rax
jne 0x160c1
jmp 0x16309
xorl %r14d, %r14d
movq 0x48(%rsp), %rcx
testb $0x1, %cl
movq 0x40(%rsp), %r12
je 0x16327
leal 0x1(%rdx), %eax
testl %edx, %edx
setne %cl
movl %ecx, 0xc(%rsp)
movl %eax, %edx
je 0x160a8
movzbl 0xc(%rsp), %r14d
andl $0x1, %r14d
jmp 0x15fca