// File: /workspace/github_cpp_makefile_star2_O2/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 0x14a26
testl %eax, %eax
je 0x14ec5
cmpq $0x0, 0x8(%r14)
je 0x14e71
movq (%r15), %rdi
movq (%r13), %rsi
callq 0x2286c
movq %rax, %rbp
movslq 0x20(%r14), %rax
movsd 0x18(%r14), %xmm0
ucomisd 0x8(%rbp,%rax,8), %xmm0
jb 0x14e1e
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x35c9e
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 0x14d76
leaq 0x10(%rsp), %rdi
callq 0x36966
movslq 0x20(%r14), %rax
movsd 0x18(%r14), %xmm0
movsd 0x20(%rbp,%rax,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x14ec5
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x35c9e
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 0x14d76
leaq 0x10(%rsp), %rdi
callq 0x36966
jmp 0x14ec5
movq %r12, %rdi
callq 0x362b8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x14e85
jnp 0x14ec5
cmpl $0x3f, %ebx
jg 0x14eb2
mulsd %xmm0, %xmm0
movsd 0x8(%r13), %xmm1
divsd %xmm0, %xmm1
ucomisd 0x85710(%rip), %xmm1    # 0x9a5b0
jae 0x14eb2
leaq 0x38(%r14), %rbp
cmpl $0x7f, 0x44(%r14)
jg 0x14ed4
movq %rbp, %rdi
jmp 0x14eb9
addq $0x28, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x1378a
incl 0x10(%r13)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0xe450
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x17a26
movq 0x8(%rsp), %rax
movq %rax, 0x8(%r14)
pushq $0x48
popq %rdi
callq 0xe450
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x17a26
movq 0x8(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r12, %rdi
callq 0x36226
movl %eax, 0x20(%r14)
cltq
movsd 0x20(%r12,%rax,8), %xmm0
addsd 0x8(%r12,%rax,8), %xmm0
mulsd 0x850d6(%rip), %xmm0    # 0x9a010
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 0x14d76
xorl %r13d, %r13d
movslq 0x44(%r14), %rax
cmpq %rax, %r13
jge 0x14f85
movq 0x38(%r14), %rax
movq (%rax,%r13,8), %rsi
movq %r15, %rdi
movq %r14, %rdx
movq %r12, %rcx
movl %ebx, %r8d
callq 0x14d76
incq %r13
jmp 0x14f5e
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
cmpq %rcx, %rax
je 0x14fa1
movq (%rbp), %rdx
movq (%rdx,%rcx,8), %rdx
decl 0x10(%rdx)
incq %rcx
jmp 0x14f8c
movq %rbp, %rdi
xorl %esi, %esi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6d10c
jmp 0x14fbb
movq %rax, %rbx
pushq $0x48
popq %rsi
movq 0x8(%rsp), %rdi
callq 0xe460
jmp 0x14fdc
jmp 0x14fcf
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x36966
movq %rbx, %rdi
callq 0xe820