// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
testq %rdi, %rdi
je 0x25f37
movq %rdi, %rbx
cmpl $0x0, 0x10(%rdi)
je 0x25f37
movl %esi, %ebp
movsd %xmm0, 0x38(%rsp)
leaq 0x18(%rbx), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x6df88
movq %r14, %rdi
callq 0x6e7a4
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x25f4f
jp 0x25f4f
xorl %r14d, %r14d
leaq 0x40(%rsp), %rdi
callq 0x6ede0
jmp 0x25f3a
xorl %r14d, %r14d
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsp), %rdi
movsd 0xb8b9c(%rip), %xmm0    # 0xdeaf8
callq 0x6e892
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x134e0
movq %rax, %r14
movsd 0x10(%rsp), %xmm2
subsd 0x8(%rsp), %xmm2
movsd 0x38(%rsp), %xmm1
divsd %xmm1, %xmm2
movsd 0xb816c(%rip), %xmm0    # 0xde140
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
movl $0x2, %eax
cmovll %eax, %ecx
cmpl $0x3, %edx
cmovll %eax, %edx
cmpl $0x3, %ebp
cmovll %eax, %ebp
leaq 0x40(%rsp), %r8
movq %r14, %rdi
movl %ebp, %esi
callq 0x76b8c
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0x25f2b
cmpl $0x0, 0x10(%rbx)
jle 0x25f2b
cltd
idivl 0x24c(%r14)
incl %eax
movslq %eax, %r13
movq %r13, %rbp
shlq $0x4, %rbp
movl $0x8, %r15d
xorl %r12d, %r12d
movq 0x8(%rbx), %rax
movq -0x8(%rax,%r15), %rcx
movq (%rax,%r15), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
leaq 0x78(%rsp), %rdi
callq 0x84d7e
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x77cba
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
movq %r14, %rdi
movsd 0xb8052(%rip), %xmm3    # 0xde150
xorl %esi, %esi
callq 0x7c592
addq %r13, %r12
movslq 0x10(%rbx), %rax
addq %rbp, %r15
cmpq %rax, %r12
jl 0x26073
jmp 0x25f2b
movq %rax, %rbx
movl $0x250, %esi            # imm = 0x250
movq %r14, %rdi
callq 0x134f0
jmp 0x26136
jmp 0x26133
jmp 0x26133
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x6ede0
movq %rbx, %rdi
callq 0x138b0