// File: /workspace/github_cpp_makefile_star2_O1/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 0x26dfb
callq 0x37838
movq %rax, %rsi
jmp 0x26e02
leaq 0x10ebc6(%rip), %rsi    # 0x1359c8
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
callq 0x6df88
movq %rbx, %rdi
callq 0x6e7a4
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x26e2a
jp 0x26e2a
xorl %ebx, %ebx
jmp 0x270f0
leaq 0x40(%rsp), %rdi
movsd 0xb7cc1(%rip), %xmm0    # 0xdeaf8
callq 0x6e892
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 0xb7291(%rip), %xmm0    # 0xde140
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 0x76b8c
movq 0x8(%r14), %rcx
cmpl $0x0, 0x24(%rcx)
jle 0x270f0
movq 0x10(%rcx), %rax
movq %rax, 0x8(%rsp)
xorl %edx, %edx
leaq 0x78(%rsp), %r15
leaq 0x90(%rsp), %r12
movq %rcx, 0x10(%rsp)
movq 0x18(%rcx), %rax
movq %rdx, 0x18(%rsp)
movq (%rax,%rdx,8), %rcx
cmpl $0x0, 0x3c(%rcx)
jg 0x270d6
cmpl $0x0, 0x4c(%rcx)
jle 0x270d6
xorl %edx, %edx
movq %rcx, 0x20(%rsp)
movq 0x40(%rcx), %rax
movq %rdx, 0x28(%rsp)
movq (%rax,%rdx,8), %r13
cmpl $0x0, 0xb4(%r13)
jne 0x26fa2
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 0x26fa9
incl 0xb4(%r13)
cmpl $0x0, 0x8(%r13)
jle 0x2705b
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 0x84d7e
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
movq %r12, %rdi
movq %rbx, %rsi
callq 0x77cba
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
movq %rbx, %rdi
movsd 0xb7110(%rip), %xmm3    # 0xde150
xorl %esi, %esi
callq 0x7c592
incq %rbp
movslq 0x8(%r13), %rax
addq $0x30, %r14
cmpq %rax, %rbp
jl 0x26fbc
cmpl $0x1, 0xb4(%r13)
jne 0x27081
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 0x270bc
decl 0xb4(%r13)
jne 0x270bc
testb $0x2, 0x89(%r13)
je 0x270bc
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x18(%rax)
testq %r13, %r13
je 0x270bc
movq %r13, %rdi
callq 0x2ce2e
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 0x26f6f
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x10(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jl 0x26f47
leaq 0x40(%rsp), %rdi
callq 0x6ede0
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 0x2712a
jmp 0x27127
jmp 0x27127
jmp 0x27127
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x6ede0
movq %r14, %rdi
callq 0x138b0