// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movq %rsi, %rbx
xorpd %xmm2, %xmm2
movsd %xmm0, 0x10(%rsp)
ucomisd %xmm0, %xmm2
jb 0x369c2
xorl %eax, %eax
ucomisd %xmm1, %xmm2
jae 0x36b3d
movq %rdi, 0x30(%rsp)
movsd %xmm1, 0x8(%rsp)
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x9a7ae
xorl %r12d, %r12d
movslq 0x4c(%rbx), %rax
cmpq %rax, %r12
jge 0x369fb
movq 0x40(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %r15, %rdi
callq 0x1784c
incq %r12
jmp 0x369dd
movl $0x0, 0x4(%rsp)
leaq 0x20(%rsp), %r15
movslq 0x2c(%rsp), %rax
testq %rax, %rax
je 0x36b2f
movq 0x20(%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r13
movq %r15, %rdi
callq 0x17820
movq %r13, %rdi
callq 0x2a720
movq %rax, %rdi
callq 0x6426e
movsd 0x10(%rsp), %xmm0
ucomisd 0x9193a(%rip), %xmm0    # 0xc8380
movslq %eax, %r12
jbe 0x36a59
cvtsi2sdl 0x8(%r13), %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x36a87
movsd 0x8(%rsp), %xmm0
ucomisd 0x91919(%rip), %xmm0    # 0xc8380
jbe 0x36a08
movq %r13, %rdi
callq 0x2a720
movsd 0x20(%rax,%r12,8), %xmm0
subsd 0x8(%rax,%r12,8), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x36a08
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x36c66
leaq 0x88(%rsp), %rbp
imulq $0x18, %r12, %rdx
addq 0xed50e(%rip), %rdx     # 0x123fb8
movq %rbp, %rdi
movq %r14, %rsi
callq 0x76e1c
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x76d6c
movq %r14, %rdi
movq %r12, %rsi
callq 0x2d82c
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
leaq 0x38(%rsp), %r8
leaq 0x18(%rsp), %r9
callq *0x50(%rax)
testl %eax, %eax
je 0x36b22
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
je 0x36b22
cmpq $0x0, 0x18(%rsp)
je 0x36b22
movq %r15, %rdi
callq 0x1784c
movq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x1784c
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
callq 0x2ec74
jmp 0x36a08
leaq 0x20(%rsp), %rdi
callq 0x9a81c
movl 0x4(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2ec74
jmp 0x36b69
jmp 0x36b66
jmp 0x36b66
jmp 0x36b66
jmp 0x36b66
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x9a81c
movq %rbx, %rdi
callq 0x138f0
nop