// File: /workspace/github_cpp_makefile_star2_O3/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 $0x218, %rsp            # imm = 0x218
movq %rsi, %r13
movq %rdi, %rbx
movq (%rdi), %rax
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq *0x88(%rax)
xorl %r12d, %r12d
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x45cbc
movl %eax, %ebp
leaq 0x90(%rsp), %rdi
callq 0x280f4
testl %ebp, %ebp
je 0x45c8f
leaq 0x20(%rbx), %rdi
movq %r13, %rsi
callq 0x45244
testl %eax, %eax
je 0x45c8f
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x90, %rdi
callq 0x3c530
addq $0x18, %r14
cmpq $0x180, %r14            # imm = 0x180
jne 0x459b2
movq 0x8(%rbx), %rsi
leaq 0x90(%rsp), %rdi
callq 0x3c89a
movq 0x10(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x3c89a
movq 0x18(%rbx), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x3c89a
movb $0x1, %cl
movl $0x3, %r14d
leaq 0x20(%r13), %rax
addq $0x8, %r13
movq %r13, 0x38(%rsp)
movq %rax, 0x30(%rsp)
testb $0x1, %cl
cmovneq %r13, %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
movl %ecx, 0xc(%rsp)
movslq %r14d, %rbx
leaq (%rbx,%rbx,2), %rax
leaq (%rsp,%rax,8), %rdi
addq $0x90, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3c89a
testl %ebx, %ebx
jle 0x45c43
xorl %r15d, %r15d
movl 0xc(%rsp), %ecx
xorpd %xmm2, %xmm2
movslq %r15d, %r12
leaq (%r12,%r12,2), %rax
leaq (%rsp,%rax,8), %rbx
addq $0x90, %rbx
leal 0x1(%r12), %eax
cltq
leaq (%rax,%rax,2), %rax
leaq 0x90(%rsp,%rax,8), %rbp
movsd (%rbx,%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, %cl
je 0x45abb
movapd 0x5c56d(%rip), %xmm1    # 0xa2020
xorpd %xmm1, %xmm3
xorpd %xmm1, %xmm0
ucomisd %xmm3, %xmm2
jbe 0x45b7e
ucomisd %xmm2, %xmm0
jbe 0x45c35
movapd %xmm0, %xmm1
subsd %xmm3, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x5c53b(%rip), %xmm3    # 0xa2020
movapd %xmm3, 0x10(%rsp)
divsd %xmm1, %xmm0
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x3cf76
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3cf76
leaq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rbx, %rdx
callq 0x3cf30
movl %r14d, 0x10(%rsp)
cmpl %r15d, %r14d
jle 0x45b60
movslq 0x10(%rsp), %r14
leaq (%r14,%r14,2), %rax
leaq (%rsp,%rax,8), %rbx
addq $0x90, %rbx
leaq 0x18(%rbx), %rdi
movq %rbx, %rsi
callq 0x3c89a
decq %r14
addq $-0x18, %rbx
cmpq %r12, %r14
jg 0x45b48
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x3c89a
movl 0x10(%rsp), %r14d
incl %r14d
addl $0x2, %r15d
jmp 0x45c2d
ucomisd %xmm2, %xmm3
jbe 0x45c35
ucomisd %xmm0, %xmm2
jbe 0x45bf9
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd 0x5c47c(%rip), %xmm0    # 0xa2020
divsd %xmm1, %xmm0
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
movapd %xmm3, 0x10(%rsp)
callq 0x3cf76
movapd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3cf76
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
leaq 0x48(%rsp), %rsi
movq %r12, %rdx
callq 0x3cf30
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3c89a
jmp 0x45c2d
movl %r14d, 0x10(%rsp)
cmpl %r14d, %r15d
jge 0x45c22
movslq 0x10(%rsp), %r14
subq %r12, %r14
leaq 0x18(%rbx), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3c89a
movq %rbp, %rbx
decq %r14
jne 0x45c0b
movl 0x10(%rsp), %r14d
decl %r14d
decl %r15d
movl 0xc(%rsp), %ecx
xorpd %xmm2, %xmm2
incl %r15d
cmpl %r14d, %r15d
jl 0x45a61
jmp 0x45c47
movl 0xc(%rsp), %ecx
testl %r14d, %r14d
je 0x45c8c
incq %r13
cmpq $0x3, %r13
jne 0x45a2f
testb $0x1, %cl
movl $0x0, %ecx
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %rax
jne 0x45a1c
xorl %eax, %eax
cmpl $0x2, %r14d
setne %al
leal 0x4(,%rax,2), %r12d
cmpl $0x1, %r14d
cmovel %r14d, %r12d
jge 0x45c8f
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x280f4
movq %rbx, %rdi
callq 0xe7e0