// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::PrincipleAxes(gaps::R3Point const*, double*) const
R3Triad R3Mesh::
PrincipleAxes(const R3Point *center, RNScalar *variances) const
{
  // Get centroid
  R3Point centroid = (center) ? *center : Centroid();

  // Compute covariance matrix
  RNArea area = 0;
  RNScalar m[9] = { 0 };
  if (NFaces() > 0) {
    for (int i = 0; i < NFaces(); i++) {
      R3MeshFace *face = Face(i);
      RNArea face_area = FaceArea(face);
      R3Point face_centroid = FaceCentroid(face);
      RNScalar x = face_centroid[0] - centroid[0];
      RNScalar y = face_centroid[1] - centroid[1];
      RNScalar z = face_centroid[2] - centroid[2];
      m[0] += face_area * x*x;
      m[4] += face_area * y*y;
      m[8] += face_area * z*z;
      m[1] += face_area * x*y;
      m[3] += face_area * x*y;
      m[2] += face_area * x*z;
      m[6] += face_area * x*z;
      m[5] += face_area * y*z;
      m[7] += face_area * y*z;
      area += face_area;
    }
  }
  else {
    for (int i = 0; i < NVertices(); i++) {
      R3MeshVertex *vertex = Vertex(i);
      R3Point position = VertexPosition(vertex);
      RNScalar x = position[0] - centroid[0];
      RNScalar y = position[1] - centroid[1];
      RNScalar z = position[2] - centroid[2];
      m[0] += x*x;
      m[4] += y*y;
      m[8] += z*z;
      m[1] += x*y;
      m[3] += x*y;
      m[2] += x*z;
      m[6] += x*z;
      m[5] += y*z;
      m[7] += y*z;
      area += 1.0;
    }
  }

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

  // 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 axes so that "heavier" on positive side for first two dimensions
  int positive_count[3] = { 0, 0, 0 };
  int negative_count[3] = { 0, 0, 0 };
  for (int i = 0; i < NVertices(); i++) {
    R3MeshVertex *vertex = Vertex(i);
    R3Vector vertex_vector = VertexPosition(vertex) - centroid;
    for (int j = 0; j < 2; j++) {
      RNScalar dot = axes[j].Dot(vertex_vector);
      if (dot > 0.0) positive_count[j]++;
      else negative_count[j]++;
    }
  }
  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 $0x328, %rsp            # imm = 0x328
movq %rdi, 0x70(%rsp)
movq %rdi, %rax
movq %rax, 0x78(%rsp)
movq %rdi, 0x320(%rsp)
movq %rsi, 0x318(%rsp)
movq %rdx, 0x310(%rsp)
movq %rcx, 0x308(%rsp)
movq 0x318(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x310(%rsp)
je 0x15886
movq 0x310(%rsp), %rsi
leaq 0x2f0(%rsp), %rdi
callq 0x6eb90
jmp 0x1589b
movq 0x80(%rsp), %rsi
leaq 0x2f0(%rsp), %rdi
callq 0x14e10
xorps %xmm0, %xmm0
movsd %xmm0, 0x2e8(%rsp)
leaq 0x2a0(%rsp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0xf280
movq 0x80(%rsp), %rdi
callq 0x135a0
cmpl $0x0, %eax
jle 0x15c29
movl $0x0, 0x29c(%rsp)
movq 0x80(%rsp), %rdi
movl 0x29c(%rsp), %eax
movl %eax, 0x6c(%rsp)
callq 0x135a0
movl %eax, %ecx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x15c24
movq 0x80(%rsp), %rdi
movl 0x29c(%rsp), %esi
callq 0x136d0
movq 0x80(%rsp), %rdi
movq %rax, 0x290(%rsp)
movq 0x290(%rsp), %rsi
callq 0x36aa0
movq 0x80(%rsp), %rsi
movsd %xmm0, 0x288(%rsp)
movq 0x290(%rsp), %rdx
leaq 0x270(%rsp), %rdi
callq 0x14ff0
leaq 0x270(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0x2f0(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x50(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x268(%rsp)
leaq 0x270(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0x2f0(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x58(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x260(%rsp)
leaq 0x270(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x2f0(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x60(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x258(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x268(%rsp), %xmm0
movsd 0x268(%rsp), %xmm2
movsd 0x2a0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a0(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x260(%rsp), %xmm0
movsd 0x260(%rsp), %xmm2
movsd 0x2c0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2c0(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x258(%rsp), %xmm0
movsd 0x258(%rsp), %xmm2
movsd 0x2e0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2e0(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x268(%rsp), %xmm0
movsd 0x260(%rsp), %xmm2
movsd 0x2a8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a8(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x268(%rsp), %xmm0
movsd 0x260(%rsp), %xmm2
movsd 0x2b8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b8(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x268(%rsp), %xmm0
movsd 0x258(%rsp), %xmm2
movsd 0x2b0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b0(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x268(%rsp), %xmm0
movsd 0x258(%rsp), %xmm2
movsd 0x2d0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2d0(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x260(%rsp), %xmm0
movsd 0x258(%rsp), %xmm2
movsd 0x2c8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2c8(%rsp)
movsd 0x288(%rsp), %xmm0
mulsd 0x260(%rsp), %xmm0
movsd 0x258(%rsp), %xmm2
movsd 0x2d8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2d8(%rsp)
movsd 0x288(%rsp), %xmm0
addsd 0x2e8(%rsp), %xmm0
movsd %xmm0, 0x2e8(%rsp)
movl 0x29c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x29c(%rsp)
jmp 0x158dc
jmp 0x15f16
movl $0x0, 0x254(%rsp)
movq 0x80(%rsp), %rdi
movl 0x254(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x15f14
movq 0x80(%rsp), %rdi
movl 0x254(%rsp), %esi
callq 0x13630
movq 0x80(%rsp), %rdi
movq %rax, 0x248(%rsp)
movq 0x248(%rsp), %rsi
callq 0x13660
movq %rax, %rsi
leaq 0x230(%rsp), %rdi
callq 0x6eb90
leaq 0x230(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x2f0(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x30(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x228(%rsp)
leaq 0x230(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x2f0(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x38(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x220(%rsp)
leaq 0x230(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd (%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0x2f0(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x40(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x218(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x228(%rsp), %xmm2
movsd 0x2a0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a0(%rsp)
movsd 0x220(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x2c0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2c0(%rsp)
movsd 0x218(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2e0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2e0(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x2a8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2a8(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x220(%rsp), %xmm2
movsd 0x2b8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b8(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2b0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2b0(%rsp)
movsd 0x228(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2d0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2d0(%rsp)
movsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2c8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2c8(%rsp)
movsd 0x220(%rsp), %xmm0
movsd 0x218(%rsp), %xmm2
movsd 0x2d8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x2d8(%rsp)
movsd 0x11d11c(%rip), %xmm0   # 0x133008
addsd 0x2e8(%rsp), %xmm0
movsd %xmm0, 0x2e8(%rsp)
movl 0x254(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x254(%rsp)
jmp 0x15c34
jmp 0x15f16
movsd 0x2e8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x15f40
jp 0x15f40
movq 0x70(%rsp), %rdi
leaq 0x162bd2(%rip), %rsi    # 0x178b08
callq 0x4ebe0
jmp 0x16315
movl $0x0, 0x214(%rsp)
cmpl $0x9, 0x214(%rsp)
jge 0x15f8f
movsd 0x2e8(%rsp), %xmm1
movslq 0x214(%rsp), %rax
movsd 0x2a0(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x2a0(%rsp,%rax,8)
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
jmp 0x15f4b
leaq 0x2a0(%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, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x6fca0
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x28(%rsp)
jne 0x15fd4
movl $0x0, 0xfc(%rsp)
cmpl $0x3, 0xfc(%rsp)
jge 0x160a1
movl $0x0, 0xf8(%rsp)
cmpl $0x3, 0xf8(%rsp)
jge 0x16089
imull $0x3, 0xfc(%rsp), %eax
addl 0xf8(%rsp), %eax
cltq
movsd 0x150(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movslq 0xfc(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
movl 0xf8(%rsp), %esi
callq 0x36c00
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0xf8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf8(%rsp)
jmp 0x1601f
jmp 0x1608b
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x16006
leaq 0xec(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0xf280
leaq 0xe0(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0xf280
movl $0x0, 0xdc(%rsp)
movq 0x80(%rsp), %rdi
movl 0xdc(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x161ff
movq 0x80(%rsp), %rdi
movl 0xdc(%rsp), %esi
callq 0x13630
movq 0x80(%rsp), %rdi
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rsi
callq 0x13660
movq %rax, %rsi
leaq 0xb8(%rsp), %rdi
leaq 0x2f0(%rsp), %rdx
callq 0x6eeb0
movl $0x0, 0xb4(%rsp)
cmpl $0x2, 0xb4(%rsp)
jge 0x161e7
movslq 0xb4(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x161b6
movslq 0xb4(%rsp), %rax
movl 0xec(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0xec(%rsp,%rax,4)
jmp 0x161cf
movslq 0xb4(%rsp), %rax
movl 0xe0(%rsp,%rax,4), %ecx
addl $0x1, %ecx
movl %ecx, 0xe0(%rsp,%rax,4)
jmp 0x161d1
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x1614e
jmp 0x161e9
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x160d4
movl $0x0, 0xa4(%rsp)
cmpl $0x2, 0xa4(%rsp)
jge 0x16265
movslq 0xa4(%rsp), %rax
movl 0xec(%rsp,%rax,4), %eax
movslq 0xa4(%rsp), %rcx
cmpl 0xe0(%rsp,%rcx,4), %eax
jge 0x16250
movslq 0xa4(%rsp), %rax
leaq 0x100(%rsp), %rdi
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x70490
jmp 0x16252
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x1620a
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x88(%rsp), %rdi
callq 0x70af0
leaq 0x100(%rsp), %rdi
addq $0x30, %rdi
leaq 0x88(%rsp), %rsi
callq 0x70970
cmpq $0x0, 0x308(%rsp)
je 0x162eb
movsd 0x1a0(%rsp), %xmm0
movq 0x308(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x1a8(%rsp), %xmm0
movq 0x308(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0x1b0(%rsp), %xmm0
movq 0x308(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0x70(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0x100(%rsp), %rdx
addq $0x18, %rdx
leaq 0x100(%rsp), %rcx
addq $0x30, %rcx
callq 0x4ec80
movq 0x78(%rsp), %rax
addq $0x328, %rsp            # imm = 0x328
retq
nopw %cs:(%rax,%rax)