// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %rax
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq *0x88(%rax)
xorl %r12d, %r12d
movq %r15, %rdi
movq %r14, %r15
movq %r14, %rsi
xorl %edx, %edx
callq 0x46418
movl %eax, %ebp
leaq 0xa0(%rsp), %rdi
callq 0x28348
testl %ebp, %ebp
je 0x463eb
leaq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x458ed
testl %eax, %eax
je 0x463eb
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0xa0, %rdi
callq 0x3ca24
addq $0x18, %r14
cmpq $0x180, %r14            # imm = 0x180
jne 0x460d6
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x3cd94
movq 0x10(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3cd94
movq 0x18(%rbx), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x3cd94
xorl %edx, %edx
movl $0x3, %r14d
movb $0x1, 0x7(%rsp)
leaq 0x20(%r15), %rax
movq %rax, 0x38(%rsp)
addq $0x8, %r15
movq %r15, 0x40(%rsp)
testl %edx, %edx
movq 0x38(%rsp), %rax
cmoveq 0x40(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
movq %rdx, 0x8(%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 0x3cd94
testl %ebx, %ebx
jle 0x46380
xorl %r15d, %r15d
movq 0x8(%rsp), %rdx
xorpd %xmm2, %xmm2
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 0x461f1
movapd 0x5be37(%rip), %xmm1    # 0xa2020
xorpd %xmm1, %xmm3
xorpd %xmm1, %xmm0
ucomisd %xmm3, %xmm2
jbe 0x462b7
ucomisd %xmm2, %xmm0
jbe 0x46372
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x5be05(%rip), %xmm3    # 0xa2020
movapd %xmm3, 0x10(%rsp)
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x3d470
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3d470
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x3d42a
movl %r14d, 0x10(%rsp)
cmpl %r15d, %r14d
jle 0x46299
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 0x3cd94
decq %r14
addq $-0x18, %rbx
cmpq %r12, %r14
jg 0x46281
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x3cd94
movl 0x10(%rsp), %r14d
incl %r14d
addl $0x2, %r15d
jmp 0x46369
ucomisd %xmm2, %xmm3
jbe 0x46372
ucomisd %xmm0, %xmm2
jbe 0x46335
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x5bd43(%rip), %xmm0    # 0xa2020
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
movapd %xmm3, 0x10(%rsp)
callq 0x3d470
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3d470
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
movq %r12, %rdx
callq 0x3d42a
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3cd94
jmp 0x46369
movl %r14d, 0x10(%rsp)
cmpl %r14d, %r15d
jge 0x4635e
movslq 0x10(%rsp), %r14
subq %r12, %r14
leaq 0x18(%rbx), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3cd94
movq %rbp, %rbx
decq %r14
jne 0x46347
movl 0x10(%rsp), %r14d
decl %r14d
decl %r15d
movq 0x8(%rsp), %rdx
xorpd %xmm2, %xmm2
incl %r15d
cmpl %r14d, %r15d
jl 0x46199
jmp 0x46385
movq 0x8(%rsp), %rdx
testl %r14d, %r14d
je 0x463a4
cmpq $0x2, %r13
leaq 0x1(%r13), %rax
setae %cl
movq %rax, %r13
cmpq $0x3, %rax
jne 0x46161
jmp 0x463ac
xorl %r14d, %r14d
movq 0x48(%rsp), %rcx
testb $0x1, %cl
je 0x463c3
leal 0x1(%rdx), %eax
testl %edx, %edx
sete 0x7(%rsp)
movl %eax, %edx
je 0x46145
xorl %eax, %eax
cmpl $0x2, %r14d
setne %al
leal 0x4(,%rax,2), %r12d
xorl %eax, %eax
cmpl $0x1, %r14d
cmovel %r14d, %r12d
cmovll %eax, %r12d
testb $0x1, 0x7(%rsp)
cmovnel %eax, %r12d
movl %r12d, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x28348
movq %rbx, %rdi
callq 0xe7e0