// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::GridPrincipleAxes(gaps::R2Point const*, double*) const
R2Diad R2Grid::
GridPrincipleAxes(const R2Point *grid_centroid, RNScalar *variances) const
{
  // Get centroid
  R2Point center = (grid_centroid) ? *grid_centroid : GridCentroid();

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

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

  // Compute eigenvalues and eigenvectors
  RNScalar U[4];
  RNScalar W[2];
  RNScalar Vt[4];
  RNSvdDecompose(2, 2, 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)
  R2Vector axes[3];
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      axes[i][j] = Vt[2*i+j];
    }
  }

  // Find heavier side of first axis
  grid_valuesp = grid_values;
  RNScalar positive_count = 0;
  RNScalar negative_count = 0;
  for (int j = 0; j < grid_resolution[1]; j++) {
    for (int i = 0; i < grid_resolution[0]; i++) {
      R2Point position(i, j);
      RNScalar value = *(grid_valuesp++);
      R2Vector vector = position - center;
      RNScalar dot = axes[0].Dot(vector);
      if (dot > 0.0) positive_count += value;
      else negative_count += value;
    }
  }

  // Set second axis to form orthonormal triad with first one
  if (positive_count < negative_count) axes[0].Flip();
  axes[1] = axes[0]; axes[1].Rotate(RN_PI_OVER_TWO);

  // Just checking
  assert(RNIsEqual(axes[0].Length(), 1.0, RN_BIG_EPSILON));
  assert(RNIsEqual(axes[1].Length(), 1.0, RN_BIG_EPSILON));
  assert(RNIsZero(axes[0].Dot(axes[1]), RN_BIG_EPSILON));

  // Return variances (eigenvalues)
  if (variances) {
    variances[0] = W[0];
    variances[1] = W[1];
  }

  // Return triad
  return R2Diad(axes[0], axes[1]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rcx, 0x80(%rsp)
movq %rsi, %r15
movq %rdi, %r13
leaq 0x60(%rsp), %rdi
testq %rdx, %rdx
je 0x5ac2b
movq %rdx, %rsi
callq 0x68e38
jmp 0x5ac33
movq %r15, %rsi
callq 0x5ab06
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
cmpl $0x0, 0xc4(%r15)
xorpd %xmm3, %xmm3
jle 0x5ad10
movq 0xb8(%r15), %rbx
xorl %ebp, %ebp
leaq 0x90(%rsp), %r14
cmpl $0x0, 0xc0(%r15)
jle 0x5ad01
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
movapd %xmm3, 0x50(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq %r14, %rdi
movsd 0x20(%rsp), %xmm1
callq 0x68e2e
movapd 0x50(%rsp), %xmm3
movsd (%rbx), %xmm0
addq $0x8, %rbx
movapd 0x90(%rsp), %xmm1
subpd 0x60(%rsp), %xmm1
addsd %xmm0, %xmm3
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
addpd 0x30(%rsp), %xmm2
movapd %xmm2, 0x30(%rsp)
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulpd %xmm0, %xmm1
addpd 0x40(%rsp), %xmm1
movapd %xmm1, 0x40(%rsp)
incl %r12d
cmpl 0xc0(%r15), %r12d
jl 0x5ac84
incl %ebp
cmpl 0xc4(%r15), %ebp
jl 0x5ac66
ucomisd 0x474d0(%rip), %xmm3    # 0xa21e8
jne 0x5ad30
jp 0x5ad30
leaq 0x48d28d(%rip), %rsi    # 0x4e7fb0
movq %r13, %rdi
callq 0x66d26
jmp 0x5af5c
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
xorl %eax, %eax
movapd 0x30(%rsp,%rax,8), %xmm0
divpd %xmm3, %xmm0
movapd %xmm0, 0x30(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x5ad36
leaq 0x30(%rsp), %rdx
leaq 0x110(%rsp), %rcx
leaq 0xc0(%rsp), %r8
leaq 0xd0(%rsp), %r9
movl $0x2, %edi
movl $0x2, %esi
callq 0x720bd
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
callq 0x69158
addq $0x10, %rbx
cmpq $0x30, %rbx
jne 0x5ad7e
xorl %eax, %eax
movapd 0xd0(%rsp,%rax), %xmm0
movapd %xmm0, 0x90(%rsp,%rax)
addq $0x10, %rax
cmpq $0x20, %rax
jne 0x5ad9a
movq %r13, 0x78(%rsp)
cmpl $0x0, 0xc4(%r15)
jle 0x5aed8
movq 0xb8(%r15), %rbx
xorl %eax, %eax
leaq 0x100(%rsp), %r12
leaq 0xf0(%rsp), %r13
leaq 0x90(%rsp), %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
cmpl $0x0, 0xc0(%r15)
movl %eax, 0xc(%rsp)
jle 0x5aebb
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x88(%rsp)
xorl %ebp, %ebp
movapd %xmm0, 0x50(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movq %r12, %rdi
movsd 0x88(%rsp), %xmm1
callq 0x68e2e
movsd (%rbx), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0x60(%rsp), %rdx
callq 0x6906b
movq %r14, %rdi
movq %r13, %rsi
callq 0x692f6
ucomisd 0x47385(%rip), %xmm0    # 0xa21e8
ja 0x5ae7d
movapd 0x10(%rsp), %xmm1
movapd 0x20(%rsp), %xmm3
addsd %xmm3, %xmm1
movapd %xmm1, 0x10(%rsp)
jmp 0x5ae83
movapd 0x20(%rsp), %xmm3
addq $0x8, %rbx
movapd 0x50(%rsp), %xmm2
addsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
andpd %xmm1, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
incl %ebp
movapd %xmm1, %xmm0
cmpl 0xc0(%r15), %ebp
jl 0x5ae18
jmp 0x5aebf
movapd %xmm0, %xmm1
movl 0xc(%rsp), %eax
incl %eax
movapd %xmm1, %xmm0
cmpl 0xc4(%r15), %eax
jl 0x5adf4
jmp 0x5aee6
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x5aeff
leaq 0x90(%rsp), %rdi
callq 0x693c2
leaq 0xa0(%rsp), %r14
leaq 0x90(%rsp), %rsi
movq %r14, %rdi
callq 0x6950a
leaq 0x4f09a(%rip), %rax     # 0xa9fb8
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x693e2
movq 0x80(%rsp), %rax
testq %rax, %rax
movq 0x78(%rsp), %r13
je 0x5af49
movapd 0xc0(%rsp), %xmm0
movupd %xmm0, (%rax)
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x66d6a
movq %r13, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop