// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateGrid(gaps::R3SurfelPointSet*, double, int)
R3Grid *
CreateGrid(R3SurfelPointSet *pointset,
  RNLength grid_spacing, int max_resolution)
{
  // Check pointset
  if (!pointset) return NULL;
  if (pointset->NPoints() == 0) return NULL;

  // Compute bbox
  R3Box bbox = pointset->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
  if (pointset->NPoints() > 0) {
    int step = pointset->NPoints() / grid->NEntries() + 1;
    for (int j = 0; j < pointset->NPoints(); j += step) {
      const R3SurfelPoint *point = pointset->Point(j);
      R3Point position = point->Position();
      grid->RasterizeWorldPoint(position, 1);
    }
  }

  // Return grid
  return grid;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
testq %rdi, %rdi
je 0x24338
movq %rdi, %rbx
cmpl $0x0, 0x10(%rdi)
je 0x24338
movl %esi, %ebp
movsd %xmm0, (%rsp)
leaq 0x18(%rbx), %rsi
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x63ce6
movq %r14, %rdi
callq 0x6449c
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x24340
jp 0x24340
xorl %r14d, %r14d
jmp 0x24496
xorl %r14d, %r14d
jmp 0x244a0
leaq 0x38(%rsp), %rdi
movsd 0xa47ab(%rip), %xmm0    # 0xc8af8
callq 0x6455e
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x134f0
movq %rax, %r14
movsd 0x10(%rsp), %xmm2
subsd 0x8(%rsp), %xmm2
movsd (%rsp), %xmm1
divsd %xmm1, %xmm2
movsd 0xa3d74(%rip), %xmm0    # 0xc8138
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %ecx
cmpl %ebp, %ecx
cmovgel %ebp, %ecx
movsd 0x20(%rsp), %xmm2
subsd 0x18(%rsp), %xmm2
divsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %edx
cmpl %ebp, %edx
cmovgel %ebp, %edx
movsd 0x30(%rsp), %xmm2
subsd 0x28(%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 %r14, %rdi
movl %ebp, %esi
callq 0x6ba00
movl 0x10(%rbx), %ecx
testl %ecx, %ecx
jle 0x24496
movl %ecx, %eax
cltd
movq %r14, (%rsp)
idivl 0x24c(%r14)
incl %eax
movslq %eax, %r12
movq %r12, %r13
shlq $0x4, %r13
xorl %ebp, %ebp
leaq 0x70(%rsp), %r15
xorl %r14d, %r14d
movslq %ecx, %rax
cmpq %rax, %r14
jge 0x24492
movq 0x8(%rbx), %rsi
addq %rbp, %rsi
movq %r15, %rdi
callq 0x16310
movq (%rsp), %rdi
movq %r15, %rsi
movsd 0xa3cc8(%rip), %xmm0    # 0xc8148
xorl %edx, %edx
callq 0x285cc
addq %r12, %r14
movl 0x10(%rbx), %ecx
addq %r13, %rbp
jmp 0x2445a
movq (%rsp), %r14
leaq 0x38(%rsp), %rdi
callq 0x649ae
movq %r14, %rax
addq $0x88, %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
jmp 0x244ce
jmp 0x244cb
jmp 0x244cb
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x649ae
movq %rbx, %rdi
callq 0x138f0