// File: /workspace/github_cpp_makefile_star2_O3/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 0x15ece
testl %eax, %eax
je 0x1647e
cmpq $0x0, 0x8(%r14)
je 0x16332
movq (%r15), %rdi
movq (%r13), %rsi
callq 0x2574a
movq %rax, %rbp
movslq 0x20(%r14), %rax
movsd 0x18(%r14), %xmm0
ucomisd 0x8(%rbp,%rax,8), %xmm0
jb 0x162dc
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3aa88
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 0x16234
leaq 0x10(%rsp), %rdi
callq 0x3b8a8
movslq 0x20(%r14), %rax
movsd 0x18(%r14), %xmm0
movsd 0x20(%rbp,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x1647e
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3aa88
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 0x16234
leaq 0x10(%rsp), %rdi
callq 0x3b8a8
jmp 0x1647e
movq %r12, %rdi
callq 0x3b0dc
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1634a
jnp 0x1647e
cmpl $0x3f, %ebx
jg 0x16467
mulsd %xmm0, %xmm0
movsd 0x8(%r13), %xmm1
divsd %xmm0, %xmm1
ucomisd 0x94247(%rip), %xmm1    # 0xaa5b0
jae 0x16467
leaq 0x38(%r14), %rdi
movl 0x44(%r14), %edx
cmpl $0x7f, %edx
jle 0x16472
movq %rdi, 0x8(%rsp)
movl $0x48, %edi
callq 0xe440
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
callq 0x192e0
movq %rbp, 0x8(%r14)
movl $0x48, %edi
callq 0xe440
movq %rax, %rbp
movq %rax, %rdi
movq %r14, %rsi
callq 0x192e0
movq %rbp, 0x10(%r14)
movq %r12, %rdi
callq 0x3b04a
movl %eax, 0x20(%r14)
cltq
movsd 0x20(%r12,%rax,8), %xmm0
addsd 0x8(%r12,%rax,8), %xmm0
mulsd 0x93c7f(%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 0x16234
cmpl $0x0, 0x44(%r14)
movq 0x8(%rsp), %rbp
jle 0x1644f
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 0x16234
incq %r13
movslq 0x44(%r14), %rax
cmpq %rax, %r13
jl 0x16411
testl %eax, %eax
jle 0x1644f
movq (%rbp), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
decl 0x10(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x16440
movq %rbp, %rdi
xorl %esi, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x77868
movl 0x34(%r14), %edx
addq $0x28, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x7769e
incl 0x10(%r13)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1648f
movq %rax, %rbx
movl $0x48, %esi
movq %rbp, %rdi
callq 0xe450
jmp 0x164b0
jmp 0x164a3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3b8a8
movq %rbx, %rdi
callq 0xe7f0