// File: 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:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x198(%rsp)
movsd %xmm0, 0x190(%rsp)
movl %esi, 0x18c(%rsp)
movq 0x198(%rsp), %rdi
callq 0x441f0
movq %rax, %rsi
leaq 0x150(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0xc9de0
movq 0xa0(%rsp), %rdi
callq 0xcb720
movsd %xmm0, 0xa8(%rsp)
jmp 0x3b9ec
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x3ba35
jp 0x3ba35
movq $0x0, 0x1a0(%rsp)
movl $0x1, 0x140(%rsp)
jmp 0x3bfba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x3bfd7
leaq 0x150(%rsp), %rdi
movsd 0x16909b(%rip), %xmm0   # 0x1a4ae0
callq 0xcb8b0
jmp 0x3ba4c
leaq 0x150(%rsp), %rdi
callq 0x44130
movsd %xmm0, 0x98(%rsp)
jmp 0x3ba64
movsd 0x98(%rsp), %xmm0
movsd 0x190(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x1686d6(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x13c(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x44150
movsd %xmm0, 0x90(%rsp)
jmp 0x3baa9
movsd 0x90(%rsp), %xmm0
movsd 0x190(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x168691(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x138(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x44170
movsd %xmm0, 0x88(%rsp)
jmp 0x3baee
movsd 0x88(%rsp), %xmm0
divsd 0x190(%rsp), %xmm0
movsd 0x168650(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x134(%rsp)
movl 0x13c(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jle 0x3bb35
movl 0x18c(%rsp), %eax
movl %eax, 0x13c(%rsp)
movl 0x138(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jle 0x3bb53
movl 0x18c(%rsp), %eax
movl %eax, 0x138(%rsp)
movl 0x134(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jle 0x3bb71
movl 0x18c(%rsp), %eax
movl %eax, 0x134(%rsp)
cmpl $0x2, 0x13c(%rsp)
jge 0x3bb86
movl $0x2, 0x13c(%rsp)
cmpl $0x2, 0x138(%rsp)
jge 0x3bb9b
movl $0x2, 0x138(%rsp)
cmpl $0x2, 0x134(%rsp)
jge 0x3bbb0
movl $0x2, 0x134(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x13570
movq %rax, 0x80(%rsp)
jmp 0x3bbc4
movq 0x80(%rsp), %rdi
movl 0x13c(%rsp), %esi
movl 0x138(%rsp), %edx
movl 0x134(%rsp), %ecx
leaq 0x150(%rsp), %r8
callq 0xdef40
jmp 0x3bbf0
movq 0x80(%rsp), %rax
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
jne 0x3bc62
leaq 0x169073(%rip), %rdi    # 0x1a4c85
xorl %eax, %eax
callq 0x1464b0
jmp 0x3bc1b
movq $0x0, 0x1a0(%rsp)
movl $0x1, 0x140(%rsp)
jmp 0x3bfba
movq 0x80(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x250, %esi            # imm = 0x250
callq 0x13580
jmp 0x3bfd7
movq 0x198(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x78(%rsp)
jmp 0x3bc76
movq 0x78(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rdi
callq 0x16d10
movq %rax, 0x70(%rsp)
jmp 0x3bc97
movq 0x70(%rsp), %rax
movq %rax, 0x118(%rsp)
movl $0x0, 0x114(%rsp)
movl 0x114(%rsp), %eax
movl %eax, 0x68(%rsp)
movq 0x120(%rsp), %rdi
callq 0x16cf0
movl %eax, 0x6c(%rsp)
jmp 0x3bccd
movl 0x68(%rsp), %eax
movl 0x6c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3bf9f
movq 0x120(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x30a90
movq %rax, 0x60(%rsp)
jmp 0x3bcf8
movq 0x60(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rdi
callq 0x31660
movl %eax, 0x5c(%rsp)
jmp 0x3bd18
movl 0x5c(%rsp), %eax
cmpl $0x0, %eax
jle 0x3bd26
jmp 0x3bf89
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x108(%rsp), %rdi
callq 0x186b0
movl %eax, 0x58(%rsp)
jmp 0x3bd4f
movl 0x54(%rsp), %eax
movl 0x58(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3bf87
movq 0x108(%rsp), %rdi
movl 0x104(%rsp), %esi
callq 0x186d0
movq %rax, 0x48(%rsp)
jmp 0x3bd7a
movq 0x48(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x189c0
movq %rax, 0x40(%rsp)
jmp 0x3bd9b
movq 0x40(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x118(%rsp), %rdi
movq 0xf8(%rsp), %rsi
callq 0x31140
jmp 0x3bdbf
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1c0e0
movl %eax, 0x3c(%rsp)
jmp 0x3bde8
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3bf58
movq 0xf8(%rsp), %rdi
movl 0xec(%rsp), %esi
callq 0x43660
movq %rax, 0x30(%rsp)
jmp 0x3be13
movq 0x30(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x28(%rsp)
jmp 0x3be35
movq 0xe0(%rsp), %rdi
callq 0x44190
movss %xmm0, 0x24(%rsp)
jmp 0x3be4a
movsd 0x28(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x187c0
movsd %xmm0, 0x18(%rsp)
jmp 0x3be7c
movq 0xe0(%rsp), %rdi
callq 0x441a0
movss %xmm0, 0x14(%rsp)
jmp 0x3be91
movsd 0x18(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x187a0
movsd %xmm0, 0x8(%rsp)
jmp 0x3bec3
movq 0xe0(%rsp), %rdi
callq 0x441b0
movss %xmm0, 0x4(%rsp)
jmp 0x3bed8
movsd 0x8(%rsp), %xmm0
movss 0x4(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movsd 0xc8(%rsp), %xmm2
leaq 0xb0(%rsp), %rdi
callq 0xf8350
jmp 0x3bf1f
movq 0x128(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movsd 0x168231(%rip), %xmm0   # 0x1a4168
xorl %edx, %edx
callq 0x43ff0
jmp 0x3bf40
jmp 0x3bf42
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x3bdca
movq 0x118(%rsp), %rdi
movq 0xf8(%rsp), %rsi
callq 0x311c0
jmp 0x3bf6f
jmp 0x3bf71
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x3bd31
jmp 0x3bf89
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x3bcaf
movq 0x128(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movl $0x1, 0x140(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x1b990
movq 0x1a0(%rsp), %rax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
leaq 0x150(%rsp), %rdi
callq 0x1b990
movq 0x148(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)