// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::SplitObject(gaps::R3SurfelObject*, gaps::R3SurfelConstraint const*, gaps::R3SurfelObject**, gaps::R3SurfelObject**)
int
SplitObject(R3SurfelObject *object, const R3SurfelConstraint *constraint,
  R3SurfelObject **resultA, R3SurfelObject **resultB)
{
  // Get useful variables
  if (!object) return 0;
  if (!constraint) return 0;
  if (object->NNodes() == 0) return 0;
  assert(strcmp(object->Name(), "Root"));
  R3SurfelScene *scene = object->Scene();
  if (!scene) return 0;
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return 0;

  // Create constraint
  R3SurfelMultiConstraint multi_constraint;
  R3SurfelObjectConstraint object_constraint(object);
  multi_constraint.InsertConstraint(constraint);
  multi_constraint.InsertConstraint(&object_constraint);

  // Create array of nodes
  RNArray<R3SurfelNode *> nodes;
  for (int i = 0; i < object->NNodes(); i++) {
    R3SurfelNode *node = object->Node(i);
    nodes.Insert(node);
  }

  // Split all nodes
  RNArray<R3SurfelNode *> nodesA, nodesB;
  for (int i = 0; i < nodes.NEntries(); i++) {
    R3SurfelNode *node = nodes.Kth(i);
    tree->SplitLeafNodes(node, *constraint, &nodesA, &nodesB);
  }

  // Check nodesA
  if (nodesA.NEntries() == 0) {
    if (resultA) *resultA = NULL;
    if (resultB) *resultB = object; 
    return 0;
  }
    
  // Check nodesB
  if (nodesB.NEntries() == 0) {
    if (resultA) *resultA = object;
    if (resultB) *resultB = NULL; 
    return 0;
  }

  // Create new objects
  R3SurfelObject *objectA = new R3SurfelObject();
  R3SurfelObject *objectB = new R3SurfelObject();
  if (!objectA || !objectB) return 0;
      
  // Remove nodes from object
  while (object->NNodes() > 0) {
    R3SurfelNode *node = object->Node(0);
    object->RemoveNode(node);
  }
  
  // Insert nodes into objectA
  for (int j = 0; j < nodesA.NEntries(); j++) {
    R3SurfelNode *nodeA = nodesA.Kth(j);
    if (nodeA->Object()) continue;
    objectA->InsertNode(nodeA);
  }
  
  // Insert nodes into objectB
  for (int j = 0; j < nodesB.NEntries(); j++) {
    R3SurfelNode *nodeB = nodesB.Kth(j);
    if (nodeB->Object()) continue;
    objectB->InsertNode(nodeB);
  }
      
  // Copy result
  if (resultA) *resultA = objectA;
  if (resultB) *resultB = objectB;
  
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
xorl %ebp, %ebp
orb %al, %cl
jne 0x245f7
movq %rdi, %r15
cmpl $0x0, 0x6c(%rdi)
je 0x245f7
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x245f7
movq 0x8(%rax), %r13
testq %r13, %r13
je 0x245f7
movq %rdx, %r14
movq %rsi, %r12
leaq 0x48(%rsp), %rdi
callq 0x31f4c
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x31c0a
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x31f60
leaq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x31f60
movq %r14, 0x8(%rsp)
movq %rbx, 0x40(%rsp)
leaq 0x30(%rsp), %rdi
callq 0xaaf34
cmpl $0x0, 0x6c(%r15)
jle 0x24426
xorl %r14d, %r14d
leaq 0x30(%rsp), %rbx
movq 0x60(%r15), %rax
movq (%rax,%r14,8), %rsi
movl 0x3c(%rsp), %edx
movq %rbx, %rdi
callq 0xaafca
incq %r14
movslq 0x6c(%r15), %rax
cmpq %rax, %r14
jl 0x24406
leaq 0x20(%rsp), %rdi
callq 0xaaf34
leaq 0x10(%rsp), %rdi
callq 0xaaf34
cmpl $0x0, 0x3c(%rsp)
jle 0x24477
xorl %r14d, %r14d
leaq 0x20(%rsp), %rbp
leaq 0x10(%rsp), %rbx
movq 0x30(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq *0x30(%rax)
incq %r14
movslq 0x3c(%rsp), %rax
cmpq %rax, %r14
jl 0x2444e
cmpl $0x0, 0x2c(%rsp)
je 0x2456c
cmpl $0x0, 0x1c(%rsp)
movq 0x40(%rsp), %r14
je 0x24590
movl $0xf8, %edi
callq 0x134e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x16bb8
movl $0xf8, %edi
callq 0x134e0
movq %rax, %r13
movq %rax, %rdi
xorl %esi, %esi
callq 0x16bb8
cmpl $0x0, 0x6c(%r15)
jle 0x244d8
movq 0x60(%r15), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x16fc4
jmp 0x244c0
cmpl $0x0, 0x2c(%rsp)
jle 0x24514
xorl %r15d, %r15d
movq 0x20(%rsp), %rax
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x37a16
testq %rax, %rax
jne 0x24507
movq %r12, %rdi
movq %rbx, %rsi
callq 0x17e86
incq %r15
movslq 0x2c(%rsp), %rax
cmpq %rax, %r15
jl 0x244e2
cmpl $0x0, 0x1c(%rsp)
jle 0x24550
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x37a16
testq %rax, %rax
jne 0x24543
movq %r13, %rdi
movq %rbx, %rsi
callq 0x17e86
incq %r15
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jl 0x2451e
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x2455d
movq %r12, (%rax)
movl $0x1, %eax
movq %r13, %r15
movl $0x1, %ebp
jmp 0x24589
movq 0x8(%rsp), %rax
testq %rax, %rax
movq 0x40(%rsp), %r14
je 0x24582
movq $0x0, (%rax)
xorl %eax, %eax
movl $0x0, %ebp
testq %r14, %r14
jne 0x245a7
jmp 0x245ac
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x2459d
movq %r15, (%rax)
xorl %eax, %eax
testq %r14, %r14
je 0x24608
xorl %r15d, %r15d
movq %r15, (%r14)
movl %eax, %ebp
leaq 0x10(%rsp), %rdi
callq 0xaaf9e
leaq 0x20(%rsp), %rdi
callq 0xaaf9e
leaq 0x30(%rsp), %rdi
callq 0xaaf9e
leaq 0x60(%rsp), %rdi
callq 0x321ae
leaq 0x10b03d(%rip), %rax    # 0x12f618
addq $0x10, %rax
leaq 0x50(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xaaf9e
leaq 0x48(%rsp), %rdi
callq 0x321ae
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
jmp 0x245ac
movq %rax, %rbx
movl $0xf8, %esi
movq %r13, %rdi
jmp 0x24626
jmp 0x24648
movq %rax, %rbx
movl $0xf8, %esi
movq %r12, %rdi
callq 0x134f0
jmp 0x2464b
jmp 0x24648
movq %rax, %rbx
jmp 0x24655
jmp 0x24661
jmp 0x2463d
movq %rax, %rbx
jmp 0x24678
movq %rax, %rbx
jmp 0x2466e
jmp 0x24648
jmp 0x24648
jmp 0x24648
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xaaf9e
leaq 0x20(%rsp), %rdi
callq 0xaaf9e
jmp 0x24664
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xaaf9e
leaq 0x60(%rsp), %rdi
callq 0x321ae
leaq 0x10af99(%rip), %rax    # 0x12f618
addq $0x10, %rax
leaq 0x50(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0xaaf9e
leaq 0x48(%rsp), %rdi
callq 0x321ae
movq %rbx, %rdi
callq 0x138b0