// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateGrid(gaps::R3SurfelScene*, double, int)
R3Grid *
CreateGrid(R3SurfelScene *scene, 
  RNLength grid_spacing, int max_resolution)
{
  // Compute bbox
  R3Box bbox = scene->BBox();
  if (bbox.Volume() == 0) return NULL;
  bbox.Inflate(1.1);

  // Compute grid resolution
  int xres = (int) (bbox.XLength() / grid_spacing + 0.5);
  int yres = (int) (bbox.YLength() / grid_spacing + 0.5);
  int zres = (int) (bbox.ZLength() / grid_spacing + 0.5);
  if (xres > max_resolution) xres = max_resolution;
  if (yres > max_resolution) yres = max_resolution;
  if (zres > max_resolution) zres = max_resolution;
  if (xres < 2) xres = 2;
  if (yres < 2) yres = 2;
  if (zres < 2) zres = 2;
  
  // Create grid
  R3Grid *grid = new R3Grid(xres, yres, zres, bbox);
  if (!grid) {
    RNFail("Unable to allocate grid\n");
    return NULL;
  }

  // Rasterize points into grid
  R3SurfelTree *tree = scene->Tree();
  R3SurfelDatabase *database = tree->Database();
  for (int i = 0; i < tree->NNodes(); i++) {
    R3SurfelNode *node = tree->Node(i);
    if (node->NParts() > 0) continue;
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);
      const R3Point& origin = block->PositionOrigin();
      database->ReadBlock(block);
      for (int k = 0; k < block->NSurfels(); k++) {
        const R3Surfel *surfel = block->Surfel(k);
        double px = origin.X() + surfel->X();
        double py = origin.Y() + surfel->Y();
        double pz = origin.Z() + surfel->Z();
        R3Point position(px, py, pz);
        grid->RasterizeWorldPoint(position, 1.0);
      }
      database->ReleaseBlock(block);
    }
  }

  // Return grid
  return grid;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %esi, %ebp
movsd %xmm0, 0x28(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x26ae7
callq 0x3755c
movq %rax, %rsi
jmp 0x26aee
leaq 0x10deda(%rip), %rsi    # 0x1349c8
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x6d354
movq %rbx, %rdi
callq 0x6db44
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x26b16
jp 0x26b16
xorl %ebx, %ebx
jmp 0x26def
leaq 0x40(%rsp), %rdi
movsd 0xb6fdd(%rip), %xmm0    # 0xddb00
callq 0x6dc32
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x134e0
movq %rax, %rbx
movsd 0x38(%rsp), %xmm2
subsd 0x30(%rsp), %xmm2
movsd 0x28(%rsp), %xmm1
divsd %xmm1, %xmm2
movsd 0xb659d(%rip), %xmm0    # 0xdd138
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %ecx
cmpl %ebp, %ecx
cmovgel %ebp, %ecx
movsd 0x18(%rsp), %xmm2
subsd 0x10(%rsp), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %edx
cmpl %ebp, %edx
cmovgel %ebp, %edx
movsd 0x8(%rsp), %xmm2
subsd 0x20(%rsp), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
cmpl %ebp, %eax
cmovll %eax, %ebp
cmpl $0x3, %ecx
movl $0x2, %eax
cmovll %eax, %ecx
cmpl $0x3, %edx
cmovll %eax, %edx
cmpl $0x3, %ebp
cmovll %eax, %ebp
leaq 0x40(%rsp), %r8
movq %rbx, %rdi
movl %ebp, %esi
callq 0x75dd2
movq 0x8(%r14), %rdx
movl 0x24(%rdx), %eax
testl %eax, %eax
jle 0x26def
movq 0x10(%rdx), %rcx
movq %rcx, 0x8(%rsp)
xorl %esi, %esi
leaq 0x78(%rsp), %r15
leaq 0x90(%rsp), %r12
movq %rdx, 0x10(%rsp)
movq 0x18(%rdx), %rcx
movq (%rcx,%rsi,8), %rcx
cmpl $0x0, 0x3c(%rcx)
jg 0x26de0
cmpl $0x0, 0x4c(%rcx)
jle 0x26de0
movq %rsi, 0x18(%rsp)
xorl %edx, %edx
movq %rcx, 0x20(%rsp)
movq 0x40(%rcx), %rax
movq %rdx, 0x28(%rsp)
movq (%rax,%rdx,8), %r13
movl 0xb4(%r13), %eax
testl %eax, %eax
jne 0x26c97
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r13, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x26ca0
movl 0xb4(%r13), %eax
incl %eax
movl %eax, 0xb4(%r13)
cmpl $0x0, 0x8(%r13)
jle 0x26d52
movl $0x8, %r14d
xorl %ebp, %ebp
movq (%r13), %rax
xorps %xmm0, %xmm0
cvtss2sd -0x8(%rax,%r14), %xmm0
addsd 0x10(%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x4(%rax,%r14), %xmm1
addsd 0x18(%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd (%rax,%r14), %xmm2
addsd 0x20(%r13), %xmm2
movq %r15, %rdi
callq 0x83dc2
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq %r12, %rdi
movq %rbx, %rsi
callq 0x76f0c
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
movq %rbx, %rdi
movsd 0xb6411(%rip), %xmm3    # 0xdd148
xorl %esi, %esi
callq 0x7b91e
incq %rbp
movslq 0x8(%r13), %rax
addq $0x30, %r14
cmpq %rax, %rbp
jl 0x26cb3
movl 0xb4(%r13), %eax
cmpl $0x1, %eax
jne 0x26d81
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x26db9
movl 0xb4(%r13), %eax
decl %eax
movl %eax, 0xb4(%r13)
jne 0x26db9
testb $0x2, 0x89(%r13)
je 0x26db9
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
movq 0x28(%rsp), %rdx
incq %rdx
movq 0x20(%rsp), %rcx
movslq 0x4c(%rcx), %rax
cmpq %rax, %rdx
jl 0x26c5c
movq 0x10(%rsp), %rdx
movl 0x24(%rdx), %eax
movq 0x18(%rsp), %rsi
incq %rsi
movslq %eax, %rcx
cmpq %rcx, %rsi
jl 0x26c34
leaq 0x40(%rsp), %rdi
callq 0x6e174
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x250, %esi            # imm = 0x250
movq %rbx, %rdi
callq 0x134f0
jmp 0x26e29
jmp 0x26e26
jmp 0x26e26
jmp 0x26e26
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x6e174
movq %r14, %rdi
callq 0x138b0