// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::FitPlane(gaps::R3SurfelPointSet*)
R3Plane
FitPlane(R3SurfelPointSet *pointset)
{
  // Allocate points
  int max_points = 1024;
  int npoints = pointset->NPoints();
  if (npoints > max_points) npoints = max_points;
  if (npoints == 0) return R3null_plane;
  R3Point *points = new R3Point [npoints];
  int step = pointset->NPoints() / npoints;
  if (step == 0) step = 1;
  for (int i = 0; i < npoints; i++) {
    const R3SurfelPoint *point = pointset->Point(i*step);
    points[i] = point->Position();
  }

  // Solve for plane
  R3Point centroid = R3Centroid(npoints, points);
  R3Triad triad = R3PrincipleAxes(centroid, npoints, points);
  R3Vector normal = triad[2];
  if (normal[2] < 0) normal.Flip();
  R3Plane plane(centroid, normal);

  // Delete points
  delete [] points;

  // Return plane
  return plane;
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x38(%rsp)
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movl $0x400, 0x104(%rsp) # imm = 0x400
movq 0x108(%rsp), %rdi
callq 0x18700
movl %eax, 0x100(%rsp)
movl 0x100(%rsp), %eax
cmpl 0x104(%rsp), %eax
jle 0x3cde1
movl 0x104(%rsp), %eax
movl %eax, 0x100(%rsp)
cmpl $0x0, 0x100(%rsp)
jne 0x3ce01
movq 0x38(%rsp), %rdi
leaq 0x5c0ee9(%rip), %rsi    # 0x5fdce0
callq 0xf5990
jmp 0x3d03c
movslq 0x100(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x18, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x20(%rsp)
callq 0x13030
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x30(%rsp)
cmpq $0x0, %rax
je 0x3ce8e
movq 0x30(%rsp), %rax
movq 0x28(%rsp), %rcx
imulq $0x18, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xf8340
jmp 0x3ce76
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jne 0x3ce65
movq 0x30(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x108(%rsp), %rdi
callq 0x18700
cltd
idivl 0x100(%rsp)
movl %eax, 0xe8(%rsp)
cmpl $0x0, 0xe8(%rsp)
jne 0x3cef6
movl $0x1, 0xe8(%rsp)
jmp 0x3cef6
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
callq 0x13a90
jmp 0x3d049
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
cmpl 0x100(%rsp), %eax
jge 0x3cf81
movq 0x108(%rsp), %rdi
movl 0xe4(%rsp), %esi
imull 0xe8(%rsp), %esi
callq 0x18710
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x18730
movq 0xf8(%rsp), %rdi
movslq 0xe4(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xc0(%rsp), %rsi
callq 0xf89e0
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x3cf01
movl 0x100(%rsp), %esi
movq 0xf8(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %ecx
callq 0x97b20
movl 0x100(%rsp), %edx
movq 0xf8(%rsp), %rcx
leaq 0x60(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x97be0
leaq 0x60(%rsp), %rdi
movl $0x2, %esi
callq 0x44220
movq %rax, %rsi
leaq 0x48(%rsp), %rdi
callq 0xf94f0
leaq 0x48(%rsp), %rdi
movl $0x2, %esi
callq 0x31d80
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
jbe 0x3d008
leaq 0x48(%rsp), %rdi
callq 0xf9c90
movq 0x38(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0xf5ab0
movq 0xf8(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x3d03a
movq (%rsp), %rdi
callq 0x13750
jmp 0x3d03c
movq 0x40(%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xf0(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)