// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelPointSet.cpp
// Function: gaps::R3SurfelPointSet::InsertPoints(gaps::R3SurfelBlock*, double)
void R3SurfelPointSet::
InsertPoints(R3SurfelBlock *block, RNScalar max_resolution)
{
  // Check block
  if (block->NSurfels() == 0) return;

  // Update bounding box
  bbox.Union(block->BBox());

  // Update timestamp range
  timestamp_range.Union(block->TimestampRange());

  // Compute number of surfels to insert
  int block_nsurfels = block->NSurfels();
  if (max_resolution > 0) {
    RNScalar block_resolution = block->Resolution();
    if (block_resolution > max_resolution) {
      RNScalar sampling_factor = max_resolution / block_resolution;
      block_nsurfels = block->NSurfels() * sampling_factor;
      if (block_nsurfels == 0) block_nsurfels = 1;
    }
  }
  
  // Allocate space for points
  AllocatePoints(npoints + block_nsurfels);

  // Read block
  if (block->database) block->database->ReadBlock(block);

  // Copy surfels
  if (block_nsurfels < block->NSurfels()) {
    // Subsample surfels
    int step = block->NSurfels() / block_nsurfels + 1;
    for (int i = 0; i < block->NSurfels(); i+= step) {
      const R3Surfel *surfel = block->Surfel(i);
      points[npoints].Reset(block, surfel);
      npoints++;
    }
  }
  else {
    // Copy all surfels
    for (int i = 0; i < block->NSurfels(); i++) {
      const R3Surfel *surfel = block->Surfel((int) i);
      points[npoints].Reset(block, surfel);
      npoints++;
    }
  }

  // Release block
  if (block->database) block->database->ReleaseBlock(block);
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movsd %xmm0, 0x60(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rdi
callq 0x1c0e0
cmpl $0x0, %eax
jne 0x580c2
jmp 0x58312
movq 0x20(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x10(%rsp)
movq 0x68(%rsp), %rdi
callq 0x4a500
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0xcbc30
movq 0x20(%rsp), %rax
addq $0x50, %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rdi
callq 0x4a780
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x1bbc0
movq 0x68(%rsp), %rdi
callq 0x1c0e0
movl %eax, 0x5c(%rsp)
movsd 0x60(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x58188
movq 0x68(%rsp), %rdi
callq 0x4aa00
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
ucomisd 0x60(%rsp), %xmm0
jbe 0x58186
movsd 0x60(%rsp), %xmm0
divsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0x68(%rsp), %rdi
callq 0x1c0e0
cvtsi2sd %eax, %xmm0
mulsd 0x48(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
jne 0x58184
movl $0x1, 0x5c(%rsp)
jmp 0x58186
jmp 0x58188
movq 0x20(%rsp), %rdi
movl 0x10(%rdi), %esi
addl 0x5c(%rsp), %esi
movq (%rdi), %rax
callq *0x80(%rax)
movq 0x68(%rsp), %rax
cmpq $0x0, 0x98(%rax)
je 0x581c2
movq 0x68(%rsp), %rax
movq 0x98(%rax), %rdi
movq 0x68(%rsp), %rsi
callq 0x31140
movl 0x5c(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x68(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x58273
movq 0x68(%rsp), %rdi
callq 0x1c0e0
cltd
idivl 0x5c(%rsp)
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x68(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x58271
movq 0x68(%rsp), %rdi
movl 0x40(%rsp), %esi
callq 0x43660
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x38(%rsp)
movq 0x8(%rax), %rdi
movslq 0x10(%rax), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x68(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x558d0
movq 0x20(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movl 0x44(%rsp), %eax
addl 0x40(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x58200
jmp 0x582ed
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x68(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x582eb
movq 0x68(%rsp), %rdi
movl 0x34(%rsp), %esi
callq 0x43660
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq 0x8(%rax), %rdi
movslq 0x10(%rax), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x68(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x558d0
movq 0x20(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x5827b
jmp 0x582ed
movq 0x68(%rsp), %rax
cmpq $0x0, 0x98(%rax)
je 0x58312
movq 0x68(%rsp), %rax
movq 0x98(%rax), %rdi
movq 0x68(%rsp), %rsi
callq 0x311c0
addq $0x78, %rsp
retq
nopw (%rax,%rax)