// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rsi, %rbx
movsd 0x20(%rsi), %xmm1
subsd 0x8(%rsi), %xmm1
movsd 0x28(%rsi), %xmm2
divsd %xmm0, %xmm1
movsd 0xb6942(%rip), %xmm3    # 0xdd138
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
movl $0x2, %eax
cmovll %eax, %ebp
cmpl $0x3, %r15d
cmovll %eax, %r15d
movq %rdi, %r13
cmpl $0x3, %r12d
cmovll %eax, %r12d
movl $0x250, %edi            # imm = 0x250
callq 0x134e0
movq %rax, %r14
movq %rax, %rdi
movl %ebp, %esi
movl %r15d, %edx
movl %r12d, %ecx
movq %rbx, %r8
callq 0x75dd2
movq 0x8(%r13), %rcx
cmpl $0x0, 0x24(%rcx)
jle 0x26a87
movq 0x10(%rcx), %rax
movq %rax, (%rsp)
xorl %edx, %edx
leaq 0x28(%rsp), %r12
movq %rcx, 0x8(%rsp)
movq 0x18(%rcx), %rax
movq %rdx, 0x10(%rsp)
movq (%rax,%rdx,8), %r13
cmpl $0x0, 0x3c(%r13)
jle 0x268c9
movq 0x10(%rsp), %rdx
incq %rdx
movq 0x8(%rsp), %rcx
movslq 0x24(%rcx), %rax
cmpq %rax, %rdx
jl 0x2689a
jmp 0x26a87
movq %r13, %rdi
callq 0x3755c
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x4c7cc
testl %eax, %eax
je 0x268ae
cmpl $0x0, 0x4c(%r13)
jle 0x268ae
xorl %ebp, %ebp
movq %r13, 0x18(%rsp)
movq 0x40(%r13), %rax
movq (%rax,%rbp,8), %r15
movq %r15, %rdi
callq 0x2cb9e
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x4c7cc
testl %eax, %eax
je 0x26a72
movl 0xb4(%r15), %eax
testl %eax, %eax
jne 0x26942
movq (%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r15, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x2694b
movl 0xb4(%r15), %eax
incl %eax
movl %eax, 0xb4(%r15)
movq %rbp, 0x20(%rsp)
cmpl $0x0, 0x8(%r15)
jle 0x26a03
movl $0x8, %r13d
xorl %ebp, %ebp
movq (%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd -0x8(%rax,%r13), %xmm0
addsd 0x10(%r15), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x4(%rax,%r13), %xmm1
addsd 0x18(%r15), %xmm1
xorps %xmm2, %xmm2
cvtss2sd (%rax,%r13), %xmm2
addsd 0x20(%r15), %xmm2
movq %r12, %rdi
callq 0x83dc2
movq %r12, %rdi
movq %rbx, %rsi
callq 0x48d4d
testl %eax, %eax
je 0x269ef
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x76f0c
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x50(%rsp), %xmm2
movq %r14, %rdi
movsd 0xb6760(%rip), %xmm3    # 0xdd148
xorl %esi, %esi
callq 0x7b91e
incq %rbp
movslq 0x8(%r15), %rax
addq $0x30, %r13
cmpq %rax, %rbp
jl 0x26963
movl 0xb4(%r15), %eax
cmpl $0x1, %eax
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %rbp
jne 0x26a3b
movq (%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r15, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x26a72
movl 0xb4(%r15), %eax
decl %eax
movl %eax, 0xb4(%r15)
jne 0x26a72
testb $0x2, 0x89(%r15)
je 0x26a72
movq (%rsp), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x18(%rax)
movq %r15, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r15, %rdi
callq 0x134f0
incq %rbp
movslq 0x4c(%r13), %rax
cmpq %rax, %rbp
jl 0x268f0
jmp 0x268ae
movq %r14, %rax
addq $0x58, %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 0x134f0
movq %rbx, %rdi
callq 0x138b0