// File: 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:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movsd %xmm0, 0xe0(%rsp)
movl %esi, 0xdc(%rsp)
cmpq $0x0, 0xe8(%rsp)
jne 0x39b7b
movq $0x0, 0xf0(%rsp)
jmp 0x39f3c
movq 0xe8(%rsp), %rdi
callq 0x18700
cmpl $0x0, %eax
jne 0x39b9e
movq $0x0, 0xf0(%rsp)
jmp 0x39f3c
movq 0xe8(%rsp), %rdi
callq 0x435d0
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xc9de0
movq 0x40(%rsp), %rdi
callq 0xcb720
movsd %xmm0, 0x48(%rsp)
jmp 0x39bd2
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x39c18
jp 0x39c18
movq $0x0, 0xf0(%rsp)
movl $0x1, 0x90(%rsp)
jmp 0x39f1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x39f2d
leaq 0xa0(%rsp), %rdi
movsd 0x16aeb8(%rip), %xmm0   # 0x1a4ae0
callq 0xcb8b0
jmp 0x39c2f
leaq 0xa0(%rsp), %rdi
callq 0x44130
movsd %xmm0, 0x38(%rsp)
jmp 0x39c44
movsd 0x38(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x16a4f9(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x8c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x44150
movsd %xmm0, 0x30(%rsp)
jmp 0x39c83
movsd 0x30(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x16a4ba(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x88(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x44170
movsd %xmm0, 0x28(%rsp)
jmp 0x39cc2
movsd 0x28(%rsp), %xmm0
divsd 0xe0(%rsp), %xmm0
movsd 0x16a47f(%rip), %xmm1   # 0x1a4158
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x84(%rsp)
movl 0x8c(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jle 0x39d06
movl 0xdc(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x88(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jle 0x39d24
movl 0xdc(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xdc(%rsp), %eax
jle 0x39d42
movl 0xdc(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x2, 0x8c(%rsp)
jge 0x39d57
movl $0x2, 0x8c(%rsp)
cmpl $0x2, 0x88(%rsp)
jge 0x39d6c
movl $0x2, 0x88(%rsp)
cmpl $0x2, 0x84(%rsp)
jge 0x39d81
movl $0x2, 0x84(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x13570
movq %rax, 0x20(%rsp)
jmp 0x39d92
movq 0x20(%rsp), %rdi
movl 0x8c(%rsp), %esi
movl 0x88(%rsp), %edx
movl 0x84(%rsp), %ecx
leaq 0xa0(%rsp), %r8
callq 0xdef40
jmp 0x39dbb
movq 0x20(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x39e21
leaq 0x16aeb1(%rip), %rdi    # 0x1a4c85
xorl %eax, %eax
callq 0x1464b0
jmp 0x39ddd
movq $0x0, 0xf0(%rsp)
movl $0x1, 0x90(%rsp)
jmp 0x39f1e
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x250, %esi            # imm = 0x250
callq 0x13580
jmp 0x39f2d
movq 0xe8(%rsp), %rdi
callq 0x18700
movl %eax, 0x1c(%rsp)
jmp 0x39e34
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jle 0x39f06
movq 0xe8(%rsp), %rdi
callq 0x18700
movl %eax, 0x18(%rsp)
jmp 0x39e54
movq 0x78(%rsp), %rdi
callq 0x43fd0
movl %eax, 0x14(%rsp)
jmp 0x39e64
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %eax
cltd
idivl %ecx
addl $0x1, %eax
movl %eax, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x18700
movl %eax, 0x10(%rsp)
jmp 0x39e99
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ecx
cmpl %ecx, %eax
jge 0x39f04
movq 0xe8(%rsp), %rdi
movl 0x70(%rsp), %esi
callq 0x18710
movq %rax, (%rsp)
jmp 0x39ebc
movq (%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x18730
jmp 0x39ed6
movq 0x78(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movsd 0x16a280(%rip), %xmm0   # 0x1a4168
xorl %edx, %edx
callq 0x43ff0
jmp 0x39ef1
jmp 0x39ef3
movl 0x74(%rsp), %eax
addl 0x70(%rsp), %eax
movl %eax, 0x70(%rsp)
jmp 0x39e7e
jmp 0x39f06
movq 0x78(%rsp), %rax
movq %rax, 0xf0(%rsp)
movl $0x1, 0x90(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x1b990
jmp 0x39f3c
leaq 0xa0(%rsp), %rdi
callq 0x1b990
jmp 0x39f4c
movq 0xf0(%rsp), %rax
addq $0xf8, %rsp
retq
movq 0x98(%rsp), %rdi
callq 0x13970
nopl (%rax)