// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateClusterObjects(gaps::R3SurfelScene*, gaps::R3SurfelNode*, gaps::R3SurfelConstraint const*, gaps::R3SurfelObject*, gaps::R3SurfelNode*, int, double, double, double, int, double)
RNArray<R3SurfelObject *> *
CreateClusterObjects(R3SurfelScene *scene, 
  R3SurfelNode *source_node, const R3SurfelConstraint *constraint,
  R3SurfelObject *parent_object, R3SurfelNode *parent_node, 
  int max_neighbors, RNLength max_neighbor_distance, 
  RNLength max_offplane_distance, RNAngle max_normal_angle,
  int min_points_per_object, 
  RNLength chunk_size)
{
  // Get convenient variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return NULL;
  if (!source_node) source_node = tree->RootNode();
  const R3Box& source_bbox = source_node->BBox();
  int nxchunks = 1;
  int nychunks = 1;
  RNLength xchunk = source_bbox.XLength();
  RNLength ychunk = source_bbox.YLength();
  if (chunk_size > 0) {
    nxchunks = (int) (source_bbox.XLength() / chunk_size) + 1;
    nychunks = (int) (source_bbox.YLength() / chunk_size) + 1;
    xchunk = source_bbox.XLength() / nxchunks;
    ychunk = source_bbox.YLength() / nychunks;
  }

  // Create array of objects
  RNArray<R3SurfelObject *> *objects = new RNArray<R3SurfelObject *>();
  if (!objects) {
    RNFail("Unable to allocate array of objects\n");
    return NULL;
  }

  // Fill array of objects chunk-by-chunk
  for (int j = 0; j < nychunks; j++) {
    for (int i = 0; i < nxchunks; i++) {
      // Compute chunk bounding box
      R3Box chunk_bbox = source_bbox;
      chunk_bbox[0][0] = source_bbox.XMin() + i     * xchunk;
      chunk_bbox[1][0] = source_bbox.XMin() + (i+1) * xchunk;
      chunk_bbox[0][1] = source_bbox.YMin() + j     * ychunk;
      chunk_bbox[1][1] = source_bbox.YMin() + (j+1) * ychunk;

      // Print debug message
      printf("  %6d/%6d %6d/%6d\n",j, nychunks, i, nxchunks);

      // Create graph
      R3SurfelBoxConstraint box_constraint(chunk_bbox);
      R3SurfelMultiConstraint multi_constraint;
      multi_constraint.InsertConstraint(&box_constraint);
      if (constraint) multi_constraint.InsertConstraint(constraint);
      R3SurfelPointGraph *graph = CreatePointGraph(scene, 
        source_node, &multi_constraint, 
        max_neighbors, max_neighbor_distance);
      if (!graph) continue;

      printf("    %9d : %9.3f %9.3f %9.3f  %9.3f %9.3f %9.3f\n",  graph->NPoints(),
            graph->BBox().XMin(), graph->BBox().YMin(), graph->BBox().ZMin(), 
            graph->BBox().XMax(), graph->BBox().YMax(), graph->BBox().ZMax());

      // Create objects
      RNArray<R3SurfelObject *> *chunk_objects = CreateClusterObjects(scene, graph, parent_object, parent_node, min_points_per_object);
      if (!chunk_objects) { delete graph; continue; }

      // Insert into result
      for (int k = 0; k < chunk_objects->NEntries(); k++) {
        R3SurfelObject *object = chunk_objects->Kth(k);
        char object_name[256];
        sprintf(object_name, "ClusterObject%d\n", objects->NEntries());
        object->SetName(object_name);
        objects->Insert(object);
      }

      // Delete array of chunk objects
      delete chunk_objects;

      // Delete graph
      delete graph;
    }
  }

  // Return objects
  return objects;
}

// Assembly:
subq $0x398, %rsp            # imm = 0x398
movl 0x3a0(%rsp), %eax
movq %rdi, 0x388(%rsp)
movq %rsi, 0x380(%rsp)
movq %rdx, 0x378(%rsp)
movq %rcx, 0x370(%rsp)
movq %r8, 0x368(%rsp)
movl %r9d, 0x364(%rsp)
movsd %xmm0, 0x358(%rsp)
movsd %xmm1, 0x350(%rsp)
movsd %xmm2, 0x348(%rsp)
movsd %xmm3, 0x340(%rsp)
movq 0x388(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x338(%rsp)
cmpq $0x0, 0x338(%rsp)
jne 0x425b3
movq $0x0, 0x390(%rsp)
jmp 0x42ed2
cmpq $0x0, 0x380(%rsp)
jne 0x425d3
movq 0x338(%rsp), %rdi
callq 0x31030
movq %rax, 0x380(%rsp)
movq 0x380(%rsp), %rdi
callq 0x5ee30
movq %rax, 0x330(%rsp)
movl $0x1, 0x32c(%rsp)
movl $0x1, 0x328(%rsp)
movq 0x330(%rsp), %rdi
callq 0x44130
movsd %xmm0, 0x320(%rsp)
movq 0x330(%rsp), %rdi
callq 0x44150
movsd %xmm0, 0x318(%rsp)
movsd 0x340(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x426ce
movq 0x330(%rsp), %rdi
callq 0x44130
divsd 0x340(%rsp), %xmm0
cvttsd2si %xmm0, %eax
addl $0x1, %eax
movl %eax, 0x32c(%rsp)
movq 0x330(%rsp), %rdi
callq 0x44150
divsd 0x340(%rsp), %xmm0
cvttsd2si %xmm0, %eax
addl $0x1, %eax
movl %eax, 0x328(%rsp)
movq 0x330(%rsp), %rdi
callq 0x44130
cvtsi2sdl 0x32c(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x320(%rsp)
movq 0x330(%rsp), %rdi
callq 0x44150
cvtsi2sdl 0x328(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x318(%rsp)
movl $0x10, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x138(%rsp)
callq 0x1b920
jmp 0x426ed
movq 0x138(%rsp), %rax
movq %rax, 0x310(%rsp)
cmpq $0x0, 0x310(%rsp)
jne 0x42752
leaq 0x16264b(%rip), %rdi    # 0x1a4d5a
movb $0x0, %al
callq 0x1464b0
movq $0x0, 0x390(%rsp)
jmp 0x42ed2
movq 0x138(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x308(%rsp)
movl %eax, 0x304(%rsp)
movl $0x10, %esi
callq 0x13580
jmp 0x42ee2
movl $0x0, 0x300(%rsp)
movl 0x300(%rsp), %eax
cmpl 0x328(%rsp), %eax
jge 0x42ec2
movl $0x0, 0x2fc(%rsp)
movl 0x2fc(%rsp), %eax
cmpl 0x32c(%rsp), %eax
jge 0x42eaa
movq 0x330(%rsp), %rsi
leaq 0x2c0(%rsp), %rdi
callq 0xc9de0
movq 0x330(%rsp), %rdi
callq 0x446e0
movsd %xmm0, 0x130(%rsp)
jmp 0x427bd
movsd 0x130(%rsp), %xmm1
cvtsi2sdl 0x2fc(%rsp), %xmm0
movsd 0x320(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x120(%rsp)
leaq 0x2c0(%rsp), %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, 0x128(%rsp)
jmp 0x42802
movq 0x128(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0x118(%rsp)
jmp 0x4281b
movq 0x118(%rsp), %rax
movsd 0x120(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x330(%rsp), %rdi
callq 0x446e0
movsd %xmm0, 0x110(%rsp)
jmp 0x42848
movsd 0x110(%rsp), %xmm1
movl 0x2fc(%rsp), %eax
incl %eax
cvtsi2sd %eax, %xmm0
movsd 0x320(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
leaq 0x2c0(%rsp), %rdi
movl $0x1, %esi
callq 0x1bda0
movq %rax, 0x108(%rsp)
jmp 0x42894
movq 0x108(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0xf8(%rsp)
jmp 0x428ad
movq 0xf8(%rsp), %rax
movsd 0x100(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x330(%rsp), %rdi
callq 0x44700
movsd %xmm0, 0xf0(%rsp)
jmp 0x428da
movsd 0xf0(%rsp), %xmm1
cvtsi2sdl 0x300(%rsp), %xmm0
movsd 0x318(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xe0(%rsp)
leaq 0x2c0(%rsp), %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, 0xe8(%rsp)
jmp 0x4291f
movq 0xe8(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0xd8(%rsp)
jmp 0x4293b
movq 0xd8(%rsp), %rax
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x330(%rsp), %rdi
callq 0x44700
movsd %xmm0, 0xd0(%rsp)
jmp 0x42968
movsd 0xd0(%rsp), %xmm1
movl 0x300(%rsp), %eax
incl %eax
cvtsi2sd %eax, %xmm0
movsd 0x318(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
leaq 0x2c0(%rsp), %rdi
movl $0x1, %esi
callq 0x1bda0
movq %rax, 0xc8(%rsp)
jmp 0x429b4
movq 0xc8(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0xb8(%rsp)
jmp 0x429d0
movq 0xb8(%rsp), %rax
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x300(%rsp), %esi
movl 0x328(%rsp), %edx
movl 0x2fc(%rsp), %ecx
movl 0x32c(%rsp), %r8d
leaq 0x162410(%rip), %rdi    # 0x1a4e19
xorl %eax, %eax
callq 0x13070
jmp 0x42a12
leaq 0x280(%rsp), %rdi
leaq 0x2c0(%rsp), %rsi
callq 0x50d10
jmp 0x42a29
leaq 0x268(%rsp), %rdi
callq 0x53f90
jmp 0x42a38
leaq 0x268(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x53ff0
jmp 0x42a4f
cmpq $0x0, 0x378(%rsp)
je 0x42acb
movq 0x378(%rsp), %rsi
leaq 0x268(%rsp), %rdi
callq 0x53ff0
jmp 0x42a71
jmp 0x42acb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x308(%rsp)
movl %eax, 0x304(%rsp)
jmp 0x42e9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x308(%rsp)
movl %eax, 0x304(%rsp)
jmp 0x42e8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x308(%rsp)
movl %eax, 0x304(%rsp)
leaq 0x268(%rsp), %rdi
callq 0x43a50
jmp 0x42e8e
movq 0x388(%rsp), %rdi
movq 0x380(%rsp), %rsi
movl 0x364(%rsp), %ecx
movsd 0x358(%rsp), %xmm0
leaq 0x268(%rsp), %rdx
callq 0x33910
movq %rax, 0xb0(%rsp)
jmp 0x42b02
movq 0xb0(%rsp), %rax
movq %rax, 0x260(%rsp)
cmpq $0x0, 0x260(%rsp)
jne 0x42b2d
movl $0x7, 0x25c(%rsp)
jmp 0x42e40
movq 0x260(%rsp), %rdi
callq 0x43e20
movl %eax, 0xac(%rsp)
jmp 0x42b43
movq 0x260(%rsp), %rdi
callq 0x44410
movq %rax, 0xa0(%rsp)
jmp 0x42b5a
movq 0xa0(%rsp), %rdi
callq 0x446e0
movsd %xmm0, 0x98(%rsp)
jmp 0x42b72
movq 0x260(%rsp), %rdi
callq 0x44410
movq %rax, 0x90(%rsp)
jmp 0x42b89
movq 0x90(%rsp), %rdi
callq 0x44700
movsd %xmm0, 0x88(%rsp)
jmp 0x42ba1
movq 0x260(%rsp), %rdi
callq 0x44410
movq %rax, 0x80(%rsp)
jmp 0x42bb8
movq 0x80(%rsp), %rdi
callq 0x435e0
movsd %xmm0, 0x78(%rsp)
jmp 0x42bcd
movq 0x260(%rsp), %rdi
callq 0x44410
movq %rax, 0x70(%rsp)
jmp 0x42be1
movq 0x70(%rsp), %rdi
callq 0x44e00
movsd %xmm0, 0x68(%rsp)
jmp 0x42bf3
movq 0x260(%rsp), %rdi
callq 0x44410
movq %rax, 0x60(%rsp)
jmp 0x42c07
movq 0x60(%rsp), %rdi
callq 0x44e20
movsd %xmm0, 0x58(%rsp)
jmp 0x42c19
movq 0x260(%rsp), %rdi
callq 0x44410
movq %rax, 0x50(%rsp)
jmp 0x42c2d
movq 0x50(%rsp), %rdi
callq 0x44e40
movsd %xmm0, 0x48(%rsp)
jmp 0x42c3f
movsd 0x48(%rsp), %xmm5
movsd 0x58(%rsp), %xmm4
movsd 0x68(%rsp), %xmm3
movsd 0x78(%rsp), %xmm2
movsd 0x88(%rsp), %xmm1
movsd 0x98(%rsp), %xmm0
movl 0xac(%rsp), %esi
leaq 0x1621b5(%rip), %rdi    # 0x1a4e2c
movb $0x6, %al
callq 0x13070
jmp 0x42c80
movq 0x388(%rsp), %rdi
movq 0x260(%rsp), %rsi
movq 0x370(%rsp), %rdx
movq 0x368(%rsp), %rcx
cvtsi2sdl 0x3a0(%rsp), %xmm0
movsd 0x1616e7(%rip), %xmm1   # 0x1a4398
movl $0x19, %r8d
callq 0x409f0
movq %rax, 0x40(%rsp)
jmp 0x42cc3
movq 0x40(%rsp), %rax
movq %rax, 0x250(%rsp)
cmpq $0x0, 0x250(%rsp)
jne 0x42d09
movq 0x260(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x42cf9
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x7, 0x25c(%rsp)
jmp 0x42e40
movl $0x0, 0x24c(%rsp)
movl 0x24c(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x250(%rsp), %rdi
callq 0x16d50
movl %eax, 0x34(%rsp)
jmp 0x42d32
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x42deb
movq 0x250(%rsp), %rdi
movl 0x24c(%rsp), %esi
callq 0x18840
movq %rax, 0x28(%rsp)
jmp 0x42d5d
movq 0x28(%rsp), %rax
movq %rax, 0x240(%rsp)
leaq 0x140(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x310(%rsp), %rdi
callq 0x16d50
movl %eax, 0x24(%rsp)
jmp 0x42d8a
movl 0x24(%rsp), %edx
movq 0x18(%rsp), %rdi
leaq 0x1620c2(%rip), %rsi    # 0x1a4e5c
xorl %eax, %eax
callq 0x130f0
movq 0x240(%rsp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x140(%rsp), %rsi
callq *%rax
jmp 0x42dbc
movq 0x310(%rsp), %rdi
movq 0x240(%rsp), %rsi
callq 0x1be70
jmp 0x42dd3
jmp 0x42dd5
movl 0x24c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24c(%rsp)
jmp 0x42d14
movq 0x250(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x42e17
movq 0x10(%rsp), %rdi
callq 0x1b9e0
movq 0x10(%rsp), %rdi
movl $0x10, %esi
callq 0x13580
movq 0x260(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x42e35
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x0, 0x25c(%rsp)
leaq 0x268(%rsp), %rdi
callq 0x43a50
leaq 0x280(%rsp), %rdi
callq 0x43740
leaq 0x2c0(%rsp), %rdi
callq 0x1b990
movl 0x25c(%rsp), %eax
testl %eax, %eax
je 0x42e76
jmp 0x42e74
jmp 0x42e78
jmp 0x42e78
movl 0x2fc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2fc(%rsp)
jmp 0x4277c
leaq 0x280(%rsp), %rdi
callq 0x43740
leaq 0x2c0(%rsp), %rdi
callq 0x1b990
jmp 0x42ee2
jmp 0x42eac
movl 0x300(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x300(%rsp)
jmp 0x4275d
movq 0x310(%rsp), %rax
movq %rax, 0x390(%rsp)
movq 0x390(%rsp), %rax
addq $0x398, %rsp            # imm = 0x398
retq
movq 0x308(%rsp), %rdi
callq 0x13970
nop