// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rcx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x80(%rsp), %rdi
testq %rdx, %rdx
je 0x12179
movq %rdx, %rsi
callq 0x3ca36
jmp 0x12181
movq %r15, %rsi
callq 0x11b04
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x50(%rsp)
cmpl $0x0, 0x34(%r15)
jle 0x122e2
cmpl $0x0, 0x34(%r15)
jle 0x123cf
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
leaq 0x130(%rsp), %r12
movsd %xmm1, 0x8(%rsp)
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %r13
testb $0x2, 0xa8(%r13)
jne 0x121f1
movq (%r15), %rax
movq %r15, %rdi
movq %r13, %rsi
callq *0x2c0(%rax)
movsd 0x90(%r13), %xmm0
movaps %xmm0, 0xa0(%rsp)
movq %r12, %rdi
movq %r13, %rdx
callq 0x11c54
movsd 0x140(%rsp), %xmm0
subsd 0x90(%rsp), %xmm0
movapd 0xa0(%rsp), %xmm5
movapd %xmm5, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd 0x50(%rsp), %xmm1
movsd %xmm1, 0x50(%rsp)
movapd 0x130(%rsp), %xmm2
subpd 0x80(%rsp), %xmm2
movapd %xmm2, %xmm3
movsd %xmm0, %xmm3            # xmm3 = xmm0[0],xmm3[1]
movapd %xmm2, %xmm1
shufpd $0x1, %xmm0, %xmm1      # xmm1 = xmm1[1],xmm0[0]
movapd %xmm5, %xmm4
movapd %xmm5, %xmm6
unpcklpd %xmm5, %xmm4            # xmm4 = xmm4[0],xmm5[0]
mulpd %xmm2, %xmm4
movapd %xmm4, %xmm5
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm4, %xmm0
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
mulpd %xmm4, %xmm2
addpd 0x10(%rsp), %xmm2
movapd %xmm2, 0x10(%rsp)
mulpd %xmm3, %xmm4
addpd 0x20(%rsp), %xmm4
movapd %xmm4, 0x20(%rsp)
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
mulpd %xmm1, %xmm5
addpd 0x30(%rsp), %xmm5
movapd %xmm5, 0x30(%rsp)
addpd 0x40(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
movsd 0x8(%rsp), %xmm1
addsd %xmm6, %xmm1
incq %rbp
movslq 0x34(%r15), %rax
cmpq %rax, %rbp
jl 0x121ca
jmp 0x123d3
cmpl $0x0, 0x14(%r15)
jle 0x123cf
xorpd %xmm1, %xmm1
xorl %r13d, %r13d
leaq 0x130(%rsp), %r12
movsd %xmm1, 0x8(%rsp)
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rsi
addq $0x18, %rsi
movq %r12, %rdi
callq 0x3ca36
movsd 0x140(%rsp), %xmm0
subsd 0x90(%rsp), %xmm0
movapd %xmm0, %xmm2
movapd 0x130(%rsp), %xmm1
mulsd %xmm0, %xmm2
subpd 0x80(%rsp), %xmm1
addsd 0x50(%rsp), %xmm2
movapd %xmm1, %xmm3
movapd %xmm1, %xmm4
unpcklpd %xmm1, %xmm3            # xmm3 = xmm3[0],xmm1[0]
movsd %xmm0, %xmm4            # xmm4 = xmm0[0],xmm4[1]
mulpd %xmm3, %xmm4
movsd %xmm2, 0x50(%rsp)
mulpd %xmm1, %xmm3
addpd 0x10(%rsp), %xmm3
movapd %xmm3, 0x10(%rsp)
addpd 0x20(%rsp), %xmm4
movapd %xmm1, %xmm2
shufpd $0x1, %xmm0, %xmm2      # xmm2 = xmm2[1],xmm0[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
movapd %xmm4, 0x20(%rsp)
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulpd %xmm1, %xmm2
movsd 0x8(%rsp), %xmm1
addpd 0x30(%rsp), %xmm2
movapd %xmm2, 0x30(%rsp)
addpd 0x40(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
addsd 0x8fc7b(%rip), %xmm1    # 0xa2038
incq %r13
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jl 0x122fc
jmp 0x123d3
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x123f3
jp 0x123f3
leaq 0xd3dd2(%rip), %rsi     # 0xe61b8
movq %rbx, %rdi
callq 0x2ad8e
jmp 0x125d5
movq %rbx, 0x8(%rsp)
xorl %eax, %eax
movsd 0x10(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp,%rax,8)
incq %rax
cmpq $0x9, %rax
jne 0x123fa
movq %r14, 0xa0(%rsp)
leaq 0x10(%rsp), %rdx
leaq 0x130(%rsp), %rcx
leaq 0x110(%rsp), %r8
leaq 0x190(%rsp), %r9
movl $0x3, %edi
movl $0x3, %esi
callq 0x720bd
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0xc0, %rdi
callq 0x3d4bc
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x12449
xorl %eax, %eax
movq 0x1a0(%rsp,%rax), %rcx
movq %rcx, 0xd0(%rsp,%rax)
movupd 0x190(%rsp,%rax), %xmm0
movupd %xmm0, 0xc0(%rsp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0x12465
xorl %eax, %eax
movl %eax, 0x78(%rsp)
movq %rax, 0x70(%rsp)
movl %eax, 0x68(%rsp)
movq %rax, 0x60(%rsp)
cmpl %eax, 0x14(%r15)
jle 0x12533
xorl %r14d, %r14d
leaq 0x178(%rsp), %r12
leaq 0x70(%rsp), %r13
leaq 0x60(%rsp), %rbx
movq %r15, 0xb8(%rsp)
movq 0x8(%r15), %rax
movq (%rax,%r14,8), %rsi
addq $0x18, %rsi
movq %r12, %rdi
leaq 0x80(%rsp), %rdx
callq 0x3cc88
leaq 0xc0(%rsp), %rbp
xorl %r15d, %r15d
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3d756
ucomisd 0x8fce2(%rip), %xmm0    # 0xa21e8
movq %r13, %rax
ja 0x1250e
movq %rbx, %rax
incl (%rax,%r15,4)
incq %r15
addq $0x18, %rbp
cmpq $0x1, %r15
je 0x124f3
incq %r14
movq 0xb8(%rsp), %r15
movslq 0x14(%r15), %rax
cmpq %rax, %r14
jl 0x124cc
leaq 0xc0(%rsp), %r15
xorl %ebx, %ebx
movq 0xa0(%rsp), %r14
movl 0x70(%rsp,%rbx,4), %eax
cmpl 0x60(%rsp,%rbx,4), %eax
jge 0x12557
movq %r15, %rdi
callq 0x3d84c
incq %rbx
addq $0x18, %r15
cmpq $0x1, %rbx
je 0x12545
leaq 0xd8(%rsp), %r15
leaq 0x178(%rsp), %r13
leaq 0xc0(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x3de12
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x3dc6a
testq %r14, %r14
je 0x125ba
movaps 0x110(%rsp), %xmm0
movups %xmm0, (%r14)
movsd 0x120(%rsp), %xmm0
movsd %xmm0, 0x10(%r14)
leaq 0xc0(%rsp), %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x2ade0
movq %rbx, %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq