// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::SplitBlocks(gaps::R3SurfelNode*, double, double)
int R3SurfelTree::
SplitBlocks(R3SurfelNode *node, RNScalar max_complexity, RNScalar max_extent)
{
  // Just checking
  assert(node);
  assert(node->Tree() == this);
  assert(node->tree_index >= 0);
  assert(nodes.Kth(node->tree_index) == node);
  if ((max_extent <= 0) && (max_complexity <= 0)) return 0;
  int status = 0;
  
  // Make a temporary array of blocks
  RNArray<R3SurfelBlock *> splittable_blocks;
  for (int i = 0; i < node->NBlocks(); i++) {
    R3SurfelBlock *block = node->Block(i);
    splittable_blocks.Insert(block);
  }

  // Split blocks that are too big or have too much complexity
  while (!splittable_blocks.IsEmpty()) {
    R3SurfelBlock *block = splittable_blocks.Tail();
    splittable_blocks.RemoveTail();
    int dim = block->BBox().LongestAxis();
    if (((max_complexity > 0) && (block->NSurfels() > max_complexity)) || 
        ((max_extent > 0) && (block->BBox().AxisLength(dim) > max_extent))) {
      R3SurfelBlock *blockA, *blockB;
      R3Plane split(block->Centroid(), R3xyz_triad[dim]);
      R3SurfelHalfspaceConstraint constraint(R3Halfspace(split, 0));
      if (SplitBlock(node, block, constraint, &blockA, &blockB) && blockA && blockB) {
        splittable_blocks.Insert(blockA);
        splittable_blocks.Insert(blockB);
        status = 1;
      }
    }
  }

  // Return whether any blocks were split
  return status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %rbx
movq %rdi, %r14
xorpd %xmm2, %xmm2
movsd %xmm0, 0x18(%rsp)
ucomisd %xmm0, %xmm2
jb 0x3ae37
xorl %ebp, %ebp
ucomisd %xmm1, %xmm2
jae 0x3afc7
movsd %xmm1, 0x10(%rsp)
movq %rsp, %rdi
callq 0xa9ca8
cmpl $0x0, 0x4c(%rbx)
jle 0x3ae71
xorl %r12d, %r12d
movq %rsp, %r15
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rsi
movl 0xc(%rsp), %edx
movq %r15, %rdi
callq 0xa9d3e
incq %r12
movslq 0x4c(%rbx), %rax
cmpq %rax, %r12
jl 0x3ae51
movl 0xc(%rsp), %eax
xorl %ebp, %ebp
testl %eax, %eax
je 0x3afbf
leaq 0x50(%rsp), %r12
movq (%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r13
decl %esi
movq %rsp, %rdi
callq 0xa9ec8
movq %r13, %rdi
callq 0x2cb9e
movq %rax, %rdi
callq 0x6d916
movl %eax, %r15d
movsd 0x18(%rsp), %xmm0
ucomisd 0xa24cd(%rip), %xmm0    # 0xdd388
jbe 0x3aece
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%r13), %xmm0
ucomisd 0x18(%rsp), %xmm0
ja 0x3af05
movsd 0x10(%rsp), %xmm0
ucomisd 0xa24ac(%rip), %xmm0    # 0xdd388
jbe 0x3afb3
movq %r13, %rdi
callq 0x2cb9e
movslq %r15d, %rcx
movsd 0x20(%rax,%rcx,8), %xmm0
subsd 0x8(%rax,%rcx,8), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x3afb3
movq %r13, %rdi
callq 0x2cb9e
movq (%rax), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq *0x68(%rcx)
movslq %r15d, %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xf8085(%rip), %rdx     # 0x132fb0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x82942
leaq 0x78(%rsp), %r15
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x8288e
movq %r12, %rdi
movq %r15, %rsi
callq 0x3057a
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
leaq 0x28(%rsp), %r8
leaq 0x20(%rsp), %r9
callq *0x50(%rax)
testl %eax, %eax
je 0x3afab
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x3afab
cmpq $0x0, 0x20(%rsp)
je 0x3afab
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0xa9d3e
movq 0x20(%rsp), %rsi
movl $0x1, %ebp
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0xa9d3e
movq %r12, %rdi
callq 0x31dba
movl 0xc(%rsp), %eax
testl %eax, %eax
jne 0x3ae84
movq %rsp, %rdi
callq 0xa9d12
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3aff2
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x31dba
jmp 0x3aff5
jmp 0x3aff2
jmp 0x3aff2
jmp 0x3aff2
movq %rax, %rbx
movq %rsp, %rdi
callq 0xa9d12
movq %rbx, %rdi
callq 0x138b0
nop