// File: /workspace/github_cpp_makefile_star2_O2/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, 0x68(%rsp)
movq %rsi, %r15
movq %rdi, %r13
leaq 0x50(%rsp), %rdi
testq %rdx, %rdx
je 0x5254c
movq %rdx, %rsi
callq 0x5f596
jmp 0x52554
movq %r15, %rsi
callq 0x5243c
xorpd %xmm1, %xmm1
movapd %xmm1, 0x80(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm1, 0x70(%rsp)
movq 0xb8(%r15), %rbx
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
leaq 0x90(%rsp), %r12
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
cmpl 0xc4(%r15), %ebp
jge 0x52651
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r14d, %r14d
cmpl 0xc0(%r15), %r14d
jge 0x5264a
cvtsi2sd %r14d, %xmm0
movq %r12, %rdi
movsd 0x8(%rsp), %xmm1
callq 0x5f58c
movsd (%rbx), %xmm0
addq $0x8, %rbx
movapd 0x90(%rsp), %xmm1
subpd 0x50(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
movapd %xmm0, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
movapd 0x30(%rsp), %xmm3
addpd %xmm2, %xmm3
movapd %xmm3, 0x30(%rsp)
movapd %xmm3, 0x70(%rsp)
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulpd %xmm0, %xmm1
movapd 0x40(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x80(%rsp)
incl %r14d
jmp 0x525b0
incl %ebp
jmp 0x52596
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x52677
jp 0x52677
leaq 0x48a956(%rip), %rsi    # 0x4dcfc0
movq %r13, %rdi
callq 0x5d660
jmp 0x5287b
xorl %eax, %eax
cmpq $0x4, %rax
je 0x52696
movsd 0x70(%rsp,%rax,8), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x52679
pushq $0x2
popq %rdi
leaq 0x70(%rsp), %rdx
leaq 0x110(%rsp), %rcx
leaq 0xc0(%rsp), %r8
leaq 0xf0(%rsp), %r14
movl %edi, %esi
movq %r14, %r9
callq 0x678f6
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x90, %rdi
callq 0x5f8b2
addq $0x10, %rbx
cmpq $0x30, %rbx
jne 0x526c2
xorl %eax, %eax
cmpq $0x2, %rax
je 0x5270a
xorl %ecx, %ecx
cmpq $0x2, %rcx
je 0x526fd
movsd (%r14,%rcx,8), %xmm0
movsd %xmm0, (%r12,%rcx,8)
incq %rcx
jmp 0x526e6
incq %rax
addq $0x10, %r14
addq $0x10, %r12
jmp 0x526de
movq %r13, 0x60(%rsp)
movq 0xb8(%r15), %rbx
xorl %eax, %eax
leaq 0xe0(%rsp), %r12
leaq 0xd0(%rsp), %r13
leaq 0x90(%rsp), %r14
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
cmpl 0xc4(%r15), %eax
jge 0x52808
movl %eax, 0x2c(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x40(%rsp)
xorl %ebp, %ebp
cmpl 0xc0(%r15), %ebp
jge 0x527fd
movapd %xmm1, 0x10(%rsp)
cvtsi2sd %ebp, %xmm0
movq %r12, %rdi
movsd 0x40(%rsp), %xmm1
callq 0x5f58c
movsd (%rbx), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %r13, %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
callq 0x5f7c9
movq %r14, %rdi
movq %r13, %rsi
callq 0x5fa20
ucomisd 0x40a3b(%rip), %xmm0    # 0x931e8
ja 0x527cd
movsd 0x8(%rsp), %xmm1
movapd 0x30(%rsp), %xmm3
addsd %xmm3, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd 0x10(%rsp), %xmm2
jmp 0x527d9
movapd 0x10(%rsp), %xmm2
movapd 0x30(%rsp), %xmm3
addq $0x8, %rbx
addsd %xmm2, %xmm3
xorpd %xmm1, %xmm1
cmpltsd %xmm0, %xmm1
andpd %xmm1, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
incl %ebp
jmp 0x5275b
movl 0x2c(%rsp), %eax
incl %eax
jmp 0x5273e
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x52821
leaq 0x90(%rsp), %rdi
callq 0x5fada
leaq 0xa0(%rsp), %r14
leaq 0x90(%rsp), %rsi
movq %r14, %rdi
callq 0x5fbfa
leaq 0x48718(%rip), %rax     # 0x9af58
movsd (%rax), %xmm0
movq %r14, %rdi
callq 0x5fafa
movq 0x68(%rsp), %rax
testq %rax, %rax
movq 0x60(%rsp), %r13
je 0x52868
movapd 0xc0(%rsp), %xmm0
movupd %xmm0, (%rax)
leaq 0x90(%rsp), %rsi
movq %r13, %rdi
movq %r14, %rdx
callq 0x5d6a4
movq %r13, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq