// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelPointSet.cpp
// Function: gaps::R3SurfelPointSet::PrincipleAxes(gaps::R3Point const*, double*) const
R3Triad R3SurfelPointSet::
PrincipleAxes(const R3Point *center, RNScalar *variances) const
{
  // Check points
  if (npoints == 0) {
    if (variances) { 
      variances[0] = 0.0;
      variances[1] = 0.0;
      variances[2] = 0.0;
    }
    return R3xyz_triad;
  }

  // Get centroid
  R3Point centroid = (center) ? *center : Centroid();

  // Compute covariance matrix
  RNScalar m[9] = { 0 };
  for (int i = 0; i < npoints; i++) {
    R3SurfelPoint& point = points[i];
    RNScalar x = point.X() - centroid[0];
    RNScalar y = point.Y() - centroid[1];
    RNScalar z = point.Z() - centroid[2];
    m[0] += x*x;
    m[4] += y*y;
    m[8] += z*z;
    m[1] += x*y;
    m[3] += x*y;
    m[2] += x*z;
    m[6] += x*z;
    m[5] += y*z;
    m[7] += y*z;
  }

  // Normalize covariance matrix
  for (int i = 0; i < 9; i++) m[i] /= npoints;

  // Compute eigenvalues and eigenvectors
  RNScalar U[9];
  RNScalar W[3];
  RNScalar Vt[9];
  RNSvdDecompose(3, 3, m, U, W, Vt);  // m == U . DiagonalMatrix(W) . Vt

  // Copy principle axes into more convenient form
  // W has eigenvalues (greatest to smallest) and Vt has eigenvectors (normalized)
  R3Vector axes[3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      axes[i][j] = Vt[3*i+j];
    }
  }

  // Flip axes so that "heavier" on positive side for first two dimensions
  int positive_count[3] = { 0, 0, 0 };
  int negative_count[3] = { 0, 0, 0 };
  for (int i = 0; i < npoints; i++) {
    R3SurfelPoint& point = points[i];
    RNScalar x = point.X() - centroid[0];
    RNScalar y = point.Y() - centroid[1];
    RNScalar z = point.Z() - centroid[2];
    R3Vector vector(x, y, z);
    for (int j = 0; j < 2; j++) {
      RNScalar dot = axes[j].Dot(vector);
      if (dot > 0.0) positive_count[j]++;
      else negative_count[j]++;
    }
  }
  for (int j =0; j < 2; j++) {
    if (positive_count[j] < negative_count[j]) {
      axes[j].Flip();
    }
  }

  // Set third axis to form orthonormal triad with other two
  axes[2] = axes[0] % axes[1];

  // Just checking
  assert(RNIsEqual(axes[0].Length(), 1.0, RN_BIG_EPSILON));
  assert(RNIsEqual(axes[1].Length(), 1.0, RN_BIG_EPSILON));
  assert(RNIsEqual(axes[2].Length(), 1.0, RN_BIG_EPSILON));
  assert(RNIsZero(axes[0].Dot(axes[1]), RN_BIG_EPSILON));
  assert(RNIsZero(axes[1].Dot(axes[2]), RN_BIG_EPSILON));
  assert(RNIsZero(axes[0].Dot(axes[2]), RN_BIG_EPSILON));

  // Return variances (eigenvalues)
  if (variances) {
    variances[0] = W[0];
    variances[1] = W[1];
    variances[2] = W[2];
  }

  // Return triad
  return R3Triad(axes[0], axes[1], axes[2]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, %rbx
cmpl $0x0, 0x10(%rsi)
je 0x2f7de
movq %rsi, %r15
movq %rcx, 0x18(%rsp)
movq %rsp, %rdi
testq %rdx, %rdx
je 0x2f804
movq %rdx, %rsi
callq 0x781cc
jmp 0x2f80c
testq %rcx, %rcx
je 0x2f7f0
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx)
andq $0x0, 0x10(%rcx)
leaq 0xf6511(%rip), %rsi     # 0x125d08
movq %rbx, %rdi
callq 0x672a8
jmp 0x2fb29
movq %r15, %rsi
callq 0x2f6e6
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movl 0x10(%r15), %eax
movq 0x8(%r15), %rcx
movapd (%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
xorl %edx, %edx
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
shlq $0x4, %rsi
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
cmpq %rdx, %rsi
je 0x2f90f
movq (%rcx,%rdx), %rdi
movq 0x8(%rcx,%rdx), %r8
cvtps2pd (%r8), %xmm7
movupd 0x10(%rdi), %xmm8
addpd %xmm7, %xmm8
cvtss2sd 0x8(%r8), %xmm7
addsd 0x20(%rdi), %xmm7
subpd %xmm1, %xmm8
subsd %xmm2, %xmm7
movapd %xmm7, %xmm9
mulsd %xmm7, %xmm9
addsd %xmm9, %xmm3
movsd %xmm3, 0x70(%rsp)
movapd %xmm8, %xmm9
unpcklpd %xmm8, %xmm9            # xmm9 = xmm9[0],xmm8[0]
movapd %xmm8, %xmm10
movsd %xmm7, %xmm10           # xmm10 = xmm7[0],xmm10[1]
mulpd %xmm9, %xmm10
mulpd %xmm8, %xmm9
addpd %xmm9, %xmm0
addpd %xmm10, %xmm4
movapd %xmm4, 0x40(%rsp)
movapd %xmm8, %xmm9
shufpd $0x1, %xmm7, %xmm9      # xmm9 = xmm9[1],xmm7[0]
unpcklpd %xmm7, %xmm7            # xmm7 = xmm7[0,0]
mulpd %xmm8, %xmm7
unpckhpd %xmm8, %xmm8            # xmm8 = xmm8[1,1]
mulpd %xmm8, %xmm9
addpd %xmm9, %xmm5
movapd %xmm5, 0x50(%rsp)
addpd %xmm7, %xmm6
movapd %xmm6, 0x60(%rsp)
addq $0x10, %rdx
jmp 0x2f85b
movapd %xmm0, 0x30(%rsp)
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
cmpq $0x9, %rax
je 0x2f936
movsd 0x30(%rsp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp,%rax,8)
incq %rax
jmp 0x2f91b
movq %rbx, 0x20(%rsp)
pushq $0x3
popq %rdi
leaq 0x30(%rsp), %rdx
leaq 0x170(%rsp), %rcx
leaq 0xa0(%rsp), %r8
leaq 0x120(%rsp), %r12
movl %edi, %esi
movq %r12, %r9
callq 0x9bd9a
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0xc0, %rdi
callq 0x7897a
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x2f967
leaq 0xc0(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x2f9b6
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x2f9a9
movsd (%r12,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x2f993
incq %rcx
addq $0x18, %rax
addq $0x18, %r12
jmp 0x2f98b
leaq 0x90(%rsp), %rbp
andl $0x0, 0x8(%rbp)
andq $0x0, (%rbp)
leaq 0x80(%rsp), %rbx
andl $0x0, 0x8(%rbx)
andq $0x0, (%rbx)
xorl %r14d, %r14d
leaq 0x108(%rsp), %r12
movq %r15, 0x28(%rsp)
movslq 0x10(%r15), %rax
cmpq %rax, %r14
jge 0x2fa84
movq 0x8(%r15), %rax
movq %r14, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rdx
movq 0x8(%rax,%rcx), %rax
cvtss2sd (%rax), %xmm0
addsd 0x10(%rdx), %xmm0
subsd (%rsp), %xmm0
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rdx), %xmm1
subsd 0x8(%rsp), %xmm1
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rdx), %xmm2
subsd 0x10(%rsp), %xmm2
movq %r12, %rdi
callq 0x7897c
leaq 0xc0(%rsp), %r13
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x2fa77
movq %r13, %rdi
movq %r12, %rsi
callq 0x78bf6
ucomisd 0x9891e(%rip), %xmm0    # 0xc8380
movq %rbp, %rax
ja 0x2fa6a
movq %rbx, %rax
incl (%rax,%r15,4)
incq %r15
addq $0x18, %r13
jmp 0x2fa49
incq %r14
movq 0x28(%rsp), %r15
jmp 0x2f9e7
leaq 0xc0(%rsp), %r15
xorl %ebx, %ebx
cmpq $0x2, %rbx
je 0x2fab5
movl 0x90(%rsp,%rbx,4), %eax
cmpl 0x80(%rsp,%rbx,4), %eax
jge 0x2faac
movq %r15, %rdi
callq 0x78cd2
incq %rbx
addq $0x18, %r15
jmp 0x2fa8e
leaq 0xd8(%rsp), %r15
leaq 0x108(%rsp), %r13
leaq 0xc0(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x7902e
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x78eec
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x2fb0e
movaps 0xa0(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0xc0(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x672fa
movq %rbx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq