// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, %rbx
cmpl $0x0, 0x10(%rsi)
je 0x328e2
movq %rsi, %r15
movq %rcx, 0x30(%rsp)
leaq 0x18(%rsp), %rdi
testq %rdx, %rdx
je 0x3290b
movq %rdx, %rsi
callq 0x83dd2
jmp 0x32913
testq %rcx, %rcx
je 0x328f7
xorpd %xmm0, %xmm0
movupd %xmm0, (%rcx)
movq $0x0, 0x10(%rcx)
leaq 0x1023ea(%rip), %rsi    # 0x134ce8
movq %rbx, %rdi
callq 0x70bc6
jmp 0x32c7b
movq %r15, %rsi
callq 0x327dc
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movq $0x0, 0x80(%rsp)
movslq 0x10(%r15), %rax
testq %rax, %rax
jle 0x32a3b
movq 0x8(%r15), %rcx
movsd 0x18(%rsp), %xmm2
movupd 0x20(%rsp), %xmm3
movq %rax, %rdx
shlq $0x4, %rdx
xorpd %xmm1, %xmm1
xorl %esi, %esi
xorpd %xmm4, %xmm4
xorpd %xmm6, %xmm6
xorpd %xmm5, %xmm5
movq 0x8(%rcx,%rsi), %rdi
xorps %xmm7, %xmm7
cvtss2sd (%rdi), %xmm7
movq (%rcx,%rsi), %r8
addsd 0x10(%r8), %xmm7
subsd %xmm2, %xmm7
movapd %xmm7, %xmm8
mulsd %xmm7, %xmm8
cvtps2pd 0x4(%rdi), %xmm9
addsd %xmm8, %xmm1
movupd 0x18(%r8), %xmm8
addpd %xmm9, %xmm8
subpd %xmm3, %xmm8
movapd %xmm8, %xmm9
movsd %xmm7, %xmm9            # xmm9 = xmm7[0],xmm9[1]
mulpd %xmm8, %xmm9
addpd %xmm9, %xmm5
movhpd %xmm5, 0x80(%rsp)
movapd %xmm7, %xmm9
unpcklpd %xmm7, %xmm9            # xmm9 = xmm9[0],xmm7[0]
mulpd %xmm8, %xmm9
addpd %xmm9, %xmm0
movapd %xmm0, %xmm9
shufpd $0x1, %xmm0, %xmm9      # xmm9 = xmm9[1],xmm0[0]
movapd %xmm9, 0x50(%rsp)
movapd %xmm8, %xmm9
unpcklpd %xmm8, %xmm9            # xmm9 = xmm9[0],xmm8[0]
mulpd %xmm8, %xmm9
addpd %xmm9, %xmm4
movapd %xmm4, 0x60(%rsp)
unpcklpd %xmm8, %xmm7            # xmm7 = xmm7[0],xmm8[0]
unpckhpd %xmm8, %xmm8            # xmm8 = xmm8[1,1]
mulpd %xmm7, %xmm8
addpd %xmm8, %xmm6
movapd %xmm6, 0x70(%rsp)
addq $0x10, %rsi
cmpq %rsi, %rdx
jne 0x32971
movlpd %xmm5, 0x48(%rsp)
jmp 0x32a3f
xorpd %xmm1, %xmm1
movq %rbx, 0x38(%rsp)
movsd %xmm1, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
xorl %eax, %eax
movsd 0x40(%rsp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rsp,%rax,8)
incq %rax
cmpq $0x9, %rax
jne 0x32a53
leaq 0x40(%rsp), %rdx
leaq 0x160(%rsp), %rcx
leaq 0xe0(%rsp), %r8
leaq 0x110(%rsp), %r9
movl $0x3, %edi
movl $0x3, %esi
callq 0xab552
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
callq 0x84854
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x32a9a
xorl %eax, %eax
movq 0x120(%rsp,%rax), %rcx
movq %rcx, 0xa0(%rsp,%rax)
movupd 0x110(%rsp,%rax), %xmm0
movupd %xmm0, 0x90(%rsp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0x32ab6
xorl %eax, %eax
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
cmpl %eax, 0x10(%r15)
jle 0x32bc1
movl $0x8, %r14d
xorl %ebx, %ebx
leaq 0xf8(%rsp), %r13
leaq 0x14(%rsp), %r12
leaq 0x10(%rsp), %rbp
movq 0x8(%r15), %rax
movq -0x8(%rax,%r14), %rcx
movq (%rax,%r14), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
addsd 0x10(%rcx), %xmm0
subsd 0x18(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax), %xmm1
addsd 0x18(%rcx), %xmm1
subsd 0x20(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rax), %xmm2
addsd 0x20(%rcx), %xmm2
subsd 0x28(%rsp), %xmm2
movq %r13, %rdi
callq 0x84856
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x84ae2
ucomisd 0xaa80b(%rip), %xmm0    # 0xdd388
movq %r12, %rax
ja 0x32b87
leaq 0xc(%rsp), %rax
incl (%rax)
leaq 0xa8(%rsp), %rdi
movq %r13, %rsi
callq 0x84ae2
ucomisd 0xaa7e7(%rip), %xmm0    # 0xdd388
movq %rbp, %rax
ja 0x32bab
leaq 0x8(%rsp), %rax
incl (%rax)
incq %rbx
movslq 0x10(%r15), %rax
addq $0x10, %r14
cmpq %rax, %rbx
jl 0x32b18
leaq 0x90(%rsp), %rdi
leaq 0x14(%rsp), %rcx
leaq 0xc(%rsp), %rax
movb $0x1, %bpl
leaq 0x10(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0xa8(%rsp), %r15
movl (%rcx), %ecx
cmpl (%rax), %ecx
jge 0x32bf3
callq 0x84bc4
movq %r14, %rax
movq %rbx, %rcx
movq %r15, %rdi
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x32be8
leaq 0xa8(%rsp), %r15
leaq 0xf8(%rsp), %r13
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x8518a
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x84fe2
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x32c60
movaps 0xe0(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0x90(%rsp), %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x70c18
movq %rbx, %rax
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq