// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Align.cpp
// Function: gaps::R3PrincipleAxes(gaps::R3Point const&, gaps::RNArray<gaps::R3Point*> const&, double const*, double*)
R3Triad 
R3PrincipleAxes(const R3Point& centroid, const RNArray<R3Point *>& points, const RNScalar *weights, RNScalar *variances)
{
  // Compute covariance matrix
  RNScalar m[9] = { 0 };
  RNScalar total_weight = 0;
  for (int i = 0; i < points.NEntries(); i++) {
    RNScalar weight = (weights) ? weights[i] : 1;
    RNScalar x = points[i]->X() - centroid[0];
    RNScalar y = points[i]->Y() - centroid[1];
    RNScalar z = points[i]->Z() - centroid[2];
    m[0] += weight * x*x;
    m[1] += weight * x*y;
    m[2] += weight * x*z;
    m[3] += weight * y*x;
    m[4] += weight * y*y;
    m[5] += weight * y*z;
    m[6] += weight * z*x;
    m[7] += weight * z*y;
    m[8] += weight * z*z;
    total_weight += weight;
  }

  // Normalize covariance matrix
  if (total_weight == 0) return R3xyz_triad;
  for (int i = 0; i < 9; i++) {
    m[i] /= total_weight;
  }

  // Calculate SVD of second order moments
  RNScalar U[9];
  RNScalar W[3];
  RNScalar Vt[9];
  RNSvdDecompose(3, 3, m, U, W, Vt);  // m == U . DiagonalMatrix(W) . Vt

  // Principle axes are in Vt
  R3Vector axes[3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      axes[i][j] = Vt[3*i+j];
    }
  }
  
  // Normalize all axis vectors (probably not necessary)
  RNLength length0 = axes[0].Length();
  RNLength length1 = axes[1].Length();
  RNLength length2 = axes[2].Length();
  if (RNIsPositive(length0)) axes[0] /= length0;
  if (RNIsPositive(length1)) axes[1] /= length1;
  if (RNIsPositive(length2)) axes[2] /= length2;

  // Flip axes so that "heavier" on positive side
  int positive_count[3] = { 0, 0, 0 };
  int negative_count[3] = { 0, 0, 0 };
  for (int i = 0; i < points.NEntries(); i++) {
    R3Point *point = points[i];
    for (int j = 0; j < 3; j++) {
      RNScalar dot = axes[j].Dot(point->Vector());
      if (dot > 0.0) positive_count[j]++;
      else negative_count[j]++;
    }
  }
  for (int j =0; j < 3; j++) {
    if (positive_count[j] < negative_count[j]) {
      axes[j].Flip();
    }
  }

  // Compute orthonormal triad of axes
  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 of axes
  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
movslq 0xc(%rdx), %rax
testq %rax, %rax
jle 0x4c3cf
movq %rdx, %r14
movq (%rdx), %rdx
movupd (%rsi), %xmm1
movsd 0x10(%rsi), %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
xorl %esi, %esi
movsd 0x55d5d(%rip), %xmm5    # 0xa2038
xorpd %xmm0, %xmm0
xorpd %xmm6, %xmm6
xorpd %xmm7, %xmm7
xorpd %xmm8, %xmm8
movapd %xmm5, %xmm9
testq %rcx, %rcx
je 0x4c2fc
movsd (%rcx,%rsi,8), %xmm9
movq (%rdx,%rsi,8), %rdi
movsd 0x10(%rdi), %xmm12
subsd %xmm2, %xmm12
movupd (%rdi), %xmm10
subpd %xmm1, %xmm10
movapd %xmm9, %xmm11
unpcklpd %xmm9, %xmm11           # xmm11 = xmm11[0],xmm9[0]
mulpd %xmm10, %xmm11
movapd %xmm11, %xmm13
unpcklpd %xmm11, %xmm13          # xmm13 = xmm13[0],xmm11[0]
mulpd %xmm10, %xmm13
addpd %xmm13, %xmm3
movapd %xmm10, %xmm13
shufpd $0x1, %xmm12, %xmm13    # xmm13 = xmm13[1],xmm12[0]
addsd %xmm9, %xmm0
mulsd %xmm12, %xmm9
movapd %xmm9, %xmm14
mulsd %xmm12, %xmm9
unpcklpd %xmm10, %xmm12          # xmm12 = xmm12[0],xmm10[0]
mulpd %xmm11, %xmm12
addpd %xmm12, %xmm6
movapd %xmm3, 0xc0(%rsp)
movapd %xmm6, 0xd0(%rsp)
unpckhpd %xmm11, %xmm11          # xmm11 = xmm11[1,1]
mulpd %xmm11, %xmm13
addpd %xmm13, %xmm7
movapd %xmm7, 0xe0(%rsp)
unpcklpd %xmm14, %xmm14          # xmm14 = xmm14[0,0]
mulpd %xmm10, %xmm14
addpd %xmm14, %xmm8
movapd %xmm8, 0xf0(%rsp)
addsd %xmm9, %xmm4
movsd %xmm4, 0x100(%rsp)
incq %rsi
cmpq %rsi, %rax
jne 0x4c2ec
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4c3e3
jp 0x4c3e3
leaq 0x99de2(%rip), %rsi     # 0xe61b8
movq %rbx, %rdi
callq 0x2ab46
jmp 0x4c646
movq %r8, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
xorl %eax, %eax
movsd 0xc0(%rsp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0xc0(%rsp,%rax,8)
incq %rax
cmpq $0x9, %rax
jne 0x4c3ef
leaq 0xc0(%rsp), %rdx
leaq 0x170(%rsp), %rcx
leaq 0xa0(%rsp), %r8
leaq 0x120(%rsp), %r9
movl $0x3, %edi
movl $0x3, %esi
callq 0x710b6
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x50, %rdi
callq 0x3cfc4
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x4c43f
xorl %eax, %eax
movq 0x130(%rsp,%rax), %rcx
movq %rcx, 0x60(%rsp,%rax)
movupd 0x120(%rsp,%rax), %xmm0
movupd %xmm0, 0x50(%rsp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0x4c458
leaq 0x50(%rsp), %rdi
callq 0x3d0cc
movsd %xmm0, 0x28(%rsp)
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x3d0cc
movsd %xmm0, 0x30(%rsp)
leaq 0x80(%rsp), %r12
movq %r12, %rdi
callq 0x3d0cc
movapd %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
leaq 0x996d6(%rip), %rbx     # 0xe5b98
movsd (%rbx), %xmm2
ucomisd %xmm2, %xmm0
movsd %xmm1, (%rsp)
jbe 0x4c4e4
leaq 0x50(%rsp), %rdi
callq 0x3db98
movsd (%rsp), %xmm1
movsd (%rbx), %xmm2
movsd 0x30(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x4c501
movq %r15, %rdi
callq 0x3db98
movsd (%rsp), %xmm1
movsd (%rbx), %xmm2
ucomisd %xmm2, %xmm1
jbe 0x4c513
movq %r12, %rdi
movapd %xmm1, %xmm0
callq 0x3db98
xorl %eax, %eax
movl %eax, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movl %eax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cmpl %eax, 0xc(%r14)
jle 0x4c5b5
xorl %ecx, %ecx
leaq 0x108(%rsp), %r12
leaq 0x18(%rsp), %rbx
leaq 0x8(%rsp), %r15
movq %r14, 0x48(%rsp)
movq (%r14), %rax
movq %rcx, (%rsp)
movq (%rax,%rcx,8), %rbp
leaq 0x50(%rsp), %r13
xorl %r14d, %r14d
movsd (%rbp), %xmm0
movsd 0x8(%rbp), %xmm1
movsd 0x10(%rbp), %xmm2
movq %r12, %rdi
callq 0x3cfc6
movq %r13, %rdi
movq %r12, %rsi
callq 0x3d252
ucomisd 0x55c61(%rip), %xmm0    # 0xa21e8
movq %rbx, %rax
ja 0x4c58f
movq %r15, %rax
incl (%rax,%r14,4)
incq %r14
addq $0x18, %r13
cmpq $0x3, %r14
jne 0x4c55d
movq (%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %r14
movslq 0xc(%r14), %rax
cmpq %rax, %rcx
jl 0x4c54a
xorl %ebx, %ebx
leaq 0x68(%rsp), %r14
leaq 0x80(%rsp), %r12
leaq 0x50(%rsp), %r15
movl 0x18(%rsp,%rbx,4), %eax
cmpl 0x8(%rsp,%rbx,4), %eax
jge 0x4c5db
movq %r15, %rdi
callq 0x3d334
incq %rbx
addq $0x18, %r15
cmpq $0x3, %rbx
jne 0x4c5c9
leaq 0x108(%rsp), %r15
leaq 0x50(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x3d8fa
movq %r12, %rdi
movq %r15, %rsi
callq 0x3d752
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x4c62e
movaps 0xa0(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0x50(%rsp), %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0x2ab98
movq %rbx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq