// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateGrid(gaps::R3SurfelScene*, gaps::R3Box const&, double, int)
R3Grid *
CreateGrid(R3SurfelScene *scene, const R3Box& bbox,
  RNLength grid_spacing, int max_resolution)
{
  // 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;
    if (!R3Intersects(bbox, node->BBox())) continue;
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);
      if (!R3Intersects(bbox, block->BBox())) continue;
      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);
        if (!R3Intersects(bbox, position)) continue;
        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 $0x48, %rsp
movq %rsi, %rbx
movsd 0x20(%rsi), %xmm1
subsd 0x8(%rsi), %xmm1
movsd 0x28(%rsi), %xmm2
divsd %xmm0, %xmm1
movsd 0xa33b0(%rip), %xmm3    # 0xc8138
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %ebp
subsd 0x10(%rsi), %xmm2
divsd %xmm0, %xmm2
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %r15d
movsd 0x30(%rsi), %xmm1
subsd 0x18(%rsi), %xmm1
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %r12d
cmpl %edx, %ebp
cmovgel %edx, %ebp
cmpl %edx, %r15d
cmovgel %edx, %r15d
cmpl %edx, %r12d
cmovgel %edx, %r12d
cmpl $0x3, %ebp
pushq $0x2
popq %rax
cmovll %eax, %ebp
cmpl $0x3, %r15d
cmovll %eax, %r15d
movq %rdi, %r13
cmpl $0x3, %r12d
cmovll %eax, %r12d
movl $0x250, %edi            # imm = 0x250
callq 0x134f0
movq %rax, %r14
movq %rax, %rdi
movl %ebp, %esi
movl %r15d, %edx
movl %r12d, %ecx
movq %rbx, %r8
callq 0x6ba00
movq 0x8(%r13), %rax
movq 0x10(%rax), %r15
xorl %edx, %edx
leaq 0x30(%rsp), %r12
movq %rax, 0x8(%rsp)
movq %r15, 0x18(%rsp)
movq %rax, %rcx
movslq 0x24(%rax), %rax
cmpq %rax, %rdx
jge 0x24f40
movq 0x18(%rcx), %rax
movq %rdx, 0x10(%rsp)
movq (%rax,%rdx,8), %r13
cmpl $0x0, 0x3c(%r13)
jle 0x24e54
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rax
jmp 0x24e21
movq %r13, %rdi
callq 0x33720
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x469dc
testl %eax, %eax
je 0x24e45
xorl %ecx, %ecx
movq %r13, 0x20(%rsp)
movslq 0x4c(%r13), %rax
cmpq %rax, %rcx
jge 0x24e45
movq 0x40(%r13), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rbp
movq %rbp, %rdi
callq 0x2a720
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x469dc
testl %eax, %eax
je 0x24f33
movq %r15, %rdi
movq %rbp, %rsi
callq 0x20a64
pushq $0x8
popq %r13
xorl %r15d, %r15d
movslq 0x8(%rbp), %rax
cmpq %rax, %r15
jge 0x24f1e
movq (%rbp), %rax
cvtss2sd -0x8(%rax,%r13), %xmm0
addsd 0x10(%rbp), %xmm0
cvtss2sd -0x4(%rax,%r13), %xmm1
addsd 0x18(%rbp), %xmm1
cvtss2sd (%rax,%r13), %xmm2
addsd 0x20(%rbp), %xmm2
movq %r12, %rdi
callq 0x781bc
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4318d
testl %eax, %eax
je 0x24f15
movq %r14, %rdi
movq %r12, %rsi
movsd 0xa323a(%rip), %xmm0    # 0xc8148
xorl %edx, %edx
callq 0x285cc
incq %r15
addq $0x30, %r13
jmp 0x24eb9
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x20aa0
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rcx
incq %rcx
jmp 0x24e74
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x250, %esi            # imm = 0x250
movq %r14, %rdi
callq 0x13500
movq %rbx, %rdi
callq 0x138f0