// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreatePlanarObjects(gaps::R3SurfelScene*, gaps::R3SurfelNode*, gaps::R3SurfelConstraint const*, gaps::R3SurfelObject*, gaps::R3SurfelNode*, int, double, double, double, double, double, int, double, double, double)
RNArray<R3SurfelObject *> *
CreatePlanarObjects(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,
  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 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;

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

      // Delete graph
      delete graph;

      // Create objects 
      for (int k = 0; k < chunk_grids->NEntries(); k++) {
        R3PlanarGrid *grid = chunk_grids->Kth(k);
        char object_name[256];
        sprintf(object_name, "PlanarGrid%d\n", objects->NEntries());
        R3SurfelPlanarGridConstraint planar_grid_constraint(grid, max_offplane_distance);
        multi_constraint.InsertConstraint(&planar_grid_constraint);
        R3SurfelObject *object = CreateObject(scene, source_node, &multi_constraint, 
          parent_object, object_name, parent_node, object_name);
        multi_constraint.RemoveConstraint(&planar_grid_constraint);
        printf("  %6d/%6d %6d/%6d %6d/%6d : %9.3g %9.3g\n", j, nychunks, i, nxchunks, k, chunk_grids->NEntries(), grid->L1Norm(), grid->Area());
        if (object) objects->Insert(object);
        delete grid;
      }

      // Delete array of grids
      delete chunk_grids;
    }
  }

  // Return objects
  return objects;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2d8, %rsp            # imm = 0x2D8
movsd %xmm6, 0xa8(%rsp)
movsd %xmm5, 0xa0(%rsp)
movsd %xmm4, 0x98(%rsp)
movsd %xmm3, 0x90(%rsp)
movsd %xmm2, 0x88(%rsp)
movsd %xmm1, 0x48(%rsp)
movsd %xmm0, 0xc0(%rsp)
movl %r9d, 0x18(%rsp)
movq %r8, 0xe0(%rsp)
movq %rcx, 0xd8(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x8(%rdi), %rax
testq %rax, %rax
je 0x293e4
movq %rdx, %r15
movq %rsi, %r12
movaps %xmm7, 0x20(%rsp)
testq %rsi, %rsi
jne 0x29377
movq 0x18(%rax), %rax
movq (%rax), %r12
movq %r12, %rdi
callq 0x3755c
movupd 0x8(%rax), %xmm0
movq %rax, 0xb8(%rsp)
movupd 0x20(%rax), %xmm2
subpd %xmm0, %xmm2
movl $0x1, (%rsp)
xorpd %xmm0, %xmm0
movapd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x293ec
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, 0x70(%rsp)
movd %xmm0, 0x4(%rsp)
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, (%rsp)
jmp 0x293fa
xorl %r13d, %r13d
jmp 0x2985e
movapd %xmm2, 0x70(%rsp)
movl $0x1, 0x4(%rsp)
movl $0x10, %edi
callq 0x134e0
movq %rax, %r13
movq %rax, %rdi
callq 0xa9ca8
cmpl $0x0, (%rsp)
movapd 0x70(%rsp), %xmm0
jle 0x2985e
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm0, 0x100(%rsp)
leaq 0x118(%rsp), %rbp
leaq 0x150(%rsp), %r14
leaq 0x1051d5(%rip), %rax    # 0x12e618
addq $0x10, %rax
movq %rax, 0x30(%rsp)
leaq 0x104c25(%rip), %rax    # 0x12e078
addq $0x10, %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x8(%rsp)
movq %r15, 0xb0(%rsp)
movq %r12, 0x40(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x29845
movq 0x8(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
incl %eax
movl %eax, 0x14(%rsp)
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
xorl %eax, %eax
movapd 0x100(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd %xmm1, 0xd0(%rsp)
mulsd %xmm0, %xmm2
movsd %xmm2, 0xc8(%rsp)
movq %r12, %r15
movl %eax, %r12d
movq %rbp, %rdi
movq 0xb8(%rsp), %rbx
movq %rbx, %rsi
callq 0x6d354
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movapd 0x70(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd 0x8(%rbx), %xmm0
movq %r12, 0xe8(%rsp)
leal 0x1(%r12), %eax
movl %eax, 0x1c(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm0, 0x120(%rsp)
mulsd %xmm2, %xmm1
addsd 0x8(%rbx), %xmm1
movsd %xmm1, 0x138(%rsp)
movsd 0x10(%rbx), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0x128(%rsp)
movsd 0x10(%rbx), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x30310
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x31b58
movq %r15, %r12
movq 0xb0(%rsp), %r15
movq %r14, %rdi
leaq 0x150(%rsp), %rsi
callq 0x31b6c
testq %r15, %r15
je 0x2958f
movq %r14, %rdi
movq %r15, %rsi
callq 0x31b6c
movq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
movl 0x18(%rsp), %ecx
movsd 0xc0(%rsp), %xmm0
callq 0x229bb
movq %rax, %rbx
testq %rax, %rax
je 0x297db
movq %rbx, %rdi
movsd 0x48(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
movsd 0x90(%rsp), %xmm2
movsd 0x98(%rsp), %xmm3
movl 0x310(%rsp), %esi
movsd 0xa0(%rsp), %xmm4
movsd 0xa8(%rsp), %xmm5
callq 0x28094
movq (%rbx), %rcx
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
callq *0x8(%rcx)
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
je 0x297db
cmpl $0x0, 0xc(%rcx)
leaq 0x190(%rsp), %rbx
jle 0x297be
xorl %r12d, %r12d
movq (%rcx), %rax
movq (%rax,%r12,8), %r15
movl 0xc(%r13), %edx
leaq 0x1d0(%rsp), %rdi
leaq 0xb465f(%rip), %rsi     # 0xddca4
xorl %eax, %eax
callq 0x130e0
leaq 0x10a5b5(%rip), %rax    # 0x133c08
movsd (%rax), %xmm1
xorpd %xmm2, %xmm2
xorps %xmm3, %xmm3
movq %rbx, %rdi
movq %r15, %rsi
movsd 0x48(%rsp), %xmm0
movl $0x4, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x309de
movq %r14, %rdi
movq %rbx, %rsi
callq 0x31b6c
movq %r13, %rbp
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
movq 0xe0(%rsp), %rcx
leaq 0x1d8(%rsp), %r8
movq 0xe8(%rsp), %r9
pushq $0x1
pushq $0x0
pushq %r8
callq 0x238fc
addq $0x20, %rsp
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x31b78
movq 0x20(%rsp), %rax
movl 0xc(%rax), %r14d
leaq 0x130(%r15), %r13
movq %r13, %rdi
callq 0x91a6e
movsd %xmm0, 0xf8(%rsp)
movsd 0x1e0(%r15), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq %r13, %rdi
callq 0x91b24
movsd 0xb3a2e(%rip), %xmm0    # 0xdd148
divsd 0xf0(%rsp), %xmm0
cvtsi2sd %eax, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
subq $0x8, %rsp
leaq 0xb4578(%rip), %rdi     # 0xddcb2
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %edx
movq 0xf0(%rsp), %rcx
movl 0xc(%rsp), %r8d
movl %r12d, %r9d
movsd 0x100(%rsp), %xmm0
movb $0x2, %al
pushq %r14
callq 0x13070
addq $0x10, %rsp
testq %rbx, %rbx
je 0x2977c
movq %rbp, %rdi
movl 0xc(%rbp), %edx
movq %rbx, %rsi
callq 0xa9d3e
movq %r15, %rdi
callq 0x7544a
movl $0x240, %esi            # imm = 0x240
movq %r15, %rdi
callq 0x134f0
leaq 0x190(%rsp), %rbx
movq %rbx, %rdi
callq 0x31dba
incq %r12
movq 0x20(%rsp), %rcx
movslq 0xc(%rcx), %rax
cmpq %rax, %r12
movq %rbp, %r13
leaq 0x58(%rsp), %r14
jl 0x2962b
movq %rcx, %rbx
movq %rcx, %rdi
callq 0xa9d12
movl $0x10, %esi
movq %rbx, %rdi
callq 0x134f0
movq 0x40(%rsp), %r12
movq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xa9d12
movq %r14, %rdi
callq 0x31dba
movq 0x38(%rsp), %rax
movq %rax, 0x150(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x6e174
leaq 0x150(%rsp), %r14
movq %r14, %rdi
callq 0x31dba
leaq 0x118(%rsp), %rbp
movq %rbp, %rdi
callq 0x6e174
movl 0x1c(%rsp), %eax
cmpl 0x4(%rsp), %eax
jne 0x294bb
movl 0x14(%rsp), %ecx
jmp 0x2984e
movq 0x8(%rsp), %rax
incl %eax
movl %eax, %ecx
movl %ecx, %eax
movq %rax, 0x8(%rsp)
cmpl (%rsp), %ecx
jne 0x29472
movq %r13, %rax
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x10, %esi
movq %r13, %rdi
callq 0x134f0
jmp 0x298fd
jmp 0x29897
jmp 0x29897
movq %rax, %r14
jmp 0x298cb
movq %rax, %r14
jmp 0x298f0
jmp 0x29897
jmp 0x2989c
movq %rax, %r14
jmp 0x298ac
movq %rax, %r14
leaq 0x190(%rsp), %rdi
callq 0x31dba
leaq 0x58(%rsp), %rbx
movq 0x30(%rsp), %rax
movq %rax, (%rbx)
leaq 0x60(%rsp), %rdi
callq 0xa9d12
movq %rbx, %rdi
callq 0x31dba
leaq 0x150(%rsp), %rbx
movq 0x38(%rsp), %rax
movq %rax, (%rbx)
leaq 0x158(%rsp), %rdi
callq 0x6e174
movq %rbx, %rdi
callq 0x31dba
leaq 0x118(%rsp), %rdi
callq 0x6e174
movq %r14, %rdi
callq 0x138b0