// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movl %esi, %ebp
movsd %xmm0, 0x18(%rsp)
movq %rdi, %r14
callq 0x28654
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x63ce6
movq %rbx, %rdi
callq 0x6449c
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x24fd0
jp 0x24fd0
xorl %ebx, %ebx
leaq 0x38(%rsp), %rdi
callq 0x649ae
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38(%rsp), %rdi
movsd 0xa3b1b(%rip), %xmm0    # 0xc8af8
callq 0x6455e
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x134f0
movq %rax, %rbx
movsd 0x28(%rsp), %xmm2
subsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm1
divsd %xmm1, %xmm2
movsd 0xa30e4(%rip), %xmm0    # 0xc8138
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %ecx
cmpl %ebp, %ecx
cmovgel %ebp, %ecx
movsd (%rsp), %xmm2
subsd 0x30(%rsp), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %edx
cmpl %ebp, %edx
cmovgel %ebp, %edx
movsd 0x10(%rsp), %xmm2
subsd 0x8(%rsp), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %eax
cmpl %ebp, %eax
cmovll %eax, %ebp
cmpl $0x3, %ecx
pushq $0x2
popq %rax
cmovll %eax, %ecx
cmpl $0x3, %edx
cmovll %eax, %edx
cmpl $0x3, %ebp
cmovll %eax, %ebp
leaq 0x38(%rsp), %r8
movq %rbx, %rdi
movl %ebp, %esi
callq 0x6ba00
movq 0x8(%r14), %rcx
movq 0x10(%rcx), %r14
xorl %edx, %edx
leaq 0x70(%rsp), %r15
movq %rcx, (%rsp)
movq %r14, 0x10(%rsp)
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jge 0x24fb1
movq 0x18(%rcx), %rax
movq %rdx, 0x8(%rsp)
movq (%rax,%rdx,8), %rcx
cmpl $0x0, 0x3c(%rcx)
jle 0x25104
movq 0x8(%rsp), %rdx
incq %rdx
movq (%rsp), %rcx
jmp 0x250d6
xorl %edx, %edx
movq %rcx, 0x18(%rsp)
movslq 0x4c(%rcx), %rax
cmpq %rax, %rdx
jge 0x250f6
movq 0x40(%rcx), %rax
movq %rdx, %r13
movq (%rax,%rdx,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x20a64
pushq $0x8
popq %r14
xorl %ebp, %ebp
movslq 0x8(%r12), %rax
cmpq %rax, %rbp
jge 0x2518d
movq (%r12), %rax
cvtss2sd -0x8(%rax,%r14), %xmm0
addsd 0x10(%r12), %xmm0
cvtss2sd -0x4(%rax,%r14), %xmm1
addsd 0x18(%r12), %xmm1
cvtss2sd (%rax,%r14), %xmm2
addsd 0x20(%r12), %xmm2
movq %r15, %rdi
callq 0x781bc
movq %rbx, %rdi
movq %r15, %rsi
movsd 0xa2fcb(%rip), %xmm0    # 0xc8148
xorl %edx, %edx
callq 0x285cc
incq %rbp
addq $0x30, %r14
jmp 0x25130
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x20aa0
movq %r13, %rdx
incq %rdx
movq 0x18(%rsp), %rcx
jmp 0x2510b
movq %rax, %r14
movl $0x250, %esi            # imm = 0x250
movq %rbx, %rdi
callq 0x13500
jmp 0x251c8
jmp 0x251c5
jmp 0x251c5
jmp 0x251c5
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0x649ae
movq %r14, %rdi
callq 0x138f0