// File: /workspace/github_cpp_makefile_star2_O2/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 $0x218, %rsp            # imm = 0x218
movq %rcx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x50(%rsp), %rdi
testq %rdx, %rdx
je 0x118dc
movq %rdx, %rsi
callq 0x37b5c
jmp 0x118e4
movq %r15, %rsi
callq 0x1133a
xorpd %xmm0, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
andq $0x0, 0xc0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movl 0x34(%r15), %eax
testl %eax, %eax
jle 0x11a90
xorps %xmm2, %xmm2
xorpd %xmm7, %xmm7
xorl %ebp, %ebp
leaq 0x170(%rsp), %r12
xorpd %xmm1, %xmm1
xorpd %xmm3, %xmm3
xorpd %xmm4, %xmm4
cltq
cmpq %rax, %rbp
jge 0x11bde
movq 0x28(%r15), %rax
movq (%rax,%rbp,8), %r13
movq %r15, %rdi
movq %r13, %rsi
movsd %xmm1, 0x28(%rsp)
movaps %xmm2, 0x10(%rsp)
movsd %xmm7, 0x30(%rsp)
movapd %xmm3, 0x40(%rsp)
movapd %xmm4, 0x70(%rsp)
movapd %xmm0, (%rsp)
callq 0x2125e
movapd %xmm0, 0xf0(%rsp)
movq %r12, %rdi
movq %r13, %rdx
callq 0x11472
movsd 0x30(%rsp), %xmm7
movsd 0x180(%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movapd 0xf0(%rsp), %xmm5
movapd %xmm5, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm7
movapd 0x170(%rsp), %xmm2
subpd 0x50(%rsp), %xmm2
movsd %xmm7, 0xc0(%rsp)
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 0x10(%rsp), %xmm6
addpd %xmm2, %xmm6
movapd %xmm6, 0x10(%rsp)
movaps 0x10(%rsp), %xmm2
movaps %xmm2, 0x80(%rsp)
mulpd %xmm3, %xmm4
movapd 0x40(%rsp), %xmm3
addpd %xmm4, %xmm3
movapd 0x70(%rsp), %xmm4
movapd %xmm3, 0x90(%rsp)
unpckhpd %xmm5, %xmm5            # xmm5 = xmm5[1,1]
mulpd %xmm1, %xmm5
movsd 0x28(%rsp), %xmm1
addpd %xmm5, %xmm4
movapd %xmm4, 0xa0(%rsp)
movapd (%rsp), %xmm5
addpd %xmm0, %xmm5
movapd %xmm5, (%rsp)
movapd (%rsp), %xmm0
movapd %xmm0, 0xb0(%rsp)
addsd %xmm8, %xmm1
incq %rbp
movl 0x34(%r15), %eax
jmp 0x1193e
movapd %xmm0, 0x40(%rsp)
xorpd %xmm5, %xmm5
xorl %r13d, %r13d
leaq 0x170(%rsp), %r12
xorpd %xmm1, %xmm1
xorpd %xmm3, %xmm3
xorps %xmm2, %xmm2
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jge 0x11bde
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rsi
addq $0x18, %rsi
movq %r12, %rdi
movsd %xmm1, 0x28(%rsp)
movsd %xmm5, 0x70(%rsp)
movapd %xmm3, 0x10(%rsp)
movaps %xmm2, (%rsp)
movapd %xmm0, 0x30(%rsp)
callq 0x37b5c
movsd 0x70(%rsp), %xmm5
movsd 0x180(%rsp), %xmm0
subsd 0x60(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm5
movsd %xmm5, 0xc0(%rsp)
movapd 0x170(%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
movapd 0x40(%rsp), %xmm4
addpd %xmm2, %xmm4
movapd %xmm4, 0x40(%rsp)
movapd %xmm4, 0x80(%rsp)
movapd 0x10(%rsp), %xmm2
addpd %xmm3, %xmm2
movapd %xmm2, 0x10(%rsp)
movapd 0x10(%rsp), %xmm3
movapd %xmm3, 0x90(%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 0x28(%rsp), %xmm1
movapd (%rsp), %xmm4
addpd %xmm2, %xmm4
movapd %xmm4, (%rsp)
movaps (%rsp), %xmm2
movaps %xmm2, 0xa0(%rsp)
movapd 0x30(%rsp), %xmm4
addpd %xmm0, %xmm4
movapd %xmm4, 0x30(%rsp)
movapd 0x30(%rsp), %xmm0
movapd %xmm0, 0xb0(%rsp)
addsd 0x81432(%rip), %xmm1    # 0x93008
incq %r13
jmp 0x11ab0
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x11bfe
jp 0x11bfe
leaq 0xc95d7(%rip), %rsi     # 0xdb1c8
movq %rbx, %rdi
callq 0x28198
jmp 0x11dea
xorl %eax, %eax
cmpq $0x9, %rax
je 0x11c21
movsd 0x80(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp,%rax,8)
incq %rax
jmp 0x11c00
movq %rbx, 0x10(%rsp)
pushq $0x3
popq %rdi
leaq 0x80(%rsp), %rdx
leaq 0x170(%rsp), %rcx
leaq 0x100(%rsp), %r8
leaq 0x1d0(%rsp), %r12
movl %edi, %esi
movq %r12, %r9
callq 0x678f6
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x120, %rdi            # imm = 0x120
callq 0x3830a
addq $0x18, %rbx
cmpq $0x48, %rbx
jne 0x11c55
leaq 0x120(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x11ca4
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x11c97
movsd (%r12,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x11c81
incq %rcx
addq $0x18, %rax
addq $0x18, %r12
jmp 0x11c79
movq %r14, (%rsp)
leaq 0xe0(%rsp), %r14
andl $0x0, 0x8(%r14)
andq $0x0, (%r14)
leaq 0xd0(%rsp), %rbx
andl $0x0, 0x8(%rbx)
andq $0x0, (%rbx)
xorl %r13d, %r13d
leaq 0x1b8(%rsp), %r12
movq %r15, 0xc8(%rsp)
movslq 0x14(%r15), %rax
cmpq %rax, %r13
jge 0x11d44
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rsi
addq $0x18, %rsi
movq %r12, %rdi
leaq 0x50(%rsp), %rdx
callq 0x37d29
leaq 0x120(%rsp), %rbp
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x11d37
movq %rbp, %rdi
movq %r12, %rsi
callq 0x38586
ucomisd 0x814c6(%rip), %xmm0    # 0x931e8
movq %r14, %rax
ja 0x11d2a
movq %rbx, %rax
incl (%rax,%r15,4)
incq %r15
addq $0x18, %rbp
jmp 0x11d09
incq %r13
movq 0xc8(%rsp), %r15
jmp 0x11cdc
leaq 0x120(%rsp), %r15
xorl %ebx, %ebx
movq (%rsp), %r14
cmpq $0x2, %rbx
je 0x11d79
movl 0xe0(%rsp,%rbx,4), %eax
cmpl 0xd0(%rsp,%rbx,4), %eax
jge 0x11d70
movq %r15, %rdi
callq 0x38662
incq %rbx
addq $0x18, %r15
jmp 0x11d52
leaq 0x138(%rsp), %r15
leaq 0x1b8(%rsp), %r13
leaq 0x120(%rsp), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x389be
leaq 0x150(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x3887c
testq %r14, %r14
je 0x11dcf
movaps 0x100(%rsp), %xmm0
movups %xmm0, (%r14)
movsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x10(%r14)
leaq 0x120(%rsp), %rsi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x281ea
movq %rbx, %rax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop