// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreatePlanarGrids(gaps::R3SurfelScene*, gaps::R3SurfelNode*, gaps::R3SurfelConstraint const*, int, double, double, double, double, double, int, double, double, double)
RNArray<R3PlanarGrid *> *
CreatePlanarGrids(R3SurfelScene *scene, 
  R3SurfelNode *source_node, const R3SurfelConstraint *constraint,
  int max_neighbors, RNLength max_neighbor_distance, 
  RNLength max_offplane_distance, RNAngle max_normal_angle,
  RNArea min_area, RNScalar min_density, int min_points,
  RNLength grid_spacing, RNScalar accuracy_factor,
  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 planar grids
  RNArray<R3PlanarGrid *> *grids = new RNArray<R3PlanarGrid *>();
  if (!grids) {
    RNFail("Unable to allocate array of grids\n");
    return NULL;
  }

  // Fill array of planar grids 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;

      // 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;

      // Create planar grids
      RNArray<R3PlanarGrid *> *chunk_grids = CreatePlanarGrids(graph, 
        max_offplane_distance, max_normal_angle, 
        min_area, min_density, min_points, 
        grid_spacing, accuracy_factor);
      if (!chunk_grids) { delete graph; continue; }

      // Insert grids for chunk
      for (int k = 0; k < chunk_grids->NEntries(); k++) {
        R3PlanarGrid *grid = chunk_grids->Kth(k);
        printf("  %6d/%6d %6d/%6d %6d/%6d : %9.3f %9.3f\n", j, nychunks, i, nxchunks, k, chunk_grids->NEntries(), grid->L1Norm(), grid->Area());
        grids->Insert(grid);
      }

      // Delete array of grids for chunk
      delete chunk_grids;

      // Delete graph
      delete graph;
    }
  }

  // Return planar grids
  return grids;
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x258(%rsp)
movq %rsi, 0x250(%rsp)
movq %rdx, 0x248(%rsp)
movl %ecx, 0x244(%rsp)
movsd %xmm0, 0x238(%rsp)
movsd %xmm1, 0x230(%rsp)
movsd %xmm2, 0x228(%rsp)
movsd %xmm3, 0x220(%rsp)
movsd %xmm4, 0x218(%rsp)
movl %r8d, 0x214(%rsp)
movsd %xmm5, 0x208(%rsp)
movsd %xmm6, 0x200(%rsp)
movsd %xmm7, 0x1f8(%rsp)
movq 0x258(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x1f0(%rsp)
cmpq $0x0, 0x1f0(%rsp)
jne 0x3f777
movq $0x0, 0x260(%rsp)
jmp 0x3ff6f
cmpq $0x0, 0x250(%rsp)
jne 0x3f797
movq 0x1f0(%rsp), %rdi
callq 0x31030
movq %rax, 0x250(%rsp)
movq 0x250(%rsp), %rdi
callq 0x5ee30
movq %rax, 0x1e8(%rsp)
movl $0x1, 0x1e4(%rsp)
movl $0x1, 0x1e0(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x44130
movsd %xmm0, 0x1d8(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x44150
movsd %xmm0, 0x1d0(%rsp)
movsd 0x1f8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3f892
movq 0x1e8(%rsp), %rdi
callq 0x44130
divsd 0x1f8(%rsp), %xmm0
cvttsd2si %xmm0, %eax
addl $0x1, %eax
movl %eax, 0x1e4(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x44150
divsd 0x1f8(%rsp), %xmm0
cvttsd2si %xmm0, %eax
addl $0x1, %eax
movl %eax, 0x1e0(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x44130
cvtsi2sdl 0x1e4(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x1d8(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x44150
cvtsi2sdl 0x1e0(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x1d0(%rsp)
movl $0x10, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0xf0(%rsp)
callq 0x44510
jmp 0x3f8b1
movq 0xf0(%rsp), %rax
movq %rax, 0x1c8(%rsp)
cmpq $0x0, 0x1c8(%rsp)
jne 0x3f916
leaq 0x16543b(%rip), %rdi    # 0x1a4d0e
movb $0x0, %al
callq 0x1464b0
movq $0x0, 0x260(%rsp)
jmp 0x3ff6f
movq 0xf0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x10, %esi
callq 0x13580
jmp 0x3ff7f
movl $0x0, 0x1b8(%rsp)
movl 0x1b8(%rsp), %eax
cmpl 0x1e0(%rsp), %eax
jge 0x3ff5f
movl $0x0, 0x1b4(%rsp)
movl 0x1b4(%rsp), %eax
cmpl 0x1e4(%rsp), %eax
jge 0x3ff47
movq 0x1e8(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0xc9de0
movq 0x1e8(%rsp), %rdi
callq 0x446e0
movsd %xmm0, 0xe8(%rsp)
jmp 0x3f981
movsd 0xe8(%rsp), %xmm1
cvtsi2sdl 0x1b4(%rsp), %xmm0
movsd 0x1d8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
leaq 0x178(%rsp), %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, 0xe0(%rsp)
jmp 0x3f9c6
movq 0xe0(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0xd0(%rsp)
jmp 0x3f9df
movq 0xd0(%rsp), %rax
movsd 0xd8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rdi
callq 0x446e0
movsd %xmm0, 0xc8(%rsp)
jmp 0x3fa0c
movsd 0xc8(%rsp), %xmm1
movl 0x1b4(%rsp), %eax
incl %eax
cvtsi2sd %eax, %xmm0
movsd 0x1d8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
leaq 0x178(%rsp), %rdi
movl $0x1, %esi
callq 0x1bda0
movq %rax, 0xc0(%rsp)
jmp 0x3fa58
movq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movq %rax, 0xb0(%rsp)
jmp 0x3fa71
movq 0xb0(%rsp), %rax
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rdi
callq 0x44700
movsd %xmm0, 0xa8(%rsp)
jmp 0x3fa9e
movsd 0xa8(%rsp), %xmm1
cvtsi2sdl 0x1b8(%rsp), %xmm0
movsd 0x1d0(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0x178(%rsp), %rdi
xorl %esi, %esi
callq 0x1bda0
movq %rax, 0xa0(%rsp)
jmp 0x3fae3
movq 0xa0(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0x90(%rsp)
jmp 0x3faff
movq 0x90(%rsp), %rax
movsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rdi
callq 0x44700
movsd %xmm0, 0x88(%rsp)
jmp 0x3fb2c
movsd 0x88(%rsp), %xmm1
movl 0x1b8(%rsp), %eax
incl %eax
cvtsi2sd %eax, %xmm0
movsd 0x1d0(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
leaq 0x178(%rsp), %rdi
movl $0x1, %esi
callq 0x1bda0
movq %rax, 0x80(%rsp)
jmp 0x3fb75
movq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movq %rax, 0x70(%rsp)
jmp 0x3fb8e
movq 0x70(%rsp), %rax
movsd 0x78(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x138(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x50d10
jmp 0x3fbb4
leaq 0x120(%rsp), %rdi
callq 0x53f90
jmp 0x3fbc3
leaq 0x120(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x53ff0
jmp 0x3fbda
cmpq $0x0, 0x248(%rsp)
je 0x3fc56
movq 0x248(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x53ff0
jmp 0x3fbfc
jmp 0x3fc56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x3ff38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x3ff2b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x43a50
jmp 0x3ff2b
movq 0x258(%rsp), %rdi
movq 0x250(%rsp), %rsi
movl 0x244(%rsp), %ecx
movsd 0x238(%rsp), %xmm0
leaq 0x120(%rsp), %rdx
callq 0x33910
movq %rax, 0x68(%rsp)
jmp 0x3fc8a
movq 0x68(%rsp), %rax
movq %rax, 0x118(%rsp)
cmpq $0x0, 0x118(%rsp)
jne 0x3fcb2
movl $0x7, 0x114(%rsp)
jmp 0x3fedd
movq 0x118(%rsp), %rdi
movsd 0x230(%rsp), %xmm0
movsd 0x228(%rsp), %xmm1
movsd 0x220(%rsp), %xmm2
movsd 0x218(%rsp), %xmm3
movl 0x214(%rsp), %esi
movsd 0x208(%rsp), %xmm4
movsd 0x200(%rsp), %xmm5
callq 0x3e120
movq %rax, 0x60(%rsp)
jmp 0x3fd03
movq 0x60(%rsp), %rax
movq %rax, 0x108(%rsp)
cmpq $0x0, 0x108(%rsp)
jne 0x3fd49
movq 0x118(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x3fd39
movq 0x58(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x7, 0x114(%rsp)
jmp 0x3fedd
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x108(%rsp), %rdi
callq 0x16d50
movl %eax, 0x54(%rsp)
jmp 0x3fd72
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3fe88
movq 0x108(%rsp), %rdi
movl 0x104(%rsp), %esi
callq 0x44720
movq %rax, 0x48(%rsp)
jmp 0x3fd9d
movq 0x48(%rsp), %rax
movq %rax, 0xf8(%rsp)
movl 0x1b8(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x1e0(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x1b4(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x1e4(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0x108(%rsp), %rdi
callq 0x16d50
movl %eax, 0x44(%rsp)
jmp 0x3fdf4
movq 0xf8(%rsp), %rdi
callq 0x44740
movsd %xmm0, 0x28(%rsp)
jmp 0x3fe09
movq 0xf8(%rsp), %rdi
callq 0x44760
movsd %xmm0, 0x20(%rsp)
jmp 0x3fe1e
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rsp), %xmm0
movl 0x40(%rsp), %r9d
movl 0x3c(%rsp), %r8d
movl 0x38(%rsp), %ecx
movl 0x34(%rsp), %edx
movl 0x30(%rsp), %esi
movl 0x44(%rsp), %edi
movq %rsp, %rax
movl %edi, (%rax)
leaq 0x164ee1(%rip), %rdi    # 0x1a4d31
movb $0x2, %al
callq 0x13070
jmp 0x3fe59
movq 0x1c8(%rsp), %rdi
movq 0xf8(%rsp), %rsi
callq 0x446b0
jmp 0x3fe70
jmp 0x3fe72
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x3fd54
movq 0x108(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x3feb4
movq 0x18(%rsp), %rdi
callq 0x44780
movq 0x18(%rsp), %rdi
movl $0x10, %esi
callq 0x13580
movq 0x118(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3fed2
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0x0, 0x114(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x43a50
leaq 0x138(%rsp), %rdi
callq 0x43740
leaq 0x178(%rsp), %rdi
callq 0x1b990
movl 0x114(%rsp), %eax
testl %eax, %eax
je 0x3ff13
jmp 0x3ff11
jmp 0x3ff15
jmp 0x3ff15
movl 0x1b4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1b4(%rsp)
jmp 0x3f940
leaq 0x138(%rsp), %rdi
callq 0x43740
leaq 0x178(%rsp), %rdi
callq 0x1b990
jmp 0x3ff7f
jmp 0x3ff49
movl 0x1b8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1b8(%rsp)
jmp 0x3f921
movq 0x1c8(%rsp), %rax
movq %rax, 0x260(%rsp)
movq 0x260(%rsp), %rax
addq $0x268, %rsp            # imm = 0x268
retq
movq 0x1c0(%rsp), %rdi
callq 0x13970
nopl (%rax)