// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelPointSet.cpp
// Function: gaps::R3SurfelPointSet::PrincipleAxes(gaps::R3Point const*, double*) const
R3Triad R3SurfelPointSet::
PrincipleAxes(const R3Point *center, RNScalar *variances) const
{
  // Check points
  if (npoints == 0) {
    if (variances) { 
      variances[0] = 0.0;
      variances[1] = 0.0;
      variances[2] = 0.0;
    }
    return R3xyz_triad;
  }

  // Get centroid
  R3Point centroid = (center) ? *center : Centroid();

  // Compute covariance matrix
  RNScalar m[9] = { 0 };
  for (int i = 0; i < npoints; i++) {
    R3SurfelPoint& point = points[i];
    RNScalar x = point.X() - centroid[0];
    RNScalar y = point.Y() - centroid[1];
    RNScalar z = point.Z() - centroid[2];
    m[0] += x*x;
    m[4] += y*y;
    m[8] += z*z;
    m[1] += x*y;
    m[3] += x*y;
    m[2] += x*z;
    m[6] += x*z;
    m[5] += y*z;
    m[7] += y*z;
  }

  // Normalize covariance matrix
  for (int i = 0; i < 9; i++) m[i] /= npoints;

  // Compute eigenvalues and eigenvectors
  RNScalar U[9];
  RNScalar W[3];
  RNScalar Vt[9];
  RNSvdDecompose(3, 3, m, U, W, Vt);  // m == U . DiagonalMatrix(W) . Vt

  // Copy principle axes into more convenient form
  // W has eigenvalues (greatest to smallest) and Vt has eigenvectors (normalized)
  R3Vector axes[3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      axes[i][j] = Vt[3*i+j];
    }
  }

  // Flip axes so that "heavier" on positive side for first two dimensions
  int positive_count[3] = { 0, 0, 0 };
  int negative_count[3] = { 0, 0, 0 };
  for (int i = 0; i < npoints; i++) {
    R3SurfelPoint& point = points[i];
    RNScalar x = point.X() - centroid[0];
    RNScalar y = point.Y() - centroid[1];
    RNScalar z = point.Z() - centroid[2];
    R3Vector vector(x, y, z);
    for (int j = 0; j < 2; j++) {
      RNScalar dot = axes[j].Dot(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();
    }
  }

  // Set third axis to form orthonormal triad with other two
  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
  return R3Triad(axes[0], axes[1], axes[2]);
}

// Assembly:
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rdi, 0x58(%rsp)
movq %rdi, %rax
movq %rax, 0x60(%rsp)
movq %rdi, 0x2c0(%rsp)
movq %rsi, 0x2b8(%rsp)
movq %rdx, 0x2b0(%rsp)
movq %rcx, 0x2a8(%rsp)
movq 0x2b8(%rsp), %rax
movq %rax, 0x68(%rsp)
cmpl $0x0, 0x10(%rax)
jne 0x56317
cmpq $0x0, 0x2a8(%rsp)
je 0x56301
movq 0x2a8(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movq 0x2a8(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x2a8(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq 0x58(%rsp), %rdi
leaq 0x1a731b(%rip), %rsi    # 0x1fd628
callq 0xd5be0
jmp 0x56a49
cmpq $0x0, 0x2b0(%rsp)
je 0x56339
movq 0x2b0(%rsp), %rsi
leaq 0x290(%rsp), %rdi
callq 0xf8390
jmp 0x5634b
movq 0x68(%rsp), %rsi
leaq 0x290(%rsp), %rdi
callq 0x560c0
leaq 0x240(%rsp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0x132c0
movl $0x0, 0x23c(%rsp)
movq 0x68(%rsp), %rcx
movl 0x23c(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0x565e6
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0x23c(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x230(%rsp)
movq 0x230(%rsp), %rdi
callq 0x5b1c0
movsd %xmm0, 0x40(%rsp)
leaq 0x290(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movsd 0x40(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x228(%rsp)
movq 0x230(%rsp), %rdi
callq 0x5b1d0
movsd %xmm0, 0x48(%rsp)
leaq 0x290(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movsd 0x48(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x220(%rsp)
movq 0x230(%rsp), %rdi
callq 0x5b1e0
movsd %xmm0, 0x50(%rsp)
leaq 0x290(%rsp), %rdi
movl $0x2, %esi
callq 0x1bde0
movsd 0x50(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x218(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x228(%rsp), %xmm2
movsd 0x240(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp)
movsd 0x220(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x260(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x260(%rsp)
movsd 0x218(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x280(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x280(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x248(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x248(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x258(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x258(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x250(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x250(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x270(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x270(%rsp)
movsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x268(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x268(%rsp)
movsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x278(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x278(%rsp)
movl 0x23c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x23c(%rsp)
jmp 0x5636a
movl $0x0, 0x214(%rsp)
cmpl $0x9, 0x214(%rsp)
jge 0x56636
movq 0x68(%rsp), %rax
cvtsi2sdl 0x10(%rax), %xmm1
movslq 0x214(%rsp), %rax
movsd 0x240(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp,%rax,8)
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
jmp 0x565f1
leaq 0x240(%rsp), %rdx
leaq 0x1c0(%rsp), %rcx
leaq 0x1a0(%rsp), %r8
leaq 0x150(%rsp), %r9
movl $0x3, %esi
movl %esi, %edi
callq 0x1445e0
leaq 0x100(%rsp), %rax
movq %rax, %rcx
addq $0x48, %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xf94a0
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x38(%rsp)
jne 0x5667b
movl $0x0, 0xfc(%rsp)
cmpl $0x3, 0xfc(%rsp)
jge 0x56748
movl $0x0, 0xf8(%rsp)
cmpl $0x3, 0xf8(%rsp)
jge 0x56730
imull $0x3, 0xfc(%rsp), %eax
addl 0xf8(%rsp), %eax
cltq
movsd 0x150(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x20(%rsp)
movslq 0xfc(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl 0xf8(%rsp), %esi
callq 0x31d80
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0x566c6
jmp 0x56732
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x566ad
leaq 0xec(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x132c0
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x132c0
movl $0x0, 0xdc(%rsp)
movq 0x68(%rsp), %rcx
movl 0xdc(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0x56939
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0xdc(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x5b1c0
movsd %xmm0, 0x8(%rsp)
leaq 0x290(%rsp), %rdi
xorl %esi, %esi
callq 0x1bde0
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x5b1d0
movsd %xmm0, 0x10(%rsp)
leaq 0x290(%rsp), %rdi
movl $0x1, %esi
callq 0x1bde0
movsd 0x10(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x5b1e0
movsd %xmm0, 0x18(%rsp)
leaq 0x290(%rsp), %rdi
movl $0x2, %esi
callq 0x1bde0
movsd 0x18(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0xc0(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm2
leaq 0xa0(%rsp), %rdi
callq 0xf94b0
movl $0x0, 0x9c(%rsp)
cmpl $0x2, 0x9c(%rsp)
jge 0x56921
movslq 0x9c(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xa0(%rsp), %rsi
callq 0xf9ac0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x568f0
movslq 0x9c(%rsp), %rax
movl 0xec(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0xec(%rsp,%rax,4)
jmp 0x56909
movslq 0x9c(%rsp), %rax
movl 0xe0(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0xe0(%rsp,%rax,4)
jmp 0x5690b
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x56888
jmp 0x56923
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x5677b
movl $0x0, 0x8c(%rsp)
cmpl $0x2, 0x8c(%rsp)
jge 0x5699f
movslq 0x8c(%rsp), %rax
movl 0xec(%rsp,%rax,4), %eax
movslq 0x8c(%rsp), %rcx
cmpl 0xe0(%rsp,%rcx,4), %eax
jge 0x5698a
movslq 0x8c(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0xf9c90
jmp 0x5698c
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x56944
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x70(%rsp), %rdi
callq 0xfa2f0
leaq 0x100(%rsp), %rdi
addq $0x30, %rdi
leaq 0x70(%rsp), %rsi
callq 0xfa170
cmpq $0x0, 0x2a8(%rsp)
je 0x56a1f
movsd 0x1a0(%rsp), %xmm0
movq 0x2a8(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x1a8(%rsp), %xmm0
movq 0x2a8(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x1b0(%rsp), %xmm0
movq 0x2a8(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0x58(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x100(%rsp), %rcx
addq $0x30, %rcx
callq 0xd5c80
movq 0x60(%rsp), %rax
addq $0x2c8, %rsp            # imm = 0x2C8
retq
nopw %cs:(%rax,%rax)