// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelScene.cpp
// Function: gaps::R3SurfelScene::RemoveObject(gaps::R3SurfelObject*)
void R3SurfelScene::
RemoveObject(R3SurfelObject *object)
{
  // Just checking
  assert(object);
  assert(object->scene == this);
  assert(object->scene_index >= 0);
  assert(objects.Kth(object->scene_index) == object);

  // Remove object propertys from scene
  while (object->NObjectProperties() > 0) {
    R3SurfelObjectProperty *property = object->ObjectProperty(object->NObjectProperties()-1);
    RemoveObjectProperty(property);
  }

  // Remove object relationships from scene
  while (object->NObjectRelationships() > 0) {
    R3SurfelObjectRelationship *relationship = object->ObjectRelationship(object->NObjectRelationships()-1);
    RemoveObjectRelationship(relationship);
  }

  // Remove label assignments
  while (object->NLabelAssignments() > 0) {
    R3SurfelLabelAssignment *assignment = object->LabelAssignment(object->NLabelAssignments()-1);
    RemoveLabelAssignment(assignment);
  }

  // Update object
  object->UpdateBeforeRemove(this);

  // Remove object from parent
  if (object->parent) {
    object->parent->parts.Remove(object);
    object->parent = NULL;
  }

  // Remove object from parts
  for (int i = 0; i < object->NParts(); i++) {
    R3SurfelObject *part = object->Part(i);
    part->parent = NULL;
  }

  // Remove parts from object
  object->parts.Empty();

  // Remove object from scene
  RNArrayEntry *entry = objects.KthEntry(object->scene_index);
  R3SurfelObject *tail = objects.Tail();
  tail->scene_index = object->scene_index;
  objects.EntryContents(entry) = tail;
  objects.RemoveTail();
  object->scene_index = -1;
  object->scene = NULL;

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

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x78(%rsp), %rdi
callq 0x1ba20
cmpl $0x0, %eax
jle 0x1e5aa
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x78(%rsp), %rdi
callq 0x1ba20
movq 0x28(%rsp), %rdi
movl %eax, %esi
subl $0x1, %esi
callq 0x1ba40
movq 0x30(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rsi
movq (%rdi), %rax
callq *0x48(%rax)
jmp 0x1e561
jmp 0x1e5ac
movq 0x78(%rsp), %rdi
callq 0x1ba70
cmpl $0x0, %eax
jle 0x1e5f5
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rdi
callq 0x1ba70
movq 0x20(%rsp), %rdi
movl %eax, %esi
subl $0x1, %esi
callq 0x1ba90
movq 0x30(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
movq (%rdi), %rax
callq *0x68(%rax)
jmp 0x1e5ac
jmp 0x1e5f7
movq 0x78(%rsp), %rdi
callq 0x1bac0
cmpl $0x0, %eax
jle 0x1e643
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
callq 0x1bac0
movq 0x18(%rsp), %rdi
movl %eax, %esi
subl $0x1, %esi
callq 0x1bae0
movq 0x30(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rsi
movq (%rdi), %rax
callq *0x88(%rax)
jmp 0x1e5f7
movq 0x30(%rsp), %rsi
movq 0x78(%rsp), %rdi
callq 0x1b2d0
movq 0x78(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x1e682
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rdi
addq $0x20, %rdi
movq 0x78(%rsp), %rsi
callq 0x1be40
movq 0x78(%rsp), %rax
movq $0x0, 0x18(%rax)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x78(%rsp), %rdi
callq 0x1bb10
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x1e6d3
movq 0x78(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0x1bb30
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq $0x0, 0x18(%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1e68a
movq 0x78(%rsp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x142550
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movq 0x78(%rsp), %rax
movl 0x10(%rax), %esi
callq 0x1c080
movq 0x30(%rsp), %rdi
movq %rax, 0x48(%rsp)
addq $0x10, %rdi
callq 0x30ad0
movq 0x30(%rsp), %rdi
movq %rax, 0x40(%rsp)
movq 0x78(%rsp), %rax
movl 0x10(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
addq $0x10, %rdi
movq 0x48(%rsp), %rsi
callq 0x30ac0
movq (%rsp), %rcx
movq 0x30(%rsp), %rdi
movq %rcx, (%rax)
addq $0x10, %rdi
callq 0x1c0c0
movq 0x30(%rsp), %rax
movq 0x78(%rsp), %rcx
movl $0xffffffff, 0x10(%rcx) # imm = 0xFFFFFFFF
movq 0x78(%rsp), %rcx
movq $0x0, 0x8(%rcx)
addq $0x1e8, %rax            # imm = 0x1E8
movq %rax, 0x8(%rsp)
leaq 0x38(%rsp), %rdi
movl $0x1, %esi
callq 0x1b970
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x1c2d0
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)