// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movsd %xmm6, 0x98(%rsp)
movsd %xmm5, 0x90(%rsp)
movl %r8d, 0x24(%rsp)
movsd %xmm4, 0x88(%rsp)
movsd %xmm3, 0x80(%rsp)
movsd %xmm2, 0x78(%rsp)
movsd %xmm1, 0x70(%rsp)
movsd %xmm0, 0xb0(%rsp)
movl %ecx, 0x28(%rsp)
movq %rdi, 0xa8(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x28ec7
movq %rdx, %r14
movq %rsi, %r13
movaps %xmm7, 0x50(%rsp)
testq %rsi, %rsi
jne 0x28e58
movq 0x18(%rax), %rax
movq (%rax), %r13
movq %r13, %rdi
callq 0x3755c
movupd 0x8(%rax), %xmm0
movq %rax, 0xa0(%rsp)
movupd 0x20(%rax), %xmm2
subpd %xmm0, %xmm2
movl $0x1, 0x8(%rsp)
xorpd %xmm0, %xmm0
movapd 0x50(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x28ece
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, 0x40(%rsp)
movd %xmm0, 0xc(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, 0x8(%rsp)
jmp 0x28edc
xorl %eax, %eax
jmp 0x29254
movapd %xmm2, 0x40(%rsp)
movl $0x1, 0xc(%rsp)
movl $0x10, %edi
callq 0x134e0
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0xa9ca8
cmpl $0x0, 0x8(%rsp)
movapd 0x40(%rsp), %xmm0
jle 0x2924f
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0xf0(%rsp)
movq $0x0, 0x18(%rsp)
leaq 0x100(%rsp), %rbx
leaq 0x138(%rsp), %rbp
leaq 0x1056e7(%rip), %rax    # 0x12e618
addq $0x10, %rax
movq %rax, 0x30(%rsp)
leaq 0x105137(%rip), %rax    # 0x12e078
addq $0x10, %rax
movq %rax, 0x38(%rsp)
movq %r14, 0x68(%rsp)
movq %r13, 0x60(%rsp)
cmpl $0x0, 0xc(%rsp)
jle 0x29235
movq 0x18(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
incl %eax
movl %eax, 0x20(%rsp)
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
xorl %r12d, %r12d
movapd 0xf0(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd %xmm1, 0xc0(%rsp)
mulsd %xmm0, %xmm2
movsd %xmm2, 0xb8(%rsp)
movq %rbp, %r15
movq %rbx, %rbp
movq %rbx, %rdi
movq 0xa0(%rsp), %rbx
movq %rbx, %rsi
callq 0x6d354
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movapd 0x40(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd 0x8(%rbx), %xmm0
leal 0x1(%r12), %eax
movl %eax, 0x2c(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm0, 0x108(%rsp)
mulsd %xmm2, %xmm1
addsd 0x8(%rbx), %xmm1
movsd %xmm1, 0x120(%rsp)
movsd 0x10(%rbx), %xmm0
addsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x10(%rbx), %xmm0
addsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x128(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x30310
leaq 0xd8(%rsp), %r15
movq %r15, %rdi
callq 0x31b58
movq %r15, %rdi
leaq 0x138(%rsp), %rbp
movq %rbp, %rsi
callq 0x31b6c
testq %r14, %r14
je 0x29065
movq %r15, %rdi
movq %r14, %rsi
callq 0x31b6c
movq 0xa8(%rsp), %rdi
movq %r13, %rsi
movq %r15, %rdx
movl 0x28(%rsp), %ecx
movsd 0xb0(%rsp), %xmm0
callq 0x229bb
testq %rax, %rax
je 0x291ca
movq %rax, 0xc8(%rsp)
movq %rax, %rdi
movsd 0x70(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x80(%rsp), %xmm2
movsd 0x88(%rsp), %xmm3
movl 0x24(%rsp), %esi
movsd 0x90(%rsp), %xmm4
movsd 0x98(%rsp), %xmm5
callq 0x28094
movq %rax, %rbx
testq %rax, %rax
je 0x291bc
movl 0xc(%rbx), %ebp
testl %ebp, %ebp
jle 0x2918d
xorl %r15d, %r15d
movq (%rbx), %rax
movq (%rax,%r15,8), %r14
leaq 0x130(%r14), %r13
movq %r13, %rdi
callq 0x91a6e
movsd %xmm0, 0x50(%rsp)
movsd 0x1e0(%r14), %xmm0
movsd %xmm0, 0xd0(%rsp)
movq %r13, %rdi
callq 0x91b24
movsd 0xb401e(%rip), %xmm0    # 0xdd148
divsd 0xd0(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movl %ebp, (%rsp)
leaq 0xb4b2f(%rip), %rdi     # 0xddc7b
movq 0x18(%rsp), %rsi
movl 0x8(%rsp), %edx
movl %r12d, %ecx
movl 0xc(%rsp), %r8d
movl %r15d, %r9d
movsd 0x50(%rsp), %xmm0
movb $0x2, %al
callq 0x13070
movq 0x10(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %r14, %rsi
callq 0xa9d3e
incq %r15
movslq 0xc(%rbx), %rbp
cmpq %rbp, %r15
jl 0x290ec
movq %rbx, %rdi
callq 0xa9d12
movl $0x10, %esi
movq %rbx, %rdi
callq 0x134f0
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %r13
leaq 0x138(%rsp), %rbp
leaq 0xd8(%rsp), %r15
movq 0xc8(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x30(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xa9d12
movq %r15, %rdi
callq 0x31dba
movq 0x38(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x6e174
movq %rbp, %rdi
callq 0x31dba
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
callq 0x6e174
movl 0x2c(%rsp), %eax
movl %eax, %r12d
cmpl 0xc(%rsp), %eax
jne 0x28f9e
movl 0x20(%rsp), %ecx
jmp 0x2923e
movq 0x18(%rsp), %rax
incl %eax
movl %eax, %ecx
movl %ecx, %eax
movq %rax, 0x18(%rsp)
cmpl 0x8(%rsp), %ecx
jne 0x28f54
movq 0x10(%rsp), %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x10, %esi
movq 0x10(%rsp), %rdi
callq 0x134f0
jmp 0x292e4
jmp 0x2928a
movq %rax, %rbx
jmp 0x292b2
movq %rax, %rbx
jmp 0x292d7
jmp 0x2928a
jmp 0x2928a
movq %rax, %rbx
leaq 0xd8(%rsp), %r14
movq 0x30(%rsp), %rax
movq %rax, (%r14)
leaq 0xe0(%rsp), %rdi
callq 0xa9d12
movq %r14, %rdi
callq 0x31dba
leaq 0x138(%rsp), %r14
movq 0x38(%rsp), %rax
movq %rax, (%r14)
leaq 0x140(%rsp), %rdi
callq 0x6e174
movq %r14, %rdi
callq 0x31dba
leaq 0x100(%rsp), %rdi
callq 0x6e174
movq %rbx, %rdi
callq 0x138b0