// File: /workspace/github_cpp_makefile_star2_O1/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 %rdx, %r14
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x70(%rsp)
movslq 0xc(%rdx), %rax
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
testq %rax, %rax
jle 0x4ceeb
movq (%r14), %rdx
movsd 0x10(%rsi), %xmm2
movupd (%rsi), %xmm3
movapd 0x30(%rsp), %xmm4
movapd 0x40(%rsp), %xmm5
movapd 0x50(%rsp), %xmm6
movapd 0x60(%rsp), %xmm7
movsd 0x70(%rsp), %xmm8
xorl %esi, %esi
movsd 0x55215(%rip), %xmm9    # 0xa2038
movapd %xmm9, %xmm10
testq %rcx, %rcx
je 0x4ce33
movsd (%rcx,%rsi,8), %xmm10
movq (%rdx,%rsi,8), %rdi
movsd 0x10(%rdi), %xmm13
subsd %xmm2, %xmm13
movupd (%rdi), %xmm11
subpd %xmm3, %xmm11
movapd %xmm10, %xmm12
unpcklpd %xmm10, %xmm12          # xmm12 = xmm12[0],xmm10[0]
mulpd %xmm11, %xmm12
movapd %xmm12, %xmm14
unpcklpd %xmm12, %xmm14          # xmm14 = xmm14[0],xmm12[0]
mulpd %xmm11, %xmm14
addpd %xmm14, %xmm4
movapd %xmm11, %xmm14
shufpd $0x1, %xmm13, %xmm14    # xmm14 = xmm14[1],xmm13[0]
addsd %xmm10, %xmm0
mulsd %xmm13, %xmm10
movapd %xmm10, %xmm15
mulsd %xmm13, %xmm10
unpcklpd %xmm11, %xmm13          # xmm13 = xmm13[0],xmm11[0]
mulpd %xmm12, %xmm13
addpd %xmm13, %xmm5
movapd %xmm4, 0x30(%rsp)
movapd %xmm5, 0x40(%rsp)
unpckhpd %xmm12, %xmm12          # xmm12 = xmm12[1,1]
mulpd %xmm12, %xmm14
addpd %xmm14, %xmm6
movapd %xmm6, 0x50(%rsp)
unpcklpd %xmm15, %xmm15          # xmm15 = xmm15[0,0]
mulpd %xmm11, %xmm15
addpd %xmm15, %xmm7
movapd %xmm7, 0x60(%rsp)
addsd %xmm10, %xmm8
movsd %xmm8, 0x70(%rsp)
incq %rsi
cmpq %rsi, %rax
jne 0x4ce23
ucomisd %xmm1, %xmm0
jne 0x4cf07
jp 0x4cf07
leaq 0x992be(%rip), %rsi     # 0xe61b8
movq %rbx, %rdi
callq 0x2ad8e
jmp 0x4d193
movq %r8, 0x88(%rsp)
movq %rbx, 0x90(%rsp)
xorl %eax, %eax
movsd 0x30(%rsp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp,%rax,8)
incq %rax
cmpq $0x9, %rax
jne 0x4cf19
leaq 0x30(%rsp), %rdx
leaq 0x170(%rsp), %rcx
leaq 0xf0(%rsp), %r8
leaq 0x120(%rsp), %r9
movl $0x3, %edi
movl $0x3, %esi
callq 0x720bd
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0xa0, %rdi
callq 0x3d4bc
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x4cf60
xorl %eax, %eax
movq 0x130(%rsp,%rax), %rcx
movq %rcx, 0xb0(%rsp,%rax)
movupd 0x120(%rsp,%rax), %xmm0
movupd %xmm0, 0xa0(%rsp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0x4cf7c
leaq 0xa0(%rsp), %rdi
callq 0x3d5c4
movsd %xmm0, 0x78(%rsp)
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
callq 0x3d5c4
movsd %xmm0, 0x80(%rsp)
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
callq 0x3d5c4
movsd 0x78(%rsp), %xmm1
leaq 0x98ba7(%rip), %rbx     # 0xe5b98
ucomisd (%rbx), %xmm1
movsd %xmm0, 0x8(%rsp)
jbe 0x4d014
leaq 0xa0(%rsp), %rdi
movapd %xmm1, %xmm0
callq 0x3e0ec
movsd 0x8(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
ucomisd (%rbx), %xmm1
jbe 0x4d035
movq %r15, %rdi
movapd %xmm1, %xmm0
callq 0x3e0ec
movsd 0x8(%rsp), %xmm0
ucomisd (%rbx), %xmm0
jbe 0x4d043
movq %r12, %rdi
callq 0x3e0ec
xorl %eax, %eax
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpl %eax, 0xc(%r14)
jle 0x4d0f0
xorl %ecx, %ecx
leaq 0x108(%rsp), %r12
leaq 0x20(%rsp), %rbx
leaq 0x10(%rsp), %r15
movq %r14, 0x98(%rsp)
movq (%r14), %rax
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx,8), %rbp
leaq 0xa0(%rsp), %r13
xorl %r14d, %r14d
movsd (%rbp), %xmm0
movsd 0x8(%rbp), %xmm1
movsd 0x10(%rbp), %xmm2
movq %r12, %rdi
callq 0x3d4be
movq %r13, %rdi
movq %r12, %rsi
callq 0x3d756
ucomisd 0x5512a(%rip), %xmm0    # 0xa21e8
movq %rbx, %rax
ja 0x4d0c6
movq %r15, %rax
incl (%rax,%r14,4)
incq %r14
addq $0x18, %r13
cmpq $0x3, %r14
jne 0x4d094
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x98(%rsp), %r14
movslq 0xc(%r14), %rax
cmpq %rax, %rcx
jl 0x4d07d
xorl %ebx, %ebx
leaq 0xb8(%rsp), %r14
leaq 0xd0(%rsp), %r12
leaq 0xa0(%rsp), %r15
movl 0x20(%rsp,%rbx,4), %eax
cmpl 0x10(%rsp,%rbx,4), %eax
jge 0x4d11c
movq %r15, %rdi
callq 0x3d84c
incq %rbx
addq $0x18, %r15
cmpq $0x3, %rbx
jne 0x4d10a
leaq 0x108(%rsp), %r15
leaq 0xa0(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x3de12
movq %r12, %rdi
movq %r15, %rsi
callq 0x3dc6a
movq 0x88(%rsp), %rax
testq %rax, %rax
je 0x4d175
movaps 0xf0(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0x100(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0xa0(%rsp), %rsi
movq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0x2ade0
movq %rbx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq