// File: /workspace/github_cpp_makefile_star2_O2/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, %r13
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 %r13, %rsi
xorl %edx, %edx
callq 0x406ec
movl %eax, %ebp
leaq 0xa0(%rsp), %rdi
callq 0x25832
testl %ebp, %ebp
je 0x406bf
leaq 0x20(%rbx), %rdi
movq %r13, %rsi
callq 0x3fcf1
testl %eax, %eax
je 0x406bf
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0xa0, %rdi
callq 0x37b4a
addq $0x18, %r14
cmpq $0x180, %r14            # imm = 0x180
jne 0x403eb
movq 0x8(%rbx), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x37e36
movq 0x10(%rbx), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x37e36
movq 0x18(%rbx), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x37e36
pushq $0x3
popq %rdx
xorl %r14d, %r14d
leaq 0x20(%r13), %rax
movq %rax, 0x40(%rsp)
addq $0x8, %r13
movq %r13, 0x38(%rsp)
cmpl $0x2, %r14d
je 0x406a3
testl %r14d, %r14d
movq 0x40(%rsp), %rax
cmoveq %r13, %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
movq %r14, 0x50(%rsp)
cmpq $0x3, %r13
je 0x40696
movslq %edx, %rax
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %rdi
addq $0xa0, %rdi
leaq 0xa0(%rsp), %rsi
movl %edx, %ebx
callq 0x37e36
xorpd %xmm2, %xmm2
movl %ebx, %edx
xorl %r15d, %r15d
cmpl %edx, %r15d
jge 0x40689
movslq %r15d, %r12
imulq $0x18, %r12, %rax
leal 0x1(%r15), %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
leaq (%rsp,%rax), %rbx
addq $0xa0, %rbx
leaq 0xa0(%rsp,%rcx), %rbp
movsd (%rbx,%r13,8), %xmm3
movq 0x48(%rsp), %rax
movsd (%rax,%r13,8), %xmm1
subsd %xmm1, %xmm3
movsd (%rbp,%r13,8), %xmm0
subsd %xmm1, %xmm0
testl %r14d, %r14d
jne 0x4050d
movapd 0x52b2b(%rip), %xmm1    # 0x93030
xorpd %xmm1, %xmm3
xorpd %xmm1, %xmm0
ucomisd %xmm3, %xmm2
movslq %edx, %r14
jbe 0x405b3
ucomisd %xmm2, %xmm0
jbe 0x4067c
movl %edx, 0xc(%rsp)
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movapd %xmm1, 0x10(%rsp)
xorpd 0x52af2(%rip), %xmm3    # 0x93030
movapd %xmm3, 0x20(%rsp)
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x382c0
movapd 0x20(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x382c0
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbx, %rdx
callq 0x3827a
imulq $0x18, %r14, %rax
leaq (%rsp,%rax), %rbx
addq $0xa0, %rbx
cmpq %r12, %r14
jle 0x40656
leaq 0x18(%rbx), %rdi
movq %rbx, %rsi
callq 0x37e36
decq %r14
addq $-0x18, %rbx
jmp 0x40595
ucomisd %xmm2, %xmm3
jbe 0x4067c
movl %edx, 0xc(%rsp)
ucomisd %xmm0, %xmm2
jbe 0x4063a
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
xorpd 0x52a53(%rip), %xmm0    # 0x93030
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movapd %xmm3, 0x20(%rsp)
callq 0x382c0
movapd 0x20(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
leaq 0x88(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x382c0
leaq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x3827a
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x37e36
movl 0xc(%rsp), %edx
jmp 0x40678
cmpq %r14, %r12
jge 0x4066f
incq %r12
leaq 0x18(%rbx), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x37e36
movq %rbp, %rbx
jmp 0x4063a
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x37e36
movl 0xc(%rsp), %edx
incl %edx
addl $0x2, %r15d
jmp 0x40678
movl 0xc(%rsp), %edx
decl %edx
decl %r15d
xorpd %xmm2, %xmm2
incl %r15d
movq 0x50(%rsp), %r14
jmp 0x404aa
incq %r13
testl %edx, %edx
jne 0x40476
jmp 0x406bc
incl %r14d
movq 0x38(%rsp), %r13
jmp 0x40453
xorl %eax, %eax
cmpl $0x2, %edx
setne %al
leal 0x4(,%rax,2), %r12d
cmpl $0x1, %edx
cmovel %edx, %r12d
jge 0x406bf
xorl %r12d, %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 0x25832
movq %rbx, %rdi
callq 0xe810