// File: /workspace/github_cpp_makefile_star2_O1/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 0x3b1d3
xorl %ebp, %ebp
ucomisd %xmm1, %xmm2
jae 0x3b363
movsd %xmm1, 0x10(%rsp)
movq %rsp, %rdi
callq 0xaaf34
cmpl $0x0, 0x4c(%rbx)
jle 0x3b20d
xorl %r12d, %r12d
movq %rsp, %r15
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rsi
movl 0xc(%rsp), %edx
movq %r15, %rdi
callq 0xaafca
incq %r12
movslq 0x4c(%rbx), %rax
cmpq %rax, %r12
jl 0x3b1ed
movl 0xc(%rsp), %eax
xorl %ebp, %ebp
testl %eax, %eax
je 0x3b35b
leaq 0x50(%rsp), %r12
movq (%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r13
decl %esi
movq %rsp, %rdi
callq 0xab13e
movq %r13, %rdi
callq 0x2cfc6
movq %rax, %rdi
callq 0x6e576
movl %eax, %r15d
movsd 0x18(%rsp), %xmm0
ucomisd 0xa3131(%rip), %xmm0    # 0xde388
jbe 0x3b26a
xorps %xmm0, %xmm0
cvtsi2sdl 0x8(%r13), %xmm0
ucomisd 0x18(%rsp), %xmm0
ja 0x3b2a1
movsd 0x10(%rsp), %xmm0
ucomisd 0xa3110(%rip), %xmm0    # 0xde388
jbe 0x3b34f
movq %r13, %rdi
callq 0x2cfc6
movslq %r15d, %rcx
movsd 0x20(%rax,%rcx,8), %xmm0
subsd 0x8(%rax,%rcx,8), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x3b34f
movq %r13, %rdi
callq 0x2cfc6
movq (%rax), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq *0x68(%rcx)
movslq %r15d, %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0xf8ce9(%rip), %rdx     # 0x133fb0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x838fe
leaq 0x78(%rsp), %r15
movq %r15, %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
callq 0x8384a
movq %r12, %rdi
movq %r15, %rsi
callq 0x3096e
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 0x3b347
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x3b347
cmpq $0x0, 0x20(%rsp)
je 0x3b347
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0xaafca
movq 0x20(%rsp), %rsi
movl $0x1, %ebp
movl 0xc(%rsp), %edx
movq %rsp, %rdi
callq 0xaafca
movq %r12, %rdi
callq 0x321ae
movl 0xc(%rsp), %eax
testl %eax, %eax
jne 0x3b220
movq %rsp, %rdi
callq 0xaaf9e
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3b38e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x321ae
jmp 0x3b391
jmp 0x3b38e
jmp 0x3b38e
jmp 0x3b38e
movq %rax, %rbx
movq %rsp, %rdi
callq 0xaaf9e
movq %rbx, %rdi
callq 0x138b0
nop