// File: 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:
subq $0x2f8, %rsp            # imm = 0x2F8
movq %rdi, 0x40(%rsp)
movq %rdi, %rax
movq %rax, 0x48(%rsp)
movq %rdi, 0x2f0(%rsp)
movq %rsi, 0x2e8(%rsp)
movq %rdx, 0x2e0(%rsp)
movq %rcx, 0x2d8(%rsp)
movq 0x2e8(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x2e0(%rsp)
je 0x57b43
movq 0x2e0(%rsp), %rsi
leaq 0x2c0(%rsp), %rdi
callq 0x6eb90
jmp 0x57b55
movq 0x50(%rsp), %rsi
leaq 0x2c0(%rsp), %rdi
callq 0x57960
leaq 0x270(%rsp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0xf280
movq 0x50(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x268(%rsp)
movq 0x230(%rax), %rax
movq %rax, 0x260(%rsp)
movl $0x0, 0x25c(%rsp)
movq 0x50(%rsp), %rcx
movl 0x25c(%rsp), %eax
cmpl 0x240(%rcx), %eax
jge 0x57f39
movl $0x0, 0x258(%rsp)
movq 0x50(%rsp), %rcx
movl 0x258(%rsp), %eax
cmpl 0x23c(%rcx), %eax
jge 0x57f21
movl $0x0, 0x254(%rsp)
movq 0x50(%rsp), %rcx
movl 0x254(%rsp), %eax
cmpl 0x238(%rcx), %eax
jge 0x57f09
cvtsi2sdl 0x254(%rsp), %xmm0
cvtsi2sdl 0x258(%rsp), %xmm1
cvtsi2sdl 0x25c(%rsp), %xmm2
leaq 0x238(%rsp), %rdi
callq 0x6eb50
movq 0x260(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x260(%rsp)
movsd (%rax), %xmm0
movsd %xmm0, 0x230(%rsp)
leaq 0x238(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x2c0(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x28(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x228(%rsp)
leaq 0x238(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x2c0(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x30(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x220(%rsp)
leaq 0x238(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x2c0(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x38(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x218(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x228(%rsp), %xmm0
movsd 0x228(%rsp), %xmm2
movsd 0x270(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x270(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x290(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x290(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x218(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2b0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b0(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x228(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x278(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x278(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x228(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x288(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x288(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x228(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x280(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x280(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x228(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2a0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a0(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x298(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x298(%rsp)
movsd 0x230(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2a8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a8(%rsp)
movsd 0x230(%rsp), %xmm0
addsd 0x268(%rsp), %xmm0
movsd %xmm0, 0x268(%rsp)
movl 0x254(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x254(%rsp)
jmp 0x57bda
jmp 0x57f0b
movl 0x258(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x258(%rsp)
jmp 0x57bb7
jmp 0x57f23
movl 0x25c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x25c(%rsp)
jmp 0x57b94
movsd 0x268(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x57f63
jp 0x57f63
movq 0x40(%rsp), %rdi
leaq 0x120baf(%rip), %rsi    # 0x178b08
callq 0x4ebe0
jmp 0x583cf
movl $0x0, 0x214(%rsp)
cmpl $0x9, 0x214(%rsp)
jge 0x57fb2
movsd 0x268(%rsp), %xmm1
movslq 0x214(%rsp), %rax
movsd 0x270(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x270(%rsp,%rax,8)
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
jmp 0x57f6e
leaq 0x270(%rsp), %rdx
leaq 0x1c0(%rsp), %rcx
leaq 0x1a0(%rsp), %r8
leaq 0x150(%rsp), %r9
movl $0x3, %esi
movl %esi, %edi
callq 0xd3dc0
leaq 0x100(%rsp), %rax
movq %rax, %rcx
addq $0x48, %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x6fca0
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x20(%rsp)
jne 0x57ff7
movl $0x0, 0xfc(%rsp)
cmpl $0x3, 0xfc(%rsp)
jge 0x580c4
movl $0x0, 0xf8(%rsp)
cmpl $0x3, 0xf8(%rsp)
jge 0x580ac
imull $0x3, 0xfc(%rsp), %eax
addl 0xf8(%rsp), %eax
cltq
movsd 0x150(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xfc(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl 0xf8(%rsp), %esi
callq 0x36c00
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0x58042
jmp 0x580ae
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x58029
movq 0x50(%rsp), %rax
movq 0x230(%rax), %rax
movq %rax, 0x260(%rsp)
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xf280
leaq 0xd0(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xf280
movl $0x0, 0xcc(%rsp)
movq 0x50(%rsp), %rcx
movl 0xcc(%rsp), %eax
cmpl 0x240(%rcx), %eax
jge 0x582cc
movl $0x0, 0xc8(%rsp)
movq 0x50(%rsp), %rcx
movl 0xc8(%rsp), %eax
cmpl 0x23c(%rcx), %eax
jge 0x582b4
movl $0x0, 0xc4(%rsp)
movq 0x50(%rsp), %rcx
movl 0xc4(%rsp), %eax
cmpl 0x238(%rcx), %eax
jge 0x5829c
cvtsi2sdl 0xc4(%rsp), %xmm0
cvtsi2sdl 0xc8(%rsp), %xmm1
cvtsi2sdl 0xcc(%rsp), %xmm2
leaq 0xa8(%rsp), %rdi
callq 0x6eb50
movq 0x260(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x260(%rsp)
movsd (%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x88(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
leaq 0x2c0(%rsp), %rdx
callq 0x6eeb0
movl $0x0, 0x84(%rsp)
cmpl $0x2, 0x84(%rsp)
jge 0x58284
movslq 0x84(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0x88(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x58249
movsd 0xa0(%rsp), %xmm0
movslq 0x84(%rsp), %rax
addsd 0xe0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0xe0(%rsp,%rax,8)
jmp 0x5826c
movsd 0xa0(%rsp), %xmm0
movslq 0x84(%rsp), %rax
addsd 0xd0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0xd0(%rsp,%rax,8)
jmp 0x5826e
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x581dd
jmp 0x58286
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x58151
jmp 0x5829e
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x5812e
jmp 0x582b6
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x5810b
movl $0x0, 0x74(%rsp)
cmpl $0x2, 0x74(%rsp)
jge 0x58325
movslq 0x74(%rsp), %rax
movsd 0xe0(%rsp,%rax,8), %xmm1
movslq 0x74(%rsp), %rax
movsd 0xd0(%rsp,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x58316
movslq 0x74(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x70490
jmp 0x58318
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x582d4
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x58(%rsp), %rdi
callq 0x70af0
leaq 0x100(%rsp), %rdi
addq $0x30, %rdi
leaq 0x58(%rsp), %rsi
callq 0x70970
cmpq $0x0, 0x2d8(%rsp)
je 0x583a5
movsd 0x1a0(%rsp), %xmm0
movq 0x2d8(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x1a8(%rsp), %xmm0
movq 0x2d8(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x1b0(%rsp), %xmm0
movq 0x2d8(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0x40(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x100(%rsp), %rcx
addq $0x30, %rcx
callq 0x4ec80
movq 0x48(%rsp), %rax
addq $0x2f8, %rsp            # imm = 0x2F8
retq
nopl (%rax)