// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rcx, %r15
movq %rsi, %rbx
testq %rdx, %rdx
movq %rdi, 0x28(%rsp)
leaq 0x90(%rsp), %rdi
je 0x2fb47
movq %rdx, %rsi
callq 0x3ca36
jmp 0x2fb4f
movq %rbx, %rsi
callq 0x2f9e6
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x70(%rsp)
cmpl $0x0, 0x240(%rbx)
xorpd %xmm7, %xmm7
movq %rbx, 0x10(%rsp)
jle 0x2fce0
movq %rbx, %rax
movq 0x230(%rbx), %r14
xorl %ebp, %ebp
leaq 0x150(%rsp), %r12
cmpl $0x0, 0x23c(%rax)
jle 0x2fcd2
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
xorl %r13d, %r13d
movsd %xmm1, (%rsp)
cmpl $0x0, 0x238(%rax)
jle 0x2fcc2
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebx, %ebx
movsd %xmm7, 0x18(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movq %r12, %rdi
movsd 0x8(%rsp), %xmm1
movsd (%rsp), %xmm2
callq 0x3ca26
movsd 0x18(%rsp), %xmm7
movq 0x10(%rsp), %rax
movsd 0x160(%rsp), %xmm0
movsd (%r14), %xmm1
subsd 0xa0(%rsp), %xmm0
movapd %xmm1, %xmm2
addsd %xmm1, %xmm7
mulsd %xmm0, %xmm1
movapd 0x150(%rsp), %xmm3
mulsd %xmm0, %xmm1
subpd 0x90(%rsp), %xmm3
addsd 0x70(%rsp), %xmm1
movapd %xmm3, %xmm4
movsd %xmm0, %xmm4            # xmm4 = xmm0[0],xmm4[1]
movapd %xmm3, %xmm5
shufpd $0x1, %xmm0, %xmm5      # xmm5 = xmm5[1],xmm0[0]
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm3, %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd %xmm2, %xmm6
mulpd %xmm2, %xmm0
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
movsd %xmm1, 0x70(%rsp)
mulpd %xmm2, %xmm3
addpd 0x30(%rsp), %xmm3
movapd %xmm3, 0x30(%rsp)
mulpd %xmm4, %xmm2
addpd 0x40(%rsp), %xmm2
movapd %xmm2, 0x40(%rsp)
unpckhpd %xmm6, %xmm6            # xmm6 = xmm6[1,1]
mulpd %xmm5, %xmm6
addpd 0x50(%rsp), %xmm6
movapd %xmm6, 0x50(%rsp)
addpd 0x60(%rsp), %xmm0
addq $0x8, %r14
movapd %xmm0, 0x60(%rsp)
incl %ebx
cmpl 0x238(%rax), %ebx
jl 0x2fbd7
incl %r13d
cmpl 0x23c(%rax), %r13d
jl 0x2fbba
incl %ebp
cmpl 0x240(%rax), %ebp
jl 0x2fb9e
ucomisd 0x72500(%rip), %xmm7    # 0xa21e8
jne 0x2fd05
jp 0x2fd05
leaq 0xb64c5(%rip), %rsi     # 0xe61b8
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x2ad8e
jmp 0x2ffa2
xorl %eax, %eax
movsd 0x30(%rsp,%rax,8), %xmm0
divsd %xmm7, %xmm0
movsd %xmm0, 0x30(%rsp,%rax,8)
incq %rax
cmpq $0x9, %rax
jne 0x2fd07
leaq 0x30(%rsp), %rdx
leaq 0x150(%rsp), %rcx
leaq 0x120(%rsp), %r8
leaq 0x1a0(%rsp), %r9
movl $0x3, %edi
movl $0x3, %esi
callq 0x720bd
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0xd0, %rdi
callq 0x3d4bc
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x2fd4e
movq %r15, 0x78(%rsp)
xorl %eax, %eax
movq 0x1b0(%rsp,%rax), %rcx
movq %rcx, 0xe0(%rsp,%rax)
movupd 0x1a0(%rsp,%rax), %xmm0
movupd %xmm0, 0xd0(%rsp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0x2fd6f
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
cmpl $0x0, 0x240(%rax)
jle 0x2fefb
xorl %ecx, %ecx
leaq 0x1f0(%rsp), %r13
leaq 0xc0(%rsp), %rbp
leaq 0xb0(%rsp), %r12
cmpl $0x0, 0x23c(%rax)
movq %rcx, 0x80(%rsp)
jle 0x2fee5
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x88(%rsp)
xorl %ecx, %ecx
cmpl $0x0, 0x238(%rax)
movl %ecx, 0x24(%rsp)
jle 0x2fed3
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, (%rsp)
xorl %r15d, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
leaq 0x138(%rsp), %r14
movq %r14, %rdi
movsd (%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
callq 0x3ca26
movq %rbx, 0x8(%rsp)
movsd (%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
movq %r14, %rsi
leaq 0x90(%rsp), %rdx
callq 0x3cc88
leaq 0xd0(%rsp), %r14
xorl %ebx, %ebx
movq %r14, %rdi
movq %r13, %rsi
callq 0x3d756
ucomisd 0x72358(%rip), %xmm0    # 0xa21e8
movq %rbp, %rax
ja 0x2fe98
movq %r12, %rax
movsd (%rax,%rbx,8), %xmm0
addsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
addq $0x18, %r14
cmpq $0x1, %rbx
je 0x2fe7d
movq 0x8(%rsp), %rbx
addq $0x8, %rbx
incl %r15d
movq 0x10(%rsp), %rax
cmpl 0x238(%rax), %r15d
jl 0x2fe2b
movl 0x24(%rsp), %ecx
incl %ecx
cmpl 0x23c(%rax), %ecx
jl 0x2fe0b
movq 0x80(%rsp), %rcx
incl %ecx
cmpl 0x240(%rax), %ecx
jl 0x2fde4
leaq 0xd0(%rsp), %r14
xorl %ebx, %ebx
movsd 0xb0(%rsp,%rbx,8), %xmm0
ucomisd 0xc0(%rsp,%rbx,8), %xmm0
jbe 0x2ff21
movq %r14, %rdi
callq 0x3d84c
incq %rbx
addq $0x18, %r14
cmpq $0x1, %rbx
je 0x2ff05
leaq 0xe8(%rsp), %r14
leaq 0x138(%rsp), %r12
leaq 0xd0(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x3de12
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3dc6a
movq 0x78(%rsp), %rax
testq %rax, %rax
je 0x2ff87
movaps 0x120(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0xd0(%rsp), %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2ade0
movq %rbx, %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop