// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::GridPrincipleAxes(gaps::R3Point const*, double*) const
R3Triad R3Grid::
GridPrincipleAxes(const R3Point *grid_centroid, RNScalar *variances) const
{
  // Get centroid
  R3Point center = (grid_centroid) ? *grid_centroid : GridCentroid();

  // Compute covariance matrix
  RNScalar m[9] = { 0 };
  RNScalar total_value = 0;
  RNScalar *grid_valuesp = grid_values;
  for (int k = 0; k < grid_resolution[2]; k++) {
    for (int j = 0; j < grid_resolution[1]; j++) {
      for (int i = 0; i < grid_resolution[0]; i++) {
        R3Point position(i, j, k);
        RNScalar value = *(grid_valuesp++);
        RNScalar x = position[0] - center[0];
        RNScalar y = position[1] - center[1];
        RNScalar z = position[2] - center[2];
        m[0] += value * x*x;
        m[4] += value * y*y;
        m[8] += value * z*z;
        m[1] += value * x*y;
        m[3] += value * x*y;
        m[2] += value * x*z;
        m[6] += value * x*z;
        m[5] += value * y*z;
        m[7] += value * y*z;
        total_value += value;
      }
    }
  }

  // Normalize covariance matrix
  if (total_value == 0) return R3xyz_triad;
  for (int i = 0; i < 9; i++) m[i] /= total_value;

  // 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 first two axes so that "heavier" on positive side 
  grid_valuesp = grid_values;
  RNScalar positive_count[2] = { 0, 0 };
  RNScalar negative_count[2] = { 0, 0 };
  for (int k = 0; k < grid_resolution[2]; k++) {
    for (int j = 0; j < grid_resolution[1]; j++) {
      for (int i = 0; i < grid_resolution[0]; i++) {
        R3Point position(i, j, k);
        RNScalar value = *(grid_valuesp++);
        R3Vector vector = position - center;
        for (int j = 0; j < 2; j++) {
          RNScalar dot = axes[j].Dot(vector);
          if (dot > 0.0) positive_count[j] += value;
          else negative_count[j] += value;
        }
      }
    }
  }
  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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
testq %rdx, %rdx
movq %rcx, 0x88(%rsp)
movq %rdi, %r15
leaq 0xa0(%rsp), %rdi
je 0x2c504
movq %rsi, %rbx
movq %rdx, %rsi
callq 0x37b5c
jmp 0x2c50c
movq %rsi, %rbx
callq 0x2c3c2
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x70(%rsp)
movq 0x230(%rbx), %r14
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
leaq 0x160(%rsp), %r12
movq %rbx, %rax
movq %rbx, 0x20(%rsp)
cmpl 0x240(%rax), %ebp
jge 0x2c687
movsd %xmm1, 0x8(%rsp)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
cmpl 0x23c(%rax), %r13d
jge 0x2c67a
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %ebx, %ebx
cmpl 0x238(%rax), %ebx
jge 0x2c672
cvtsi2sd %ebx, %xmm0
movq %r12, %rdi
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
callq 0x37b4c
movq 0x20(%rsp), %rax
movsd (%r14), %xmm1
movsd 0x170(%rsp), %xmm0
subsd 0xb0(%rsp), %xmm0
addq $0x8, %r14
movapd %xmm1, %xmm2
movsd 0x8(%rsp), %xmm3
addsd %xmm1, %xmm3
movsd %xmm3, 0x8(%rsp)
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd 0x70(%rsp), %xmm1
movsd %xmm1, 0x70(%rsp)
movapd 0x160(%rsp), %xmm1
subpd 0xa0(%rsp), %xmm1
movapd %xmm1, %xmm3
movsd %xmm0, %xmm3            # xmm3 = xmm0[0],xmm3[1]
movapd %xmm1, %xmm4
shufpd $0x1, %xmm0, %xmm4      # xmm4 = xmm4[1],xmm0[0]
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm1, %xmm2
movapd %xmm2, %xmm5
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm2, %xmm0
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm2, %xmm1
addpd 0x30(%rsp), %xmm1
movapd %xmm1, 0x30(%rsp)
mulpd %xmm3, %xmm2
addpd 0x40(%rsp), %xmm2
movapd %xmm2, 0x40(%rsp)
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
mulpd %xmm4, %xmm5
addpd 0x50(%rsp), %xmm5
movapd %xmm5, 0x50(%rsp)
addpd 0x60(%rsp), %xmm0
movapd %xmm0, 0x60(%rsp)
incl %ebx
jmp 0x2c584
incl %r13d
jmp 0x2c56a
incl %ebp
movsd 0x8(%rsp), %xmm1
jmp 0x2c54b
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x2c6aa
jp 0x2c6aa
leaq 0xaeb2e(%rip), %rsi     # 0xdb1c8
movq %r15, %rbx
movq %r15, %rdi
callq 0x28198
jmp 0x2c935
xorl %eax, %eax
cmpq $0x9, %rax
je 0x2c6c7
movsd 0x30(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp,%rax,8)
incq %rax
jmp 0x2c6ac
movq %r15, 0x80(%rsp)
pushq $0x3
popq %rdi
leaq 0x30(%rsp), %rdx
leaq 0x160(%rsp), %rcx
leaq 0xe0(%rsp), %r8
leaq 0x1c0(%rsp), %r12
movl %edi, %esi
movq %r12, %r9
callq 0x678f6
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x100, %rdi            # imm = 0x100
callq 0x3830a
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x2c6fb
leaq 0x100(%rsp), %rax
xorl %ecx, %ecx
movq 0x20(%rsp), %rsi
cmpq $0x3, %rcx
je 0x2c74f
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x2c742
movsd (%r12,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x2c72c
incq %rcx
addq $0x18, %rax
addq $0x18, %r12
jmp 0x2c724
movq 0x230(%rsi), %rbx
xorpd %xmm0, %xmm0
leaq 0xd0(%rsp), %rbp
movapd %xmm0, (%rbp)
leaq 0xc0(%rsp), %r12
movapd %xmm0, (%r12)
xorl %eax, %eax
leaq 0x1a8(%rsp), %r13
cmpl 0x240(%rsi), %eax
jge 0x2c886
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x98(%rsp)
xorl %ecx, %ecx
movq %rax, 0x90(%rsp)
cmpl 0x23c(%rsi), %ecx
jge 0x2c87f
movl %ecx, 0x2c(%rsp)
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
cmpl 0x238(%rsi), %r15d
jge 0x2c86c
cvtsi2sd %r15d, %xmm0
leaq 0x148(%rsp), %r14
movq %r14, %rdi
movsd 0x10(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
callq 0x37b4c
movq %rbx, 0x18(%rsp)
movsd (%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %r14, %rsi
leaq 0xa0(%rsp), %rdx
callq 0x37d29
leaq 0x100(%rsp), %r14
xorl %ebx, %ebx
cmpq $0x2, %rbx
je 0x2c856
movq %r14, %rdi
movq %r13, %rsi
callq 0x38586
ucomisd 0x669b3(%rip), %xmm0    # 0x931e8
movq %rbp, %rax
ja 0x2c83d
movq %r12, %rax
movsd (%rax,%rbx,8), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
addq $0x18, %r14
jmp 0x2c81c
movq 0x18(%rsp), %rbx
addq $0x8, %rbx
incl %r15d
movq 0x20(%rsp), %rsi
jmp 0x2c7bf
movl 0x2c(%rsp), %ecx
incl %ecx
movq 0x90(%rsp), %rax
jmp 0x2c7a2
incl %eax
jmp 0x2c77f
leaq 0x100(%rsp), %r14
xorl %ebx, %ebx
cmpq $0x2, %rbx
je 0x2c8bb
movsd 0xc0(%rsp,%rbx,8), %xmm0
ucomisd 0xd0(%rsp,%rbx,8), %xmm0
jbe 0x2c8b2
movq %r14, %rdi
callq 0x38662
incq %rbx
addq $0x18, %r14
jmp 0x2c890
leaq 0x118(%rsp), %r14
leaq 0x148(%rsp), %r12
leaq 0x100(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x389be
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3887c
movq 0x88(%rsp), %rax
testq %rax, %rax
je 0x2c917
movaps 0xe0(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0x100(%rsp), %rsi
movq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x281ea
movq %rbx, %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq