// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelScene.cpp
// Function: gaps::R3SurfelScene::MergeObject(gaps::R3SurfelObject*, gaps::R3SurfelObject*)
void R3SurfelScene::
MergeObject(R3SurfelObject *dst_object, R3SurfelObject *src_object)
{
  // Just checking
  assert(dst_object && src_object);
  assert(dst_object->scene == this);
  assert(dst_object->scene_index >= 0);
  assert(src_object->scene == this);
  assert(src_object->scene_index >= 0);

  // Move all nodes from src_object into dst_object
  while (src_object->NNodes() > 0) {
    R3SurfelNode *node = src_object->Node(src_object->NNodes()-1);
    src_object->RemoveNode(node);
    dst_object->InsertNode(node);
  }

  // Move all parts from src_object into dst_object
  while (src_object->NParts() > 0) {
    R3SurfelObject *part = src_object->Part(src_object->NParts()-1);
    RemoveObject(part);
    InsertObject(part, dst_object);
  }

  // Move all object properties from src_object into dst_object
  while (src_object->NObjectProperties() > 0) {
    R3SurfelObjectProperty *property = src_object->ObjectProperty(src_object->NObjectProperties()-1);
    src_object->UpdateBeforeRemoveObjectProperty(property);
    property->object = dst_object;
    dst_object->UpdateAfterInsertObjectProperty(property);
  }

  // Move all object relationships from src_object into dst_object
  while (src_object->NObjectRelationships() > 0) {
    R3SurfelObjectRelationship *relationship = src_object->ObjectRelationship(src_object->NObjectRelationships()-1);
    src_object->UpdateBeforeRemoveObjectRelationship(relationship);
    for (int i = 0; i < relationship->objects.NEntries(); i++) {
      if (relationship->objects.Kth(i) == src_object) {
        RNArrayEntry *entry = relationship->objects.KthEntry(i);
        relationship->objects.EntryContents(entry) = dst_object;
        break;
      }
    }
    dst_object->UpdateAfterInsertObjectRelationship(relationship);
  }

  // Move all label assignments from src_object into dst_object
  while (src_object->NLabelAssignments() > 0) {
    R3SurfelLabelAssignment *assignment = src_object->LabelAssignment(src_object->NLabelAssignments()-1);
    src_object->UpdateBeforeRemoveLabelAssignment(assignment);
    assignment->object = dst_object;
    dst_object->UpdateAfterInsertLabelAssignment(assignment);
  }

  // Remove src_object
  RemoveObject(src_object);

  // Delete src_object
  delete src_object;

  // Mark scene as dirty
  flags.Add(R3_SURFEL_SCENE_DIRTY_FLAG);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movslq 0x6c(%r14), %rax
testq %rax, %rax
jle 0x18604
movq 0x60(%r14), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1677a
movq %r15, %rdi
movq %r12, %rsi
callq 0x17274
jmp 0x185b6
movq 0x20(%r14), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x20(%rax)
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq *0x18(%rax)
movslq 0x2c(%r14), %rax
testq %rax, %rax
jg 0x185e0
jmp 0x18633
movq 0x30(%r14), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x175e0
movq %r15, 0x18(%r12)
movq %r15, %rdi
movq %r12, %rsi
callq 0x175d6
movslq 0x3c(%r14), %rax
testq %rax, %rax
jg 0x1860f
xorl %r13d, %r13d
movslq 0x4c(%r14), %rax
testq %rax, %rax
jle 0x186b6
movq 0x40(%r14), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x175f4
movl 0x24(%r12), %eax
testl %eax, %eax
cmovlel %r13d, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x18685
movq 0x18(%r12), %rdx
leaq 0x1(%rcx), %rsi
cmpq %r14, (%rdx,%rcx,8)
movq %rsi, %rcx
jne 0x18669
movq %r15, -0x8(%rdx,%rsi,8)
movq %r15, %rdi
movq %r12, %rsi
callq 0x175ea
jmp 0x1863f
movq 0x50(%r14), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1760e
movq %r15, 0x18(%r12)
movq %r15, %rdi
movq %r12, %rsi
callq 0x175fe
movslq 0x5c(%r14), %rax
testq %rax, %rax
jg 0x18692
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x20(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
orb $0x1, 0x1e8(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop