// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Align.cpp
// Function: gaps::R3EstimatePlaneWithRansac(gaps::RNArray<gaps::R3Point*> const&, double const*, double, int, double*, double*)
R3Plane
R3EstimatePlaneWithRansac(const RNArray<R3Point *>& points, const RNScalar *weights,
  RNScalar tolerance, int max_iterations,
  RNScalar *max_inlier_fraction, RNScalar *avg_inlier_fraction)
{
  // Check number of points
  if (points.NEntries() < 3) return R3null_plane;
  
  // Try PCA plane
  RNScalar variances[3];
  RNArray<R3Point *> inliers;
  RNScalar *w = (weights) ? new RNScalar [ points.NEntries() ] : NULL;
  R3Point centroid = R3Centroid(points);
  R3Triad axes = R3PrincipleAxes(centroid, points, weights, variances);
  if (RNIsZero(variances[1])) return R3null_plane;
  R3Plane plane(centroid, axes[2]);
  if (tolerance == 0) tolerance = sqrt(variances[2]);
  for (int i = 0; i < points.NEntries(); i++) {
    R3Point *point = points[i];
    RNScalar d = R3Distance(plane, *point);
    if (d > tolerance) continue; 
    if (w) w[inliers.NEntries()] = weights[i];
    inliers.Insert(point);
  }

  // Initialize best score
  R3Plane best_plane = plane;
  RNScalar score = (RNScalar) inliers.NEntries() / (RNScalar) points.NEntries();
  RNScalar best_score = score;
  RNScalar total_score = score;
  int nscores = 1;

  // Search for a best normal
  for (int i = 0; i < max_iterations; i++) {
    // Guess normal by selecting three random points
    R3Point *p0 = points[(int) (RNRandomScalar() * points.NEntries()) ];
    R3Point *p1 = points[(int) (RNRandomScalar() * points.NEntries()) ];
    R3Point *p2 = points[(int) (RNRandomScalar() * points.NEntries()) ];
    if (R3Contains(*p0, *p1) || R3Contains(*p1, *p2) || R3Contains(*p2, *p0)) continue;
    R3Plane plane(*p0, *p1, *p2);
    
    // Find inliers
    inliers.Empty();
    for (int i = 0; i < points.NEntries(); i++) {
      R3Point *point = points[i];
      RNScalar d = R3Distance(plane, *point);
      if (d > tolerance) continue; 
      if (w) w[inliers.NEntries()] = weights[i];
      inliers.Insert(point);
    }

    // Compute  score
    RNScalar score = (RNScalar) inliers.NEntries() / (RNScalar) points.NEntries();
    total_score += score;
    nscores++;

    // Check score
    if (score <= best_score) continue;
    
    // Recompute plane with just inliers
    plane = R3EstimatePlaneWithPCA(inliers, w);
    if (plane == R3null_plane) continue;
    
    // Update best stuff
    best_plane = plane;
    best_score = score;
  }

  // Update returned results
  if (max_inlier_fraction) *max_inlier_fraction = best_score;
  if (avg_inlier_fraction) *avg_inlier_fraction = total_score / nscores;
  
  // Return best plane
  return best_plane;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %r9, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movl %ecx, 0x4(%rsp)
movsd %xmm0, 0x8(%rsp)
cmpl $0x2, 0xc(%rsi)
jg 0x46e25
leaq 0x49488b(%rip), %rsi    # 0x4db6a0
movq %rdi, %rbx
callq 0x36720
movq %rbx, %rax
jmp 0x471ac
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, 0x28(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x6630a
testq %rbx, %rbx
je 0x46e5f
movslq 0xc(%r13), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r14
jmp 0x46e62
xorl %r14d, %r14d
leaq 0x98(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x45a87
leaq 0xf0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x60(%rsp), %r8
movq %r13, %rdx
movq %rbx, %rcx
callq 0x45b2f
movsd 0x68(%rsp), %xmm0
leaq 0x93d07(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
movapd 0x4c183(%rip), %xmm2    # 0x93030
xorpd %xmm1, %xmm2
ucomisd %xmm2, %xmm0
jb 0x46ed3
ucomisd %xmm0, %xmm1
jb 0x46ed3
leaq 0x4947dc(%rip), %rsi    # 0x4db6a0
movq 0x28(%rsp), %rdi
callq 0x36720
jmp 0x4719d
movq %rbx, 0x48(%rsp)
leaq 0x120(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x367ac
xorpd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jne 0x46f24
jp 0x46f24
movsd 0x70(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x46f19
sqrtsd %xmm0, %xmm0
jmp 0x46f1e
callq 0xe350
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
leaq 0xb0(%rsp), %rbx
leaq 0x18(%rsp), %r15
movslq 0xc(%r13), %rax
cmpq %rax, %rbp
jge 0x46f81
movq (%r13), %rax
movq (%rax,%rbp,8), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x433d6
ucomisd 0x8(%rsp), %xmm0
ja 0x46f7c
testq %r14, %r14
je 0x46f71
movq 0x48(%rsp), %rax
movsd (%rax,%rbp,8), %xmm0
movslq 0x24(%rsp), %rax
movsd %xmm0, (%r14,%rax,8)
movq %r15, %rdi
movq %r12, %rsi
callq 0x22bfc
incq %rbp
jmp 0x46f33
leaq 0xb0(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x36720
leaq 0x18(%rsp), %rax
cvtsi2sdl 0xc(%rax), %xmm1
cvtsi2sdl 0xc(%r13), %xmm0
divsd %xmm0, %xmm1
xorl %ebx, %ebx
movl 0x4(%rsp), %eax
testl %eax, %eax
cmovlel %ebx, %eax
movl %eax, 0x4(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movsd %xmm1, 0x30(%rsp)
movsd %xmm1, 0x38(%rsp)
cmpl 0x4(%rsp), %ebx
je 0x4716b
callq 0x67e16
cvtsi2sdl 0xc(%r13), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movq (%r13), %rcx
cltq
movq (%rcx,%rax,8), %r15
callq 0x67e16
cvtsi2sdl 0xc(%r13), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movq (%r13), %rcx
cltq
movq (%rcx,%rax,8), %r12
callq 0x67e16
cvtsi2sdl 0xc(%r13), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movq (%r13), %rcx
cltq
movq (%rcx,%rax,8), %rbp
movq %r15, %rdi
movq %r12, %rsi
callq 0x40dd7
testl %eax, %eax
jne 0x47164
movq %r12, %rdi
movq %rbp, %rsi
callq 0x40dd7
testl %eax, %eax
jne 0x47164
movq %rbp, %rdi
movq %r15, %rsi
callq 0x40dd7
testl %eax, %eax
jne 0x47164
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x36918
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x66528
xorl %r12d, %r12d
movslq 0xc(%r13), %rax
cmpq %rax, %r12
jge 0x470d8
movq (%r13), %rax
movq (%rax,%r12,8), %r15
movq %r15, %rdi
leaq 0x78(%rsp), %rsi
callq 0x433d6
ucomisd 0x8(%rsp), %xmm0
ja 0x470d3
testq %r14, %r14
je 0x470c6
movq 0x48(%rsp), %rax
movsd (%rax,%r12,8), %xmm0
movslq 0x24(%rsp), %rax
movsd %xmm0, (%r14,%rax,8)
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x22bfc
incq %r12
jmp 0x47085
cvtsi2sdl 0x24(%rsp), %xmm1
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
movsd %xmm1, 0x40(%rsp)
ucomisd %xmm1, %xmm0
jae 0x4714e
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
leaq 0x18(%rsp), %rsi
movq %r14, %rdx
callq 0x46d40
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x223a4
movq %r15, %rdi
leaq 0x494576(%rip), %rsi    # 0x4db6a0
callq 0x477ca
testl %eax, %eax
jne 0x4714e
movq 0x28(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x223a4
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x38(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
incl 0x14(%rsp)
incl %ebx
jmp 0x46fc9
movq 0x50(%rsp), %rax
testq %rax, %rax
je 0x4717f
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x4719d
cvtsi2sdl 0x14(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rax)
leaq 0x18(%rsp), %rdi
callq 0x66378
movq 0x28(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
jmp 0x471d4
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x66378
movq %rbx, %rdi
callq 0xe810