// File: /workspace/github_cpp_makefile_star2_O3/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 $0x208, %rsp            # imm = 0x208
movq %rcx, %rbp
movq %rsi, %r15
movq %rdi, %r13
leaq 0x50(%rsp), %rdi
testq %rdx, %rdx
je 0x12124
movq %rdx, %rsi
callq 0x3c542
jmp 0x1212c
movq %r15, %rsi
callq 0x11ac4
xorpd %xmm2, %xmm2
movapd %xmm2, 0xc0(%rsp)
movapd %xmm2, 0xb0(%rsp)
movapd %xmm2, 0xa0(%rsp)
movapd %xmm2, 0x90(%rsp)
movq $0x0, 0xd0(%rsp)
cmpl $0x0, 0x34(%r15)
jle 0x122ec
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
leaq 0x160(%rsp), %r12
xorpd %xmm1, %xmm1
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
movapd %xmm5, 0x70(%rsp)
movapd %xmm4, 0x80(%rsp)
movapd %xmm3, 0x30(%rsp)
movsd %xmm0, 0x40(%rsp)
movapd %xmm2, 0x20(%rsp)
movsd %xmm1, 0x8(%rsp)
movq 0x28(%r15), %rax
movq (%rax,%rbx,8), %r14
testb $0x2, 0xa8(%r14)
jne 0x121d1
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x2c0(%rax)
movsd 0x90(%r14), %xmm0
movaps %xmm0, 0x130(%rsp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x11c12
movsd 0x170(%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movapd 0x130(%rsp), %xmm5
movapd %xmm5, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x40(%rsp), %xmm7
addsd %xmm1, %xmm7
movsd %xmm7, 0xd0(%rsp)
movapd 0x160(%rsp), %xmm2
subpd 0x50(%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, %xmm8
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
movapd 0x20(%rsp), %xmm6
addpd %xmm2, %xmm6
movapd %xmm6, %xmm2
movapd %xmm6, 0x90(%rsp)
mulpd %xmm3, %xmm4
movapd 0x30(%rsp), %xmm3
addpd %xmm4, %xmm3
movapd %xmm3, 0xa0(%rsp)
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
mulpd %xmm1, %xmm5
movapd 0x80(%rsp), %xmm4
addpd %xmm5, %xmm4
movapd %xmm4, 0xb0(%rsp)
movapd 0x70(%rsp), %xmm5
addpd %xmm0, %xmm5
movapd %xmm5, 0xc0(%rsp)
movsd 0x8(%rsp), %xmm1
addsd %xmm8, %xmm1
incq %rbx
movslq 0x34(%r15), %rax
cmpq %rax, %rbx
movapd %xmm7, %xmm0
jl 0x12189
jmp 0x1244b
cmpl $0x0, 0x14(%r15)
jle 0x12457
xorpd %xmm4, %xmm4
xorpd %xmm5, %xmm5
xorl %ebx, %ebx
leaq 0x160(%rsp), %r12
xorpd %xmm1, %xmm1
xorpd %xmm3, %xmm3
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movapd %xmm2, 0x40(%rsp)
movapd %xmm3, 0x20(%rsp)
movsd %xmm5, 0x70(%rsp)
movapd %xmm4, 0x80(%rsp)
movsd %xmm1, 0x8(%rsp)
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %rsi
addq $0x18, %rsi
movq %r12, %rdi
callq 0x3c542
movsd 0x70(%rsp), %xmm5
movapd 0x80(%rsp), %xmm4
movsd 0x170(%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm5
movsd %xmm5, 0xd0(%rsp)
movapd 0x160(%rsp), %xmm1
subpd 0x50(%rsp), %xmm1
movapd %xmm1, %xmm2
unpcklpd %xmm1, %xmm2            # xmm2 = xmm2[0],xmm1[0]
movapd %xmm1, %xmm3
movsd %xmm0, %xmm3            # xmm3 = xmm0[0],xmm3[1]
mulpd %xmm2, %xmm3
mulpd %xmm1, %xmm2
addpd %xmm2, %xmm4
movapd %xmm4, 0x90(%rsp)
movapd 0x20(%rsp), %xmm2
addpd %xmm3, %xmm2
movapd %xmm2, 0x20(%rsp)
movapd 0x20(%rsp), %xmm3
movapd %xmm3, 0xa0(%rsp)
movapd %xmm1, %xmm2
shufpd $0x1, %xmm0, %xmm2      # xmm2 = xmm2[1],xmm0[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
mulpd %xmm1, %xmm2
movsd 0x8(%rsp), %xmm1
movapd 0x40(%rsp), %xmm6
addpd %xmm2, %xmm6
movapd %xmm6, 0x40(%rsp)
movapd 0x40(%rsp), %xmm2
movapd %xmm2, 0xb0(%rsp)
movapd 0x30(%rsp), %xmm6
addpd %xmm0, %xmm6
movapd %xmm6, 0x30(%rsp)
movapd 0x30(%rsp), %xmm0
movapd %xmm0, 0xc0(%rsp)
addsd 0x8fbfd(%rip), %xmm1    # 0xa2038
incq %rbx
movslq 0x14(%r15), %rax
cmpq %rax, %rbx
jl 0x12315
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x1246b
jp 0x1246b
leaq 0xd3d5a(%rip), %rsi     # 0xe61b8
movq %r13, %rdi
callq 0x2ab46
jmp 0x12669
xorl %eax, %eax
movsd 0x90(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp,%rax,8)
incq %rax
cmpq $0x9, %rax
jne 0x1246d
leaq 0x90(%rsp), %rdx
leaq 0x160(%rsp), %rcx
leaq 0x140(%rsp), %r8
leaq 0x1c0(%rsp), %r9
movl $0x3, %edi
movl $0x3, %esi
callq 0x710b6
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0xe0, %rdi
callq 0x3cfc4
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x124bd
movq %r13, 0x8(%rsp)
xorl %eax, %eax
movq 0x1d0(%rsp,%rax), %rcx
movq %rcx, 0xf0(%rsp,%rax)
movupd 0x1c0(%rsp,%rax), %xmm0
movupd %xmm0, 0xe0(%rsp,%rax)
addq $0x18, %rax
cmpq $0x48, %rax
jne 0x124de
movq %rbp, 0x20(%rsp)
xorl %eax, %eax
movl %eax, 0x1c(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
cmpl %eax, 0x14(%r15)
jle 0x125af
xorl %ebx, %ebx
leaq 0x1a8(%rsp), %r13
leaq 0xe0(%rsp), %r14
leaq 0x1c(%rsp), %r12
leaq 0x18(%rsp), %rbp
movq 0x8(%r15), %rax
movq (%rax,%rbx,8), %rsi
addq $0x18, %rsi
movq %r13, %rdi
leaq 0x50(%rsp), %rdx
callq 0x3c78e
movq %r14, %rdi
movq %r13, %rsi
callq 0x3d252
ucomisd 0x8fc75(%rip), %xmm0    # 0xa21e8
movq %r12, %rax
ja 0x1257d
leaq 0x14(%rsp), %rax
incl (%rax)
leaq 0xf8(%rsp), %rdi
movq %r13, %rsi
callq 0x3d252
ucomisd 0x8fc51(%rip), %xmm0    # 0xa21e8
movq %rbp, %rax
ja 0x125a1
leaq 0x10(%rsp), %rax
incl (%rax)
incq %rbx
movslq 0x14(%r15), %rax
cmpq %rax, %rbx
jl 0x12547
leaq 0xe0(%rsp), %rdi
leaq 0x1c(%rsp), %rcx
leaq 0x14(%rsp), %rax
movb $0x1, %bpl
leaq 0x18(%rsp), %rbx
leaq 0x10(%rsp), %r14
movq 0x8(%rsp), %r13
leaq 0xf8(%rsp), %r15
movl (%rcx), %ecx
cmpl (%rax), %ecx
jge 0x125e6
callq 0x3d334
movq %r14, %rax
movq %rbx, %rcx
movq %r15, %rdi
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x125db
leaq 0xf8(%rsp), %r14
leaq 0x1a8(%rsp), %r12
leaq 0xe0(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x3d8fa
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x3d752
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x12653
movaps 0x140(%rsp), %xmm0
movups %xmm0, (%rax)
movsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
leaq 0xe0(%rsp), %rsi
movq %r13, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x2ab98
movq %r13, %rax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq