// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movsd %xmm0, 0x80(%rsp)
movl %r9d, 0x18(%rsp)
movq %r8, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x27f4d
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movapd %xmm3, 0x30(%rsp)
testq %rsi, %rsi
jne 0x27ee4
movq 0x18(%rax), %rax
movq (%rax), %rbx
movq %rbx, %rdi
callq 0x33720
movupd 0x8(%rax), %xmm0
movq %rax, 0x78(%rsp)
movupd 0x20(%rax), %xmm2
subpd %xmm0, %xmm2
xorpd %xmm0, %xmm0
movapd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq %rbx, 0x68(%rsp)
jbe 0x27f64
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm2, %xmm0
divpd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
cvtdq2pd %xmm0, %xmm1
divpd %xmm1, %xmm2
movapd %xmm2, 0x20(%rsp)
movd %xmm0, 0x8(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, 0xc(%rsp)
jmp 0x27f75
xorl %ebp, %ebp
movq %rbp, %rax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movapd %xmm2, 0x20(%rsp)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
pushq $0x10
popq %rdi
callq 0x134f0
movq %rax, %rbp
movq %rax, %rdi
callq 0x9a7ae
cvtsi2sdl 0x280(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x8(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, 0x1c(%rsp)
movl 0xc(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
movapd %xmm1, 0xa0(%rsp)
leaq 0x140(%rsp), %rbx
xorl %eax, %eax
movq %r14, 0x48(%rsp)
movq %r12, 0x70(%rsp)
movapd %xmm0, 0x20(%rsp)
cmpl 0x10(%rsp), %eax
je 0x27f4f
cvtsi2sd %eax, %xmm2
movq %rax, 0x98(%rsp)
incl %eax
movl %eax, 0x14(%rsp)
cvtsi2sd %eax, %xmm3
xorl %r15d, %r15d
movapd 0xa0(%rsp), %xmm1
mulsd %xmm1, %xmm2
movsd %xmm2, 0x90(%rsp)
mulsd %xmm1, %xmm3
movsd %xmm3, 0x88(%rsp)
cmpl 0x1c(%rsp), %r15d
je 0x28259
movq %r14, %r12
leaq 0xc8(%rsp), %r13
movq %r13, %rdi
movq 0x78(%rsp), %r14
movq %r14, %rsi
callq 0x63ce6
cvtsi2sd %r15d, %xmm0
movapd 0x20(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd 0x8(%r14), %xmm0
leal 0x1(%r15), %eax
movl %eax, 0x30(%rsp)
cvtsi2sd %eax, %xmm1
movsd %xmm0, 0xd0(%rsp)
mulsd %xmm2, %xmm1
addsd 0x8(%r14), %xmm1
movsd %xmm1, 0xe8(%rsp)
movsd 0x10(%r14), %xmm0
addsd 0x90(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0x10(%r14), %xmm0
addsd 0x88(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
leaq 0xa0c17(%rip), %rdi     # 0xc8ce3
movq 0x98(%rsp), %rsi
movl 0xc(%rsp), %edx
movl %r15d, %ecx
movl 0x8(%rsp), %r8d
xorl %eax, %eax
callq 0x13070
leaq 0x100(%rsp), %rdi
movq %r13, %rsi
callq 0x2d5c8
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
callq 0x2ea82
movq %r13, %rdi
leaq 0x100(%rsp), %rsi
callq 0x2ea96
movq %r12, %r14
testq %r12, %r12
je 0x2812a
movq %r13, %rdi
movq %r14, %rsi
callq 0x2ea96
movq 0x70(%rsp), %r12
movq %r12, %rdi
movq 0x68(%rsp), %rsi
movq %r13, %rdx
movl 0x18(%rsp), %ecx
movsd 0x80(%rsp), %xmm0
callq 0x21634
movq %rax, %r15
testq %rax, %rax
je 0x2822b
movl 0x18(%r15), %esi
movsd 0x28(%r15), %xmm0
movsd 0x30(%r15), %xmm1
movsd 0x38(%r15), %xmm2
movsd 0x40(%r15), %xmm3
movsd 0x48(%r15), %xmm4
movsd 0x50(%r15), %xmm5
leaq 0xa0b6f(%rip), %rdi     # 0xc8cf6
movb $0x6, %al
callq 0x13070
movq %r12, %rdi
movq %r15, %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
movsd 0x50(%rsp), %xmm0
movsd 0xa01c4(%rip), %xmm1    # 0xc8370
pushq $0x19
popq %r8
callq 0x27677
movq %rax, %r12
testq %rax, %rax
je 0x28222
xorl %r13d, %r13d
movslq 0xc(%r12), %rax
cmpq %rax, %r13
jge 0x28202
movq (%r12), %rax
movq (%rax,%r13,8), %r14
movl 0xc(%rbp), %edx
movq %rbx, %rdi
leaq 0xa0b47(%rip), %rsi     # 0xc8d26
xorl %eax, %eax
callq 0x130e0
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x10(%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1784c
incq %r13
jmp 0x281c0
movq %r12, %rdi
callq 0x9a81c
movq %r12, %rdi
pushq $0x10
popq %rsi
callq 0x13500
movq 0x48(%rsp), %r14
leaq 0xb0(%rsp), %r13
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq %r13, %rdi
callq 0x28504
leaq 0x100(%rsp), %rdi
callq 0x284c2
leaq 0xc8(%rsp), %rdi
callq 0x649ae
movl 0x30(%rsp), %eax
movl %eax, %r15d
jmp 0x28032
movl 0x14(%rsp), %eax
jmp 0x27fec
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %rbp, %rdi
callq 0x13500
jmp 0x282ac
jmp 0x28282
movq %rax, %rbx
jmp 0x2829f
movq %rax, %rbx
jmp 0x28292
jmp 0x28282
jmp 0x28282
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x28504
leaq 0x100(%rsp), %rdi
callq 0x284c2
leaq 0xc8(%rsp), %rdi
callq 0x649ae
movq %rbx, %rdi
callq 0x138f0