// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Align.cpp
// Function: gaps::R2PrincipleAxes(gaps::R2Point const&, gaps::RNArray<gaps::R2Point*> const&, double const*, double*)
R2Diad 
R2PrincipleAxes(const R2Point& centroid, const RNArray<R2Point *>& points, const RNScalar *weights, RNScalar *variances)
{
  // Compute covariance matrix
  RNScalar m[4] = { 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];
    m[0] += weight * x*x;
    m[1] += weight * x*y;
    m[2] += weight * y*x;
    m[3] += weight * y*y;
    total_weight += weight;
  }

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

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

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

  // Flip axes so that "heavier" on positive side
  int positive_count[2] = { 0, 0 };
  int negative_count[2] = { 0, 0 };
  for (int i = 0; i < points.NEntries(); i++) {
    R2Point *point = points[i];
    for (int j = 0; j < 2; j++) {
      RNScalar dot = axes[j].Dot(point->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();
    }
  }

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

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

  // Return diad of axes
  return R2Diad(axes[0], axes[1]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, %rbx
movslq 0xc(%rdx), %rax
testq %rax, %rax
jle 0x6da32
movq %r8, %r14
movq %rdx, %r12
movq (%rdx), %rdx
movupd (%rsi), %xmm1
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
xorl %esi, %esi
movsd 0x34674(%rip), %xmm3    # 0xa2038
xorpd %xmm4, %xmm4
movapd %xmm3, %xmm5
testq %rcx, %rcx
je 0x6d9d6
movsd (%rcx,%rsi,8), %xmm5
movq (%rdx,%rsi,8), %rdi
movupd (%rdi), %xmm6
subpd %xmm1, %xmm6
movapd %xmm5, %xmm7
mulsd %xmm6, %xmm7
unpcklpd %xmm7, %xmm7            # xmm7 = xmm7[0,0]
mulpd %xmm6, %xmm7
addpd %xmm7, %xmm2
movapd %xmm2, 0x60(%rsp)
movapd %xmm6, %xmm7
unpckhpd %xmm6, %xmm7            # xmm7 = xmm7[1],xmm6[1]
mulsd %xmm5, %xmm7
unpcklpd %xmm7, %xmm7            # xmm7 = xmm7[0,0]
mulpd %xmm6, %xmm7
addpd %xmm7, %xmm4
movapd %xmm4, 0x70(%rsp)
addsd %xmm5, %xmm0
incq %rsi
cmpq %rsi, %rax
jne 0x6d9c8
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x6da46
jp 0x6da46
leaq 0x47a577(%rip), %rsi    # 0x4e7fb0
movq %rbx, %rdi
callq 0x65f9e
jmp 0x6dc46
movq %rbx, 0x50(%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
xorl %eax, %eax
movapd 0x60(%rsp,%rax,8), %xmm1
divpd %xmm0, %xmm1
movapd %xmm1, 0x60(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x6da51
leaq 0x60(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0x80(%rsp), %r8
leaq 0x90(%rsp), %r9
movl $0x2, %edi
movl $0x2, %esi
callq 0x710b6
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x20, %rdi
callq 0x683d0
addq $0x10, %rbx
cmpq $0x20, %rbx
jne 0x6da99
movapd 0x90(%rsp), %xmm0
movaps 0xa0(%rsp), %xmm1
leaq 0x30(%rsp), %r15
movapd %xmm0, -0x10(%r15)
movaps %xmm1, (%r15)
leaq 0x20(%rsp), %rdi
callq 0x68476
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
callq 0x68476
movsd 0x8(%rsp), %xmm2
movapd %xmm0, %xmm1
leaq 0x7809f(%rip), %rbx     # 0xe5b98
movsd (%rbx), %xmm0
ucomisd %xmm0, %xmm2
jbe 0x6db21
leaq 0x20(%rsp), %rdi
movapd %xmm2, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x68842
movsd 0x8(%rsp), %xmm1
movsd (%rbx), %xmm0
movq %r14, 0x48(%rsp)
ucomisd %xmm0, %xmm1
jbe 0x6db38
movq %r15, %rdi
movapd %xmm1, %xmm0
callq 0x68842
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpl $0x0, 0xc(%r12)
jle 0x6dbdc
xorl %ecx, %ecx
leaq 0xb0(%rsp), %rbp
leaq 0x18(%rsp), %r15
movq %r12, 0x58(%rsp)
movq (%r12), %rax
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx,8), %r13
movb $0x1, %bl
leaq 0x20(%rsp), %r12
xorl %r14d, %r14d
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movq %rbp, %rdi
callq 0x683d2
movq %r12, %rdi
movq %rbp, %rsi
callq 0x68540
ucomisd 0x34646(%rip), %xmm0    # 0xa21e8
movq %r15, %rax
ja 0x6dbac
leaq 0x10(%rsp), %rax
incl (%rax,%r14,4)
movl $0x1, %r14d
leaq 0x30(%rsp), %r12
testb $0x1, %bl
movl $0x0, %ebx
jne 0x6db7b
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x58(%rsp), %r12
movslq 0xc(%r12), %rax
cmpq %rax, %rcx
jl 0x6db64
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %rax
movb $0x1, %bl
movq 0x48(%rsp), %r14
leaq 0x14(%rsp), %r15
leaq 0x1c(%rsp), %r12
leaq 0x20(%rsp), %rdi
movl (%rcx), %ecx
cmpl (%rax), %ecx
jge 0x6dc07
callq 0x685fe
movq %r15, %rax
movq %r12, %rcx
leaq 0x30(%rsp), %rdi
testb $0x1, %bl
movl $0x0, %ebx
jne 0x6dbfc
testq %r14, %r14
je 0x6dc2f
movapd 0x80(%rsp), %xmm0
movupd %xmm0, (%r14)
leaq 0x20(%rsp), %rsi
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x65fe2
movq %rbx, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq