// File: tomfunkhouser[P]gaps/apps/sfl2pts/sfl2pts.cpp
// Function: SamplePoints(gaps::RNArray<PlyPoint*>&, int)
static int
SamplePoints(RNArray<PlyPoint *>& points, int max_points)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Check if need to sample
  if ((max_points == 0) || (points.NEntries() < max_points)) {
    return 1;
  }

  // Sort points
  if (sample_by_confidence) {
    points.Sort(ComparePlyPoints);
  }

  // Mark extra points 
  int n = points.NEntries() - max_points;
  for (int i = 0; i < 8*points.NEntries(); i++) {
    RNScalar r = RNRandomScalar();
    if (sample_by_confidence) r = 1.0 - r*r;
    int j = r * points.NEntries();
    if (j >= points.NEntries()) j = points.NEntries()-1;
    PlyPoint *point = points.Kth(j);
    if (point->confidence == 0) continue;
    point->confidence = 0;
    if (--n == 0) break;
  }

  // Delete extra points
  RNScalar total_confidence = 0;
  RNArray<PlyPoint *> survivors;
  for (int i = 0; i < points.NEntries(); i++) {
    PlyPoint *point = points.Kth(i);
    if (point->confidence == 0) { delete point; continue; }
    total_confidence += point->confidence;
    survivors.Insert(point);
  }

  // Keep only survivors
  points = survivors;

  // Print debug statistics
  if (print_verbose) {
    printf("Sampled points ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Points = %u\n", points.NEntries());
    printf("  Confidence = %g\n", total_confidence / (255.0 * points.NEntries()));
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movl %esi, 0xb4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x143310
leaq 0xa0(%rsp), %rdi
callq 0x18850
cmpl $0x0, 0xb4(%rsp)
je 0x177f0
movq 0xb8(%rsp), %rdi
callq 0x18810
cmpl 0xb4(%rsp), %eax
jge 0x17800
movl $0x1, 0xc4(%rsp)
jmp 0x17b9b
cmpl $0x0, 0x1e7fcd(%rip)    # 0x1ff7d4
je 0x1781d
movq 0xb8(%rsp), %rdi
leaq 0xf38(%rip), %rsi       # 0x18750
callq 0x1445a0
movq 0xb8(%rsp), %rdi
callq 0x18810
subl 0xb4(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x18810
movl %eax, %ecx
movl 0x44(%rsp), %eax
shll $0x3, %ecx
cmpl %ecx, %eax
jge 0x179a8
callq 0x146d10
movsd %xmm0, 0x90(%rsp)
cmpl $0x0, 0x1e7f53(%rip)    # 0x1ff7d4
je 0x178c5
movsd 0x90(%rsp), %xmm0
movsd 0x90(%rsp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0x18e750(%rip), %xmm1   # 0x1a6008
addsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x18810
movsd 0x38(%rsp), %xmm0
cvtsi2sd %eax, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x40(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x18810
movl %eax, %ecx
movl 0x40(%rsp), %eax
cmpl %ecx, %eax
jl 0x17933
movq 0xb8(%rsp), %rdi
callq 0x18810
subl $0x1, %eax
movl %eax, 0x8c(%rsp)
movq 0xb8(%rsp), %rdi
movl 0x8c(%rsp), %esi
callq 0x192e0
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
movss 0x1c(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x17968
jp 0x17968
jmp 0x17992
movq 0x80(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x1c(%rax)
movl 0x9c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x9c(%rsp)
cmpl $0x0, %eax
jne 0x17990
jmp 0x179a8
jmp 0x17992
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x17843
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x18800
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x18810
movl %eax, 0x34(%rsp)
jmp 0x179de
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x17aa3
movq 0xb8(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x192e0
movq %rax, 0x28(%rsp)
jmp 0x17a06
movq 0x28(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movss 0x1c(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x17a62
jp 0x17a62
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x17a43
movq 0x20(%rsp), %rdi
movl $0x24, %esi
callq 0x13580
jmp 0x17a93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x18840
jmp 0x17baa
movq 0x48(%rsp), %rax
movss 0x1c(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0x18f20
jmp 0x17a91
jmp 0x17a93
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x179c3
movq 0xb8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x19300
jmp 0x17ab7
cmpl $0x0, 0x1e7d0a(%rip)    # 0x1ff7c8
je 0x17b86
leaq 0x18e7e4(%rip), %rdi    # 0x1a62af
xorl %eax, %eax
callq 0x13070
jmp 0x17ad4
leaq 0xa0(%rsp), %rdi
callq 0x1433d0
movsd %xmm0, 0x18(%rsp)
jmp 0x17ae9
movsd 0x18(%rsp), %xmm0
leaq 0x18e6d1(%rip), %rdi    # 0x1a61c7
movb $0x1, %al
callq 0x13070
jmp 0x17aff
movq 0xb8(%rsp), %rdi
callq 0x18810
movl %eax, 0x14(%rsp)
jmp 0x17b12
movl 0x14(%rsp), %esi
leaq 0x18e766(%rip), %rdi    # 0x1a6283
xorl %eax, %eax
callq 0x13070
jmp 0x17b26
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x18810
movl %eax, 0x10(%rsp)
jmp 0x17b45
movsd 0x8(%rsp), %xmm0
movl 0x10(%rsp), %eax
cvtsi2sd %eax, %xmm1
movsd 0x18e4bd(%rip), %xmm2   # 0x1a6018
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x18e759(%rip), %rdi    # 0x1a62c3
movb $0x1, %al
callq 0x13070
jmp 0x17b73
movq 0x1de416(%rip), %rax    # 0x1f5f90
movq (%rax), %rdi
callq 0x13680
jmp 0x17b84
jmp 0x17b86
movl $0x1, 0xc4(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x18840
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)