// File: 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:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rdi, 0x60(%rsp)
movq %rdi, %rax
movq %rax, 0x68(%rsp)
movq %rdi, 0x2b0(%rsp)
movq %rsi, 0x2a8(%rsp)
movq %rdx, 0x2a0(%rsp)
movq %rcx, 0x298(%rsp)
movq %r8, 0x290(%rsp)
leaq 0x240(%rsp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0xf280
xorps %xmm0, %xmm0
movsd %xmm0, 0x238(%rsp)
movl $0x0, 0x234(%rsp)
movl 0x234(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x13600
movl %eax, %ecx
movl 0x5c(%rsp), %eax
cmpl %ecx, %eax
jge 0x8d20d
cmpq $0x0, 0x298(%rsp)
je 0x8cf05
movq 0x298(%rsp), %rax
movslq 0x234(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x50(%rsp)
jmp 0x8cf15
movsd 0xa60fb(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x50(%rsp)
jmp 0x8cf15
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x228(%rsp)
movq 0x2a0(%rsp), %rdi
movl 0x234(%rsp), %esi
callq 0x6d3e0
movq %rax, %rdi
callq 0x37820
movsd %xmm0, 0x38(%rsp)
movq 0x2a8(%rsp), %rdi
xorl %esi, %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x220(%rsp)
movq 0x2a0(%rsp), %rdi
movl 0x234(%rsp), %esi
callq 0x6d3e0
movq %rax, %rdi
callq 0x37870
movsd %xmm0, 0x40(%rsp)
movq 0x2a8(%rsp), %rdi
movl $0x1, %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x218(%rsp)
movq 0x2a0(%rsp), %rdi
movl 0x234(%rsp), %esi
callq 0x6d3e0
movq %rax, %rdi
callq 0x378c0
movsd %xmm0, 0x48(%rsp)
movq 0x2a8(%rsp), %rdi
movl $0x2, %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x210(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x240(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x248(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x248(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd 0x210(%rsp), %xmm2
movsd 0x250(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x250(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x218(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x258(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x258(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x218(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x260(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x260(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x218(%rsp), %xmm0
movsd 0x210(%rsp), %xmm2
movsd 0x268(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x268(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x210(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x270(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x270(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x210(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x278(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x278(%rsp)
movsd 0x228(%rsp), %xmm0
mulsd 0x210(%rsp), %xmm0
movsd 0x210(%rsp), %xmm2
movsd 0x280(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x280(%rsp)
movsd 0x228(%rsp), %xmm0
addsd 0x238(%rsp), %xmm0
movsd %xmm0, 0x238(%rsp)
movl 0x234(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x234(%rsp)
jmp 0x8ceb7
movsd 0x238(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8d237
jp 0x8d237
movq 0x60(%rsp), %rdi
leaq 0xeb8db(%rip), %rsi     # 0x178b08
callq 0x4ebe0
jmp 0x8d6e6
movl $0x0, 0x20c(%rsp)
cmpl $0x9, 0x20c(%rsp)
jge 0x8d286
movsd 0x238(%rsp), %xmm1
movslq 0x20c(%rsp), %rax
movsd 0x240(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp,%rax,8)
movl 0x20c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20c(%rsp)
jmp 0x8d242
leaq 0x240(%rsp), %rdx
leaq 0x1c0(%rsp), %rcx
leaq 0x1a0(%rsp), %r8
leaq 0x150(%rsp), %r9
movl $0x3, %esi
movl %esi, %edi
callq 0xd3dc0
leaq 0x100(%rsp), %rax
movq %rax, %rcx
addq $0x48, %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6fca0
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x30(%rsp)
jne 0x8d2cb
movl $0x0, 0xfc(%rsp)
cmpl $0x3, 0xfc(%rsp)
jge 0x8d398
movl $0x0, 0xf8(%rsp)
cmpl $0x3, 0xf8(%rsp)
jge 0x8d380
imull $0x3, 0xfc(%rsp), %eax
addl 0xf8(%rsp), %eax
cltq
movsd 0x150(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x18(%rsp)
movslq 0xfc(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl 0xf8(%rsp), %esi
callq 0x36c00
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0x8d316
jmp 0x8d382
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x8d2fd
leaq 0x100(%rsp), %rdi
callq 0x6fe90
movsd %xmm0, 0xf0(%rsp)
leaq 0x100(%rsp), %rdi
addq $0x18, %rdi
callq 0x6fe90
movsd %xmm0, 0xe8(%rsp)
leaq 0x100(%rsp), %rdi
addq $0x30, %rdi
callq 0x6fe90
movsd %xmm0, 0xe0(%rsp)
movsd 0xf0(%rsp), %xmm0
leaq 0xeb0ee(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x8d416
movsd 0xf0(%rsp), %xmm0
leaq 0x100(%rsp), %rdi
callq 0x70ec0
movsd 0xe8(%rsp), %xmm0
leaq 0xeb0ba(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x8d44e
movsd 0xe8(%rsp), %xmm0
leaq 0x100(%rsp), %rdi
addq $0x18, %rdi
callq 0x70ec0
movsd 0xe0(%rsp), %xmm0
leaq 0xeb082(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x8d486
movsd 0xe0(%rsp), %xmm0
leaq 0x100(%rsp), %rdi
addq $0x30, %rdi
callq 0x70ec0
leaq 0xd4(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0xf280
leaq 0xc8(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0xf280
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x13600
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x8d5d6
movq 0x2a0(%rsp), %rdi
movl 0xc4(%rsp), %esi
callq 0x6d3e0
movq %rax, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
cmpl $0x3, 0xb4(%rsp)
jge 0x8d5be
movslq 0xb4(%rsp), %rcx
leaq 0x100(%rsp), %rax
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0xb8(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x36d10
movq 0x8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8d58d
movslq 0xb4(%rsp), %rax
movl 0xd4(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0xd4(%rsp,%rax,4)
jmp 0x8d5a6
movslq 0xb4(%rsp), %rax
movl 0xc8(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0xc8(%rsp,%rax,4)
jmp 0x8d5a8
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x8d506
jmp 0x8d5c0
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x8d4b9
movl $0x0, 0x8c(%rsp)
cmpl $0x3, 0x8c(%rsp)
jge 0x8d63c
movslq 0x8c(%rsp), %rax
movl 0xd4(%rsp,%rax,4), %eax
movslq 0x8c(%rsp), %rcx
cmpl 0xc8(%rsp,%rcx,4), %eax
jge 0x8d627
movslq 0x8c(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x70490
jmp 0x8d629
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x8d5e1
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x70(%rsp), %rdi
callq 0x70af0
leaq 0x100(%rsp), %rdi
addq $0x30, %rdi
leaq 0x70(%rsp), %rsi
callq 0x70970
cmpq $0x0, 0x290(%rsp)
je 0x8d6bc
movsd 0x1a0(%rsp), %xmm0
movq 0x290(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x1a8(%rsp), %xmm0
movq 0x290(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x1b0(%rsp), %xmm0
movq 0x290(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0x60(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x100(%rsp), %rcx
addq $0x30, %rcx
callq 0x4ec80
movq 0x68(%rsp), %rax
addq $0x2b8, %rsp            # imm = 0x2B8
retq
nopw %cs:(%rax,%rax)