// File: /workspace/github_cpp_makefile_star2_O1/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 %r8, %r14
movq %rdx, %r12
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movslq 0xc(%rdx), %rax
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testq %rax, %rax
jle 0x6e825
movq (%r12), %rdx
movupd (%rsi), %xmm2
movapd 0x40(%rsp), %xmm3
movapd 0x50(%rsp), %xmm4
xorl %esi, %esi
movsd 0x3387c(%rip), %xmm5    # 0xa2038
movapd %xmm5, %xmm6
testq %rcx, %rcx
je 0x6e7ca
movsd (%rcx,%rsi,8), %xmm6
movq (%rdx,%rsi,8), %rdi
movupd (%rdi), %xmm7
subpd %xmm2, %xmm7
movapd %xmm6, %xmm8
mulsd %xmm7, %xmm8
unpcklpd %xmm8, %xmm8            # xmm8 = xmm8[0,0]
mulpd %xmm7, %xmm8
addpd %xmm8, %xmm3
movapd %xmm3, 0x40(%rsp)
movapd %xmm7, %xmm8
unpckhpd %xmm7, %xmm8            # xmm8 = xmm8[1],xmm7[1]
mulsd %xmm6, %xmm8
unpcklpd %xmm8, %xmm8            # xmm8 = xmm8[0,0]
mulpd %xmm7, %xmm8
addpd %xmm8, %xmm4
movapd %xmm4, 0x50(%rsp)
addsd %xmm6, %xmm0
incq %rsi
cmpq %rsi, %rax
jne 0x6e7bc
ucomisd %xmm1, %xmm0
jne 0x6e841
jp 0x6e841
leaq 0x47977c(%rip), %rsi    # 0x4e7fb0
movq %rbx, %rdi
callq 0x66d26
jmp 0x6ea15
movq %rbx, 0x70(%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
xorl %eax, %eax
movapd 0x40(%rsp,%rax,8), %xmm1
divpd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x6e84c
leaq 0x40(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0x80(%rsp), %r8
leaq 0x90(%rsp), %r9
movl $0x2, %edi
movl $0x2, %esi
callq 0x720bd
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x20, %rdi
callq 0x69158
addq $0x10, %rbx
cmpq $0x20, %rbx
jne 0x6e894
xorl %eax, %eax
movapd 0x90(%rsp,%rax), %xmm0
movapd %xmm0, 0x20(%rsp,%rax)
addq $0x10, %rax
cmpq $0x20, %rax
jne 0x6e8ad
movq %r14, 0x68(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x691fe
movsd %xmm0, 0x8(%rsp)
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x691fe
movsd 0x8(%rsp), %xmm1
leaq 0x772a3(%rip), %rbx     # 0xe5b98
ucomisd (%rbx), %xmm1
jbe 0x6e915
leaq 0x20(%rsp), %rdi
movsd %xmm0, 0x60(%rsp)
movapd %xmm1, %xmm0
callq 0x6962c
movsd 0x60(%rsp), %xmm0
ucomisd (%rbx), %xmm0
jbe 0x6e923
movq %r14, %rdi
callq 0x6962c
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpl $0x0, 0xc(%r12)
jle 0x6e9c0
xorl %ecx, %ecx
leaq 0xb0(%rsp), %rbp
leaq 0x18(%rsp), %rbx
leaq 0x10(%rsp), %r15
movq %r12, 0x78(%rsp)
movq (%r12), %rax
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx,8), %r13
leaq 0x20(%rsp), %r12
xorl %r14d, %r14d
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movq %rbp, %rdi
callq 0x6915a
movq %r12, %rdi
movq %rbp, %rsi
callq 0x692f6
ucomisd 0x33858(%rip), %xmm0    # 0xa21e8
movq %rbx, %rax
ja 0x6e998
movq %r15, %rax
incl (%rax,%r14,4)
incq %r14
addq $0x10, %r12
cmpq $0x1, %r14
je 0x6e969
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x78(%rsp), %r12
movslq 0xc(%r12), %rax
cmpq %rax, %rcx
jl 0x6e954
xorl %ebx, %ebx
movq 0x68(%rsp), %r14
leaq 0x20(%rsp), %r15
movl 0x18(%rsp,%rbx,4), %eax
cmpl 0x10(%rsp,%rbx,4), %eax
jge 0x6e9de
movq %r15, %rdi
callq 0x693c2
incq %rbx
addq $0x10, %r15
cmpq $0x1, %rbx
je 0x6e9cc
testq %r14, %r14
je 0x6e9fe
movapd 0x80(%rsp), %xmm0
movupd %xmm0, (%r14)
leaq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rbx
movq %rbx, %rdi
leaq 0x30(%rsp), %rdx
callq 0x66d6a
movq %rbx, %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq