// File: /workspace/github_cpp_makefile_star2_O3/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 0x25c43
movq %rdi, %rbx
cmpl $0x0, 0x10(%rdi)
je 0x25c43
movl %esi, %ebp
movsd %xmm0, 0x38(%rsp)
leaq 0x18(%rbx), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x6d354
movq %r14, %rdi
callq 0x6db44
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x25c4b
jp 0x25c4b
xorl %r14d, %r14d
jmp 0x25e0a
xorl %r14d, %r14d
jmp 0x25e14
leaq 0x40(%rsp), %rdi
movsd 0xb7ea8(%rip), %xmm0    # 0xddb00
callq 0x6dc32
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 0xb7468(%rip), %xmm0    # 0xdd138
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 0x75dd2
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0x25e0a
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 0x83dc2
movsd 0x78(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x76f0c
movsd 0x90(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
movq %r14, %rdi
movsd 0xb7358(%rip), %xmm3    # 0xdd148
xorl %esi, %esi
callq 0x7b91e
addq %r13, %r12
movslq 0x10(%rbx), %rax
addq %rbp, %r15
cmpq %rax, %r12
jl 0x25d65
leaq 0x40(%rsp), %rdi
callq 0x6e174
movq %r14, %rax
addq $0xa8, %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
jmp 0x25e42
jmp 0x25e3f
jmp 0x25e3f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x6e174
movq %rbx, %rdi
callq 0x138b0