// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::InsertFace(gaps::R3MeshSearchTreeFace*, gaps::R3MeshSearchTreeNode*, gaps::R3Box const&, int)
void R3MeshSearchTree::
InsertFace(R3MeshSearchTreeFace *face, R3MeshSearchTreeNode *node, const R3Box& node_box, int depth) 
{
  // Check if face intersects box
  if (!R3Intersects(mesh, face->face, node_box)) return;

  // Check if interior node
  if (node->children[0]) {
    // Interior node -- Insert into children
    assert(node->children[1]);
    const R3Box& face_box = mesh->FaceBBox(face->face);
    if (face_box[RN_LO][node->split_dimension] <= node->split_coordinate) {
      R3Box node0_box(node_box);
      node0_box[RN_HI][node->split_dimension] = node->split_coordinate;
      InsertFace(face, node->children[0], node0_box, depth + 1);
    }
    if (face_box[RN_HI][node->split_dimension] >= node->split_coordinate) {
      R3Box node1_box(node_box);
      node1_box[RN_LO][node->split_dimension] = node->split_coordinate;
      InsertFace(face, node->children[1], node1_box, depth + 1);
    }
  }
  else {
    // Check face area 
    RNScalar node_diagonal = node_box.DiagonalLength();
    if (node_diagonal == 0) return;
    RNScalar node_area = node_diagonal * node_diagonal;
    RNScalar area_ratio = face->area / node_area;
    if ((area_ratio >= max_area_ratio) || 
        (depth >= max_depth)) {
      // Face is too big/deep to be sorted into children, insert into big faces list
      node->big_faces.Insert(face);
      assert(face->reference_count >= 0);
      face->reference_count++;
    }
    else {
      // Leaf node -- Check if there is room for this face
      if (node->small_faces.NEntries() < max_faces_per_node) {
        // Simply insert face into list
        node->small_faces.Insert(face);
        face->reference_count++;
      }
      else {
        // Create two children 
        node->children[0] = new R3MeshSearchTreeNode(node);
        node->children[1] = new R3MeshSearchTreeNode(node);
        node->split_dimension = node_box.LongestAxis();
        node->split_coordinate = node_box.AxisCenter(node->split_dimension);
        nnodes += 2;
      
        // Re-insert faces into subtree
        InsertFace(face, node, node_box, depth+1);
        for (int i = 0; i < node->small_faces.NEntries(); i++) {
          InsertFace(node->small_faces[i], node, node_box, depth+1);
        }
      
        // Clear out faces from node that is now interior
        for (int i = 0; i < node->small_faces.NEntries(); i++) {
          assert(node->small_faces[i]->reference_count > 0);
          node->small_faces[i]->reference_count--;
        }
        node->small_faces.Empty();
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, %ebx
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
movq (%rdi), %rdi
movq (%rsi), %rsi
movq %rcx, %rdx
callq 0x15f96
testl %eax, %eax
je 0x1657b
cmpq $0x0, 0x8(%r14)
je 0x16430
movq (%r15), %rdi
movq (%r13), %rsi
callq 0x25d8e
movq %rax, %rbp
movslq 0x20(%r14), %rax
movsd 0x18(%r14), %xmm0
ucomisd 0x8(%rbp,%rax,8), %xmm0
jb 0x163d4
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3b340
movsd 0x18(%r14), %xmm0
movslq 0x20(%r14), %rax
movsd %xmm0, 0x30(%rsp,%rax,8)
movq 0x8(%r14), %rdx
leal 0x1(%rbx), %r8d
movq %r15, %rdi
movq %r13, %rsi
leaq 0x10(%rsp), %rcx
callq 0x16336
leaq 0x10(%rsp), %rdi
callq 0x3c198
movslq 0x20(%r14), %rax
movsd 0x20(%rbp,%rax,8), %xmm0
ucomisd 0x18(%r14), %xmm0
jb 0x1657b
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3b340
movsd 0x18(%r14), %xmm0
movslq 0x20(%r14), %rax
movsd %xmm0, 0x18(%rsp,%rax,8)
movq 0x10(%r14), %rdx
incl %ebx
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rcx
movl %ebx, %r8d
callq 0x16336
leaq 0x10(%rsp), %rdi
callq 0x3c198
jmp 0x1657b
movq %r12, %rdi
callq 0x3b9c0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x16448
jnp 0x1657b
cmpl $0x3f, %ebx
jg 0x16564
mulsd %xmm0, %xmm0
movsd 0x8(%r13), %xmm1
divsd %xmm0, %xmm1
ucomisd 0x94159(%rip), %xmm1    # 0xaa5c0
jae 0x16564
leaq 0x38(%r14), %rdi
movl 0x44(%r14), %edx
cmpl $0x7f, %edx
jle 0x1656f
movq %rdi, 0x8(%rsp)
movl $0x48, %edi
callq 0xe440
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
callq 0x19406
movq %rbp, 0x8(%r14)
movl $0x48, %edi
callq 0xe440
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
callq 0x19406
movq %rbp, 0x10(%r14)
movq %r12, %rdi
callq 0x3b92e
movl %eax, 0x20(%r14)
cltq
movsd 0x20(%r12,%rax,8), %xmm0
addsd 0x8(%r12,%rax,8), %xmm0
mulsd 0x93b81(%rip), %xmm0    # 0xaa060
movsd %xmm0, 0x18(%r14)
addl $0x2, 0x10(%r15)
incl %ebx
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r12, %rcx
movl %ebx, %r8d
callq 0x16336
movl 0x44(%r14), %eax
testl %eax, %eax
jle 0x16530
xorl %r13d, %r13d
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq %r12, %rcx
movl %ebx, %r8d
callq 0x16336
incq %r13
movslq 0x44(%r14), %rax
cmpq %rax, %r13
jl 0x1650b
testl %eax, %eax
movq 0x8(%rsp), %rdi
jle 0x1654f
movq (%rdi), %rcx
movl %eax, %eax
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
decl 0x10(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x16540
xorl %esi, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x78704
movl 0x34(%r14), %edx
addq $0x28, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x7855a
incl 0x10(%r13)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1658c
movq %rax, %rbx
movl $0x48, %esi
movq %rbp, %rdi
callq 0xe450
jmp 0x165ad
jmp 0x165a0
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3c198
movq %rbx, %rdi
callq 0xe7f0
nop